Skip to content

Commit fb69dee

Browse files
committed
add testcase for diff accounts
Signed-off-by: Keefe-Liu <bianze.kernel@gmail.com>
1 parent 0a05c8f commit fb69dee

File tree

2 files changed

+320
-24
lines changed

2 files changed

+320
-24
lines changed

core/blockchain_diff_test.go

+158-16
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,79 @@ var (
4545
testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
4646
// testAddr is the Ethereum address of the tester account.
4747
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+
}
48107
)
49108

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+
50121
// testBackend is a mock implementation of the live Ethereum message handler. Its
51122
// purpose is to allow testing the request/reply workflows and wire serialization
52123
// in the `eth` protocol without actually doing any data processing.
@@ -78,13 +149,35 @@ func newTestBackendWithGenerator(blocks int, lightProcess bool) *testBackend {
78149
// lets unset (nil). Set it here to the correct value.
79150
block.SetCoinbase(testAddr)
80151

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+
}
86179
}
87-
block.AddTxWithChain(chain, tx)
180+
88181
}
89182
bs, _ := GenerateChain(params.TestChainConfig, chain.Genesis(), ethash.NewFaker(), db, blocks, generator)
90183
if _, err := chain.InsertChain(bs); err != nil {
@@ -139,12 +232,17 @@ func TestProcessDiffLayer(t *testing.T) {
139232
}
140233
blockHash := block.Hash()
141234
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)
145244
}
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)
148246
if err != nil {
149247
t.Errorf("failed to insert block %v", err)
150248
}
@@ -186,7 +284,8 @@ func TestFreezeDiffLayer(t *testing.T) {
186284
blockNum := 1024
187285
fullBackend := newTestBackend(blockNum, true)
188286
defer fullBackend.close()
189-
if fullBackend.chain.diffQueue.Size() != blockNum {
287+
// Minus one empty block.
288+
if fullBackend.chain.diffQueue.Size() != blockNum-1 {
190289
t.Errorf("size of diff queue is wrong, expected: %d, get: %d", blockNum, fullBackend.chain.diffQueue.Size())
191290
}
192291
time.Sleep(diffLayerFreezerRecheckInterval + 1*time.Second)
@@ -215,10 +314,11 @@ func TestPruneDiffLayer(t *testing.T) {
215314
for num := uint64(1); num < uint64(blockNum); num++ {
216315
header := fullBackend.chain.GetHeaderByNumber(num)
217316
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+
}
222322
}
223323
fullBackend.chain.pruneDiffLayer()
224324
if len(fullBackend.chain.diffNumToBlockHashes) != maxDiffForkDist {
@@ -261,3 +361,45 @@ func TestPruneDiffLayer(t *testing.T) {
261361
}
262362

263363
}
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

Comments
 (0)