forked from 0xPolygonHermez/zkevm-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addrqueue_test.go
153 lines (135 loc) · 4.98 KB
/
addrqueue_test.go
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
package sequencer
import (
"math/big"
"testing"
"github.com/ethereum/go-ethereum/common"
)
type notReadyTx struct {
nonce uint64
hash common.Hash
}
type addrQueueAddTxTestCase struct {
name string
hash common.Hash
nonce uint64
gasPrice *big.Int
cost *big.Int
expectedReadyTx common.Hash
expectedNotReadyTx []notReadyTx
}
var addr addrQueue
func newTestTxTracker(hash common.Hash, nonce uint64, gasPrice *big.Int, cost *big.Int) *TxTracker {
tx := TxTracker{Hash: hash, Nonce: nonce, GasPrice: gasPrice, Cost: cost}
tx.HashStr = tx.Hash.String()
return &tx
}
func processAddTxTestCases(t *testing.T, testCases []addrQueueAddTxTestCase) {
var emptyHash common.Hash = common.Hash{}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
tx := newTestTxTracker(tc.hash, tc.nonce, tc.gasPrice, tc.cost)
newReadyTx, _ := addr.addTx(tx)
if tc.expectedReadyTx.String() == emptyHash.String() {
if !(addr.readyTx == nil) {
t.Fatalf("Error readyTx. Expected=%s, Actual=%s", tc.expectedReadyTx, "")
}
if !(newReadyTx == nil) {
t.Fatalf("Error newReadyTx. Expected=nil, Actual=%s", newReadyTx.HashStr)
}
} else {
if !(addr.readyTx.Hash == tc.expectedReadyTx) {
t.Fatalf("Error readyTx. Expected=%s, Actual=%s", tc.expectedReadyTx, addr.readyTx.HashStr)
}
}
for _, nr := range tc.expectedNotReadyTx {
txTmp, found := addr.notReadyTxs[nr.nonce]
if !(found) {
t.Fatalf("Error notReadyTx nonce=%d don't exists", nr.nonce)
}
if !(txTmp.Hash == nr.hash) {
t.Fatalf("Error notReadyTx nonce=%d. Expected=%s, Actual=%s", nr.nonce, nr.hash.String(), txTmp.HashStr)
}
}
})
}
}
func TestAddrQueue(t *testing.T) {
addr = addrQueue{fromStr: "0x99999", currentNonce: 1, currentBalance: new(big.Int).SetInt64(10), notReadyTxs: make(map[uint64]*TxTracker)}
addTxTestCases := []addrQueueAddTxTestCase{
{
name: "Add not ready tx 0x02", hash: common.Hash{0x2}, nonce: 2, gasPrice: new(big.Int).SetInt64(2), cost: new(big.Int).SetInt64(5),
expectedReadyTx: common.Hash{},
expectedNotReadyTx: []notReadyTx{
{nonce: 2, hash: common.Hash{0x2}},
},
},
{
name: "Add ready tx 0x01", hash: common.Hash{0x1}, nonce: 1, gasPrice: new(big.Int).SetInt64(2), cost: new(big.Int).SetInt64(5),
expectedReadyTx: common.Hash{1},
expectedNotReadyTx: []notReadyTx{
{nonce: 2, hash: common.Hash{0x2}},
},
},
{
name: "Add not ready tx 0x04", hash: common.Hash{0x4}, nonce: 4, gasPrice: new(big.Int).SetInt64(2), cost: new(big.Int).SetInt64(5),
expectedReadyTx: common.Hash{1},
expectedNotReadyTx: []notReadyTx{
{nonce: 2, hash: common.Hash{0x2}},
{nonce: 4, hash: common.Hash{0x4}},
},
},
{
name: "Replace tx with nonce 4 for tx 0x44 with best GasPrice", hash: common.Hash{0x44}, nonce: 4, gasPrice: new(big.Int).SetInt64(3), cost: new(big.Int).SetInt64(5),
expectedReadyTx: common.Hash{1},
expectedNotReadyTx: []notReadyTx{
{nonce: 2, hash: common.Hash{0x2}},
{nonce: 4, hash: common.Hash{0x44}},
},
},
}
processAddTxTestCases(t, addTxTestCases)
t.Run("Delete readyTx 0x01", func(t *testing.T) {
tc := addTxTestCases[1]
tx := newTestTxTracker(tc.hash, tc.nonce, tc.gasPrice, tc.cost)
deltx := addr.deleteTx(tx.Hash)
if !(addr.readyTx == nil) {
t.Fatalf("Error readyTx not nil. Expected=%s, Actual=%s", "", addr.readyTx.HashStr)
}
if !(deltx.HashStr == tx.HashStr) {
t.Fatalf("Error returning deletedReadyTx. Expected=%s, Actual=%s", tx.HashStr, deltx.HashStr)
}
})
processAddTxTestCases(t, []addrQueueAddTxTestCase{
{
name: "Add tx with nonce = currentNonce but with cost > currentBalance", hash: common.Hash{0x11}, nonce: 1, gasPrice: new(big.Int).SetInt64(2), cost: new(big.Int).SetInt64(15),
expectedReadyTx: common.Hash{},
expectedNotReadyTx: []notReadyTx{
{nonce: 1, hash: common.Hash{0x11}},
{nonce: 2, hash: common.Hash{0x2}},
{nonce: 4, hash: common.Hash{0x44}},
},
},
})
t.Run("Update currentBalance = 15, set tx 0x11 as ready", func(t *testing.T) {
tmpHash := common.Hash{0x11}
addr.updateCurrentNonceBalance(&addr.currentNonce, new(big.Int).SetInt64(15))
if !(addr.readyTx != nil && addr.readyTx.Hash.String() == tmpHash.String()) {
t.Fatalf("Error readyTx. Expected=%s, Actual=%s", tmpHash, "")
}
tx, found := addr.notReadyTxs[1]
if found {
t.Fatalf("Error notReadyTx nonce=%d. Expected=%s, Actual=%s", addr.currentNonce, "", tx.Hash.String())
}
})
t.Run("Update currentNonce = 4, set tx 0x04 as ready", func(t *testing.T) {
tmpHash := common.Hash{0x44}
newNonce := uint64(4)
addr.updateCurrentNonceBalance(&newNonce, new(big.Int).SetInt64(15))
if !(addr.readyTx != nil && addr.readyTx.Hash.String() == tmpHash.String()) {
t.Fatalf("Error readyTx. Expected=%s, Actual=%s", tmpHash, addr.readyTx.Hash.String())
}
if len(addr.notReadyTxs) > 0 {
t.Fatalf("Error notReadyTx not empty. Expected=%d, Actual=%d", 0, len(addr.notReadyTxs))
}
})
}