@@ -8,16 +8,40 @@ import (
8
8
"testing"
9
9
10
10
"github.com/ethereum/go-ethereum/common"
11
- "github.com/ethereum/go-ethereum/common/math"
12
11
"github.com/ethereum/go-ethereum/core/rawdb"
13
12
"github.com/ethereum/go-ethereum/crypto"
14
13
"github.com/ethereum/go-ethereum/params"
15
14
"github.com/ethereum/go-ethereum/rlp"
16
15
)
17
16
17
+ func makeTransactions (n int ) []* types.Transaction {
18
+ txs := make ([]* types.Transaction , n )
19
+ key , _ := crypto .GenerateKey ()
20
+ signer := types .LatestSigner (params .TestChainConfig )
21
+
22
+ for i := range txs {
23
+ amount := big .NewInt (int64 (i )* 10 ^ 9 )
24
+ gas_price := big .NewInt (300_000 )
25
+ data := make ([]byte , 100 )
26
+ // randomly assigned for debugging
27
+ data [3 ] = 3
28
+ data [4 ] = 3
29
+ data [5 ] = 3
30
+ data [6 ] = 3
31
+ data [7 ] = 3
32
+ tx := types .NewTransaction (uint64 (i ), common.Address {}, amount , 10 * 10 ^ 6 , gas_price , data )
33
+ signedTx , err := types .SignTx (tx , signer , key )
34
+ if err != nil {
35
+ panic (err )
36
+ }
37
+ txs [i ] = signedTx
38
+ }
39
+ return txs
40
+ }
41
+
18
42
/*
19
- TestNonHashedTransactionsStackTrieGetProof inserts 70 transactions into a stacktrie.
20
- For each of the 70 modifications of the trie it asks for a proof - GetProof is called before
43
+ transactionsStackTrieInsertionTemplate inserts n transactions into a stacktrie.
44
+ For each of the n modifications of the trie it asks for a proof - GetProof is called before
21
45
and after the modification. The transactions in the trie are not hashed and thus GetProof
22
46
does not require to query a database to get the preimages.
23
47
@@ -62,23 +86,9 @@ The first proof element is a branch with children at position 0 (branch B) and 1
62
86
The second element is the 16-th transaction. For example, the third byte (16) represents
63
87
the transaction index.
64
88
*/
65
- func TestNonHashedTransactionsStackTrieGetProof (t * testing.T ) {
66
- txs := make ([]* types.Transaction , 70 )
67
- key , _ := crypto .GenerateKey ()
68
- signer := types .LatestSigner (params .TestChainConfig )
69
-
70
- for i := range txs {
71
- amount := math .BigPow (2 , int64 (i ))
72
- price := big .NewInt (300000 )
73
- data := make ([]byte , 100 )
74
- tx := types .NewTransaction (uint64 (i ), common.Address {}, amount , 123457 , price , data )
75
- signedTx , err := types .SignTx (tx , signer , key )
76
- if err != nil {
77
- panic (err )
78
- }
79
- txs [i ] = signedTx
80
- }
81
89
90
+ func transactionsStackTrieInsertionTemplate (n int ) {
91
+ txs := makeTransactions (n )
82
92
db := rawdb .NewMemoryDatabase ()
83
93
stackTrie := trie .NewStackTrie (db )
84
94
@@ -87,34 +97,59 @@ func TestNonHashedTransactionsStackTrieGetProof(t *testing.T) {
87
97
fmt .Println ("===" )
88
98
}
89
99
100
+ func TestStackTrieInsertion_1Tx (t * testing.T ) {
101
+ // Only one leaf
102
+ transactionsStackTrieInsertionTemplate (1 )
103
+ }
104
+
105
+ func TestStackTrieInsertion_2Txs (t * testing.T ) {
106
+ // One ext. node and one leaf
107
+ transactionsStackTrieInsertionTemplate (2 )
108
+ }
109
+
110
+ func TestStackTrieInsertion_3Txs (t * testing.T ) {
111
+ // One ext. node, one branch and one leaf
112
+ transactionsStackTrieInsertionTemplate (3 )
113
+ }
114
+
115
+ func TestStackTrieInsertion_4Txs (t * testing.T ) {
116
+ // One ext. node, one branch and two leaves
117
+ transactionsStackTrieInsertionTemplate (4 )
118
+ }
119
+
120
+ func TestStackTrieInsertion_16Txs (t * testing.T ) {
121
+ // One ext. node and one branch with full leaves (16 leaves)
122
+ transactionsStackTrieInsertionTemplate (16 )
123
+ }
124
+
125
+ func TestStackTrieInsertion_17Txs (t * testing.T ) {
126
+ // One ext. node, 3 branches and 17 leaves.
127
+ // The original ext. node turns into a branch (B1) which has children at position 0 and 1.
128
+ // At position 0 of B1, it has a branch with full leaves
129
+ // At position 1 of B1, it has a newly leaf
130
+ transactionsStackTrieInsertionTemplate (17 )
131
+ }
132
+
133
+ func TestStackTrieInsertion_33Txs (t * testing.T ) {
134
+ // Follow above test and have one more branch generated
135
+ transactionsStackTrieInsertionTemplate (33 )
136
+ }
137
+
138
+ func TestStackTrieInsertion_ManyTxs (t * testing.T ) {
139
+ // Just randomly picking a large number.
140
+ // The cap of block gas limit is 30M, the minimum gas cost of a tx is 21k
141
+ // 30M / 21k ~= 1429
142
+ transactionsStackTrieInsertionTemplate (2000 )
143
+ }
144
+
90
145
/*
91
- TestHashedTransactionsStackTrieGetProof inserts 2 transactions into a stacktrie,
146
+ batchedTransactionsStackTrieProofTemplate inserts n transactions into a stacktrie,
92
147
the trie is then hashed (DeriveSha call).
93
- The proof is asked for one of the two transactions. The transactions in the trie are hashed and thus
148
+ The proof is asked for one of the n transactions. The transactions in the trie are hashed and thus
94
149
GetProof requires to query a database to get the preimages.
95
150
*/
96
- func TestHashedTransactionsStackTrieGetProof (t * testing.T ) {
97
- txs := make ([]* types.Transaction , 2 )
98
- key , _ := crypto .GenerateKey ()
99
- signer := types .LatestSigner (params .TestChainConfig )
100
-
101
- for i := range txs {
102
- amount := math .BigPow (2 , int64 (i ))
103
- price := big .NewInt (300000 )
104
- data := make ([]byte , 100 )
105
- data [3 ] = 3
106
- data [4 ] = 3
107
- data [5 ] = 3
108
- data [6 ] = 3
109
- data [7 ] = 3
110
- tx := types .NewTransaction (uint64 (i ), common.Address {}, amount , 123457 , price , data )
111
- signedTx , err := types .SignTx (tx , signer , key )
112
- if err != nil {
113
- panic (err )
114
- }
115
- txs [i ] = signedTx
116
- }
117
-
151
+ func batchedTransactionsStackTrieProofTemplate (n int ) {
152
+ txs := makeTransactions (n )
118
153
db := rawdb .NewMemoryDatabase ()
119
154
stackTrie := trie .NewStackTrie (db )
120
155
@@ -131,6 +166,34 @@ func TestHashedTransactionsStackTrieGetProof(t *testing.T) {
131
166
}
132
167
133
168
fmt .Println (proofS )
134
-
135
169
fmt .Println ("===" )
136
170
}
171
+
172
+ func TestBatchedTxsProof_1Tx (t * testing.T ) {
173
+ batchedTransactionsStackTrieProofTemplate (1 )
174
+ }
175
+
176
+ func TestBatchedTxsProof_2Txs (t * testing.T ) {
177
+ batchedTransactionsStackTrieProofTemplate (2 )
178
+ }
179
+
180
+ func TestBatchedTxsProof_3Txs (t * testing.T ) {
181
+ batchedTransactionsStackTrieProofTemplate (3 )
182
+ }
183
+ func TestBatchedTxsProof_4Txs (t * testing.T ) {
184
+ batchedTransactionsStackTrieProofTemplate (4 )
185
+ }
186
+
187
+ func TestBatchedTxsProof_16Txs (t * testing.T ) {
188
+ batchedTransactionsStackTrieProofTemplate (16 )
189
+ }
190
+
191
+ func TestBatchedTxsProof_17Txs (t * testing.T ) {
192
+ batchedTransactionsStackTrieProofTemplate (17 )
193
+ }
194
+ func TestBatchedTxsProof_33Txs (t * testing.T ) {
195
+ batchedTransactionsStackTrieProofTemplate (33 )
196
+ }
197
+ func TestBatchedTxsProof_ManyTxs (t * testing.T ) {
198
+ batchedTransactionsStackTrieProofTemplate (2000 )
199
+ }
0 commit comments