Skip to content

Commit 98a4f34

Browse files
committed
fix TestReorg
1 parent c8c79c9 commit 98a4f34

File tree

1 file changed

+45
-7
lines changed

1 file changed

+45
-7
lines changed

miner/scroll_worker_test.go

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,9 +1051,7 @@ func TestReorg(t *testing.T) {
10511051

10521052
// This test chain imports the mined blocks.
10531053
b.genesis.MustCommit(db)
1054-
chain, _ := core.NewBlockChain(db, nil, b.chain.Config(), engine, vm.Config{
1055-
Debug: true,
1056-
Tracer: vm.NewStructLogger(&vm.LogConfig{EnableMemory: true, EnableReturnData: true})}, nil, nil)
1054+
chain, _ := core.NewBlockChain(db, nil, b.chain.Config(), engine, vm.Config{}, nil, nil)
10571055
defer chain.Stop()
10581056

10591057
// Insert local tx
@@ -1093,13 +1091,53 @@ firstReorg:
10931091
}
10941092

10951093
require.Equal(t, oldBlock.NumberU64(), newBlock.NumberU64())
1096-
require.Equal(t, oldBlock.Transactions()[:1], newBlock.Transactions())
1094+
// should skip second txn
1095+
require.Equal(t, oldBlock.Transactions()[:1].Len(), newBlock.Transactions().Len())
1096+
for i := 0; i < newBlock.Transactions().Len(); i++ {
1097+
require.Equal(t, oldBlock.Transactions()[:1][i].Hash(), newBlock.Transactions()[i].Hash())
1098+
}
1099+
1100+
time.Sleep(time.Second * 5)
1101+
1102+
const secondReorgHeight = 15
1103+
w.asyncChecker.ScheduleError(secondReorgHeight, 0)
1104+
1105+
sub.Unsubscribe()
10971106

1098-
w.asyncChecker.ScheduleError(25, 1)
10991107
// Insert local tx
1100-
for i := 0; i < 40; i++ {
1108+
for i := 0; i < 20; i++ {
11011109
b.txPool.AddLocal(b.newRandomTx(true))
11021110
}
1103-
time.Sleep(time.Second * 5)
11041111

1112+
// resubscribe
1113+
sub = w.mux.Subscribe(core.NewMinedBlockEvent{})
1114+
defer sub.Unsubscribe()
1115+
1116+
oldBlock = nil
1117+
newBlock = nil
1118+
1119+
secondReorg:
1120+
for {
1121+
select {
1122+
case ev := <-sub.Chan():
1123+
block := ev.Data.(core.NewMinedBlockEvent).Block
1124+
if block.NumberU64() == secondReorgHeight {
1125+
if oldBlock == nil {
1126+
oldBlock = block
1127+
} else {
1128+
newBlock = block
1129+
break secondReorg
1130+
}
1131+
}
1132+
case <-time.After(3 * time.Second): // Worker needs 1s to include new changes.
1133+
t.Fatalf("timeout")
1134+
}
1135+
}
1136+
1137+
require.Equal(t, oldBlock.NumberU64(), newBlock.NumberU64())
1138+
// should skip first txn and the next txn will fail nonce check
1139+
require.Equal(t, 0, newBlock.Transactions().Len())
1140+
for i := 0; i < newBlock.Transactions().Len(); i++ {
1141+
require.Equal(t, oldBlock.Transactions()[1:][i].Hash(), newBlock.Transactions()[i].Hash())
1142+
}
11051143
}

0 commit comments

Comments
 (0)