EthereumExecutionService in MockSentry (#8373)

Now we use the ethereum execution service directly:

* Changed sig of InsertChain
* Use of the service in case of PoS
This commit is contained in:
Giulio rebuffo 2023-10-05 18:30:19 +02:00 committed by GitHub
parent 0bd6d77acd
commit 2294c8c66c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 279 additions and 257 deletions

View File

@ -148,7 +148,7 @@ func (b *SimulatedBackend) Commit() {
Headers: []*types.Header{b.pendingHeader},
Blocks: []*types.Block{b.pendingBlock},
TopBlock: b.pendingBlock,
}, nil); err != nil {
}); err != nil {
panic(err)
}
//nolint:prealloc

View File

@ -82,7 +82,6 @@ func (a *ApiHandler) getBlock(r *http.Request) (data any, finalized *bool, versi
blockId, err = blockIdFromRequest(r)
if err != nil {
fmt.Println("A")
httpStatus = http.StatusBadRequest
return
}

View File

@ -430,13 +430,13 @@ func initialState1() error {
// BLOCKS
for i := 0; i < chain.Length(); i++ {
if err = m2.InsertChain(chain.Slice(i, i+1), nil); err != nil {
if err = m2.InsertChain(chain.Slice(i, i+1)); 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), nil); err != nil {
if err = m.InsertChain(chain.Slice(i, i+1)); err != nil {
return err
}
}

View File

@ -102,10 +102,10 @@ func CreateTestSentry(t *testing.T) (*mock.MockSentry, *core.ChainPack, []*core.
t.Fatal(err)
}
if err = m.InsertChain(orphanedChain, nil); err != nil {
if err = m.InsertChain(orphanedChain); err != nil {
t.Fatal(err)
}
if err = m.InsertChain(chain, nil); err != nil {
if err = m.InsertChain(chain); err != nil {
t.Fatal(err)
}
@ -428,7 +428,7 @@ func CreateTestSentryForTraces(t *testing.T) *mock.MockSentry {
t.Fatalf("generate blocks: %v", err)
}
if err := m.InsertChain(chain, nil); err != nil {
if err := m.InsertChain(chain); err != nil {
t.Fatalf("failed to insert into chain: %v", err)
}
return m
@ -534,7 +534,7 @@ func CreateTestSentryForTracesCollision(t *testing.T) *mock.MockSentry {
t.Fatalf("generate blocks: %v", err)
}
// Import the canonical chain
if err := m.InsertChain(chain, nil); err != nil {
if err := m.InsertChain(chain); err != nil {
t.Fatalf("failed to insert into chain: %v", err)
}

View File

@ -58,7 +58,7 @@ func TestEmptyBlock(t *testing.T) {
blocks[0] = block
chain := &core.ChainPack{Headers: headers, Blocks: blocks, Receipts: receipts, TopBlock: block}
err = m.InsertChain(chain, nil)
err = m.InsertChain(chain)
require.NoError(err)
}

View File

@ -106,7 +106,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), nil); err != nil {
if err := m.InsertChain(chain.Slice(0, 2)); err != nil {
t.Fatalf("failed to insert initial blocks: %v", err)
}
if err := m.DB.View(m.Ctx, func(tx kv.Tx) error {
@ -123,7 +123,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()), nil); err != nil {
if err := m.InsertChain(chain.Slice(2, chain.Length())); err != nil {
t.Fatalf("failed to insert final block: %v", err)
}
if err := m.DB.View(m.Ctx, func(tx kv.Tx) error {

View File

@ -477,7 +477,7 @@ func TestClique(t *testing.T) {
chainX.Headers[k] = b.Header()
}
chainX.TopBlock = batches[j][len(batches[j])-1]
if err = m.InsertChain(chainX, nil); err != nil {
if err = m.InsertChain(chainX); err != nil {
t.Errorf("test %d: failed to import batch %d, %v", i, j, err)
failed = true
break
@ -493,7 +493,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, nil)
err = m.InsertChain(chainX)
if tt.failure != nil && err == nil {
t.Errorf("test %d: expected failure", i)
}

View File

@ -62,7 +62,7 @@ func TestHeaderVerification(t *testing.T) {
}); err != nil {
panic(err)
}
if err = m.InsertChain(chain.Slice(i, i+1), nil); err != nil {
if err = m.InsertChain(chain.Slice(i, i+1)); err != nil {
t.Fatalf("test %d: error inserting the block: %v", i, err)
}
@ -104,7 +104,7 @@ func TestHeaderWithSealVerification(t *testing.T) {
}); err != nil {
panic(err)
}
if err = m.InsertChain(chain.Slice(i, i+1), nil); err != nil {
if err = m.InsertChain(chain.Slice(i, i+1)); err != nil {
t.Fatalf("test %d: error inserting the block: %v", i, err)
}

View File

@ -143,7 +143,7 @@ func TestCreate2Revive(t *testing.T) {
require.NoError(t, err)
// BLOCK 1
if err = m.InsertChain(chain.Slice(0, 1), nil); err != nil {
if err = m.InsertChain(chain.Slice(0, 1)); 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), nil); err != nil {
if err = m.InsertChain(chain.Slice(1, 2)); 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), nil); err != nil {
if err = m.InsertChain(chain.Slice(2, 3)); 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), nil); err != nil {
if err = m.InsertChain(chain.Slice(3, 4)); 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), nil); err != nil {
if err = m.InsertChain(chain.Slice(0, 1)); 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), nil); err != nil {
if err = m.InsertChain(chain.Slice(1, 2)); 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), nil); err != nil {
if err = m.InsertChain(chain.Slice(2, 3)); 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), nil); err != nil {
if err = m.InsertChain(chain.Slice(3, 4)); 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), nil); err != nil {
if err = m.InsertChain(chain.Slice(4, 5)); 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), nil); err != nil {
if err = m.InsertChain(chain.Slice(0, 1)); 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()), nil); err != nil {
if err = m.InsertChain(chain.Slice(1, chain.Length())); 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), nil); err != nil {
if err = m.InsertChain(longerChain.Slice(1, 4)); 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), nil); err != nil {
if err = m.InsertChain(chain.Slice(0, 1)); 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()), nil); err != nil {
if err = m.InsertChain(chain.Slice(1, chain.Length())); 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), nil); err != nil {
if err = m.InsertChain(longerChain.Slice(1, 3)); 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), nil); err != nil {
if err = m.InsertChain(chain.Slice(0, 1)); 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), nil); err != nil {
if err = m.InsertChain(chain.Slice(0, 1)); 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), nil); err != nil {
if err = m.InsertChain(chain.Slice(0, 1)); 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), nil); err != nil {
if err = m.InsertChain(chain.Slice(1, 2)); 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), nil); err != nil {
if err = m.InsertChain(chain.Slice(0, 1)); err != nil {
t.Fatal(err)
}
// BLOCKS 2
if err = m.InsertChain(chain.Slice(1, chain.Length()), nil); err != nil {
if err = m.InsertChain(chain.Slice(1, chain.Length())); 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()), nil); err != nil {
if err = m.InsertChain(longerChain.Slice(1, longerChain.Length())); 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), nil); err != nil {
if err = m.InsertChain(chain.Slice(0, 2)); 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()), nil); err != nil {
if err = m.InsertChain(chain.Slice(2, chain.Length())); 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, nil); err != nil {
if err = m.InsertChain(longerChain); 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, nil); err != nil {
if err = m.InsertChain(chain1); err != nil {
t.Fatal(err)
}
if err = m.InsertChain(chain2, nil); err != nil {
if err = m.InsertChain(chain2); err != nil {
t.Fatal(err)
}
var count uint64

View File

@ -118,7 +118,7 @@ func newTestBackend(t *testing.T) *testBackend {
t.Error(err)
}
// Construct testing chain
if err = m.InsertChain(chain, nil); err != nil {
if err = m.InsertChain(chain); err != nil {
t.Error(err)
}
return &testBackend{db: m.DB, cfg: params.TestChainConfig, blockReader: m.BlockReader}

View File

@ -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)
err := m.InsertChain(chain, nil)
err := m.InsertChain(chain)
require.NoError(t, err)
}
return m

View File

@ -132,17 +132,17 @@ func (bt *BlockTest) Run(t *testing.T, checkStateRoot 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)
if err != nil {
return err
}
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
}
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)
@ -187,7 +187,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 *mock.MockSentry, tx kv.RwTx) ([]btBlock, error) {
func (bt *BlockTest) insertBlocks(m *mock.MockSentry) ([]btBlock, error) {
validBlocks := make([]btBlock, 0)
// insert the test blocks, which will execute all transaction
for bi, b := range bt.json.Blocks {
@ -202,7 +202,7 @@ func (bt *BlockTest) insertBlocks(m *mock.MockSentry, tx kv.RwTx) ([]btBlock, er
// 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, tx)
err1 := m.InsertChain(chain)
if err1 != nil {
if b.BlockHeader == nil {
continue // OK - block is supposed to be invalid, continue with next block
@ -210,7 +210,12 @@ func (bt *BlockTest) insertBlocks(m *mock.MockSentry, tx kv.RwTx) ([]btBlock, er
return nil, fmt.Errorf("block #%v insertion into chain failed: %w", cb.Number(), err1)
}
} else if b.BlockHeader == nil {
canonical, cErr := bt.br.CanonicalHash(context.Background(), tx, cb.NumberU64())
roTx, err := m.DB.BeginRo(m.Ctx)
if err != nil {
return nil, err
}
defer roTx.Rollback()
canonical, cErr := bt.br.CanonicalHash(context.Background(), roTx, cb.NumberU64())
if cErr != nil {
return nil, cErr
}

View File

@ -111,16 +111,22 @@ func TestSelfDestructReceive(t *testing.T) {
if st.Exist(contractAddress) {
t.Error("expected contractAddress to not exist before block 0", contractAddress.String())
}
tx.Rollback()
// BLOCK 1
if err = m.InsertChain(chain.Slice(0, 1), tx); err != nil {
if err = m.InsertChain(chain.Slice(0, 1)); err != nil {
t.Fatal(err)
}
// BLOCK 2
if err = m.InsertChain(chain.Slice(1, 2), tx); err != nil {
if err = m.InsertChain(chain.Slice(1, 2)); err != nil {
t.Fatal(err)
}
tx, err = m.DB.BeginRw(context.Background())
if err != nil {
panic(err)
}
defer tx.Rollback()
// 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

View File

@ -54,7 +54,7 @@ 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, nil); err == nil {
if err = m.InsertChain(incorrectChain); err == nil {
t.Fatal("should fail")
}
@ -69,14 +69,13 @@ func TestInsertIncorrectStateRootDifferentAccounts(t *testing.T) {
t.Fatal(err)
}
if err = m.InsertChain(chain); err != nil {
t.Fatal(err)
}
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")
@ -122,7 +121,7 @@ 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, nil); err == nil {
if err = m.InsertChain(incorrectChain); err == nil {
t.Fatal("should fail")
}
@ -137,7 +136,7 @@ func TestInsertIncorrectStateRootSameAccount(t *testing.T) {
t.Fatal(err)
}
if err = m.InsertChain(chain, nil); err != nil {
if err = m.InsertChain(chain); err != nil {
t.Fatal(err)
}
@ -184,7 +183,7 @@ 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, nil); err == nil {
if err = m.InsertChain(incorrectChain); err == nil {
t.Fatal("should fail")
}
@ -199,7 +198,7 @@ func TestInsertIncorrectStateRootSameAccountSameAmount(t *testing.T) {
t.Fatal(err)
}
if err = m.InsertChain(chain, nil); err != nil {
if err = m.InsertChain(chain); err != nil {
t.Fatal(err)
}
@ -246,7 +245,7 @@ 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, nil); err == nil {
if err = m.InsertChain(incorrectChain); err == nil {
t.Fatal("should fail")
}
@ -261,7 +260,7 @@ func TestInsertIncorrectStateRootAllFundsRoot(t *testing.T) {
t.Fatal(err)
}
if err = m.InsertChain(chain, nil); err != nil {
if err = m.InsertChain(chain); err != nil {
t.Fatal(err)
}
@ -308,7 +307,7 @@ 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, nil); err == nil {
if err = m.InsertChain(incorrectChain); err == nil {
t.Fatal("should fail")
}
@ -323,7 +322,7 @@ func TestInsertIncorrectStateRootAllFunds(t *testing.T) {
t.Fatal(err)
}
if err = m.InsertChain(chain, nil); err != nil {
if err = m.InsertChain(chain); err != nil {
t.Fatal(err)
}
@ -368,7 +367,7 @@ func TestAccountDeployIncorrectRoot(t *testing.T) {
}
// BLOCK 1
if err = m.InsertChain(chain.Slice(0, 1), nil); err != nil {
if err = m.InsertChain(chain.Slice(0, 1)); err != nil {
t.Fatal(err)
}
err = m.DB.View(context.Background(), func(tx kv.Tx) error {
@ -390,7 +389,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, nil); err == nil {
if err = m.InsertChain(incorrectChain); err == nil {
t.Fatal("should fail")
}
@ -408,7 +407,7 @@ func TestAccountDeployIncorrectRoot(t *testing.T) {
require.NoError(t, err)
// BLOCK 2 - CORRECT
if err = m.InsertChain(chain.Slice(1, 2), nil); err != nil {
if err = m.InsertChain(chain.Slice(1, 2)); err != nil {
t.Fatal(err)
}
@ -454,7 +453,7 @@ func TestAccountCreateIncorrectRoot(t *testing.T) {
}
// BLOCK 1
if err = m.InsertChain(chain.Slice(0, 1), nil); err != nil {
if err = m.InsertChain(chain.Slice(0, 1)); err != nil {
t.Fatal(err)
}
@ -473,7 +472,7 @@ func TestAccountCreateIncorrectRoot(t *testing.T) {
require.NoError(t, err)
// BLOCK 2
if err = m.InsertChain(chain.Slice(1, 2), nil); err != nil {
if err = m.InsertChain(chain.Slice(1, 2)); err != nil {
t.Fatal(err)
}
err = m.DB.View(context.Background(), func(tx kv.Tx) error {
@ -496,12 +495,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, nil); err == nil {
if err = m.InsertChain(incorrectChain); err == nil {
t.Fatal("should fail")
}
// BLOCK 3
if err = m.InsertChain(chain.Slice(2, 3), nil); err != nil {
if err = m.InsertChain(chain.Slice(2, 3)); err != nil {
t.Fatal(err)
}
}
@ -538,7 +537,7 @@ func TestAccountUpdateIncorrectRoot(t *testing.T) {
}
// BLOCK 1
if err = m.InsertChain(chain.Slice(0, 1), nil); err != nil {
if err = m.InsertChain(chain.Slice(0, 1)); err != nil {
t.Fatal(err)
}
@ -557,7 +556,7 @@ func TestAccountUpdateIncorrectRoot(t *testing.T) {
require.NoError(t, err)
// BLOCK 2
if err = m.InsertChain(chain.Slice(1, 2), nil); err != nil {
if err = m.InsertChain(chain.Slice(1, 2)); err != nil {
t.Fatal(err)
}
@ -575,7 +574,7 @@ func TestAccountUpdateIncorrectRoot(t *testing.T) {
require.NoError(t, err)
// BLOCK 3
if err = m.InsertChain(chain.Slice(2, 3), nil); err != nil {
if err = m.InsertChain(chain.Slice(2, 3)); err != nil {
t.Fatal(err)
}
@ -585,12 +584,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, nil); err == nil {
if err = m.InsertChain(incorrectChain); err == nil {
t.Fatal("should fail")
}
// BLOCK 4
if err = m.InsertChain(chain.Slice(3, 4), nil); err != nil {
if err = m.InsertChain(chain.Slice(3, 4)); err != nil {
t.Fatal(err)
}
}
@ -627,7 +626,7 @@ func TestAccountDeleteIncorrectRoot(t *testing.T) {
}
// BLOCK 1
if err = m.InsertChain(chain.Slice(0, 1), nil); err != nil {
if err = m.InsertChain(chain.Slice(0, 1)); err != nil {
t.Fatal(err)
}
@ -645,7 +644,7 @@ func TestAccountDeleteIncorrectRoot(t *testing.T) {
require.NoError(t, err)
// BLOCK 2
if err = m.InsertChain(chain.Slice(1, 2), nil); err != nil {
if err = m.InsertChain(chain.Slice(1, 2)); err != nil {
t.Fatal(err)
}
@ -663,7 +662,7 @@ func TestAccountDeleteIncorrectRoot(t *testing.T) {
require.NoError(t, err)
// BLOCK 3
if err = m.InsertChain(chain.Slice(2, 3), nil); err != nil {
if err = m.InsertChain(chain.Slice(2, 3)); err != nil {
t.Fatal(err)
}
@ -672,12 +671,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, nil); err == nil {
if err = m.InsertChain(incorrectChain); err == nil {
t.Fatal("should fail")
}
// BLOCK 4
if err = m.InsertChain(chain.Slice(3, 4), nil); err != nil {
if err = m.InsertChain(chain.Slice(3, 4)); err != nil {
t.Fatal(err)
}
}

View File

@ -34,6 +34,7 @@ func (e *EthereumExecutionModule) InsertBlocks(ctx context.Context, req *executi
if err != nil || parentTd == nil {
return nil, fmt.Errorf("parent's total difficulty not found with hash %x and height %d: %v", header.ParentHash, header.Number.Uint64()-1, err)
}
// Sum TDs.
td := parentTd.Add(parentTd, header.Difficulty)
if err := rawdb.WriteHeader(tx, header); err != nil {

View File

@ -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), nil); err != nil {
if err = m.InsertChain(chain.Slice(i, i+1)); 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, nil); err != nil {
if err = m.InsertChain(chainA); 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), nil); err != nil {
if err = m.InsertChain(chainB.Slice(0, 12)); 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), nil); err != nil {
if err = m.InsertChain(chainB.Slice(12, 20)); 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), nil); err != nil {
if err = m.InsertChain(chain.Slice(i, i+1)); err != nil {
t.Fatalf("inserting chain: %v", err)
}
}
@ -205,7 +205,7 @@ func TestFilterAddressIntersection(t *testing.T) {
})
require.NoError(t, err, "generate chain")
err = m.InsertChain(chain, nil)
err = m.InsertChain(chain)
require.NoError(t, err, "inserting chain")
fromBlock, toBlock := uint64(1), uint64(15)

View File

@ -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)
err := m.InsertChain(chain, nil)
err := m.InsertChain(chain)
require.NoError(t, err)
}
return m

View File

@ -524,7 +524,7 @@ func chainWithDeployedContract(t *testing.T) (*mock.MockSentry, libcommon.Addres
t.Fatalf("generate blocks: %v", err)
}
err = m.InsertChain(chain, nil)
err = m.InsertChain(chain)
assert.NoError(t, err)
tx, err := db.BeginRo(context.Background())

View File

@ -81,7 +81,7 @@ func createGasPriceTestKV(t *testing.T, chainSize int) *mock.MockSentry {
t.Error(err)
}
// Construct testing chain
if err = m.InsertChain(chain, nil); err != nil {
if err = m.InsertChain(chain); err != nil {
t.Error(err)
}

View File

@ -28,7 +28,7 @@ func TestTxPoolContent(t *testing.T) {
b.SetCoinbase(libcommon.Address{1})
})
require.NoError(err)
err = m.InsertChain(chain, nil)
err = m.InsertChain(chain)
require.NoError(err)
ctx, conn := rpcdaemontest.CreateTestGrpcConn(t, m)

View File

@ -271,7 +271,7 @@ func createDumpTestKV(t *testing.T, chainConfig *chain.Config, chainSize int) *m
t.Fatal(err)
}
// Construct testing chain
if err = m.InsertChain(chain, nil); err != nil {
if err = m.InsertChain(chain); err != nil {
t.Fatal(err)
}

View File

@ -80,7 +80,7 @@ func newCanonical(t *testing.T, n int) *mock.MockSentry {
// Full block-chain requested
chain := makeBlockChain(m.Genesis, n, m, canonicalSeed)
if err := m.InsertChain(chain, nil); err != nil {
if err := m.InsertChain(chain); err != nil {
t.Fatal(err)
}
return m
@ -146,7 +146,7 @@ func testFork(t *testing.T, m *mock.MockSentry, i, n int, comparator func(td1, t
})
require.NoError(t, err)
if err = m.InsertChain(blockChainB, nil); err != nil {
if err = m.InsertChain(blockChainB); 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 *mock.MockSentry, i, n int, comparator func(td1, t
require.NoError(t, err)
// Sanity check that the forked chain can be imported into the original
if err := canonicalMock.InsertChain(blockChainB, nil); err != nil {
if err := canonicalMock.InsertChain(blockChainB); err != nil {
t.Fatalf("failed to import forked block chain: %v", err)
}
// Compare the total difficulties of the chains
@ -174,7 +174,7 @@ func TestLastBlock(t *testing.T) {
var err error
chain := makeBlockChain(current(m, nil), 1, m, 0)
if err = m.InsertChain(chain, nil); err != nil {
if err = m.InsertChain(chain); err != nil {
t.Fatalf("Failed to insert block: %v", err)
}
@ -275,7 +275,7 @@ func testBrokenChain(t *testing.T) {
chain := makeBlockChain(current(m, nil), 5, m, forkSeed)
brokenChain := chain.Slice(1, chain.Length())
if err := m.InsertChain(brokenChain, nil); err == nil {
if err := m.InsertChain(brokenChain); err == nil {
t.Errorf("broken block chain not reported")
}
}
@ -325,20 +325,18 @@ func testReorg(t *testing.T, first, second []int64, td int64) {
t.Fatalf("generate chain: %v", err)
}
if err = m.InsertChain(easyChain); err != nil {
t.Fatalf("failed to insert easy chain: %v", err)
}
if err = m.InsertChain(diffChain); err != nil {
t.Fatalf("failed to insert difficult chain: %v", err)
}
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, tx); err != nil {
t.Fatalf("failed to insert difficult chain: %v", err)
}
// Check that the chain is valid number and link wise
prev, err := m.BlockReader.CurrentBlock(tx)
require.NoError(err)
@ -380,7 +378,7 @@ func testBadHashes(t *testing.T) {
core.BadHashes[chain.Headers[2].Hash()] = true
defer func() { delete(core.BadHashes, chain.Headers[2].Hash()) }()
err = m.InsertChain(chain, nil)
err = m.InsertChain(chain)
if !errors.Is(err, core.ErrBlacklistedHash) {
t.Errorf("error mismatch: have: %v, want: %v", err, core.ErrBlacklistedHash)
}
@ -449,7 +447,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, nil); err1 != nil {
if err1 := m.InsertChain(chain); err1 != nil {
t.Fatalf("failed to insert original chain: %v", err1)
}
@ -476,7 +474,7 @@ func TestChainTxReorgs(t *testing.T) {
t.Fatalf("generate chain: %v", err)
}
if err := m.InsertChain(chain, nil); err != nil {
if err := m.InsertChain(chain); err != nil {
t.Fatalf("failed to insert forked chain: %v", err)
}
tx, err := m.DB.BeginRo(context.Background())
@ -564,7 +562,7 @@ func TestCanonicalBlockRetrieval(t *testing.T) {
if err2 != nil {
t.Fatalf("generate chain: %v", err2)
}
err := m.InsertChain(chain, nil)
err := m.InsertChain(chain)
require.NoError(t, err)
tx, err := m.DB.BeginRo(m.Ctx)
@ -657,7 +655,7 @@ func TestEIP155Transition(t *testing.T) {
t.Fatalf("generate chain: %v", chainErr)
}
if chainErr = m.InsertChain(chain, nil); chainErr != nil {
if chainErr = m.InsertChain(chain); chainErr != nil {
t.Fatal(chainErr)
}
if err := m.DB.View(context.Background(), func(tx kv.Tx) error {
@ -697,7 +695,7 @@ func TestEIP155Transition(t *testing.T) {
if chainErr != nil {
t.Fatalf("generate blocks: %v", chainErr)
}
if err := m.InsertChain(chain, nil); err == nil {
if err := m.InsertChain(chain); err == nil {
t.Errorf("expected error")
}
}
@ -781,7 +779,7 @@ func doModesTest(t *testing.T, pm prune.Mode) error {
return fmt.Errorf("generate blocks: %w", err)
}
if err = m.InsertChain(chain, nil); err != nil {
if err = m.InsertChain(chain); err != nil {
return err
}
@ -979,33 +977,47 @@ func TestEIP161AccountRemoval(t *testing.T) {
if err != nil {
t.Fatalf("generate blocks: %v", err)
}
// account must exist pre eip 161
if err = m.InsertChain(chain.Slice(0, 1)); err != nil {
t.Fatal(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), tx); err != nil {
t.Fatal(err)
}
if st := state.New(m.NewStateReader(tx)); !st.Exist(theAddr) {
t.Error("expected account to exist")
}
tx.Rollback()
// account needs to be deleted post eip 161
if err = m.InsertChain(chain.Slice(1, 2), tx); err != nil {
if err = m.InsertChain(chain.Slice(1, 2)); err != nil {
t.Fatal(err)
}
tx, err = m.DB.BeginRw(m.Ctx)
if err != nil {
fmt.Printf("beginro error: %v\n", err)
return
}
defer tx.Rollback()
if st := state.New(m.NewStateReader(tx)); st.Exist(theAddr) {
t.Error("account should not exist")
}
tx.Rollback()
// account mustn't be created post eip 161
if err = m.InsertChain(chain.Slice(2, 3), tx); err != nil {
if err = m.InsertChain(chain.Slice(2, 3)); err != nil {
t.Fatal(err)
}
tx, err = m.DB.BeginRw(m.Ctx)
if err != nil {
fmt.Printf("beginro error: %v\n", err)
return
}
defer tx.Rollback()
if st := state.New(m.NewStateReader(tx)); st.Exist(theAddr) {
t.Error("account should not exist")
}
@ -1057,14 +1069,14 @@ func TestDoubleAccountRemoval(t *testing.T) {
t.Fatalf("generate blocks: %v", err)
}
err = m.InsertChain(chain)
assert.NoError(t, err)
tx, err := m.DB.BeginRw(m.Ctx)
if err != nil {
fmt.Printf("beginro error: %v\n", err)
return
}
defer tx.Rollback()
err = m.InsertChain(chain, tx)
assert.NoError(t, err)
st := state.New(m.NewStateReader(tx))
assert.NoError(t, err)
@ -1117,7 +1129,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), nil); err != nil {
if err := m2.InsertChain(chain.Slice(i, i+1)); err != nil {
t.Fatalf("block %d: failed to insert into chain: %v", i, err)
}
@ -1130,7 +1142,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], nil); err != nil {
if err := m2.InsertChain(forks[i]); err != nil {
t.Fatalf(" fork %d: failed to insert into chain: %v", i, err)
}
b, err = m.BlockReader.CurrentBlock(tx)
@ -1184,20 +1196,20 @@ func TestLargeReorgTrieGC(t *testing.T) {
}
// Import the shared chain and the original canonical one
if err := m2.InsertChain(shared, nil); err != nil {
if err := m2.InsertChain(shared); err != nil {
t.Fatalf("failed to insert shared chain: %v", err)
}
if err := m2.InsertChain(original, nil); err != nil {
if err := m2.InsertChain(original); 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), nil); err != nil {
if err := m2.InsertChain(competitor.Slice(0, competitor.Length()-2)); 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()), nil); err != nil {
if err := m2.InsertChain(competitor.Slice(competitor.Length()-2, competitor.Length())); err != nil {
t.Fatalf("failed to finalize competitor chain: %v", err)
}
}
@ -1238,12 +1250,12 @@ func TestLowDiffLongChain(t *testing.T) {
// Import the canonical chain
m2 := mock.Mock(t)
if err := m2.InsertChain(chain, nil); err != nil {
if err := m2.InsertChain(chain); err != nil {
t.Fatalf("failed to insert into chain: %v", err)
}
// And now import the fork
if err := m2.InsertChain(fork, nil); err != nil {
if err := m2.InsertChain(fork); err != nil {
t.Fatalf("failed to insert into chain: %v", err)
}
@ -1336,7 +1348,7 @@ func TestDeleteCreateRevert(t *testing.T) {
t.Fatalf("generate blocks: %v", err)
}
if err := m.InsertChain(chain, nil); err != nil {
if err := m.InsertChain(chain); err != nil {
t.Fatalf("failed to insert into chain: %v", err)
}
}
@ -1441,7 +1453,7 @@ func TestDeleteRecreateSlots(t *testing.T) {
t.Fatalf("generate blocks: %v", err)
}
// Import the canonical chain
if err := m.InsertChain(chain, nil); err != nil {
if err := m.InsertChain(chain); err != nil {
t.Fatalf("failed to insert into chain: %v", err)
}
err = m.DB.View(m.Ctx, func(tx kv.Tx) error {
@ -1559,7 +1571,7 @@ func TestCVE2020_26265(t *testing.T) {
t.Fatalf("generate blocks: %v", err)
}
// Import the canonical chain
if err := m.InsertChain(chain, nil); err != nil {
if err := m.InsertChain(chain); err != nil {
t.Fatalf("failed to insert into chain: %v", err)
}
err = m.DB.View(m.Ctx, func(tx kv.Tx) error {
@ -1626,7 +1638,7 @@ func TestDeleteRecreateAccount(t *testing.T) {
t.Fatalf("generate blocks: %v", err)
}
// Import the canonical chain
if err := m.InsertChain(chain, nil); err != nil {
if err := m.InsertChain(chain); err != nil {
t.Fatalf("failed to insert into chain: %v", err)
}
err = m.DB.View(m.Ctx, func(tx kv.Tx) error {
@ -1806,7 +1818,7 @@ func TestDeleteRecreateSlotsAcrossManyBlocks(t *testing.T) {
}
for i := range chain.Blocks {
blockNum := i + 1
if err := m.InsertChain(chain.Slice(i, i+1), nil); err != nil {
if err := m.InsertChain(chain.Slice(i, i+1)); 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 {
@ -1949,7 +1961,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), nil); err != nil {
if err := m.InsertChain(chain.Slice(0, 1)); err != nil {
t.Fatalf("block %d: failed to insert into chain: %v", block.NumberU64(), err)
}
statedb = state.New(m.NewHistoryStateReader(1, tx))
@ -1959,7 +1971,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), nil); err != nil {
if err := m.InsertChain(chain.Slice(1+i, 2+i)); err != nil {
t.Fatalf("block %d: failed to insert into chain: %v", block.NumberU64(), err)
}
}
@ -2033,7 +2045,7 @@ func TestEIP2718Transition(t *testing.T) {
// Import the canonical chain
if err = m.InsertChain(chain, nil); err != nil {
if err = m.InsertChain(chain); err != nil {
t.Fatalf("failed to insert into chain: %v", err)
}
@ -2135,12 +2147,7 @@ func TestEIP1559Transition(t *testing.T) {
}
// Import the canonical chain
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 {
if err = m.InsertChain(chain); err != nil {
t.Fatalf("failed to insert into chain: %v", err)
}
@ -2188,7 +2195,7 @@ func TestEIP1559Transition(t *testing.T) {
t.Fatalf("generate chain: %v", err)
}
if err = m.InsertChain(chain, nil); err != nil {
if err = m.InsertChain(chain); err != nil {
t.Fatalf("failed to insert into chain: %v", err)
}

View File

@ -92,6 +92,11 @@ 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.BeginRw(m.Ctx)
if err != nil {
fmt.Printf("beginro error: %v\n", err)
@ -99,12 +104,6 @@ func TestGenerateChain(t *testing.T) {
}
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, tx).Number()) != 0 {
t.Errorf("wrong block number: %d", current(m, tx).Number())

View File

@ -123,7 +123,7 @@ func TestSetupGenesis(t *testing.T) {
if err != nil {
return nil, nil, err
}
if err = m.InsertChain(chain, nil); err != nil {
if err = m.InsertChain(chain); err != nil {
return nil, nil, err
}
// This should return a compatibility error.

View File

@ -8,7 +8,10 @@ import (
"os"
"sync"
"testing"
"time"
"github.com/ledgerwatch/erigon/turbo/execution/eth1"
"github.com/ledgerwatch/erigon/turbo/execution/eth1/eth1_utils"
stages2 "github.com/ledgerwatch/erigon/turbo/stages"
"github.com/c2h5oh/datasize"
@ -22,6 +25,7 @@ import (
"github.com/ledgerwatch/erigon-lib/direct"
"github.com/ledgerwatch/erigon-lib/gointerfaces"
proto_downloader "github.com/ledgerwatch/erigon-lib/gointerfaces/downloader"
"github.com/ledgerwatch/erigon-lib/gointerfaces/execution"
proto_sentry "github.com/ledgerwatch/erigon-lib/gointerfaces/sentry"
ptypes "github.com/ledgerwatch/erigon-lib/gointerfaces/types"
"github.com/ledgerwatch/erigon-lib/kv"
@ -68,30 +72,31 @@ const MockInsertAsInitialCycle = false
type MockSentry struct {
proto_sentry.UnimplementedSentryServer
Ctx context.Context
Log log.Logger
tb testing.TB
cancel context.CancelFunc
DB kv.RwDB
Dirs datadir.Dirs
Engine consensus.Engine
gspec *types.Genesis
ChainConfig *chain.Config
Sync *stagedsync.Sync
MiningSync *stagedsync.Sync
PendingBlocks chan *types.Block
MinedBlocks chan *types.Block
sentriesClient *sentry.MultiClient
Key *ecdsa.PrivateKey
Genesis *types.Block
SentryClient direct.SentryClient
PeerId *ptypes.H512
UpdateHead func(Ctx context.Context, headHeight, headTime uint64, hash libcommon.Hash, td *uint256.Int)
streams map[proto_sentry.MessageId][]proto_sentry.Sentry_MessagesServer
sentMessages []*proto_sentry.OutboundMessageData
StreamWg sync.WaitGroup
ReceiveWg sync.WaitGroup
Address libcommon.Address
Ctx context.Context
Log log.Logger
tb testing.TB
cancel context.CancelFunc
DB kv.RwDB
Dirs datadir.Dirs
Engine consensus.Engine
gspec *types.Genesis
ChainConfig *chain.Config
Sync *stagedsync.Sync
MiningSync *stagedsync.Sync
PendingBlocks chan *types.Block
MinedBlocks chan *types.Block
sentriesClient *sentry.MultiClient
Key *ecdsa.PrivateKey
Genesis *types.Block
SentryClient direct.SentryClient
PeerId *ptypes.H512
UpdateHead func(Ctx context.Context, headHeight, headTime uint64, hash libcommon.Hash, td *uint256.Int)
streams map[proto_sentry.MessageId][]proto_sentry.Sentry_MessagesServer
sentMessages []*proto_sentry.OutboundMessageData
StreamWg sync.WaitGroup
ReceiveWg sync.WaitGroup
Address libcommon.Address
Eth1ExecutionService *eth1.EthereumExecutionModule
Notifications *shards.Notifications
@ -293,7 +298,6 @@ func MockWithEverything(tb testing.TB, gspec *types.Genesis, key *ecdsa.PrivateK
sendBodyRequest := func(context.Context, *bodydownload.BodyRequest) ([64]byte, bool) { return [64]byte{}, false }
blockPropagator := func(Ctx context.Context, header *types.Header, body *types.RawBody, td *big.Int) {}
if !cfg.DeprecatedTxPool.Disable {
poolCfg := txpoolcfg.DefaultConfig
newTxs := make(chan types2.Announcements, 1024)
@ -339,10 +343,14 @@ func MockWithEverything(tb testing.TB, gspec *types.Genesis, key *ecdsa.PrivateK
inMemoryExecution := func(batch kv.RwTx, header *types.Header, body *types.RawBody, unwindPoint uint64, headersChain []*types.Header, bodiesChain []*types.RawBody,
notifications *shards.Notifications) error {
terseLogger := log.New()
terseLogger.SetHandler(log.LvlFilterHandler(log.LvlWarn, log.StderrHandler))
// Needs its own notifications to not update RPC daemon and txpool about pending blocks
stateSync := stages2.NewInMemoryExecution(ctx, mock.DB, &ethconfig.Defaults, mock.sentriesClient, dirs, notifications, mock.BlockReader, blockWriter, agg, nil, log.New() /* logging will be discarded */)
stateSync := stages2.NewInMemoryExecution(mock.Ctx, mock.DB, &cfg, mock.sentriesClient,
dirs, notifications, mock.BlockReader, blockWriter, mock.agg, nil, terseLogger)
chainReader := stagedsync.NewChainReaderImpl(mock.ChainConfig, batch, mock.BlockReader, logger)
// We start the mining step
if err := stages2.StateStep(ctx, nil, engine, batch, blockWriter, stateSync, mock.sentriesClient.Bd, header, body, unwindPoint, headersChain, bodiesChain); err != nil {
if err := stages2.StateStep(ctx, chainReader, mock.Engine, batch, blockWriter, stateSync, mock.sentriesClient.Bd, header, body, unwindPoint, headersChain, bodiesChain); err != nil {
logger.Warn("Could not validate block", "err", err)
return err
}
@ -387,6 +395,28 @@ func MockWithEverything(tb testing.TB, gspec *types.Genesis, key *ecdsa.PrivateK
var snapshotsDownloader proto_downloader.DownloaderClient
// proof-of-stake mining
assembleBlockPOS := func(param *core.BlockBuilderParameters, interrupt *int32) (*types.BlockWithReceipts, error) {
miningStatePos := stagedsync.NewProposingState(&cfg.Miner)
miningStatePos.MiningConfig.Etherbase = param.SuggestedFeeRecipient
proposingSync := stagedsync.New(
stagedsync.MiningStages(mock.Ctx,
stagedsync.StageMiningCreateBlockCfg(mock.DB, miningStatePos, *mock.ChainConfig, mock.Engine, mock.txPoolDB, param, tmpdir, mock.BlockReader),
stagedsync.StageBorHeimdallCfg(mock.DB, miningStatePos, *mock.ChainConfig, nil, mock.BlockReader, nil, nil),
stagedsync.StageMiningExecCfg(mock.DB, miningStatePos, mock.Notifications.Events, *mock.ChainConfig, mock.Engine, &vm.Config{}, tmpdir, interrupt, param.PayloadId, mock.TxPool, mock.txPoolDB, mock.BlockReader),
stagedsync.StageHashStateCfg(mock.DB, dirs, cfg.HistoryV3),
stagedsync.StageTrieCfg(mock.DB, false, true, true, tmpdir, mock.BlockReader, nil, histV3, mock.agg),
stagedsync.StageMiningFinishCfg(mock.DB, *mock.ChainConfig, mock.Engine, miningStatePos, nil, mock.BlockReader, latestBlockBuiltStore),
), stagedsync.MiningUnwindOrder, stagedsync.MiningPruneOrder,
logger)
// We start the mining step
if err := stages2.MiningStep(ctx, mock.DB, proposingSync, tmpdir); err != nil {
return nil, err
}
block := <-miningStatePos.MiningResultPOSCh
return block, nil
}
blockRetire := freezeblocks.NewBlockRetire(1, dirs, mock.BlockReader, blockWriter, mock.DB, mock.Notifications.Events, logger)
mock.Sync = stagedsync.New(
stagedsync.DefaultStages(mock.Ctx,
@ -429,6 +459,13 @@ func MockWithEverything(tb testing.TB, gspec *types.Genesis, key *ecdsa.PrivateK
logger,
)
cfg.Genesis = gspec
pipelineStages := stages2.NewPipelineStages(mock.Ctx, db, &cfg, mock.sentriesClient, mock.Notifications,
snapshotsDownloader, mock.BlockReader, blockRetire, mock.agg, nil, forkValidator, logger, checkStateRoot)
mock.posStagedSync = stagedsync.New(pipelineStages, stagedsync.PipelineUnwindOrder, stagedsync.PipelinePruneOrder, logger)
mock.Eth1ExecutionService = eth1.NewEthereumExecutionModule(mock.BlockReader, mock.DB, mock.posStagedSync, forkValidator, mock.ChainConfig, assembleBlockPOS, nil, mock.Notifications.Accumulator, mock.Notifications.StateChangesConsumer, logger, histV3)
mock.sentriesClient.Hd.StartPoSDownloader(mock.Ctx, sendHeaderRequest, penalize)
miningConfig := cfg.Miner
@ -460,9 +497,6 @@ func MockWithEverything(tb testing.TB, gspec *types.Genesis, key *ecdsa.PrivateK
)
cfg.Genesis = gspec
pipelineStages := stages2.NewPipelineStages(ctx, db, &cfg, mock.sentriesClient, mock.Notifications,
snapshotsDownloader, mock.BlockReader, blockRetire, mock.agg, nil, forkValidator, logger, checkStateRoot)
mock.posStagedSync = stagedsync.New(pipelineStages, stagedsync.PipelineUnwindOrder, stagedsync.PipelinePruneOrder, logger)
mock.StreamWg.Add(1)
go mock.sentriesClient.RecvMessageLoop(mock.Ctx, mock.SentryClient, &mock.ReceiveWg)
@ -623,105 +657,82 @@ func (ms *MockSentry) insertPoWBlocks(chain *core.ChainPack, tx kv.RwTx) error {
return nil
}
func (ms *MockSentry) insertPoSBlocks(chain *core.ChainPack, tx kv.RwTx) error {
func (ms *MockSentry) insertPoSBlocks(chain *core.ChainPack) error {
n := ms.numberOfPoWBlocks(chain)
if n >= chain.Length() {
return nil
}
var bottomBlock *types.Block
for i := n; i < chain.Length(); i++ {
if err := chain.Blocks[i].HashCheck(); err != nil {
return err
}
if bottomBlock == nil {
bottomBlock = chain.Blocks[i]
}
cr := stagedsync.ChainReader{Cfg: *ms.ChainConfig, Db: tx, BlockReader: ms.BlockReader}
if err := ms.Engine.VerifyHeader(cr, chain.Blocks[i].Header(), true); err != nil {
return err
}
if err := ms.Engine.VerifyUncles(cr, chain.Blocks[i].Header(), chain.Blocks[i].Uncles()); err != nil {
return err
}
rawdb.WriteHeader(tx, chain.Blocks[i].Header())
if _, err := rawdb.WriteRawBodyIfNotExists(tx, chain.Blocks[i].Hash(), chain.Blocks[i].NumberU64(), chain.Blocks[i].RawBody()); err != nil {
return err
}
parentTd, err := rawdb.ReadTd(tx, chain.Blocks[i].ParentHash(), chain.Blocks[i].NumberU64()-1)
if err != nil || parentTd == nil {
return fmt.Errorf("no td %s", err)
}
td := new(big.Int).Add(parentTd, chain.Blocks[i].Difficulty())
if err = rawdb.WriteTd(tx, chain.Blocks[i].Hash(), chain.Blocks[i].NumberU64(), td); err != nil {
return err
}
rawdb.WriteCanonicalHash(tx, chain.Blocks[i].Hash(), chain.Blocks[i].NumberU64())
}
if bottomBlock == nil {
return nil
}
currentHash := bottomBlock.ParentHash()
currentNumber := bottomBlock.NumberU64() - 1
for canonical, err := rawdb.IsCanonicalHash(tx, currentHash, currentNumber); !canonical; canonical, err = rawdb.IsCanonicalHash(tx, currentHash, currentNumber) {
res, err := ms.Eth1ExecutionService.InsertBlocks(ms.Ctx, &execution.InsertBlocksRequest{
Blocks: []*execution.Block{eth1_utils.ConvertBlockToRPC(chain.Blocks[i])},
})
if err != nil {
return err
}
currentHeader := rawdb.ReadHeader(tx, currentHash, currentNumber)
if currentHeader == nil {
return fmt.Errorf("missing header")
if res.Result != execution.ExecutionStatus_Success {
return fmt.Errorf("insertion failed for block %d, code: %s", chain.Blocks[i].NumberU64(), res.Result.String())
}
if err := rawdb.WriteCanonicalHash(tx, currentHash, currentNumber); err != nil {
vRes, err := ms.Eth1ExecutionService.ValidateChain(ms.Ctx, &execution.ValidationRequest{
Hash: gointerfaces.ConvertHashToH256(chain.Blocks[i].Hash()),
Number: chain.Blocks[i].NumberU64(),
})
if err != nil {
return err
}
if vRes.ValidationStatus != execution.ExecutionStatus_Success {
return fmt.Errorf("insertion failed for block %d, code: %s", chain.Blocks[i].NumberU64(), vRes.ValidationStatus.String())
}
currentHash = currentHeader.ParentHash
currentNumber--
receipt, err := ms.Eth1ExecutionService.UpdateForkChoice(ms.Ctx, &execution.ForkChoice{
HeadBlockHash: gointerfaces.ConvertHashToH256(chain.Blocks[i].Hash()),
SafeBlockHash: gointerfaces.ConvertHashToH256(chain.Blocks[i].Hash()),
FinalizedBlockHash: gointerfaces.ConvertHashToH256(chain.Blocks[i].Hash()),
Timeout: uint64(1 * time.Hour),
})
if err != nil {
return err
}
if receipt.Status != execution.ExecutionStatus_Success {
return fmt.Errorf("forkchoice failed for block %d, code: %s", chain.Blocks[i].NumberU64(), receipt.Status.String())
}
}
ms.posStagedSync.UnwindTo(currentNumber, libcommon.Hash{})
ms.posStagedSync.RunUnwind(ms.DB, tx)
hook := stages2.NewHook(ms.Ctx, ms.DB, ms.Notifications, ms.Sync, ms.BlockReader, ms.ChainConfig, ms.Log, ms.UpdateHead)
if err := stages.SaveStageProgress(tx, stages.Headers, chain.TopBlock.NumberU64()); err != nil {
return err
}
if err := stages.SaveStageProgress(tx, stages.Bodies, chain.TopBlock.NumberU64()); err != nil {
return err
}
rawdb.WriteHeadHeaderHash(tx, chain.TopBlock.Hash())
return stages2.StageLoopIteration(ms.Ctx, ms.DB, tx, ms.posStagedSync, MockInsertAsInitialCycle, ms.Log, ms.BlockReader, hook, false)
return nil
}
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
}
defer tx.Rollback()
func (ms *MockSentry) InsertChain(chain *core.ChainPack) error {
tx, err := ms.DB.BeginRw(ms.Ctx)
if err != nil {
return err
}
defer tx.Rollback()
if err := ms.insertPoWBlocks(chain, tx); err != nil {
return err
}
if err := ms.insertPoSBlocks(chain, tx); err != nil {
if err := tx.Commit(); err != nil {
return err
}
if err := ms.insertPoSBlocks(chain); err != nil {
return err
}
roTx, err := ms.DB.BeginRo(ms.Ctx)
if err != nil {
return err
}
defer roTx.Rollback()
// Check if the latest header was imported or rolled back
if rawdb.ReadHeader(tx, chain.TopBlock.Hash(), chain.TopBlock.NumberU64()) == nil {
if rawdb.ReadHeader(roTx, 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)
execAt, err := stages.GetStageProgress(roTx, stages.Execution)
if err != nil {
return err
}
@ -747,11 +758,6 @@ func (ms *MockSentry) InsertChain(chain *core.ChainPack, tx kv.RwTx) error {
//}
//}
if !externalTx {
if err := tx.Commit(); err != nil {
return err
}
}
return nil
}