Skip to content

Commit

Permalink
More careful handle of sequences in stage_headers --reset (#4023)
Browse files Browse the repository at this point in the history
* save

* save

* save

* save
  • Loading branch information
AskAlexSharov authored Apr 29, 2022
1 parent 38e8706 commit 77560b6
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 154 deletions.
11 changes: 3 additions & 8 deletions cmd/integration/commands/stages.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon/cmd/rpcdaemon/interfaces"
"github.com/ledgerwatch/erigon/cmd/sentry/sentry"
"github.com/ledgerwatch/erigon/common/dbutils"
"github.com/ledgerwatch/erigon/consensus"
"github.com/ledgerwatch/erigon/consensus/ethash"
"github.com/ledgerwatch/erigon/core"
Expand Down Expand Up @@ -483,7 +482,7 @@ func stageHeaders(db kv.RwDB, ctx context.Context) error {
return fmt.Errorf("re-read Bodies progress: %w", err)
}
{ // hard-unwind stage_body also
if err := rawdb.DeleteNewBlocks(tx, progress+1); err != nil {
if err := rawdb.TruncateBlocks(tx, progress+1); err != nil {
return err
}
progressBodies, err := stages.GetStageProgress(tx, stages.Bodies)
Expand All @@ -497,14 +496,10 @@ func stageHeaders(db kv.RwDB, ctx context.Context) error {
}
}
// remove all canonical markers from this point
if err := tx.ForEach(kv.HeaderCanonical, dbutils.EncodeBlockNumber(progress+1), func(k, v []byte) error {
return tx.Delete(kv.HeaderCanonical, k, nil)
}); err != nil {
if err = rawdb.TruncateCanonicalHash(tx, progress+1); err != nil {
return err
}
if err := tx.ForEach(kv.HeaderTD, dbutils.EncodeBlockNumber(progress+1), func(k, v []byte) error {
return tx.Delete(kv.HeaderTD, k, nil)
}); err != nil {
if err = rawdb.TruncateTd(tx, progress+1); err != nil {
return err
}
hash, err := rawdb.ReadCanonicalHash(tx, progress-1)
Expand Down
42 changes: 37 additions & 5 deletions core/genesis_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,48 @@
package core

import (
"context"
"testing"

"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/memdb"
"github.com/ledgerwatch/erigon/params"
"github.com/ledgerwatch/erigon/params/networkname"
"github.com/stretchr/testify/require"
"testing"
)

func TestDefaultBSCGenesisBlock(t *testing.T) {
genesis := DefaultBSCGenesisBlock()
db := memdb.New()
_, block, err := CommitGenesisBlock(db, genesis)
db := memdb.NewTestDB(t)
check := func(network string) {
genesis := DefaultGenesisBlockByChainName(network)
tx, err := db.BeginRw(context.Background())
if err != nil {
t.Fatal(err)
}
defer tx.Rollback()
_, block, err := WriteGenesisBlock(tx, genesis)
require.NoError(t, err)
expect := params.GenesisHashByChainName(network)
require.NotNil(t, expect, network)
require.Equal(t, block.Hash().Bytes(), expect.Bytes(), network)
}
for _, network := range networkname.All {
check(network)
}
}

func TestCommitGenesisIdempotency(t *testing.T) {
_, tx := memdb.NewTestTx(t)
genesis := DefaultGenesisBlockByChainName(networkname.MainnetChainName)
_, _, err := WriteGenesisBlock(tx, genesis)
require.NoError(t, err)
seq, err := tx.ReadSequence(kv.EthTx)
require.NoError(t, err)
require.Equal(t, uint64(2), seq)

_, _, err = WriteGenesisBlock(tx, genesis)
require.NoError(t, err)
seq, err = tx.ReadSequence(kv.EthTx)
require.NoError(t, err)
require.Equal(t, block.Hash(), params.BSCGenesisHash)
require.Equal(t, uint64(2), seq)
}
Loading

0 comments on commit 77560b6

Please sign in to comment.