|
45 | 45 | testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
46 | 46 | // testAddr is the Ethereum address of the tester account.
|
47 | 47 | testAddr = crypto.PubkeyToAddress(testKey.PublicKey)
|
| 48 | + // testBlocks is the test parameters array for specific blocks. |
| 49 | + testBlocks = []testBlockParam{ |
| 50 | + { |
| 51 | + // This txs params also used to default block. |
| 52 | + blockNr: 11, |
| 53 | + txs: []testTransactionParam{ |
| 54 | + { |
| 55 | + to: common.Address{0x01}, |
| 56 | + value: big.NewInt(1), |
| 57 | + gasPrice: big.NewInt(1), |
| 58 | + data: nil, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + { |
| 63 | + blockNr: 12, |
| 64 | + txs: []testTransactionParam{ |
| 65 | + { |
| 66 | + to: common.Address{0x01}, |
| 67 | + value: big.NewInt(1), |
| 68 | + gasPrice: big.NewInt(1), |
| 69 | + data: nil, |
| 70 | + }, |
| 71 | + { |
| 72 | + to: common.Address{0x02}, |
| 73 | + value: big.NewInt(2), |
| 74 | + gasPrice: big.NewInt(2), |
| 75 | + data: nil, |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + { |
| 80 | + blockNr: 13, |
| 81 | + txs: []testTransactionParam{ |
| 82 | + { |
| 83 | + to: common.Address{0x01}, |
| 84 | + value: big.NewInt(1), |
| 85 | + gasPrice: big.NewInt(1), |
| 86 | + data: nil, |
| 87 | + }, |
| 88 | + { |
| 89 | + to: common.Address{0x02}, |
| 90 | + value: big.NewInt(2), |
| 91 | + gasPrice: big.NewInt(2), |
| 92 | + data: nil, |
| 93 | + }, |
| 94 | + { |
| 95 | + to: common.Address{0x03}, |
| 96 | + value: big.NewInt(3), |
| 97 | + gasPrice: big.NewInt(3), |
| 98 | + data: nil, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + { |
| 103 | + blockNr: 14, |
| 104 | + txs: []testTransactionParam{}, |
| 105 | + }, |
| 106 | + } |
48 | 107 | )
|
49 | 108 |
|
| 109 | +type testTransactionParam struct { |
| 110 | + to common.Address |
| 111 | + value *big.Int |
| 112 | + gasPrice *big.Int |
| 113 | + data []byte |
| 114 | +} |
| 115 | + |
| 116 | +type testBlockParam struct { |
| 117 | + blockNr int |
| 118 | + txs []testTransactionParam |
| 119 | +} |
| 120 | + |
50 | 121 | // testBackend is a mock implementation of the live Ethereum message handler. Its
|
51 | 122 | // purpose is to allow testing the request/reply workflows and wire serialization
|
52 | 123 | // in the `eth` protocol without actually doing any data processing.
|
@@ -78,13 +149,35 @@ func newTestBackendWithGenerator(blocks int, lightProcess bool) *testBackend {
|
78 | 149 | // lets unset (nil). Set it here to the correct value.
|
79 | 150 | block.SetCoinbase(testAddr)
|
80 | 151 |
|
81 |
| - // We want to simulate an empty middle block, having the same state as the |
82 |
| - // first one. The last is needs a state change again to force a reorg. |
83 |
| - tx, err := types.SignTx(types.NewTransaction(block.TxNonce(testAddr), common.Address{0x01}, big.NewInt(1), params.TxGas, big.NewInt(1), nil), signer, testKey) |
84 |
| - if err != nil { |
85 |
| - panic(err) |
| 152 | + for idx, testBlock := range testBlocks { |
| 153 | + // Specific block setting, the index in this generator has 1 diff from specified blockNr. |
| 154 | + if i+1 == testBlock.blockNr { |
| 155 | + for _, testTransaction := range testBlock.txs { |
| 156 | + tx, err := types.SignTx(types.NewTransaction(block.TxNonce(testAddr), testTransaction.to, |
| 157 | + testTransaction.value, params.TxGas, testTransaction.gasPrice, testTransaction.data), signer, testKey) |
| 158 | + if err != nil { |
| 159 | + panic(err) |
| 160 | + } |
| 161 | + block.AddTxWithChain(chain, tx) |
| 162 | + } |
| 163 | + break |
| 164 | + } |
| 165 | + |
| 166 | + // Default block setting. |
| 167 | + if idx == len(testBlocks)-1 { |
| 168 | + // We want to simulate an empty middle block, having the same state as the |
| 169 | + // first one. The last is needs a state change again to force a reorg. |
| 170 | + for _, testTransaction := range testBlocks[0].txs { |
| 171 | + tx, err := types.SignTx(types.NewTransaction(block.TxNonce(testAddr), testTransaction.to, |
| 172 | + testTransaction.value, params.TxGas, testTransaction.gasPrice, testTransaction.data), signer, testKey) |
| 173 | + if err != nil { |
| 174 | + panic(err) |
| 175 | + } |
| 176 | + block.AddTxWithChain(chain, tx) |
| 177 | + } |
| 178 | + } |
86 | 179 | }
|
87 |
| - block.AddTxWithChain(chain, tx) |
| 180 | + |
88 | 181 | }
|
89 | 182 | bs, _ := GenerateChain(params.TestChainConfig, chain.Genesis(), ethash.NewFaker(), db, blocks, generator)
|
90 | 183 | if _, err := chain.InsertChain(bs); err != nil {
|
@@ -139,12 +232,17 @@ func TestProcessDiffLayer(t *testing.T) {
|
139 | 232 | }
|
140 | 233 | blockHash := block.Hash()
|
141 | 234 | rawDiff := fullBackend.chain.GetDiffLayerRLP(blockHash)
|
142 |
| - diff, err := rawDataToDiffLayer(rawDiff) |
143 |
| - if err != nil { |
144 |
| - t.Errorf("failed to decode rawdata %v", err) |
| 235 | + if len(rawDiff) != 0 { |
| 236 | + diff, err := rawDataToDiffLayer(rawDiff) |
| 237 | + if err != nil { |
| 238 | + t.Errorf("failed to decode rawdata %v", err) |
| 239 | + } |
| 240 | + if diff == nil { |
| 241 | + continue |
| 242 | + } |
| 243 | + lightBackend.Chain().HandleDiffLayer(diff, "testpid", true) |
145 | 244 | }
|
146 |
| - lightBackend.Chain().HandleDiffLayer(diff, "testpid", true) |
147 |
| - _, err = lightBackend.chain.insertChain([]*types.Block{block}, true) |
| 245 | + _, err := lightBackend.chain.insertChain([]*types.Block{block}, true) |
148 | 246 | if err != nil {
|
149 | 247 | t.Errorf("failed to insert block %v", err)
|
150 | 248 | }
|
@@ -186,7 +284,8 @@ func TestFreezeDiffLayer(t *testing.T) {
|
186 | 284 | blockNum := 1024
|
187 | 285 | fullBackend := newTestBackend(blockNum, true)
|
188 | 286 | defer fullBackend.close()
|
189 |
| - if fullBackend.chain.diffQueue.Size() != blockNum { |
| 287 | + // Minus one empty block. |
| 288 | + if fullBackend.chain.diffQueue.Size() != blockNum-1 { |
190 | 289 | t.Errorf("size of diff queue is wrong, expected: %d, get: %d", blockNum, fullBackend.chain.diffQueue.Size())
|
191 | 290 | }
|
192 | 291 | time.Sleep(diffLayerFreezerRecheckInterval + 1*time.Second)
|
@@ -215,10 +314,11 @@ func TestPruneDiffLayer(t *testing.T) {
|
215 | 314 | for num := uint64(1); num < uint64(blockNum); num++ {
|
216 | 315 | header := fullBackend.chain.GetHeaderByNumber(num)
|
217 | 316 | rawDiff := fullBackend.chain.GetDiffLayerRLP(header.Hash())
|
218 |
| - diff, _ := rawDataToDiffLayer(rawDiff) |
219 |
| - fullBackend.Chain().HandleDiffLayer(diff, "testpid1", true) |
220 |
| - fullBackend.Chain().HandleDiffLayer(diff, "testpid2", true) |
221 |
| - |
| 317 | + if len(rawDiff) != 0 { |
| 318 | + diff, _ := rawDataToDiffLayer(rawDiff) |
| 319 | + fullBackend.Chain().HandleDiffLayer(diff, "testpid1", true) |
| 320 | + fullBackend.Chain().HandleDiffLayer(diff, "testpid2", true) |
| 321 | + } |
222 | 322 | }
|
223 | 323 | fullBackend.chain.pruneDiffLayer()
|
224 | 324 | if len(fullBackend.chain.diffNumToBlockHashes) != maxDiffForkDist {
|
@@ -261,3 +361,45 @@ func TestPruneDiffLayer(t *testing.T) {
|
261 | 361 | }
|
262 | 362 |
|
263 | 363 | }
|
| 364 | + |
| 365 | +func TestGetDiffAccounts(t *testing.T) { |
| 366 | + t.Parallel() |
| 367 | + |
| 368 | + blockNum := 128 |
| 369 | + fullBackend := newTestBackend(blockNum, false) |
| 370 | + defer fullBackend.close() |
| 371 | + |
| 372 | + for _, testBlock := range testBlocks { |
| 373 | + block := fullBackend.chain.GetBlockByNumber(uint64(testBlock.blockNr)) |
| 374 | + if block == nil { |
| 375 | + t.Fatal("block should not be nil") |
| 376 | + } |
| 377 | + blockHash := block.Hash() |
| 378 | + accounts, err := fullBackend.chain.GetDiffAccounts(blockHash) |
| 379 | + if err != nil { |
| 380 | + t.Errorf("get diff accounts eror for block number (%d): %v", testBlock.blockNr, err) |
| 381 | + } |
| 382 | + |
| 383 | + for idx, account := range accounts { |
| 384 | + if testAddr == account { |
| 385 | + break |
| 386 | + } |
| 387 | + |
| 388 | + if idx == len(accounts)-1 { |
| 389 | + t.Errorf("the diff accounts does't include addr: %v", testAddr) |
| 390 | + } |
| 391 | + } |
| 392 | + |
| 393 | + for _, transaction := range testBlock.txs { |
| 394 | + for idx, account := range accounts { |
| 395 | + if transaction.to == account { |
| 396 | + break |
| 397 | + } |
| 398 | + |
| 399 | + if idx == len(accounts)-1 { |
| 400 | + t.Errorf("the diff accounts does't include addr: %v", transaction.to) |
| 401 | + } |
| 402 | + } |
| 403 | + } |
| 404 | + } |
| 405 | +} |
0 commit comments