File size: 5,262 Bytes
f9f6cee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
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/