Skip to content

Commit

Permalink
make contract test - gas limit
Browse files Browse the repository at this point in the history
  • Loading branch information
rabbitprincess committed Oct 10, 2023
1 parent 9e2c80a commit 02cdb92
Showing 1 changed file with 55 additions and 7 deletions.
62 changes: 55 additions & 7 deletions contract/contract_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package contract

import (
"math"
"math/big"
"testing"

Expand Down Expand Up @@ -29,7 +30,6 @@ func TestTxFee(t *testing.T) {
gasPrice *big.Int
expectFee *big.Int
}{

// v1
{1, 0, types.NewAmount(1, types.Gaer), types.NewAmount(2000000, types.Gaer)}, // gas price not affect in v1
{1, 0, types.NewAmount(5, types.Gaer), types.NewAmount(2000000, types.Gaer)},
Expand Down Expand Up @@ -77,18 +77,66 @@ func TestTxFee(t *testing.T) {
}
}

func TestValidateTxType(t *testing.T) {
initContractTest(t)
defer deinitContractTest(t)

}

func TestGasLimit(t *testing.T) {
initContractTest(t)
defer deinitContractTest(t)

for _, test := range []struct {
version int32
feeDelegation bool
txGasLimit uint64
payloadSize int
gasPrice *big.Int
usedFee *big.Int
sender *big.Int
receiver *big.Int
expectErr error
expectGasLimit uint64
}{
// no gas limit
{version: 1, expectErr: nil, expectGasLimit: 0},

// fee delegation
{version: 2, feeDelegation: true, gasPrice: types.NewAmount(1, types.Gaer), receiver: types.NewAmount(5, types.Gaer), usedFee: types.NewAmount(10, types.Gaer), expectErr: nil, expectGasLimit: math.MaxUint64}, // max
{version: 2, feeDelegation: true, gasPrice: types.NewAmount(1, types.Gaer), receiver: types.NewAmount(5, types.Gaer), usedFee: types.NewAmount(5, types.Gaer), expectErr: newVmError(types.ErrNotEnoughGas), expectGasLimit: 0}, // not enough error
{version: 2, feeDelegation: true, gasPrice: types.NewAmount(1, types.Gaer), receiver: types.NewAmount(10, types.Gaer), usedFee: types.NewAmount(5, types.Gaer), expectErr: nil, expectGasLimit: 5},

// no gas limit specified in tx, the limit is the sender's balance
{version: 2, gasPrice: types.NewAmount(1, types.Gaer), sender: types.NewAmount(5, types.Gaer), usedFee: types.NewAmount(10, types.Gaer), expectErr: nil, expectGasLimit: math.MaxUint64}, // max
{version: 2, gasPrice: types.NewAmount(1, types.Gaer), sender: types.NewAmount(5, types.Gaer), usedFee: types.NewAmount(5, types.Gaer), expectErr: newVmError(types.ErrNotEnoughGas), expectGasLimit: 0}, // not enough error
{version: 2, gasPrice: types.NewAmount(1, types.Gaer), sender: types.NewAmount(10, types.Gaer), usedFee: types.NewAmount(5, types.Gaer), expectErr: nil, expectGasLimit: 5},

// if gas limit specified in tx, check if the sender has enough balance for gas
{version: 2, txGasLimit: 100000, payloadSize: 100, expectErr: newVmError(types.ErrNotEnoughGas), expectGasLimit: 100000},
{version: 2, txGasLimit: 150000, payloadSize: 100, expectErr: nil, expectGasLimit: 50000},
{version: 2, txGasLimit: 200000, payloadSize: 100, expectErr: nil, expectGasLimit: 100000},
} {
resultFee, resultErr := GasLimit(test.version, test.feeDelegation, test.txGasLimit, test.payloadSize, test.gasPrice, test.usedFee, test.sender, test.receiver)
assert.EqualValues(t, test.expectErr, resultErr, "GasLimit(forkVersion:%d, isFeeDelegation:%t, txGasLimit:%d, payloadSize:%d, gasPrice:%s, usedFee:%s, senderBalance:%s, receiverBalance:%s)", test.version, test.feeDelegation, test.txGasLimit, test.payloadSize, test.gasPrice, test.usedFee, test.sender, test.receiver)
assert.EqualValues(t, test.expectGasLimit, resultFee, "GasLimit(forkVersion:%d, isFeeDelegation:%t, txGasLimit:%d, payloadSize:%d, gasPrice:%s, usedFee:%s, senderBalance:%s, receiverBalance:%s)", test.version, test.feeDelegation, test.txGasLimit, test.payloadSize, test.gasPrice, test.usedFee, test.sender, test.receiver)
}
}

/*
func TestValidateTxType(t *testing.T) {
initContractTest(t)
defer deinitContractTest(t)
for _, test := range []struct {
txType types.TxType
amount *big.Int
payLoadSize int
forkVersion int32
gasPrice *big.Int
expectFee *big.Int
}{} {
resultFee := validateTxType()
assert.Equal(t, test.expectFee.String(), resultFee.String(), "TxFee(forkVersion:%d, payloadSize:%d, gasPrice:%s)", test.forkVersion, test.payLoadSize, test.gasPrice)
}
}
*/

func TestCheckRedeploy(t *testing.T) {
initContractTest(t)
defer deinitContractTest(t)
Expand Down

0 comments on commit 02cdb92

Please sign in to comment.