mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2024-12-22 03:30:37 +00:00
e2: optimize tests speed (#7738)
This commit is contained in:
parent
252e57b76f
commit
d3c3be9c91
@ -144,7 +144,7 @@ func (b *SimulatedBackend) Commit() {
|
||||
Headers: []*types.Header{b.pendingHeader},
|
||||
Blocks: []*types.Block{b.pendingBlock},
|
||||
TopBlock: b.pendingBlock,
|
||||
}); err != nil {
|
||||
}, nil); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
//nolint:prealloc
|
||||
|
@ -430,13 +430,13 @@ func initialState1() error {
|
||||
// BLOCKS
|
||||
|
||||
for i := 0; i < chain.Length(); i++ {
|
||||
if err = m2.InsertChain(chain.Slice(i, i+1)); err != nil {
|
||||
if err = m2.InsertChain(chain.Slice(i, i+1), nil); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = stateDatabaseComparison(m.DB, m2.DB, i+1); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = m.InsertChain(chain.Slice(i, i+1)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(i, i+1), nil); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -52,7 +52,7 @@ func TestCallTraceOneByOne(t *testing.T) {
|
||||
api := NewTraceAPI(newBaseApiForTest(m), m.DB, &httpcfg.HttpCfg{})
|
||||
// Insert blocks 1 by 1, to tirgget possible "off by one" errors
|
||||
for i := 0; i < chain.Length(); i++ {
|
||||
if err = m.InsertChain(chain.Slice(i, i+1)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(i, i+1), nil); err != nil {
|
||||
t.Fatalf("inserting chain: %v", err)
|
||||
}
|
||||
}
|
||||
@ -96,7 +96,7 @@ func TestCallTraceUnwind(t *testing.T) {
|
||||
|
||||
api := NewTraceAPI(newBaseApiForTest(m), m.DB, &httpcfg.HttpCfg{})
|
||||
|
||||
if err = m.InsertChain(chainA); err != nil {
|
||||
if err = m.InsertChain(chainA, nil); err != nil {
|
||||
t.Fatalf("inserting chainA: %v", err)
|
||||
}
|
||||
stream := jsoniter.ConfigDefault.BorrowStream(nil)
|
||||
@ -115,7 +115,7 @@ func TestCallTraceUnwind(t *testing.T) {
|
||||
}
|
||||
assert.Equal(t, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, blockNumbersFromTraces(t, stream.Buffer()))
|
||||
|
||||
if err = m.InsertChain(chainB.Slice(0, 12)); err != nil {
|
||||
if err = m.InsertChain(chainB.Slice(0, 12), nil); err != nil {
|
||||
t.Fatalf("inserting chainB: %v", err)
|
||||
}
|
||||
stream.Reset(nil)
|
||||
@ -130,7 +130,7 @@ func TestCallTraceUnwind(t *testing.T) {
|
||||
}
|
||||
assert.Equal(t, []int{1, 2, 3, 4, 5, 11, 12}, blockNumbersFromTraces(t, stream.Buffer()))
|
||||
|
||||
if err = m.InsertChain(chainB.Slice(12, 20)); err != nil {
|
||||
if err = m.InsertChain(chainB.Slice(12, 20), nil); err != nil {
|
||||
t.Fatalf("inserting chainB: %v", err)
|
||||
}
|
||||
stream.Reset(nil)
|
||||
@ -158,7 +158,7 @@ func TestFilterNoAddresses(t *testing.T) {
|
||||
api := NewTraceAPI(newBaseApiForTest(m), m.DB, &httpcfg.HttpCfg{})
|
||||
// Insert blocks 1 by 1, to tirgget possible "off by one" errors
|
||||
for i := 0; i < chain.Length(); i++ {
|
||||
if err = m.InsertChain(chain.Slice(i, i+1)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(i, i+1), nil); err != nil {
|
||||
t.Fatalf("inserting chain: %v", err)
|
||||
}
|
||||
}
|
||||
@ -205,7 +205,7 @@ func TestFilterAddressIntersection(t *testing.T) {
|
||||
}, false /* intermediateHashes */)
|
||||
require.NoError(t, err, "generate chain")
|
||||
|
||||
err = m.InsertChain(chain)
|
||||
err = m.InsertChain(chain, nil)
|
||||
require.NoError(t, err, "inserting chain")
|
||||
|
||||
fromBlock, toBlock := uint64(1), uint64(15)
|
||||
|
@ -214,7 +214,7 @@ func mockWithGenerator(t *testing.T, blocks int, generator func(int, *core.Block
|
||||
}, testKey, false)
|
||||
if blocks > 0 {
|
||||
chain, _ := core.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, blocks, generator, true)
|
||||
err := m.InsertChain(chain)
|
||||
err := m.InsertChain(chain, nil)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
return m
|
||||
|
@ -524,7 +524,7 @@ func chainWithDeployedContract(t *testing.T) (*stages.MockSentry, libcommon.Addr
|
||||
t.Fatalf("generate blocks: %v", err)
|
||||
}
|
||||
|
||||
err = m.InsertChain(chain)
|
||||
err = m.InsertChain(chain, nil)
|
||||
assert.NoError(t, err)
|
||||
|
||||
tx, err := db.BeginRo(context.Background())
|
||||
|
@ -53,7 +53,7 @@ func TestEthSubscribe(t *testing.T) {
|
||||
highestSeenHeader := chain.TopBlock.NumberU64()
|
||||
|
||||
hook := stages.NewHook(m.Ctx, m.Notifications, m.Sync, m.BlockReader, m.ChainConfig, m.Log, m.UpdateHead)
|
||||
if _, err := stages.StageLoopStep(m.Ctx, m.DB, m.Sync, initialCycle, logger, m.BlockReader, hook); err != nil {
|
||||
if err := stages.StageLoopStep(m.Ctx, m.DB, nil, m.Sync, initialCycle, logger, m.BlockReader, hook); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
|
@ -81,7 +81,7 @@ func createGasPriceTestKV(t *testing.T, chainSize int) *stages.MockSentry {
|
||||
t.Error(err)
|
||||
}
|
||||
// Construct testing chain
|
||||
if err = m.InsertChain(chain); err != nil {
|
||||
if err = m.InsertChain(chain, nil); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
|
@ -67,7 +67,7 @@ func TestSendRawTransaction(t *testing.T) {
|
||||
m.ReceiveWg.Wait() // Wait for all messages to be processed before we proceeed
|
||||
|
||||
initialCycle := stages.MockInsertAsInitialCycle
|
||||
if _, err := stages.StageLoopStep(m.Ctx, m.DB, m.Sync, initialCycle, logger, m.BlockReader, nil); err != nil {
|
||||
if err := stages.StageLoopStep(m.Ctx, m.DB, nil, m.Sync, initialCycle, logger, m.BlockReader, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ func TestTxPoolContent(t *testing.T) {
|
||||
b.SetCoinbase(libcommon.Address{1})
|
||||
}, false /* intermediateHashes */)
|
||||
require.NoError(err)
|
||||
err = m.InsertChain(chain)
|
||||
err = m.InsertChain(chain, nil)
|
||||
require.NoError(err)
|
||||
|
||||
ctx, conn := rpcdaemontest.CreateTestGrpcConn(t, m)
|
||||
|
@ -101,10 +101,10 @@ func CreateTestSentry(t *testing.T) (*stages.MockSentry, *core.ChainPack, []*cor
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err = m.InsertChain(orphanedChain); err != nil {
|
||||
if err = m.InsertChain(orphanedChain, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = m.InsertChain(chain); err != nil {
|
||||
if err = m.InsertChain(chain, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@ -427,7 +427,7 @@ func CreateTestSentryForTraces(t *testing.T) *stages.MockSentry {
|
||||
t.Fatalf("generate blocks: %v", err)
|
||||
}
|
||||
|
||||
if err := m.InsertChain(chain); err != nil {
|
||||
if err := m.InsertChain(chain, nil); err != nil {
|
||||
t.Fatalf("failed to insert into chain: %v", err)
|
||||
}
|
||||
return m
|
||||
@ -533,7 +533,7 @@ func CreateTestSentryForTracesCollision(t *testing.T) *stages.MockSentry {
|
||||
t.Fatalf("generate blocks: %v", err)
|
||||
}
|
||||
// Import the canonical chain
|
||||
if err := m.InsertChain(chain); err != nil {
|
||||
if err := m.InsertChain(chain, nil); err != nil {
|
||||
t.Fatalf("failed to insert into chain: %v", err)
|
||||
}
|
||||
|
||||
|
@ -53,6 +53,6 @@ func TestEmptyBlock(t *testing.T) {
|
||||
blocks[0] = block
|
||||
|
||||
chain := &core.ChainPack{Headers: headers, Blocks: blocks, Receipts: receipts, TopBlock: block}
|
||||
err = m.InsertChain(chain)
|
||||
err = m.InsertChain(chain, nil)
|
||||
require.NoError(err)
|
||||
}
|
||||
|
@ -105,7 +105,7 @@ func TestReimportMirroredState(t *testing.T) {
|
||||
}
|
||||
|
||||
// Insert the first two blocks and make sure the chain is valid
|
||||
if err := m.InsertChain(chain.Slice(0, 2)); err != nil {
|
||||
if err := m.InsertChain(chain.Slice(0, 2), nil); err != nil {
|
||||
t.Fatalf("failed to insert initial blocks: %v", err)
|
||||
}
|
||||
if err := m.DB.View(m.Ctx, func(tx kv.Tx) error {
|
||||
@ -122,7 +122,7 @@ func TestReimportMirroredState(t *testing.T) {
|
||||
// Simulate a crash by creating a new chain on top of the database, without
|
||||
// flushing the dirty states out. Insert the last block, triggering a sidechain
|
||||
// reimport.
|
||||
if err := m.InsertChain(chain.Slice(2, chain.Length())); err != nil {
|
||||
if err := m.InsertChain(chain.Slice(2, chain.Length()), nil); err != nil {
|
||||
t.Fatalf("failed to insert final block: %v", err)
|
||||
}
|
||||
if err := m.DB.View(m.Ctx, func(tx kv.Tx) error {
|
||||
|
@ -476,7 +476,7 @@ func TestClique(t *testing.T) {
|
||||
chainX.Headers[k] = b.Header()
|
||||
}
|
||||
chainX.TopBlock = batches[j][len(batches[j])-1]
|
||||
if err = m.InsertChain(chainX); err != nil {
|
||||
if err = m.InsertChain(chainX, nil); err != nil {
|
||||
t.Errorf("test %d: failed to import batch %d, %v", i, j, err)
|
||||
failed = true
|
||||
break
|
||||
@ -492,7 +492,7 @@ func TestClique(t *testing.T) {
|
||||
chainX.Headers[k] = b.Header()
|
||||
}
|
||||
chainX.TopBlock = batches[len(batches)-1][len(batches[len(batches)-1])-1]
|
||||
err = m.InsertChain(chainX)
|
||||
err = m.InsertChain(chainX, nil)
|
||||
if tt.failure != nil && err == nil {
|
||||
t.Errorf("test %d: expected failure", i)
|
||||
}
|
||||
|
@ -61,7 +61,7 @@ func TestHeaderVerification(t *testing.T) {
|
||||
}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err = m.InsertChain(chain.Slice(i, i+1)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(i, i+1), nil); err != nil {
|
||||
t.Fatalf("test %d: error inserting the block: %v", i, err)
|
||||
}
|
||||
|
||||
@ -102,7 +102,7 @@ func TestHeaderWithSealVerification(t *testing.T) {
|
||||
}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err = m.InsertChain(chain.Slice(i, i+1)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(i, i+1), nil); err != nil {
|
||||
t.Fatalf("test %d: error inserting the block: %v", i, err)
|
||||
}
|
||||
|
||||
|
@ -143,7 +143,7 @@ func TestCreate2Revive(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// BLOCK 1
|
||||
if err = m.InsertChain(chain.Slice(0, 1)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(0, 1), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@ -157,7 +157,7 @@ func TestCreate2Revive(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// BLOCK 2
|
||||
if err = m.InsertChain(chain.Slice(1, 2)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(1, 2), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@ -179,7 +179,7 @@ func TestCreate2Revive(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// BLOCK 3
|
||||
if err = m.InsertChain(chain.Slice(2, 3)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(2, 3), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = m.DB.View(context.Background(), func(tx kv.Tx) error {
|
||||
@ -192,7 +192,7 @@ func TestCreate2Revive(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// BLOCK 4
|
||||
if err = m.InsertChain(chain.Slice(3, 4)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(3, 4), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = m.DB.View(context.Background(), func(tx kv.Tx) error {
|
||||
@ -349,7 +349,7 @@ func TestCreate2Polymorth(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// BLOCK 1
|
||||
if err = m.InsertChain(chain.Slice(0, 1)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(0, 1), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@ -364,7 +364,7 @@ func TestCreate2Polymorth(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// BLOCK 2
|
||||
if err = m.InsertChain(chain.Slice(1, 2)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(1, 2), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@ -384,7 +384,7 @@ func TestCreate2Polymorth(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// BLOCK 3
|
||||
if err = m.InsertChain(chain.Slice(2, 3)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(2, 3), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = m.DB.View(context.Background(), func(tx kv.Tx) error {
|
||||
@ -397,7 +397,7 @@ func TestCreate2Polymorth(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// BLOCK 4
|
||||
if err = m.InsertChain(chain.Slice(3, 4)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(3, 4), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = m.DB.View(context.Background(), func(tx kv.Tx) error {
|
||||
@ -417,7 +417,7 @@ func TestCreate2Polymorth(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// BLOCK 5
|
||||
if err = m.InsertChain(chain.Slice(4, 5)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(4, 5), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = m.DB.View(context.Background(), func(tx kv.Tx) error {
|
||||
@ -534,7 +534,7 @@ func TestReorgOverSelfDestruct(t *testing.T) {
|
||||
})
|
||||
require.NoError(t, err)
|
||||
// BLOCK 1
|
||||
if err = m.InsertChain(chain.Slice(0, 1)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(0, 1), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@ -553,7 +553,7 @@ func TestReorgOverSelfDestruct(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// BLOCKS 2 + 3
|
||||
if err = m.InsertChain(chain.Slice(1, chain.Length())); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(1, chain.Length()), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@ -567,7 +567,7 @@ func TestReorgOverSelfDestruct(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// REORG of block 2 and 3, and insert new (empty) BLOCK 2, 3, and 4
|
||||
if err = m.InsertChain(longerChain.Slice(1, 4)); err != nil {
|
||||
if err = m.InsertChain(longerChain.Slice(1, 4), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = m.DB.View(context.Background(), func(tx kv.Tx) error {
|
||||
@ -675,7 +675,7 @@ func TestReorgOverStateChange(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// BLOCK 1
|
||||
if err = m.InsertChain(chain.Slice(0, 1)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(0, 1), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@ -694,12 +694,12 @@ func TestReorgOverStateChange(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// BLOCK 2
|
||||
if err = m.InsertChain(chain.Slice(1, chain.Length())); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(1, chain.Length()), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// REORG of block 2 and 3, and insert new (empty) BLOCK 2, 3, and 4
|
||||
if err = m.InsertChain(longerChain.Slice(1, 3)); err != nil {
|
||||
if err = m.InsertChain(longerChain.Slice(1, 3), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = m.DB.View(context.Background(), func(tx kv.Tx) error {
|
||||
@ -802,7 +802,7 @@ func TestCreateOnExistingStorage(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// BLOCK 1
|
||||
if err = m.InsertChain(chain.Slice(0, 1)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(0, 1), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@ -938,7 +938,7 @@ func TestEip2200Gas(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// BLOCK 1
|
||||
if err = m.InsertChain(chain.Slice(0, 1)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(0, 1), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@ -1024,7 +1024,7 @@ func TestWrongIncarnation(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// BLOCK 1
|
||||
if err = m.InsertChain(chain.Slice(0, 1)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(0, 1), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@ -1051,7 +1051,7 @@ func TestWrongIncarnation(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// BLOCKS 2
|
||||
if err = m.InsertChain(chain.Slice(1, 2)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(1, 2), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = m.DB.View(context.Background(), func(tx kv.Tx) error {
|
||||
@ -1169,12 +1169,12 @@ func TestWrongIncarnation2(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// BLOCK 1
|
||||
if err = m.InsertChain(chain.Slice(0, 1)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(0, 1), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// BLOCKS 2
|
||||
if err = m.InsertChain(chain.Slice(1, chain.Length())); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(1, chain.Length()), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@ -1199,7 +1199,7 @@ func TestWrongIncarnation2(t *testing.T) {
|
||||
})
|
||||
require.NoError(t, err)
|
||||
// REORG of block 2 and 3, and insert new (empty) BLOCK 2, 3, and 4
|
||||
if err = m.InsertChain(longerChain.Slice(1, longerChain.Length())); err != nil {
|
||||
if err = m.InsertChain(longerChain.Slice(1, longerChain.Length()), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@ -1472,7 +1472,7 @@ func TestRecreateAndRewind(t *testing.T) {
|
||||
}
|
||||
|
||||
// BLOCKS 1 and 2
|
||||
if err = m.InsertChain(chain.Slice(0, 2)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(0, 2), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@ -1493,7 +1493,7 @@ func TestRecreateAndRewind(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// Block 3 and 4
|
||||
if err = m.InsertChain(chain.Slice(2, chain.Length())); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(2, chain.Length()), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = m.DB.View(context.Background(), func(tx kv.Tx) error {
|
||||
@ -1512,7 +1512,7 @@ func TestRecreateAndRewind(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// Reorg
|
||||
if err = m.InsertChain(longerChain); err != nil {
|
||||
if err = m.InsertChain(longerChain, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = m.DB.View(context.Background(), func(tx kv.Tx) error {
|
||||
@ -1572,10 +1572,10 @@ func TestTxLookupUnwind(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = m.InsertChain(chain1); err != nil {
|
||||
if err = m.InsertChain(chain1, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = m.InsertChain(chain2); err != nil {
|
||||
if err = m.InsertChain(chain2, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var count uint64
|
||||
|
@ -118,7 +118,7 @@ func newTestBackend(t *testing.T) *testBackend {
|
||||
t.Error(err)
|
||||
}
|
||||
// Construct testing chain
|
||||
if err = m.InsertChain(chain); err != nil {
|
||||
if err = m.InsertChain(chain, nil); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
return &testBackend{db: m.DB, cfg: params.TestChainConfig, blockReader: m.BlockReader}
|
||||
|
@ -137,7 +137,7 @@ func mockWithGenerator(t *testing.T, blocks int, generator func(int, *core.Block
|
||||
}, testKey, false)
|
||||
if blocks > 0 {
|
||||
chain, _ := core.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, blocks, generator, true)
|
||||
err := m.InsertChain(chain)
|
||||
err := m.InsertChain(chain, nil)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
return m
|
||||
|
@ -125,22 +125,23 @@ func (bt *BlockTest) Run(t *testing.T, _ bool) error {
|
||||
return fmt.Errorf("genesis block state root does not match test: computed=%x, test=%x", m.Genesis.Root().Bytes()[:6], bt.json.Genesis.StateRoot[:6])
|
||||
}
|
||||
|
||||
validBlocks, err := bt.insertBlocks(m)
|
||||
tx, err := m.DB.BeginRw(m.Ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
validBlocks, err := bt.insertBlocks(m, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tx, err1 := m.DB.BeginRo(context.Background())
|
||||
if err1 != nil {
|
||||
return fmt.Errorf("blockTest create tx: %w", err1)
|
||||
}
|
||||
defer tx.Rollback()
|
||||
cmlast := rawdb.ReadHeadBlockHash(tx)
|
||||
if libcommon.Hash(bt.json.BestBlock) != cmlast {
|
||||
return fmt.Errorf("last block hash validation mismatch: want: %x, have: %x", bt.json.BestBlock, cmlast)
|
||||
}
|
||||
newDB := state.New(m.NewStateReader(tx))
|
||||
if err = bt.validatePostState(newDB); err != nil {
|
||||
if err := bt.validatePostState(newDB); err != nil {
|
||||
return fmt.Errorf("post state validation failed: %w", err)
|
||||
}
|
||||
return bt.validateImportedHeaders(tx, validBlocks, m)
|
||||
@ -176,7 +177,7 @@ See https://github.com/ethereum/tests/wiki/Blockchain-Tests-II
|
||||
expected we are expected to ignore it and continue processing and then validate the
|
||||
post state.
|
||||
*/
|
||||
func (bt *BlockTest) insertBlocks(m *stages.MockSentry) ([]btBlock, error) {
|
||||
func (bt *BlockTest) insertBlocks(m *stages.MockSentry, tx kv.RwTx) ([]btBlock, error) {
|
||||
validBlocks := make([]btBlock, 0)
|
||||
// insert the test blocks, which will execute all transaction
|
||||
for bi, b := range bt.json.Blocks {
|
||||
@ -190,7 +191,8 @@ func (bt *BlockTest) insertBlocks(m *stages.MockSentry) ([]btBlock, error) {
|
||||
}
|
||||
// RLP decoding worked, try to insert into chain:
|
||||
chain := &core.ChainPack{Blocks: []*types.Block{cb}, Headers: []*types.Header{cb.Header()}, TopBlock: cb}
|
||||
err1 := m.InsertChain(chain)
|
||||
|
||||
err1 := m.InsertChain(chain, tx)
|
||||
if err1 != nil {
|
||||
if b.BlockHeader == nil {
|
||||
continue // OK - block is supposed to be invalid, continue with next block
|
||||
@ -198,17 +200,12 @@ func (bt *BlockTest) insertBlocks(m *stages.MockSentry) ([]btBlock, error) {
|
||||
return nil, fmt.Errorf("block #%v insertion into chain failed: %w", cb.Number(), err1)
|
||||
}
|
||||
} else if b.BlockHeader == nil {
|
||||
if err := m.DB.View(context.Background(), func(tx kv.Tx) error {
|
||||
canonical, cErr := bt.br.CanonicalHash(context.Background(), tx, cb.NumberU64())
|
||||
if cErr != nil {
|
||||
return cErr
|
||||
}
|
||||
if canonical == cb.Hash() {
|
||||
return fmt.Errorf("block (index %d) insertion should have failed due to: %v", bi, b.ExpectException)
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
canonical, cErr := bt.br.CanonicalHash(context.Background(), tx, cb.NumberU64())
|
||||
if cErr != nil {
|
||||
return nil, cErr
|
||||
}
|
||||
if canonical == cb.Hash() {
|
||||
return nil, fmt.Errorf("block (index %d) insertion should have failed due to: %v", bi, b.ExpectException)
|
||||
}
|
||||
}
|
||||
if b.BlockHeader == nil {
|
||||
@ -252,7 +249,7 @@ func validateHeader(h *btHeader, h2 *types.Header) error {
|
||||
return fmt.Errorf("receipt hash: want: %x have: %x", h.ReceiptTrie, h2.ReceiptHash)
|
||||
}
|
||||
if h.TransactionsTrie != h2.TxHash {
|
||||
return fmt.Errorf("tx hash: want: %x have: %x", h.TransactionsTrie, h2.TxHash)
|
||||
return fmt.Errorf("txn hash: want: %x have: %x", h.TransactionsTrie, h2.TxHash)
|
||||
}
|
||||
if h.StateRoot != h2.Root {
|
||||
return fmt.Errorf("state hash: want: %x have: %x", h.StateRoot, h2.Root)
|
||||
|
@ -397,13 +397,13 @@ func toMessage(tx stTransactionMarshaling, ps stPostState, baseFee *big.Int) (co
|
||||
|
||||
// Get values specific to this post state.
|
||||
if ps.Indexes.Data > len(tx.Data) {
|
||||
return nil, fmt.Errorf("tx data index %d out of bounds", ps.Indexes.Data)
|
||||
return nil, fmt.Errorf("txn data index %d out of bounds", ps.Indexes.Data)
|
||||
}
|
||||
if ps.Indexes.Value > len(tx.Value) {
|
||||
return nil, fmt.Errorf("tx value index %d out of bounds", ps.Indexes.Value)
|
||||
return nil, fmt.Errorf("txn value index %d out of bounds", ps.Indexes.Value)
|
||||
}
|
||||
if ps.Indexes.Gas > len(tx.GasLimit) {
|
||||
return nil, fmt.Errorf("tx gas limit index %d out of bounds", ps.Indexes.Gas)
|
||||
return nil, fmt.Errorf("txn gas limit index %d out of bounds", ps.Indexes.Gas)
|
||||
}
|
||||
dataHex := tx.Data[ps.Indexes.Data]
|
||||
valueHex := tx.Value[ps.Indexes.Value]
|
||||
@ -413,17 +413,17 @@ func toMessage(tx stTransactionMarshaling, ps stPostState, baseFee *big.Int) (co
|
||||
if valueHex != "0x" {
|
||||
va, ok := math.ParseBig256(valueHex)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid tx value %q", valueHex)
|
||||
return nil, fmt.Errorf("invalid txn value %q", valueHex)
|
||||
}
|
||||
v, overflow := uint256.FromBig(va)
|
||||
if overflow {
|
||||
return nil, fmt.Errorf("invalid tx value (overflowed) %q", valueHex)
|
||||
return nil, fmt.Errorf("invalid txn value (overflowed) %q", valueHex)
|
||||
}
|
||||
value = v
|
||||
}
|
||||
data, err := hex.DecodeString(strings.TrimPrefix(dataHex, "0x"))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid tx data %q", dataHex)
|
||||
return nil, fmt.Errorf("invalid txn data %q", dataHex)
|
||||
}
|
||||
var accessList types2.AccessList
|
||||
if tx.AccessLists != nil && tx.AccessLists[ps.Indexes.Data] != nil {
|
||||
|
@ -98,7 +98,7 @@ func TestSelfDestructReceive(t *testing.T) {
|
||||
t.Fatalf("generate blocks: %v", err)
|
||||
}
|
||||
|
||||
tx, err := m.DB.BeginRo(context.Background())
|
||||
tx, err := m.DB.BeginRw(context.Background())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -113,23 +113,18 @@ func TestSelfDestructReceive(t *testing.T) {
|
||||
}
|
||||
|
||||
// BLOCK 1
|
||||
if err = m.InsertChain(chain.Slice(0, 1)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(0, 1), tx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// BLOCK 2
|
||||
if err = m.InsertChain(chain.Slice(1, 2)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(1, 2), tx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// If we got this far, the newly created blockchain (with empty trie cache) loaded trie from the database
|
||||
// and that means that the state of the accounts written in the first block was correct.
|
||||
// This test checks that the storage root of the account is properly set to the root of the empty tree
|
||||
|
||||
tx, err = m.DB.BeginRo(context.Background())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer tx.Rollback()
|
||||
st = state.New(m.NewStateReader(tx))
|
||||
if !st.Exist(address) {
|
||||
t.Error("expected account to exist")
|
||||
|
@ -30,7 +30,7 @@ func TestInsertIncorrectStateRootDifferentAccounts(t *testing.T) {
|
||||
fromKey := data.keys[0]
|
||||
to := libcommon.Address{1}
|
||||
|
||||
m, chain, err := genBlocks(t, data.genesisSpec, map[int]tx{
|
||||
m, chain, err := genBlocks(t, data.genesisSpec, map[int]txn{
|
||||
0: {
|
||||
getBlockTx(from, to, uint256.NewInt(1000)),
|
||||
fromKey,
|
||||
@ -54,12 +54,12 @@ func TestInsertIncorrectStateRootDifferentAccounts(t *testing.T) {
|
||||
|
||||
incorrectBlock := types.NewBlock(&incorrectHeader, chain.Blocks[0].Transactions(), chain.Blocks[0].Uncles(), chain.Receipts[0], nil)
|
||||
incorrectChain := &core.ChainPack{Blocks: []*types.Block{incorrectBlock}, Headers: []*types.Header{&incorrectHeader}, TopBlock: incorrectBlock}
|
||||
if err = m.InsertChain(incorrectChain); err == nil {
|
||||
if err = m.InsertChain(incorrectChain, nil); err == nil {
|
||||
t.Fatal("should fail")
|
||||
}
|
||||
|
||||
// insert a correct block
|
||||
m, chain, err = genBlocks(t, data.genesisSpec, map[int]tx{
|
||||
m, chain, err = genBlocks(t, data.genesisSpec, map[int]txn{
|
||||
0: {
|
||||
getBlockTx(data.addresses[1], to, uint256.NewInt(5000)),
|
||||
data.keys[1],
|
||||
@ -69,14 +69,14 @@ func TestInsertIncorrectStateRootDifferentAccounts(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err = m.InsertChain(chain); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
tx, err := m.DB.BeginRo(context.Background())
|
||||
tx, err := m.DB.BeginRw(context.Background())
|
||||
require.NoError(t, err)
|
||||
defer tx.Rollback()
|
||||
|
||||
if err = m.InsertChain(chain, tx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
st := state.New(m.NewStateReader(tx))
|
||||
if !st.Exist(to) {
|
||||
t.Error("expected account to exist")
|
||||
@ -99,7 +99,7 @@ func TestInsertIncorrectStateRootSameAccount(t *testing.T) {
|
||||
fromKey := data.keys[0]
|
||||
to := libcommon.Address{1}
|
||||
|
||||
m, chain, err := genBlocks(t, data.genesisSpec, map[int]tx{
|
||||
m, chain, err := genBlocks(t, data.genesisSpec, map[int]txn{
|
||||
0: {
|
||||
getBlockTx(from, to, uint256.NewInt(1000)),
|
||||
fromKey,
|
||||
@ -122,12 +122,12 @@ func TestInsertIncorrectStateRootSameAccount(t *testing.T) {
|
||||
|
||||
incorrectBlock := types.NewBlock(&incorrectHeader, chain.Blocks[0].Transactions(), chain.Blocks[0].Uncles(), chain.Receipts[0], nil)
|
||||
incorrectChain := &core.ChainPack{Blocks: []*types.Block{incorrectBlock}, Headers: []*types.Header{&incorrectHeader}, TopBlock: incorrectBlock}
|
||||
if err = m.InsertChain(incorrectChain); err == nil {
|
||||
if err = m.InsertChain(incorrectChain, nil); err == nil {
|
||||
t.Fatal("should fail")
|
||||
}
|
||||
|
||||
// insert a correct block
|
||||
m, chain, err = genBlocks(t, data.genesisSpec, map[int]tx{
|
||||
m, chain, err = genBlocks(t, data.genesisSpec, map[int]txn{
|
||||
0: {
|
||||
getBlockTx(from, to, uint256.NewInt(5000)),
|
||||
fromKey,
|
||||
@ -137,7 +137,7 @@ func TestInsertIncorrectStateRootSameAccount(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err = m.InsertChain(chain); err != nil {
|
||||
if err = m.InsertChain(chain, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@ -164,7 +164,7 @@ func TestInsertIncorrectStateRootSameAccountSameAmount(t *testing.T) {
|
||||
fromKey := data.keys[0]
|
||||
to := libcommon.Address{1}
|
||||
|
||||
m, chain, err := genBlocks(t, data.genesisSpec, map[int]tx{
|
||||
m, chain, err := genBlocks(t, data.genesisSpec, map[int]txn{
|
||||
0: {
|
||||
getBlockTx(from, to, uint256.NewInt(1000)),
|
||||
fromKey,
|
||||
@ -184,12 +184,12 @@ func TestInsertIncorrectStateRootSameAccountSameAmount(t *testing.T) {
|
||||
|
||||
incorrectBlock := types.NewBlock(&incorrectHeader, chain.Blocks[0].Transactions(), chain.Blocks[0].Uncles(), chain.Receipts[0], nil)
|
||||
incorrectChain := &core.ChainPack{Blocks: []*types.Block{incorrectBlock}, Headers: []*types.Header{&incorrectHeader}, TopBlock: incorrectBlock}
|
||||
if err = m.InsertChain(incorrectChain); err == nil {
|
||||
if err = m.InsertChain(incorrectChain, nil); err == nil {
|
||||
t.Fatal("should fail")
|
||||
}
|
||||
|
||||
// insert a correct block
|
||||
m, chain, err = genBlocks(t, data.genesisSpec, map[int]tx{
|
||||
m, chain, err = genBlocks(t, data.genesisSpec, map[int]txn{
|
||||
0: {
|
||||
getBlockTx(from, to, uint256.NewInt(1000)),
|
||||
fromKey,
|
||||
@ -199,7 +199,7 @@ func TestInsertIncorrectStateRootSameAccountSameAmount(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err = m.InsertChain(chain); err != nil {
|
||||
if err = m.InsertChain(chain, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@ -226,7 +226,7 @@ func TestInsertIncorrectStateRootAllFundsRoot(t *testing.T) {
|
||||
fromKey := data.keys[0]
|
||||
to := libcommon.Address{1}
|
||||
|
||||
m, chain, err := genBlocks(t, data.genesisSpec, map[int]tx{
|
||||
m, chain, err := genBlocks(t, data.genesisSpec, map[int]txn{
|
||||
0: {
|
||||
getBlockTx(from, to, uint256.NewInt(1000)),
|
||||
fromKey,
|
||||
@ -246,12 +246,12 @@ func TestInsertIncorrectStateRootAllFundsRoot(t *testing.T) {
|
||||
|
||||
incorrectBlock := types.NewBlock(&incorrectHeader, chain.Blocks[0].Transactions(), chain.Blocks[0].Uncles(), chain.Receipts[0], nil)
|
||||
incorrectChain := &core.ChainPack{Blocks: []*types.Block{incorrectBlock}, Headers: []*types.Header{&incorrectHeader}, TopBlock: incorrectBlock}
|
||||
if err = m.InsertChain(incorrectChain); err == nil {
|
||||
if err = m.InsertChain(incorrectChain, nil); err == nil {
|
||||
t.Fatal("should fail")
|
||||
}
|
||||
|
||||
// insert a correct block
|
||||
m, chain, err = genBlocks(t, data.genesisSpec, map[int]tx{
|
||||
m, chain, err = genBlocks(t, data.genesisSpec, map[int]txn{
|
||||
0: {
|
||||
getBlockTx(from, to, uint256.NewInt(1000)),
|
||||
fromKey,
|
||||
@ -261,7 +261,7 @@ func TestInsertIncorrectStateRootAllFundsRoot(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err = m.InsertChain(chain); err != nil {
|
||||
if err = m.InsertChain(chain, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@ -288,7 +288,7 @@ func TestInsertIncorrectStateRootAllFunds(t *testing.T) {
|
||||
fromKey := data.keys[0]
|
||||
to := libcommon.Address{1}
|
||||
|
||||
m, chain, err := genBlocks(t, data.genesisSpec, map[int]tx{
|
||||
m, chain, err := genBlocks(t, data.genesisSpec, map[int]txn{
|
||||
0: {
|
||||
getBlockTx(from, to, uint256.NewInt(3000)),
|
||||
fromKey,
|
||||
@ -308,12 +308,12 @@ func TestInsertIncorrectStateRootAllFunds(t *testing.T) {
|
||||
incorrectBlock := types.NewBlock(&incorrectHeader, chain.Blocks[0].Transactions(), chain.Blocks[0].Uncles(), chain.Receipts[0], nil)
|
||||
incorrectChain := &core.ChainPack{Blocks: []*types.Block{incorrectBlock}, Headers: []*types.Header{&incorrectHeader}, TopBlock: incorrectBlock}
|
||||
|
||||
if err = m.InsertChain(incorrectChain); err == nil {
|
||||
if err = m.InsertChain(incorrectChain, nil); err == nil {
|
||||
t.Fatal("should fail")
|
||||
}
|
||||
|
||||
// insert a correct block
|
||||
m, chain, err = genBlocks(t, data.genesisSpec, map[int]tx{
|
||||
m, chain, err = genBlocks(t, data.genesisSpec, map[int]txn{
|
||||
0: {
|
||||
getBlockTx(from, to, uint256.NewInt(1000)),
|
||||
fromKey,
|
||||
@ -323,7 +323,7 @@ func TestInsertIncorrectStateRootAllFunds(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err = m.InsertChain(chain); err != nil {
|
||||
if err = m.InsertChain(chain, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@ -353,7 +353,7 @@ func TestAccountDeployIncorrectRoot(t *testing.T) {
|
||||
var contractAddress libcommon.Address
|
||||
eipContract := new(contracts.Testcontract)
|
||||
|
||||
m, chain, err := genBlocks(t, data.genesisSpec, map[int]tx{
|
||||
m, chain, err := genBlocks(t, data.genesisSpec, map[int]txn{
|
||||
0: {
|
||||
getBlockTx(from, to, uint256.NewInt(10)),
|
||||
fromKey,
|
||||
@ -368,7 +368,7 @@ func TestAccountDeployIncorrectRoot(t *testing.T) {
|
||||
}
|
||||
|
||||
// BLOCK 1
|
||||
if err = m.InsertChain(chain.Slice(0, 1)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(0, 1), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = m.DB.View(context.Background(), func(tx kv.Tx) error {
|
||||
@ -390,7 +390,7 @@ func TestAccountDeployIncorrectRoot(t *testing.T) {
|
||||
incorrectChain := &core.ChainPack{Blocks: []*types.Block{incorrectBlock}, Headers: []*types.Header{&incorrectHeader}, TopBlock: incorrectBlock}
|
||||
|
||||
// BLOCK 2 - INCORRECT
|
||||
if err = m.InsertChain(incorrectChain); err == nil {
|
||||
if err = m.InsertChain(incorrectChain, nil); err == nil {
|
||||
t.Fatal("should fail")
|
||||
}
|
||||
|
||||
@ -408,7 +408,7 @@ func TestAccountDeployIncorrectRoot(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// BLOCK 2 - CORRECT
|
||||
if err = m.InsertChain(chain.Slice(1, 2)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(1, 2), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@ -435,7 +435,7 @@ func TestAccountCreateIncorrectRoot(t *testing.T) {
|
||||
var contractAddress libcommon.Address
|
||||
eipContract := new(contracts.Testcontract)
|
||||
|
||||
m, chain, err := genBlocks(t, data.genesisSpec, map[int]tx{
|
||||
m, chain, err := genBlocks(t, data.genesisSpec, map[int]txn{
|
||||
0: {
|
||||
getBlockTx(from, to, uint256.NewInt(10)),
|
||||
fromKey,
|
||||
@ -454,7 +454,7 @@ func TestAccountCreateIncorrectRoot(t *testing.T) {
|
||||
}
|
||||
|
||||
// BLOCK 1
|
||||
if err = m.InsertChain(chain.Slice(0, 1)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(0, 1), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@ -473,7 +473,7 @@ func TestAccountCreateIncorrectRoot(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// BLOCK 2
|
||||
if err = m.InsertChain(chain.Slice(1, 2)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(1, 2), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = m.DB.View(context.Background(), func(tx kv.Tx) error {
|
||||
@ -496,12 +496,12 @@ func TestAccountCreateIncorrectRoot(t *testing.T) {
|
||||
incorrectBlock := types.NewBlock(&incorrectHeader, chain.Blocks[2].Transactions(), chain.Blocks[2].Uncles(), chain.Receipts[2], nil)
|
||||
incorrectChain := &core.ChainPack{Blocks: []*types.Block{incorrectBlock}, Headers: []*types.Header{&incorrectHeader}, TopBlock: incorrectBlock}
|
||||
|
||||
if err = m.InsertChain(incorrectChain); err == nil {
|
||||
if err = m.InsertChain(incorrectChain, nil); err == nil {
|
||||
t.Fatal("should fail")
|
||||
}
|
||||
|
||||
// BLOCK 3
|
||||
if err = m.InsertChain(chain.Slice(2, 3)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(2, 3), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
@ -515,7 +515,7 @@ func TestAccountUpdateIncorrectRoot(t *testing.T) {
|
||||
var contractAddress libcommon.Address
|
||||
eipContract := new(contracts.Testcontract)
|
||||
|
||||
m, chain, err := genBlocks(t, data.genesisSpec, map[int]tx{
|
||||
m, chain, err := genBlocks(t, data.genesisSpec, map[int]txn{
|
||||
0: {
|
||||
getBlockTx(from, to, uint256.NewInt(10)),
|
||||
fromKey,
|
||||
@ -538,7 +538,7 @@ func TestAccountUpdateIncorrectRoot(t *testing.T) {
|
||||
}
|
||||
|
||||
// BLOCK 1
|
||||
if err = m.InsertChain(chain.Slice(0, 1)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(0, 1), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@ -557,7 +557,7 @@ func TestAccountUpdateIncorrectRoot(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// BLOCK 2
|
||||
if err = m.InsertChain(chain.Slice(1, 2)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(1, 2), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@ -575,7 +575,7 @@ func TestAccountUpdateIncorrectRoot(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// BLOCK 3
|
||||
if err = m.InsertChain(chain.Slice(2, 3)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(2, 3), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@ -585,12 +585,12 @@ func TestAccountUpdateIncorrectRoot(t *testing.T) {
|
||||
incorrectBlock := types.NewBlock(&incorrectHeader, chain.Blocks[3].Transactions(), chain.Blocks[3].Uncles(), chain.Receipts[3], nil)
|
||||
incorrectChain := &core.ChainPack{Blocks: []*types.Block{incorrectBlock}, Headers: []*types.Header{&incorrectHeader}, TopBlock: incorrectBlock}
|
||||
|
||||
if err = m.InsertChain(incorrectChain); err == nil {
|
||||
if err = m.InsertChain(incorrectChain, nil); err == nil {
|
||||
t.Fatal("should fail")
|
||||
}
|
||||
|
||||
// BLOCK 4
|
||||
if err = m.InsertChain(chain.Slice(3, 4)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(3, 4), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
@ -604,7 +604,7 @@ func TestAccountDeleteIncorrectRoot(t *testing.T) {
|
||||
var contractAddress libcommon.Address
|
||||
eipContract := new(contracts.Testcontract)
|
||||
|
||||
m, chain, err := genBlocks(t, data.genesisSpec, map[int]tx{
|
||||
m, chain, err := genBlocks(t, data.genesisSpec, map[int]txn{
|
||||
0: {
|
||||
getBlockTx(from, to, uint256.NewInt(10)),
|
||||
fromKey,
|
||||
@ -627,7 +627,7 @@ func TestAccountDeleteIncorrectRoot(t *testing.T) {
|
||||
}
|
||||
|
||||
// BLOCK 1
|
||||
if err = m.InsertChain(chain.Slice(0, 1)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(0, 1), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@ -645,7 +645,7 @@ func TestAccountDeleteIncorrectRoot(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// BLOCK 2
|
||||
if err = m.InsertChain(chain.Slice(1, 2)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(1, 2), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@ -663,7 +663,7 @@ func TestAccountDeleteIncorrectRoot(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// BLOCK 3
|
||||
if err = m.InsertChain(chain.Slice(2, 3)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(2, 3), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@ -672,12 +672,12 @@ func TestAccountDeleteIncorrectRoot(t *testing.T) {
|
||||
incorrectHeader.Root = chain.Headers[1].Root
|
||||
incorrectBlock := types.NewBlock(&incorrectHeader, chain.Blocks[3].Transactions(), chain.Blocks[3].Uncles(), chain.Receipts[3], nil)
|
||||
incorrectChain := &core.ChainPack{Blocks: []*types.Block{incorrectBlock}, Headers: []*types.Header{&incorrectHeader}, TopBlock: incorrectBlock}
|
||||
if err = m.InsertChain(incorrectChain); err == nil {
|
||||
if err = m.InsertChain(incorrectChain, nil); err == nil {
|
||||
t.Fatal("should fail")
|
||||
}
|
||||
|
||||
// BLOCK 4
|
||||
if err = m.InsertChain(chain.Slice(3, 4)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(3, 4), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
@ -733,12 +733,12 @@ func getGenesis(funds ...*big.Int) initialData {
|
||||
}
|
||||
}
|
||||
|
||||
type tx struct {
|
||||
type txn struct {
|
||||
txFn blockTx
|
||||
key *ecdsa.PrivateKey
|
||||
}
|
||||
|
||||
func genBlocks(t *testing.T, gspec *types.Genesis, txs map[int]tx) (*stages.MockSentry, *core.ChainPack, error) {
|
||||
func genBlocks(t *testing.T, gspec *types.Genesis, txs map[int]txn) (*stages.MockSentry, *core.ChainPack, error) {
|
||||
key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
||||
m := stages.MockWithGenesis(t, gspec, key, false)
|
||||
|
||||
|
@ -222,7 +222,7 @@ func InsertChain(ethereum *eth.Ethereum, chain *core.ChainPack, logger log.Logge
|
||||
blockReader, _ := ethereum.BlockIO()
|
||||
|
||||
hook := stages.NewHook(ethereum.SentryCtx(), ethereum.Notifications(), ethereum.StagedSync(), blockReader, ethereum.ChainConfig(), logger, sentryControlServer.UpdateHead)
|
||||
_, err := stages.StageLoopStep(ethereum.SentryCtx(), ethereum.ChainDB(), ethereum.StagedSync(), initialCycle, logger, blockReader, hook)
|
||||
err := stages.StageLoopStep(ethereum.SentryCtx(), ethereum.ChainDB(), nil, ethereum.StagedSync(), initialCycle, logger, blockReader, hook)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -178,7 +178,7 @@ func createDumpTestKV(t *testing.T, chainSize int) *stages.MockSentry {
|
||||
t.Error(err)
|
||||
}
|
||||
// Construct testing chain
|
||||
if err = m.InsertChain(chain); err != nil {
|
||||
if err = m.InsertChain(chain, nil); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
|
@ -80,7 +80,7 @@ func newCanonical(t *testing.T, n int) *stages.MockSentry {
|
||||
|
||||
// Full block-chain requested
|
||||
chain := makeBlockChain(m.Genesis, n, m, canonicalSeed)
|
||||
if err := m.InsertChain(chain); err != nil {
|
||||
if err := m.InsertChain(chain, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return m
|
||||
@ -146,7 +146,7 @@ func testFork(t *testing.T, m *stages.MockSentry, i, n int, comparator func(td1,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
if err = m.InsertChain(blockChainB); err != nil {
|
||||
if err = m.InsertChain(blockChainB, nil); err != nil {
|
||||
t.Fatalf("failed to insert forking chain: %v", err)
|
||||
}
|
||||
currentBlockHash := blockChainB.TopBlock.Hash()
|
||||
@ -162,7 +162,7 @@ func testFork(t *testing.T, m *stages.MockSentry, i, n int, comparator func(td1,
|
||||
require.NoError(t, err)
|
||||
|
||||
// Sanity check that the forked chain can be imported into the original
|
||||
if err := canonicalMock.InsertChain(blockChainB); err != nil {
|
||||
if err := canonicalMock.InsertChain(blockChainB, nil); err != nil {
|
||||
t.Fatalf("failed to import forked block chain: %v", err)
|
||||
}
|
||||
// Compare the total difficulties of the chains
|
||||
@ -173,8 +173,8 @@ func TestLastBlock(t *testing.T) {
|
||||
m := newCanonical(t, 0)
|
||||
var err error
|
||||
|
||||
chain := makeBlockChain(current(m), 1, m, 0)
|
||||
if err = m.InsertChain(chain); err != nil {
|
||||
chain := makeBlockChain(current(m, nil), 1, m, 0)
|
||||
if err = m.InsertChain(chain, nil); err != nil {
|
||||
t.Fatalf("Failed to insert block: %v", err)
|
||||
}
|
||||
|
||||
@ -272,10 +272,10 @@ func testBrokenChain(t *testing.T) {
|
||||
m := newCanonical(t, 10)
|
||||
|
||||
// Create a forked chain, and try to insert with a missing link
|
||||
chain := makeBlockChain(current(m), 5, m, forkSeed)
|
||||
chain := makeBlockChain(current(m, nil), 5, m, forkSeed)
|
||||
brokenChain := chain.Slice(1, chain.Length())
|
||||
|
||||
if err := m.InsertChain(brokenChain); err == nil {
|
||||
if err := m.InsertChain(brokenChain, nil); err == nil {
|
||||
t.Errorf("broken block chain not reported")
|
||||
}
|
||||
}
|
||||
@ -312,27 +312,32 @@ func testReorg(t *testing.T, first, second []int64, td int64) {
|
||||
// Create a pristine chain and database
|
||||
m := newCanonical(t, 0)
|
||||
// Insert an easy and a difficult chain afterwards
|
||||
easyChain, err := core.GenerateChain(m.ChainConfig, current(m), m.Engine, m.DB, len(first), func(i int, b *core.BlockGen) {
|
||||
easyChain, err := core.GenerateChain(m.ChainConfig, current(m, nil), m.Engine, m.DB, len(first), func(i int, b *core.BlockGen) {
|
||||
b.OffsetTime(first[i])
|
||||
}, false /* intermediateHashes */)
|
||||
if err != nil {
|
||||
t.Fatalf("generate chain: %v", err)
|
||||
}
|
||||
diffChain, err := core.GenerateChain(m.ChainConfig, current(m), m.Engine, m.DB, len(second), func(i int, b *core.BlockGen) {
|
||||
diffChain, err := core.GenerateChain(m.ChainConfig, current(m, nil), m.Engine, m.DB, len(second), func(i int, b *core.BlockGen) {
|
||||
b.OffsetTime(second[i])
|
||||
}, false /* intermediateHashes */)
|
||||
if err != nil {
|
||||
t.Fatalf("generate chain: %v", err)
|
||||
}
|
||||
if err = m.InsertChain(easyChain); err != nil {
|
||||
|
||||
tx, err := m.DB.BeginRw(m.Ctx)
|
||||
if err != nil {
|
||||
fmt.Printf("beginro error: %v\n", err)
|
||||
return
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
if err = m.InsertChain(easyChain, tx); err != nil {
|
||||
t.Fatalf("failed to insert easy chain: %v", err)
|
||||
}
|
||||
if err = m.InsertChain(diffChain); err != nil {
|
||||
if err = m.InsertChain(diffChain, tx); err != nil {
|
||||
t.Fatalf("failed to insert difficult chain: %v", err)
|
||||
}
|
||||
tx, err := m.DB.BeginRo(context.Background())
|
||||
require.NoError(err)
|
||||
defer tx.Rollback()
|
||||
|
||||
// Check that the chain is valid number and link wise
|
||||
prev, err := m.BlockReader.CurrentBlock(tx)
|
||||
@ -370,12 +375,12 @@ func testBadHashes(t *testing.T) {
|
||||
var err error
|
||||
|
||||
// Create a chain, ban a hash and try to import
|
||||
chain := makeBlockChain(current(m), 3, m, 10)
|
||||
chain := makeBlockChain(current(m, nil), 3, m, 10)
|
||||
|
||||
core.BadHashes[chain.Headers[2].Hash()] = true
|
||||
defer func() { delete(core.BadHashes, chain.Headers[2].Hash()) }()
|
||||
|
||||
err = m.InsertChain(chain)
|
||||
err = m.InsertChain(chain, nil)
|
||||
if !errors.Is(err, core.ErrBlacklistedHash) {
|
||||
t.Errorf("error mismatch: have: %v, want: %v", err, core.ErrBlacklistedHash)
|
||||
}
|
||||
@ -444,7 +449,7 @@ func TestChainTxReorgs(t *testing.T) {
|
||||
t.Fatalf("generate chain: %v", err)
|
||||
}
|
||||
// Import the chain. This runs all block validation rules.
|
||||
if err1 := m.InsertChain(chain); err1 != nil {
|
||||
if err1 := m.InsertChain(chain, nil); err1 != nil {
|
||||
t.Fatalf("failed to insert original chain: %v", err1)
|
||||
}
|
||||
|
||||
@ -471,7 +476,7 @@ func TestChainTxReorgs(t *testing.T) {
|
||||
t.Fatalf("generate chain: %v", err)
|
||||
}
|
||||
|
||||
if err := m.InsertChain(chain); err != nil {
|
||||
if err := m.InsertChain(chain, nil); err != nil {
|
||||
t.Fatalf("failed to insert forked chain: %v", err)
|
||||
}
|
||||
tx, err := m.DB.BeginRo(context.Background())
|
||||
@ -559,7 +564,7 @@ func TestCanonicalBlockRetrieval(t *testing.T) {
|
||||
if err2 != nil {
|
||||
t.Fatalf("generate chain: %v", err2)
|
||||
}
|
||||
err := m.InsertChain(chain)
|
||||
err := m.InsertChain(chain, nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
tx, err := m.DB.BeginRo(m.Ctx)
|
||||
@ -652,7 +657,7 @@ func TestEIP155Transition(t *testing.T) {
|
||||
t.Fatalf("generate chain: %v", chainErr)
|
||||
}
|
||||
|
||||
if chainErr = m.InsertChain(chain); chainErr != nil {
|
||||
if chainErr = m.InsertChain(chain, nil); chainErr != nil {
|
||||
t.Fatal(chainErr)
|
||||
}
|
||||
if err := m.DB.View(context.Background(), func(tx kv.Tx) error {
|
||||
@ -692,7 +697,7 @@ func TestEIP155Transition(t *testing.T) {
|
||||
if chainErr != nil {
|
||||
t.Fatalf("generate blocks: %v", chainErr)
|
||||
}
|
||||
if err := m.InsertChain(chain); err == nil {
|
||||
if err := m.InsertChain(chain, nil); err == nil {
|
||||
t.Errorf("expected error")
|
||||
}
|
||||
}
|
||||
@ -776,7 +781,7 @@ func doModesTest(t *testing.T, pm prune.Mode) error {
|
||||
return fmt.Errorf("generate blocks: %w", err)
|
||||
}
|
||||
|
||||
if err = m.InsertChain(chain); err != nil {
|
||||
if err = m.InsertChain(chain, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@ -974,42 +979,37 @@ func TestEIP161AccountRemoval(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("generate blocks: %v", err)
|
||||
}
|
||||
tx, err := m.DB.BeginRw(m.Ctx)
|
||||
if err != nil {
|
||||
fmt.Printf("beginro error: %v\n", err)
|
||||
return
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
// account must exist pre eip 161
|
||||
if err = m.InsertChain(chain.Slice(0, 1)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(0, 1), tx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = m.DB.View(context.Background(), func(tx kv.Tx) error {
|
||||
if st := state.New(m.NewStateReader(tx)); !st.Exist(theAddr) {
|
||||
t.Error("expected account to exist")
|
||||
}
|
||||
return nil
|
||||
})
|
||||
require.NoError(t, err)
|
||||
if st := state.New(m.NewStateReader(tx)); !st.Exist(theAddr) {
|
||||
t.Error("expected account to exist")
|
||||
}
|
||||
|
||||
// account needs to be deleted post eip 161
|
||||
if err = m.InsertChain(chain.Slice(1, 2)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(1, 2), tx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = m.DB.View(m.Ctx, func(tx kv.Tx) error {
|
||||
if st := state.New(m.NewStateReader(tx)); st.Exist(theAddr) {
|
||||
t.Error("account should not exist")
|
||||
}
|
||||
return nil
|
||||
})
|
||||
require.NoError(t, err)
|
||||
if st := state.New(m.NewStateReader(tx)); st.Exist(theAddr) {
|
||||
t.Error("account should not exist")
|
||||
}
|
||||
|
||||
// account mustn't be created post eip 161
|
||||
if err = m.InsertChain(chain.Slice(2, 3)); err != nil {
|
||||
if err = m.InsertChain(chain.Slice(2, 3), tx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = m.DB.View(m.Ctx, func(tx kv.Tx) error {
|
||||
if st := state.New(m.NewStateReader(tx)); st.Exist(theAddr) {
|
||||
t.Error("account should not exist")
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if st := state.New(m.NewStateReader(tx)); st.Exist(theAddr) {
|
||||
t.Error("account should not exist")
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
}
|
||||
|
||||
func TestDoubleAccountRemoval(t *testing.T) {
|
||||
@ -1057,24 +1057,20 @@ func TestDoubleAccountRemoval(t *testing.T) {
|
||||
t.Fatalf("generate blocks: %v", err)
|
||||
}
|
||||
|
||||
err = m.InsertChain(chain)
|
||||
assert.NoError(t, err)
|
||||
|
||||
err = m.DB.View(m.Ctx, func(tx kv.Tx) error {
|
||||
st := state.New(m.NewStateReader(tx))
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, st.Exist(theAddr), "Contract should've been removed")
|
||||
return nil
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
|
||||
tx, err := m.DB.BeginRo(m.Ctx)
|
||||
tx, err := m.DB.BeginRw(m.Ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("read only db tx to read state: %v", err)
|
||||
fmt.Printf("beginro error: %v\n", err)
|
||||
return
|
||||
}
|
||||
defer tx.Rollback()
|
||||
err = m.InsertChain(chain, tx)
|
||||
assert.NoError(t, err)
|
||||
|
||||
st := state.New(m.NewHistoryStateReader(1, tx))
|
||||
st := state.New(m.NewStateReader(tx))
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, st.Exist(theAddr), "Contract should've been removed")
|
||||
|
||||
st = state.New(m.NewHistoryStateReader(1, tx))
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, st.Exist(theAddr), "Contract should not exist at block #0")
|
||||
|
||||
@ -1121,7 +1117,7 @@ func TestBlockchainHeaderchainReorgConsistency(t *testing.T) {
|
||||
// Import the canonical and fork chain side by side, verifying the current block
|
||||
// and current header consistency
|
||||
for i := 0; i < chain.Length(); i++ {
|
||||
if err := m2.InsertChain(chain.Slice(i, i+1)); err != nil {
|
||||
if err := m2.InsertChain(chain.Slice(i, i+1), nil); err != nil {
|
||||
t.Fatalf("block %d: failed to insert into chain: %v", i, err)
|
||||
}
|
||||
|
||||
@ -1134,7 +1130,7 @@ func TestBlockchainHeaderchainReorgConsistency(t *testing.T) {
|
||||
if b.Hash() != h.Hash() {
|
||||
t.Errorf("block %d: current block/header mismatch: block #%d [%x…], header #%d [%x…]", i, b.Number(), b.Hash().Bytes()[:4], h.Number, h.Hash().Bytes()[:4])
|
||||
}
|
||||
if err := m2.InsertChain(forks[i]); err != nil {
|
||||
if err := m2.InsertChain(forks[i], nil); err != nil {
|
||||
t.Fatalf(" fork %d: failed to insert into chain: %v", i, err)
|
||||
}
|
||||
b, err = m.BlockReader.CurrentBlock(tx)
|
||||
@ -1188,20 +1184,20 @@ func TestLargeReorgTrieGC(t *testing.T) {
|
||||
}
|
||||
|
||||
// Import the shared chain and the original canonical one
|
||||
if err := m2.InsertChain(shared); err != nil {
|
||||
if err := m2.InsertChain(shared, nil); err != nil {
|
||||
t.Fatalf("failed to insert shared chain: %v", err)
|
||||
}
|
||||
if err := m2.InsertChain(original); err != nil {
|
||||
if err := m2.InsertChain(original, nil); err != nil {
|
||||
t.Fatalf("failed to insert original chain: %v", err)
|
||||
}
|
||||
// Import the competitor chain without exceeding the canonical's TD and ensure
|
||||
// we have not processed any of the blocks (protection against malicious blocks)
|
||||
if err := m2.InsertChain(competitor.Slice(0, competitor.Length()-2)); err != nil {
|
||||
if err := m2.InsertChain(competitor.Slice(0, competitor.Length()-2), nil); err != nil {
|
||||
t.Fatalf("failed to insert competitor chain: %v", err)
|
||||
}
|
||||
// Import the head of the competitor chain, triggering the reorg and ensure we
|
||||
// successfully reprocess all the stashed away blocks.
|
||||
if err := m2.InsertChain(competitor.Slice(competitor.Length()-2, competitor.Length())); err != nil {
|
||||
if err := m2.InsertChain(competitor.Slice(competitor.Length()-2, competitor.Length()), nil); err != nil {
|
||||
t.Fatalf("failed to finalize competitor chain: %v", err)
|
||||
}
|
||||
}
|
||||
@ -1242,12 +1238,12 @@ func TestLowDiffLongChain(t *testing.T) {
|
||||
// Import the canonical chain
|
||||
m2 := stages.Mock(t)
|
||||
|
||||
if err := m2.InsertChain(chain); err != nil {
|
||||
if err := m2.InsertChain(chain, nil); err != nil {
|
||||
t.Fatalf("failed to insert into chain: %v", err)
|
||||
}
|
||||
|
||||
// And now import the fork
|
||||
if err := m2.InsertChain(fork); err != nil {
|
||||
if err := m2.InsertChain(fork, nil); err != nil {
|
||||
t.Fatalf("failed to insert into chain: %v", err)
|
||||
}
|
||||
|
||||
@ -1340,7 +1336,7 @@ func TestDeleteCreateRevert(t *testing.T) {
|
||||
t.Fatalf("generate blocks: %v", err)
|
||||
}
|
||||
|
||||
if err := m.InsertChain(chain); err != nil {
|
||||
if err := m.InsertChain(chain, nil); err != nil {
|
||||
t.Fatalf("failed to insert into chain: %v", err)
|
||||
}
|
||||
}
|
||||
@ -1445,7 +1441,7 @@ func TestDeleteRecreateSlots(t *testing.T) {
|
||||
t.Fatalf("generate blocks: %v", err)
|
||||
}
|
||||
// Import the canonical chain
|
||||
if err := m.InsertChain(chain); err != nil {
|
||||
if err := m.InsertChain(chain, nil); err != nil {
|
||||
t.Fatalf("failed to insert into chain: %v", err)
|
||||
}
|
||||
err = m.DB.View(m.Ctx, func(tx kv.Tx) error {
|
||||
@ -1563,7 +1559,7 @@ func TestCVE2020_26265(t *testing.T) {
|
||||
t.Fatalf("generate blocks: %v", err)
|
||||
}
|
||||
// Import the canonical chain
|
||||
if err := m.InsertChain(chain); err != nil {
|
||||
if err := m.InsertChain(chain, nil); err != nil {
|
||||
t.Fatalf("failed to insert into chain: %v", err)
|
||||
}
|
||||
err = m.DB.View(m.Ctx, func(tx kv.Tx) error {
|
||||
@ -1630,7 +1626,7 @@ func TestDeleteRecreateAccount(t *testing.T) {
|
||||
t.Fatalf("generate blocks: %v", err)
|
||||
}
|
||||
// Import the canonical chain
|
||||
if err := m.InsertChain(chain); err != nil {
|
||||
if err := m.InsertChain(chain, nil); err != nil {
|
||||
t.Fatalf("failed to insert into chain: %v", err)
|
||||
}
|
||||
err = m.DB.View(m.Ctx, func(tx kv.Tx) error {
|
||||
@ -1810,7 +1806,7 @@ func TestDeleteRecreateSlotsAcrossManyBlocks(t *testing.T) {
|
||||
}
|
||||
for i := range chain.Blocks {
|
||||
blockNum := i + 1
|
||||
if err := m.InsertChain(chain.Slice(i, i+1)); err != nil {
|
||||
if err := m.InsertChain(chain.Slice(i, i+1), nil); err != nil {
|
||||
t.Fatalf("block %d: failed to insert into chain: %v", i, err)
|
||||
}
|
||||
err = m.DB.View(m.Ctx, func(tx kv.Tx) error {
|
||||
@ -1953,7 +1949,7 @@ func TestInitThenFailCreateContract(t *testing.T) {
|
||||
// First block tries to create, but fails
|
||||
{
|
||||
block := chain.Blocks[0]
|
||||
if err := m.InsertChain(chain.Slice(0, 1)); err != nil {
|
||||
if err := m.InsertChain(chain.Slice(0, 1), nil); err != nil {
|
||||
t.Fatalf("block %d: failed to insert into chain: %v", block.NumberU64(), err)
|
||||
}
|
||||
statedb = state.New(m.NewHistoryStateReader(1, tx))
|
||||
@ -1963,7 +1959,7 @@ func TestInitThenFailCreateContract(t *testing.T) {
|
||||
}
|
||||
// Import the rest of the blocks
|
||||
for i, block := range chain.Blocks[1:] {
|
||||
if err := m.InsertChain(chain.Slice(1+i, 2+i)); err != nil {
|
||||
if err := m.InsertChain(chain.Slice(1+i, 2+i), nil); err != nil {
|
||||
t.Fatalf("block %d: failed to insert into chain: %v", block.NumberU64(), err)
|
||||
}
|
||||
}
|
||||
@ -2037,7 +2033,7 @@ func TestEIP2718Transition(t *testing.T) {
|
||||
|
||||
// Import the canonical chain
|
||||
|
||||
if err = m.InsertChain(chain); err != nil {
|
||||
if err = m.InsertChain(chain, nil); err != nil {
|
||||
t.Fatalf("failed to insert into chain: %v", err)
|
||||
}
|
||||
|
||||
@ -2139,7 +2135,12 @@ func TestEIP1559Transition(t *testing.T) {
|
||||
}
|
||||
// Import the canonical chain
|
||||
|
||||
if err = m.InsertChain(chain); err != nil {
|
||||
if err := m.DB.Update(m.Ctx, func(tx kv.RwTx) error {
|
||||
if err = m.InsertChain(chain, tx); err != nil {
|
||||
t.Fatalf("failed to insert into chain: %v", err)
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
t.Fatalf("failed to insert into chain: %v", err)
|
||||
}
|
||||
|
||||
@ -2187,7 +2188,7 @@ func TestEIP1559Transition(t *testing.T) {
|
||||
t.Fatalf("generate chain: %v", err)
|
||||
}
|
||||
|
||||
if err = m.InsertChain(chain); err != nil {
|
||||
if err = m.InsertChain(chain, nil); err != nil {
|
||||
t.Fatalf("failed to insert into chain: %v", err)
|
||||
}
|
||||
|
||||
@ -2217,7 +2218,14 @@ func TestEIP1559Transition(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func current(m *stages.MockSentry) *types.Block {
|
||||
func current(m *stages.MockSentry, tx kv.Tx) *types.Block {
|
||||
if tx != nil {
|
||||
b, err := m.BlockReader.CurrentBlock(tx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
tx, err := m.DB.BeginRo(context.Background())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
@ -92,22 +92,22 @@ func TestGenerateChain(t *testing.T) {
|
||||
fmt.Printf("generate chain: %v\n", err)
|
||||
}
|
||||
|
||||
// Import the chain. This runs all block validation rules.
|
||||
if err := m.InsertChain(chain); err != nil {
|
||||
fmt.Printf("insert error%v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
tx, err := m.DB.BeginRo(m.Ctx)
|
||||
tx, err := m.DB.BeginRw(m.Ctx)
|
||||
if err != nil {
|
||||
fmt.Printf("beginro error: %v\n", err)
|
||||
return
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
// Import the chain. This runs all block validation rules.
|
||||
if err := m.InsertChain(chain, tx); err != nil {
|
||||
fmt.Printf("insert error%v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
st := state.New(m.NewStateReader(tx))
|
||||
if big.NewInt(5).Cmp(current(m).Number()) != 0 {
|
||||
t.Errorf("wrong block number: %d", current(m).Number())
|
||||
if big.NewInt(5).Cmp(current(m, tx).Number()) != 0 {
|
||||
t.Errorf("wrong block number: %d", current(m, tx).Number())
|
||||
}
|
||||
if !uint256.NewInt(989000).Eq(st.GetBalance(addr1)) {
|
||||
t.Errorf("wrong balance of addr1: %s", st.GetBalance(addr1))
|
||||
|
@ -123,7 +123,7 @@ func TestSetupGenesis(t *testing.T) {
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if err = m.InsertChain(chain); err != nil {
|
||||
if err = m.InsertChain(chain, nil); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
// This should return a compatibility error.
|
||||
|
@ -524,7 +524,7 @@ func (ms *MockSentry) numberOfPoWBlocks(chain *core.ChainPack) int {
|
||||
return chain.NumberOfPoWBlocks()
|
||||
}
|
||||
|
||||
func (ms *MockSentry) insertPoWBlocks(chain *core.ChainPack) error {
|
||||
func (ms *MockSentry) insertPoWBlocks(chain *core.ChainPack, tx kv.RwTx) error {
|
||||
n := ms.numberOfPoWBlocks(chain)
|
||||
if n == 0 {
|
||||
// No Proof-of-Work blocks
|
||||
@ -586,7 +586,7 @@ func (ms *MockSentry) insertPoWBlocks(chain *core.ChainPack) error {
|
||||
}
|
||||
initialCycle := MockInsertAsInitialCycle
|
||||
hook := NewHook(ms.Ctx, ms.Notifications, ms.Sync, ms.BlockReader, ms.ChainConfig, ms.Log, ms.UpdateHead)
|
||||
if _, err = StageLoopStep(ms.Ctx, ms.DB, ms.Sync, initialCycle, ms.Log, ms.BlockReader, hook); err != nil {
|
||||
if err = StageLoopStep(ms.Ctx, ms.DB, tx, ms.Sync, initialCycle, ms.Log, ms.BlockReader, hook); err != nil {
|
||||
return err
|
||||
}
|
||||
if ms.TxPool != nil {
|
||||
@ -595,7 +595,7 @@ func (ms *MockSentry) insertPoWBlocks(chain *core.ChainPack) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ms *MockSentry) insertPoSBlocks(chain *core.ChainPack) error {
|
||||
func (ms *MockSentry) insertPoSBlocks(chain *core.ChainPack, tx kv.RwTx) error {
|
||||
n := ms.numberOfPoWBlocks(chain)
|
||||
if n >= chain.Length() {
|
||||
return nil
|
||||
@ -610,11 +610,11 @@ func (ms *MockSentry) insertPoSBlocks(chain *core.ChainPack) error {
|
||||
|
||||
initialCycle := MockInsertAsInitialCycle
|
||||
hook := NewHook(ms.Ctx, ms.Notifications, ms.Sync, ms.BlockReader, ms.ChainConfig, ms.Log, ms.UpdateHead)
|
||||
headBlockHash, err := StageLoopStep(ms.Ctx, ms.DB, ms.Sync, initialCycle, ms.Log, ms.BlockReader, hook)
|
||||
err := StageLoopStep(ms.Ctx, ms.DB, tx, ms.Sync, initialCycle, ms.Log, ms.BlockReader, hook)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
SendPayloadStatus(ms.HeaderDownload(), headBlockHash, err)
|
||||
SendPayloadStatus(ms.HeaderDownload(), rawdb.ReadHeadBlockHash(tx), err)
|
||||
ms.ReceivePayloadStatus()
|
||||
|
||||
fc := engineapi.ForkChoiceMessage{
|
||||
@ -623,39 +623,46 @@ func (ms *MockSentry) insertPoSBlocks(chain *core.ChainPack) error {
|
||||
FinalizedBlockHash: chain.TopBlock.Hash(),
|
||||
}
|
||||
ms.SendForkChoiceRequest(&fc)
|
||||
headBlockHash, err = StageLoopStep(ms.Ctx, ms.DB, ms.Sync, initialCycle, ms.Log, ms.BlockReader, hook)
|
||||
err = StageLoopStep(ms.Ctx, ms.DB, tx, ms.Sync, initialCycle, ms.Log, ms.BlockReader, hook)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
SendPayloadStatus(ms.HeaderDownload(), headBlockHash, err)
|
||||
SendPayloadStatus(ms.HeaderDownload(), rawdb.ReadHeadBlockHash(tx), err)
|
||||
ms.ReceivePayloadStatus()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ms *MockSentry) InsertChain(chain *core.ChainPack) error {
|
||||
if err := ms.insertPoWBlocks(chain); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ms.insertPoSBlocks(chain); err != nil {
|
||||
return err
|
||||
}
|
||||
// Check if the latest header was imported or rolled back
|
||||
if err := ms.DB.View(ms.Ctx, func(tx kv.Tx) error {
|
||||
if rawdb.ReadHeader(tx, chain.TopBlock.Hash(), chain.TopBlock.NumberU64()) == nil {
|
||||
return fmt.Errorf("did not import block %d %x", chain.TopBlock.NumberU64(), chain.TopBlock.Hash())
|
||||
}
|
||||
execAt, err := stages.GetStageProgress(tx, stages.Execution)
|
||||
func (ms *MockSentry) InsertChain(chain *core.ChainPack, tx kv.RwTx) error {
|
||||
externalTx := tx != nil
|
||||
if !externalTx {
|
||||
var err error
|
||||
tx, err = ms.DB.BeginRw(ms.Ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if execAt == 0 {
|
||||
return fmt.Errorf("sentryMock.InsertChain end up with Execution stage progress = 0")
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
defer tx.Rollback()
|
||||
}
|
||||
|
||||
if err := ms.insertPoWBlocks(chain, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ms.insertPoSBlocks(chain, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Check if the latest header was imported or rolled back
|
||||
if rawdb.ReadHeader(tx, chain.TopBlock.Hash(), chain.TopBlock.NumberU64()) == nil {
|
||||
return fmt.Errorf("did not import block %d %x", chain.TopBlock.NumberU64(), chain.TopBlock.Hash())
|
||||
}
|
||||
execAt, err := stages.GetStageProgress(tx, stages.Execution)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if execAt == 0 {
|
||||
return fmt.Errorf("sentryMock.InsertChain end up with Execution stage progress = 0")
|
||||
}
|
||||
|
||||
if ms.sentriesClient.Hd.IsBadHeader(chain.TopBlock.Hash()) {
|
||||
return fmt.Errorf("block %d %x was invalid", chain.TopBlock.NumberU64(), chain.TopBlock.Hash())
|
||||
}
|
||||
@ -673,6 +680,12 @@ func (ms *MockSentry) InsertChain(chain *core.ChainPack) error {
|
||||
// return err
|
||||
//}
|
||||
//}
|
||||
|
||||
if !externalTx {
|
||||
if err := tx.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -8,6 +8,7 @@ import (
|
||||
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
||||
"github.com/ledgerwatch/erigon-lib/gointerfaces/remote"
|
||||
"github.com/ledgerwatch/erigon-lib/gointerfaces/sentry"
|
||||
"github.com/ledgerwatch/erigon/core/rawdb"
|
||||
"github.com/ledgerwatch/log/v3"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
@ -58,7 +59,7 @@ func TestHeaderStep(t *testing.T) {
|
||||
m.ReceiveWg.Wait() // Wait for all messages to be processed before we proceed
|
||||
|
||||
initialCycle := stages.MockInsertAsInitialCycle
|
||||
if _, err := stages.StageLoopStep(m.Ctx, m.DB, m.Sync, initialCycle, m.Log, m.BlockReader, nil); err != nil {
|
||||
if err := stages.StageLoopStep(m.Ctx, m.DB, nil, m.Sync, initialCycle, m.Log, m.BlockReader, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
@ -96,7 +97,7 @@ func TestMineBlockWith1Tx(t *testing.T) {
|
||||
m.ReceiveWg.Wait() // Wait for all messages to be processed before we proceeed
|
||||
|
||||
initialCycle := stages.MockInsertAsInitialCycle
|
||||
if _, err := stages.StageLoopStep(m.Ctx, m.DB, m.Sync, initialCycle, log.New(), m.BlockReader, nil); err != nil {
|
||||
if err := stages.StageLoopStep(m.Ctx, m.DB, nil, m.Sync, initialCycle, log.New(), m.BlockReader, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
@ -164,7 +165,7 @@ func TestReorg(t *testing.T) {
|
||||
m.ReceiveWg.Wait() // Wait for all messages to be processed before we proceeed
|
||||
|
||||
initialCycle := stages.MockInsertAsInitialCycle
|
||||
if _, err := stages.StageLoopStep(m.Ctx, m.DB, m.Sync, initialCycle, m.Log, m.BlockReader, nil); err != nil {
|
||||
if err := stages.StageLoopStep(m.Ctx, m.DB, nil, m.Sync, initialCycle, m.Log, m.BlockReader, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@ -217,7 +218,7 @@ func TestReorg(t *testing.T) {
|
||||
m.ReceiveWg.Wait() // Wait for all messages to be processed before we proceeed
|
||||
|
||||
initialCycle = false
|
||||
if _, err := stages.StageLoopStep(m.Ctx, m.DB, m.Sync, initialCycle, m.Log, m.BlockReader, nil); err != nil {
|
||||
if err := stages.StageLoopStep(m.Ctx, m.DB, nil, m.Sync, initialCycle, m.Log, m.BlockReader, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@ -260,7 +261,7 @@ func TestReorg(t *testing.T) {
|
||||
m.ReceiveWg.Wait() // Wait for all messages to be processed before we proceeed
|
||||
|
||||
// This is unwind step
|
||||
if _, err := stages.StageLoopStep(m.Ctx, m.DB, m.Sync, initialCycle, m.Log, m.BlockReader, nil); err != nil {
|
||||
if err := stages.StageLoopStep(m.Ctx, m.DB, nil, m.Sync, initialCycle, m.Log, m.BlockReader, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@ -297,7 +298,7 @@ func TestReorg(t *testing.T) {
|
||||
m.ReceiveWg.Wait() // Wait for all messages to be processed before we proceeed
|
||||
|
||||
initialCycle = stages.MockInsertAsInitialCycle
|
||||
if _, err := stages.StageLoopStep(m.Ctx, m.DB, m.Sync, initialCycle, m.Log, m.BlockReader, nil); err != nil {
|
||||
if err := stages.StageLoopStep(m.Ctx, m.DB, nil, m.Sync, initialCycle, m.Log, m.BlockReader, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
@ -393,7 +394,7 @@ func TestAnchorReplace(t *testing.T) {
|
||||
m.ReceiveWg.Wait() // Wait for all messages to be processed before we proceeed
|
||||
|
||||
initialCycle := stages.MockInsertAsInitialCycle
|
||||
if _, err := stages.StageLoopStep(m.Ctx, m.DB, m.Sync, initialCycle, m.Log, m.BlockReader, nil); err != nil {
|
||||
if err := stages.StageLoopStep(m.Ctx, m.DB, nil, m.Sync, initialCycle, m.Log, m.BlockReader, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
@ -498,7 +499,7 @@ func TestAnchorReplace2(t *testing.T) {
|
||||
|
||||
initialCycle := stages.MockInsertAsInitialCycle
|
||||
hook := stages.NewHook(m.Ctx, m.Notifications, m.Sync, m.BlockReader, m.ChainConfig, m.Log, m.UpdateHead)
|
||||
if _, err := stages.StageLoopStep(m.Ctx, m.DB, m.Sync, initialCycle, m.Log, m.BlockReader, hook); err != nil {
|
||||
if err := stages.StageLoopStep(m.Ctx, m.DB, nil, m.Sync, initialCycle, m.Log, m.BlockReader, hook); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
@ -515,11 +516,14 @@ func TestForkchoiceToGenesis(t *testing.T) {
|
||||
m.SendForkChoiceRequest(&forkChoiceMessage)
|
||||
|
||||
initialCycle := stages.MockInsertAsInitialCycle
|
||||
headBlockHash, err := stages.StageLoopStep(m.Ctx, m.DB, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
tx, err := m.DB.BeginRw(m.Ctx)
|
||||
require.NoError(t, err)
|
||||
stages.SendPayloadStatus(m.HeaderDownload(), headBlockHash, err)
|
||||
defer tx.Rollback()
|
||||
err = stages.StageLoopStep(m.Ctx, m.DB, tx, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
require.NoError(t, err)
|
||||
stages.SendPayloadStatus(m.HeaderDownload(), rawdb.ReadHeadBlockHash(tx), err)
|
||||
|
||||
assert.Equal(t, m.Genesis.Hash(), headBlockHash)
|
||||
assert.Equal(t, m.Genesis.Hash(), rawdb.ReadHeadBlockHash(tx))
|
||||
|
||||
payloadStatus := m.ReceivePayloadStatus()
|
||||
assert.Equal(t, remote.EngineStatus_VALID, payloadStatus.Status)
|
||||
@ -536,10 +540,13 @@ func TestBogusForkchoice(t *testing.T) {
|
||||
}
|
||||
m.SendForkChoiceRequest(&forkChoiceMessage)
|
||||
|
||||
initialCycle := stages.MockInsertAsInitialCycle
|
||||
headBlockHash, err := stages.StageLoopStep(m.Ctx, m.DB, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
tx, err := m.DB.BeginRw(m.Ctx)
|
||||
require.NoError(t, err)
|
||||
stages.SendPayloadStatus(m.HeaderDownload(), headBlockHash, err)
|
||||
defer tx.Rollback()
|
||||
initialCycle := stages.MockInsertAsInitialCycle
|
||||
err = stages.StageLoopStep(m.Ctx, m.DB, tx, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
require.NoError(t, err)
|
||||
stages.SendPayloadStatus(m.HeaderDownload(), rawdb.ReadHeadBlockHash(tx), err)
|
||||
|
||||
payloadStatus := m.ReceivePayloadStatus()
|
||||
assert.Equal(t, remote.EngineStatus_SYNCING, payloadStatus.Status)
|
||||
@ -552,9 +559,9 @@ func TestBogusForkchoice(t *testing.T) {
|
||||
}
|
||||
m.SendForkChoiceRequest(&forkChoiceMessage)
|
||||
|
||||
headBlockHash, err = stages.StageLoopStep(m.Ctx, m.DB, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
err = stages.StageLoopStep(m.Ctx, m.DB, tx, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
require.NoError(t, err)
|
||||
stages.SendPayloadStatus(m.HeaderDownload(), headBlockHash, err)
|
||||
stages.SendPayloadStatus(m.HeaderDownload(), rawdb.ReadHeadBlockHash(tx), err)
|
||||
|
||||
payloadStatus = m.ReceivePayloadStatus()
|
||||
assert.Equal(t, remote.EngineStatus_VALID, payloadStatus.Status)
|
||||
@ -571,10 +578,13 @@ func TestPoSDownloader(t *testing.T) {
|
||||
// Send a payload whose parent isn't downloaded yet
|
||||
m.SendPayloadRequest(chain.TopBlock)
|
||||
|
||||
initialCycle := stages.MockInsertAsInitialCycle
|
||||
headBlockHash, err := stages.StageLoopStep(m.Ctx, m.DB, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
tx, err := m.DB.BeginRw(m.Ctx)
|
||||
require.NoError(t, err)
|
||||
stages.SendPayloadStatus(m.HeaderDownload(), headBlockHash, err)
|
||||
defer tx.Rollback()
|
||||
initialCycle := stages.MockInsertAsInitialCycle
|
||||
err = stages.StageLoopStep(m.Ctx, m.DB, tx, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
require.NoError(t, err)
|
||||
stages.SendPayloadStatus(m.HeaderDownload(), rawdb.ReadHeadBlockHash(tx), err)
|
||||
|
||||
payloadStatus := m.ReceivePayloadStatus()
|
||||
assert.Equal(t, remote.EngineStatus_SYNCING, payloadStatus.Status)
|
||||
@ -592,14 +602,14 @@ func TestPoSDownloader(t *testing.T) {
|
||||
m.ReceiveWg.Wait()
|
||||
|
||||
// First cycle: save the downloaded header
|
||||
headBlockHash, err = stages.StageLoopStep(m.Ctx, m.DB, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
err = stages.StageLoopStep(m.Ctx, m.DB, tx, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
require.NoError(t, err)
|
||||
stages.SendPayloadStatus(m.HeaderDownload(), headBlockHash, err)
|
||||
stages.SendPayloadStatus(m.HeaderDownload(), rawdb.ReadHeadBlockHash(tx), err)
|
||||
|
||||
// Second cycle: process the previous beacon request
|
||||
headBlockHash, err = stages.StageLoopStep(m.Ctx, m.DB, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
err = stages.StageLoopStep(m.Ctx, m.DB, tx, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
require.NoError(t, err)
|
||||
stages.SendPayloadStatus(m.HeaderDownload(), headBlockHash, err)
|
||||
stages.SendPayloadStatus(m.HeaderDownload(), rawdb.ReadHeadBlockHash(tx), err)
|
||||
|
||||
// Point forkChoice to the head
|
||||
forkChoiceMessage := engineapi.ForkChoiceMessage{
|
||||
@ -608,14 +618,14 @@ func TestPoSDownloader(t *testing.T) {
|
||||
FinalizedBlockHash: chain.TopBlock.Hash(),
|
||||
}
|
||||
m.SendForkChoiceRequest(&forkChoiceMessage)
|
||||
headBlockHash, err = stages.StageLoopStep(m.Ctx, m.DB, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
err = stages.StageLoopStep(m.Ctx, m.DB, tx, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
require.NoError(t, err)
|
||||
stages.SendPayloadStatus(m.HeaderDownload(), headBlockHash, err)
|
||||
assert.Equal(t, chain.TopBlock.Hash(), headBlockHash)
|
||||
stages.SendPayloadStatus(m.HeaderDownload(), rawdb.ReadHeadBlockHash(tx), err)
|
||||
assert.Equal(t, chain.TopBlock.Hash(), rawdb.ReadHeadBlockHash(tx))
|
||||
|
||||
payloadStatus = m.ReceivePayloadStatus()
|
||||
assert.Equal(t, remote.EngineStatus_VALID, payloadStatus.Status)
|
||||
assert.Equal(t, chain.TopBlock.Hash(), headBlockHash)
|
||||
assert.Equal(t, chain.TopBlock.Hash(), rawdb.ReadHeadBlockHash(tx))
|
||||
}
|
||||
|
||||
// https://hackmd.io/GDc0maGsQeKfP8o2C7L52w
|
||||
@ -639,10 +649,13 @@ func TestPoSSyncWithInvalidHeader(t *testing.T) {
|
||||
payloadMessage := types.NewBlockFromStorage(invalidTip.Hash(), invalidTip, chain.TopBlock.Transactions(), nil, nil)
|
||||
m.SendPayloadRequest(payloadMessage)
|
||||
|
||||
initialCycle := stages.MockInsertAsInitialCycle
|
||||
headBlockHash, err := stages.StageLoopStep(m.Ctx, m.DB, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
tx, err := m.DB.BeginRw(m.Ctx)
|
||||
require.NoError(t, err)
|
||||
stages.SendPayloadStatus(m.HeaderDownload(), headBlockHash, err)
|
||||
defer tx.Rollback()
|
||||
initialCycle := stages.MockInsertAsInitialCycle
|
||||
err = stages.StageLoopStep(m.Ctx, m.DB, tx, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
require.NoError(t, err)
|
||||
stages.SendPayloadStatus(m.HeaderDownload(), rawdb.ReadHeadBlockHash(tx), err)
|
||||
|
||||
payloadStatus1 := m.ReceivePayloadStatus()
|
||||
assert.Equal(t, remote.EngineStatus_SYNCING, payloadStatus1.Status)
|
||||
@ -659,9 +672,9 @@ func TestPoSSyncWithInvalidHeader(t *testing.T) {
|
||||
}
|
||||
m.ReceiveWg.Wait()
|
||||
|
||||
headBlockHash, err = stages.StageLoopStep(m.Ctx, m.DB, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
err = stages.StageLoopStep(m.Ctx, m.DB, tx, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
require.NoError(t, err)
|
||||
stages.SendPayloadStatus(m.HeaderDownload(), headBlockHash, err)
|
||||
stages.SendPayloadStatus(m.HeaderDownload(), rawdb.ReadHeadBlockHash(tx), err)
|
||||
|
||||
// Point forkChoice to the invalid tip
|
||||
forkChoiceMessage := engineapi.ForkChoiceMessage{
|
||||
@ -670,7 +683,7 @@ func TestPoSSyncWithInvalidHeader(t *testing.T) {
|
||||
FinalizedBlockHash: invalidTip.Hash(),
|
||||
}
|
||||
m.SendForkChoiceRequest(&forkChoiceMessage)
|
||||
_, err = stages.StageLoopStep(m.Ctx, m.DB, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
err = stages.StageLoopStep(m.Ctx, m.DB, tx, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
bad, lastValidHash := m.HeaderDownload().IsBadHeaderPoS(invalidTip.Hash())
|
||||
@ -725,10 +738,15 @@ func TestPOSWrongTrieRootReorgs(t *testing.T) {
|
||||
|
||||
//------------------------------------------
|
||||
m.SendPayloadRequest(chain0.TopBlock)
|
||||
initialCycle := stages.MockInsertAsInitialCycle
|
||||
headBlockHash, err := stages.StageLoopStep(m.Ctx, m.DB, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
tx, err := m.DB.BeginRw(m.Ctx)
|
||||
|
||||
require.NoError(err)
|
||||
stages.SendPayloadStatus(m.HeaderDownload(), headBlockHash, err)
|
||||
defer tx.Rollback()
|
||||
initialCycle := stages.MockInsertAsInitialCycle
|
||||
err = stages.StageLoopStep(m.Ctx, m.DB, tx, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
require.NoError(err)
|
||||
stages.SendPayloadStatus(m.HeaderDownload(), rawdb.ReadHeadBlockHash(tx), err)
|
||||
|
||||
payloadStatus0 := m.ReceivePayloadStatus()
|
||||
assert.Equal(t, remote.EngineStatus_VALID, payloadStatus0.Status)
|
||||
forkChoiceMessage := engineapi.ForkChoiceMessage{
|
||||
@ -737,19 +755,19 @@ func TestPOSWrongTrieRootReorgs(t *testing.T) {
|
||||
FinalizedBlockHash: chain0.TopBlock.Hash(),
|
||||
}
|
||||
m.SendForkChoiceRequest(&forkChoiceMessage)
|
||||
headBlockHash, err = stages.StageLoopStep(m.Ctx, m.DB, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
err = stages.StageLoopStep(m.Ctx, m.DB, tx, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
require.NoError(err)
|
||||
stages.SendPayloadStatus(m.HeaderDownload(), headBlockHash, err)
|
||||
assert.Equal(t, chain0.TopBlock.Hash(), headBlockHash)
|
||||
stages.SendPayloadStatus(m.HeaderDownload(), rawdb.ReadHeadBlockHash(tx), err)
|
||||
assert.Equal(t, chain0.TopBlock.Hash(), rawdb.ReadHeadBlockHash(tx))
|
||||
payloadStatus0 = m.ReceivePayloadStatus()
|
||||
assert.Equal(t, remote.EngineStatus_VALID, payloadStatus0.Status)
|
||||
assert.Equal(t, chain0.TopBlock.Hash(), headBlockHash)
|
||||
assert.Equal(t, chain0.TopBlock.Hash(), rawdb.ReadHeadBlockHash(tx))
|
||||
|
||||
//------------------------------------------
|
||||
m.SendPayloadRequest(chain1.TopBlock)
|
||||
headBlockHash, err = stages.StageLoopStep(m.Ctx, m.DB, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
err = stages.StageLoopStep(m.Ctx, m.DB, tx, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
require.NoError(err)
|
||||
stages.SendPayloadStatus(m.HeaderDownload(), headBlockHash, err)
|
||||
stages.SendPayloadStatus(m.HeaderDownload(), rawdb.ReadHeadBlockHash(tx), err)
|
||||
payloadStatus1 := m.ReceivePayloadStatus()
|
||||
assert.Equal(t, remote.EngineStatus_VALID, payloadStatus1.Status)
|
||||
forkChoiceMessage = engineapi.ForkChoiceMessage{
|
||||
@ -758,19 +776,19 @@ func TestPOSWrongTrieRootReorgs(t *testing.T) {
|
||||
FinalizedBlockHash: chain1.TopBlock.Hash(),
|
||||
}
|
||||
m.SendForkChoiceRequest(&forkChoiceMessage)
|
||||
headBlockHash, err = stages.StageLoopStep(m.Ctx, m.DB, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
err = stages.StageLoopStep(m.Ctx, m.DB, tx, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
require.NoError(err)
|
||||
stages.SendPayloadStatus(m.HeaderDownload(), headBlockHash, err)
|
||||
assert.Equal(t, chain1.TopBlock.Hash(), headBlockHash)
|
||||
stages.SendPayloadStatus(m.HeaderDownload(), rawdb.ReadHeadBlockHash(tx), err)
|
||||
assert.Equal(t, chain1.TopBlock.Hash(), rawdb.ReadHeadBlockHash(tx))
|
||||
payloadStatus1 = m.ReceivePayloadStatus()
|
||||
assert.Equal(t, remote.EngineStatus_VALID, payloadStatus1.Status)
|
||||
assert.Equal(t, chain1.TopBlock.Hash(), headBlockHash)
|
||||
assert.Equal(t, chain1.TopBlock.Hash(), rawdb.ReadHeadBlockHash(tx))
|
||||
|
||||
//------------------------------------------
|
||||
m.SendPayloadRequest(chain2.TopBlock)
|
||||
headBlockHash, err = stages.StageLoopStep(m.Ctx, m.DB, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
err = stages.StageLoopStep(m.Ctx, m.DB, tx, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
require.NoError(err)
|
||||
stages.SendPayloadStatus(m.HeaderDownload(), headBlockHash, err)
|
||||
stages.SendPayloadStatus(m.HeaderDownload(), rawdb.ReadHeadBlockHash(tx), err)
|
||||
payloadStatus2 := m.ReceivePayloadStatus()
|
||||
assert.Equal(t, remote.EngineStatus_VALID, payloadStatus2.Status)
|
||||
forkChoiceMessage = engineapi.ForkChoiceMessage{
|
||||
@ -779,26 +797,26 @@ func TestPOSWrongTrieRootReorgs(t *testing.T) {
|
||||
FinalizedBlockHash: chain2.TopBlock.Hash(),
|
||||
}
|
||||
m.SendForkChoiceRequest(&forkChoiceMessage)
|
||||
headBlockHash, err = stages.StageLoopStep(m.Ctx, m.DB, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
err = stages.StageLoopStep(m.Ctx, m.DB, tx, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
require.NoError(err)
|
||||
stages.SendPayloadStatus(m.HeaderDownload(), headBlockHash, err)
|
||||
assert.Equal(t, chain2.TopBlock.Hash(), headBlockHash)
|
||||
stages.SendPayloadStatus(m.HeaderDownload(), rawdb.ReadHeadBlockHash(tx), err)
|
||||
assert.Equal(t, chain2.TopBlock.Hash(), rawdb.ReadHeadBlockHash(tx))
|
||||
payloadStatus2 = m.ReceivePayloadStatus()
|
||||
assert.Equal(t, remote.EngineStatus_VALID, payloadStatus2.Status)
|
||||
assert.Equal(t, chain2.TopBlock.Hash(), headBlockHash)
|
||||
assert.Equal(t, chain2.TopBlock.Hash(), rawdb.ReadHeadBlockHash(tx))
|
||||
|
||||
//------------------------------------------
|
||||
preTop3 := chain3.Blocks[chain3.Length()-2]
|
||||
m.SendPayloadRequest(preTop3)
|
||||
headBlockHash, err = stages.StageLoopStep(m.Ctx, m.DB, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
err = stages.StageLoopStep(m.Ctx, m.DB, tx, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
require.NoError(err)
|
||||
stages.SendPayloadStatus(m.HeaderDownload(), headBlockHash, err)
|
||||
stages.SendPayloadStatus(m.HeaderDownload(), rawdb.ReadHeadBlockHash(tx), err)
|
||||
payloadStatus3 := m.ReceivePayloadStatus()
|
||||
assert.Equal(t, remote.EngineStatus_VALID, payloadStatus3.Status)
|
||||
m.SendPayloadRequest(chain3.TopBlock)
|
||||
headBlockHash, err = stages.StageLoopStep(m.Ctx, m.DB, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
err = stages.StageLoopStep(m.Ctx, m.DB, tx, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
require.NoError(err)
|
||||
stages.SendPayloadStatus(m.HeaderDownload(), headBlockHash, err)
|
||||
stages.SendPayloadStatus(m.HeaderDownload(), rawdb.ReadHeadBlockHash(tx), err)
|
||||
payloadStatus3 = m.ReceivePayloadStatus()
|
||||
assert.Equal(t, remote.EngineStatus_VALID, payloadStatus3.Status)
|
||||
forkChoiceMessage = engineapi.ForkChoiceMessage{
|
||||
@ -807,11 +825,11 @@ func TestPOSWrongTrieRootReorgs(t *testing.T) {
|
||||
FinalizedBlockHash: chain3.TopBlock.Hash(),
|
||||
}
|
||||
m.SendForkChoiceRequest(&forkChoiceMessage)
|
||||
headBlockHash, err = stages.StageLoopStep(m.Ctx, m.DB, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
err = stages.StageLoopStep(m.Ctx, m.DB, tx, m.Sync, initialCycle, m.Log, m.BlockReader, nil)
|
||||
require.NoError(err)
|
||||
stages.SendPayloadStatus(m.HeaderDownload(), headBlockHash, err)
|
||||
assert.Equal(t, chain3.TopBlock.Hash(), headBlockHash)
|
||||
stages.SendPayloadStatus(m.HeaderDownload(), rawdb.ReadHeadBlockHash(tx), err)
|
||||
assert.Equal(t, chain3.TopBlock.Hash(), rawdb.ReadHeadBlockHash(tx))
|
||||
payloadStatus3 = m.ReceivePayloadStatus()
|
||||
assert.Equal(t, remote.EngineStatus_VALID, payloadStatus3.Status)
|
||||
assert.Equal(t, chain3.TopBlock.Hash(), headBlockHash)
|
||||
assert.Equal(t, chain3.TopBlock.Hash(), rawdb.ReadHeadBlockHash(tx))
|
||||
}
|
||||
|
@ -89,9 +89,11 @@ func StageLoop(ctx context.Context,
|
||||
}
|
||||
|
||||
// Estimate the current top height seen from the peer
|
||||
headBlockHash, err := StageLoopStep(ctx, db, sync, initialCycle, logger, blockReader, hook)
|
||||
|
||||
SendPayloadStatus(hd, headBlockHash, err)
|
||||
err := StageLoopStep(ctx, db, nil, sync, initialCycle, logger, blockReader, hook)
|
||||
db.View(ctx, func(tx kv.Tx) error {
|
||||
SendPayloadStatus(hd, rawdb.ReadHeadBlockHash(tx), err)
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if errors.Is(err, libcommon.ErrStopped) || errors.Is(err, context.Canceled) {
|
||||
@ -122,30 +124,26 @@ func StageLoop(ctx context.Context,
|
||||
}
|
||||
}
|
||||
|
||||
func StageLoopStep(ctx context.Context, db kv.RwDB, sync *stagedsync.Sync, initialCycle bool, logger log.Logger, blockReader services.FullBlockReader, hook *Hook) (headBlockHash libcommon.Hash, err error) {
|
||||
func StageLoopStep(ctx context.Context, db kv.RwDB, tx kv.RwTx, sync *stagedsync.Sync, initialCycle bool, logger log.Logger, blockReader services.FullBlockReader, hook *Hook) (err error) {
|
||||
defer func() {
|
||||
if rec := recover(); rec != nil {
|
||||
err = fmt.Errorf("%+v, trace: %s", rec, dbg.Stack())
|
||||
}
|
||||
}() // avoid crash because Erigon's core does many things
|
||||
|
||||
var finishProgressBefore, headersProgressBefore uint64
|
||||
if err := db.View(ctx, func(tx kv.Tx) error {
|
||||
if finishProgressBefore, err = stages.GetStageProgress(tx, stages.Finish); err != nil {
|
||||
return err
|
||||
}
|
||||
if headersProgressBefore, err = stages.GetStageProgress(tx, stages.Headers); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
return headBlockHash, err
|
||||
externalTx := tx != nil
|
||||
finishProgressBefore, headersProgressBefore, err := stagesHeadersAndFinish(db, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Sync from scratch must be able Commit partial progress
|
||||
// In all other cases - process blocks batch in 1 RwTx
|
||||
// 2 corner-cases: when sync with --snapshots=false and when executed only blocks from snapshots (in this case all stages progress is equal and > 0, but node is not synced)
|
||||
isSynced := finishProgressBefore > 0 && finishProgressBefore > blockReader.FrozenBlocks() && finishProgressBefore == headersProgressBefore
|
||||
canRunCycleInOneTransaction := isSynced
|
||||
if externalTx {
|
||||
canRunCycleInOneTransaction = true
|
||||
}
|
||||
|
||||
// Main steps:
|
||||
// - process new blocks
|
||||
@ -154,57 +152,60 @@ func StageLoopStep(ctx context.Context, db kv.RwDB, sync *stagedsync.Sync, initi
|
||||
// - Send Notifications: about new blocks, new receipts, state changes, etc...
|
||||
// - Prune(limited time)+Commit(sync). Write to disk happening here.
|
||||
|
||||
var tx kv.RwTx // on this variable will run sync cycle.
|
||||
if canRunCycleInOneTransaction {
|
||||
if canRunCycleInOneTransaction && !externalTx {
|
||||
tx, err = db.BeginRwNosync(ctx)
|
||||
if err != nil {
|
||||
return headBlockHash, err
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
}
|
||||
|
||||
if hook != nil {
|
||||
if err = hook.BeforeRun(tx, canRunCycleInOneTransaction); err != nil {
|
||||
return headBlockHash, err
|
||||
if err = hook.BeforeRun(tx, isSynced); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
err = sync.Run(db, tx, initialCycle)
|
||||
if err != nil {
|
||||
return headBlockHash, err
|
||||
return err
|
||||
}
|
||||
logCtx := sync.PrintTimings()
|
||||
var tableSizes []interface{}
|
||||
var commitTime time.Duration
|
||||
if canRunCycleInOneTransaction {
|
||||
if canRunCycleInOneTransaction && !externalTx {
|
||||
tableSizes = stagedsync.PrintTables(db, tx) // Need to do this before commit to access tx
|
||||
commitStart := time.Now()
|
||||
errTx := tx.Commit()
|
||||
tx = nil
|
||||
if errTx != nil {
|
||||
return headBlockHash, errTx
|
||||
return errTx
|
||||
}
|
||||
commitTime = time.Since(commitStart)
|
||||
}
|
||||
|
||||
// -- send notifications START
|
||||
var head uint64
|
||||
if err := db.View(ctx, func(tx kv.Tx) error {
|
||||
headBlockHash = rawdb.ReadHeadBlockHash(tx)
|
||||
if head, err = stages.GetStageProgress(tx, stages.Headers); err != nil {
|
||||
return err
|
||||
}
|
||||
if externalTx {
|
||||
if hook != nil {
|
||||
if err = hook.AfterRun(tx, finishProgressBefore); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
return headBlockHash, err
|
||||
} else {
|
||||
if err := db.View(ctx, func(tx kv.Tx) error {
|
||||
if hook != nil {
|
||||
if err = hook.AfterRun(tx, finishProgressBefore); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if canRunCycleInOneTransaction && (head != finishProgressBefore || commitTime > 500*time.Millisecond) {
|
||||
if canRunCycleInOneTransaction && !externalTx && commitTime > 500*time.Millisecond {
|
||||
logger.Info("Commit cycle", "in", commitTime)
|
||||
}
|
||||
if head != finishProgressBefore && len(logCtx) > 0 { // No printing of timings or table sizes if there were no progress
|
||||
if len(logCtx) > 0 { // No printing of timings or table sizes if there were no progress
|
||||
logger.Info("Timings (slower than 50ms)", logCtx...)
|
||||
if len(tableSizes) > 0 {
|
||||
logger.Info("Tables", tableSizes...)
|
||||
@ -213,11 +214,41 @@ func StageLoopStep(ctx context.Context, db kv.RwDB, sync *stagedsync.Sync, initi
|
||||
// -- send notifications END
|
||||
|
||||
// -- Prune+commit(sync)
|
||||
if err := db.Update(ctx, func(tx kv.RwTx) error { return sync.RunPrune(db, tx, initialCycle) }); err != nil {
|
||||
return headBlockHash, err
|
||||
if err := stageLoopStepPrune(ctx, db, tx, sync, initialCycle); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return headBlockHash, nil
|
||||
return nil
|
||||
}
|
||||
func stageLoopStepPrune(ctx context.Context, db kv.RwDB, tx kv.RwTx, sync *stagedsync.Sync, initialCycle bool) (err error) {
|
||||
if tx != nil {
|
||||
return sync.RunPrune(db, tx, initialCycle)
|
||||
}
|
||||
return db.Update(ctx, func(tx kv.RwTx) error { return sync.RunPrune(db, tx, initialCycle) })
|
||||
}
|
||||
|
||||
func stagesHeadersAndFinish(db kv.RoDB, tx kv.Tx) (head, fin uint64, err error) {
|
||||
if tx != nil {
|
||||
if fin, err = stages.GetStageProgress(tx, stages.Finish); err != nil {
|
||||
return head, fin, err
|
||||
}
|
||||
if head, err = stages.GetStageProgress(tx, stages.Headers); err != nil {
|
||||
return head, fin, err
|
||||
}
|
||||
return head, fin, nil
|
||||
}
|
||||
if err := db.View(context.Background(), func(tx kv.Tx) error {
|
||||
if fin, err = stages.GetStageProgress(tx, stages.Finish); err != nil {
|
||||
return err
|
||||
}
|
||||
if head, err = stages.GetStageProgress(tx, stages.Headers); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
return head, fin, err
|
||||
}
|
||||
return head, fin, nil
|
||||
}
|
||||
|
||||
type Hook struct {
|
||||
@ -233,9 +264,9 @@ type Hook struct {
|
||||
func NewHook(ctx context.Context, notifications *shards.Notifications, sync *stagedsync.Sync, blockReader services.FullBlockReader, chainConfig *chain.Config, logger log.Logger, updateHead func(ctx context.Context, headHeight uint64, headTime uint64, hash libcommon.Hash, td *uint256.Int)) *Hook {
|
||||
return &Hook{ctx: ctx, notifications: notifications, sync: sync, blockReader: blockReader, chainConfig: chainConfig, logger: logger, updateHead: updateHead}
|
||||
}
|
||||
func (h *Hook) BeforeRun(tx kv.Tx, canRunCycleInOneTransaction bool) error {
|
||||
func (h *Hook) BeforeRun(tx kv.Tx, inSync bool) error {
|
||||
notifications := h.notifications
|
||||
if notifications != nil && notifications.Accumulator != nil && canRunCycleInOneTransaction {
|
||||
if notifications != nil && notifications.Accumulator != nil && inSync {
|
||||
stateVersion, err := rawdb.GetStateVersion(tx)
|
||||
if err != nil {
|
||||
h.logger.Error("problem reading plain state version", "err", err)
|
||||
|
Loading…
Reference in New Issue
Block a user