diff --git a/state/transition_recoverable_test.go b/state/transition_recoverable_test.go new file mode 100644 index 00000000..2a31e771 --- /dev/null +++ b/state/transition_recoverable_test.go @@ -0,0 +1,114 @@ +package state + +import ( + "math/big" + "testing" + + "github.com/0xPolygon/polygon-sdk/chain" + "github.com/0xPolygon/polygon-sdk/state/runtime" + "github.com/0xPolygon/polygon-sdk/types" + "github.com/hashicorp/go-hclog" + "github.com/stretchr/testify/assert" +) + + + +func newRecoverableTransition( + preState map[types.Address]*PreState, + blockGasLimit uint64, + remainingGas uint64, +) *Transition { + return &Transition{ + logger: hclog.NewNullLogger(), + r: &Executor{config: &chain.Params{}}, + state: newTestTxn(preState), + config: chain.ForksInTime{}, + ctx: runtime.TxContext{GasLimit: int64(blockGasLimit)}, + gasPool: remainingGas, + } +} + +func TestWriteTransactionRecoverability(t *testing.T) { + sender := types.StringToAddress("1") + receiver := types.StringToAddress("2") + + testTable := []struct { + name string + preState map[types.Address]*PreState + blockGasLimit uint64 + remainingGas uint64 + txGas uint64 + txGasPrice int64 + txNonce uint64 + isRecoverable bool + }{ + { + name: "gas exceeding the block gas limit is non-recoverable", + preState: map[types.Address]*PreState{ + sender: {Nonce: 0, Balance: 100000000000}, + }, + blockGasLimit: 8000000, + remainingGas: 8000000, + txGas: 10000000, + txGasPrice: 1, + txNonce: 0, + isRecoverable: false, + }, + { + name: "fits the block but not the remaining gas is recoverable", + preState: map[types.Address]*PreState{ + sender: {Nonce: 0, Balance: 100000000000}, + }, + blockGasLimit: 8000000, + remainingGas: 1000000, + txGas: 5000000, + txGasPrice: 1, + txNonce: 0, + isRecoverable: true, + }, + { + name: "incorrect nonce is recoverable", + preState: map[types.Address]*PreState{ + sender: {Nonce: 5, Balance: 100000000000}, + }, + blockGasLimit: 8000000, + remainingGas: 8000000, + txGas: 21000, + txGasPrice: 1, + txNonce: 0, + isRecoverable: true, + }, + { + name: "not enough funds to cover the gas cost is recoverable", + preState: map[types.Address]*PreState{ + sender: {Nonce: 0, Balance: 10}, + }, + blockGasLimit: 8000000, + remainingGas: 8000000, + txGas: 21000, + txGasPrice: 1000, + txNonce: 0, + isRecoverable: true, + }, + } + + for _, tc := range testTable { + t.Run(tc.name, func(t *testing.T) { + transition := newRecoverableTransition(tc.preState, tc.blockGasLimit, tc.remainingGas) + + txn := &types.Transaction{ + From: sender, + To: &receiver, + Nonce: tc.txNonce, + Gas: tc.txGas, + GasPrice: big.NewInt(tc.txGasPrice), + Value: big.NewInt(0), + } + + appErr := transition.Write(txn) + if assert.NotNil(t, appErr, "expected the write to fail") { + assert.Equal(t, tc.isRecoverable, appErr.IsRecoverable) + } + }) + } +} diff --git a/txpool/decrease_nonce_test.go b/txpool/decrease_nonce_test.go new file mode 100644 index 00000000..363c9b23 --- /dev/null +++ b/txpool/decrease_nonce_test.go @@ -0,0 +1,69 @@ +package txpool + +import ( + "math/big" + "testing" + + "github.com/0xPolygon/polygon-sdk/types" + "github.com/hashicorp/go-hclog" + "github.com/stretchr/testify/assert" +) + +func TestDecreaseAccountNonce(t *testing.T) { + pool, err := NewTxPool(hclog.NewNullLogger(), false, forks.At(0), &mockStore{}, nil, nil) + assert.NoError(t, err) + pool.EnableDev() + + addr := types.Address{0x1} + + tx0 := &types.Transaction{ + From: addr, + Nonce: 0, + Gas: validGasLimit, + GasPrice: big.NewInt(1), + Value: big.NewInt(0), + } + tx1 := &types.Transaction{ + From: addr, + Nonce: 1, + Gas: validGasLimit, + GasPrice: big.NewInt(1), + Value: big.NewInt(0), + } + assert.NoError(t, pool.addImpl("", tx0)) + assert.NoError(t, pool.addImpl("", tx1)) + + nonce, ok := pool.GetNonce(addr) + assert.True(t, ok) + assert.Equal(t, uint64(2), nonce) + + + pool.DecreaseAccountNonce(tx1) + + nonce, ok = pool.GetNonce(addr) + assert.True(t, ok) + assert.Equal(t, uint64(1), nonce) +} + +func TestDecreaseAccountNonce_UntrackedAccount(t *testing.T) { + pool, err := NewTxPool(hclog.NewNullLogger(), false, forks.At(0), &mockStore{}, nil, nil) + assert.NoError(t, err) + pool.EnableDev() + + untracked := types.Address{0x9} + tx := &types.Transaction{ + From: untracked, + Nonce: 0, + Gas: validGasLimit, + GasPrice: big.NewInt(1), + Value: big.NewInt(0), + } + + + assert.NotPanics(t, func() { + pool.DecreaseAccountNonce(tx) + }) + + _, ok := pool.GetNonce(untracked) + assert.False(t, ok) +} diff --git a/mimo_test_command.sh b/mimo_test_command.sh new file mode 100755 --- /dev/null +++ b/mimo_test_command.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash + + + +set -euo pipefail +HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +cd /testbed + + + + +go test ./state/... ./txpool/... ./types/... + + +go test -run 'TestWriteTransactionRecoverability' ./state/ +go test -run 'TestDecreaseAccountNonce' ./txpool/