Skip to content

Commit

Permalink
Merge pull request #21 from bitfinity-network/fix_state_root_calculation
Browse files Browse the repository at this point in the history
Initialize data for state root calculation (restore hash state initialization)
  • Loading branch information
blutooth authored Jan 25, 2024
2 parents e85d51c + 49bd515 commit 73759a4
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions cmd/blockimporter/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,36 @@ func NewState(db *DB, initialBalances []BalanceEntry, chainID int64) (*State, er
MergeNetsplitBlock: big.NewInt(0),
}
genesis.Config = &chainConfig
_, _, err := core.CommitGenesisBlock(db.GetChain(), &genesis, "")
_, block, err := core.CommitGenesisBlock(db.GetChain(), &genesis, "")
if err != nil {
return nil, err
}

state.blockNum = big.NewInt(1)
state.totalDifficulty = big.NewInt(0)
state.chainConfig = &chainConfig

// cInitialize data needed for state root calculation
tx, err := db.GetChain().BeginRw(context.Background())
if err != nil {
return nil, err
}
defer tx.Rollback()

dirs := datadir2.New(db.path)
if err = stagedsync.PromoteHashedStateCleanly("logPrefix", tx, stagedsync.StageHashStateCfg(db.chain, dirs, false, nil), context.Background()); err != nil {
return nil, fmt.Errorf("error while promoting state: %v", err)
}
if root, err := trie.CalcRoot("block state root", tx); err != nil {
return nil, err
} else if root != block.Root() {
// This error may happen if we forgot to initialize the data for state root calculation.
// Better fail here in this case rather than in the first block
return nil, fmt.Errorf("invalid root, have: %s, want: %s", root.String(), block.Root().String())
}
if err = tx.Commit(); err != nil {
return nil, err
}
} else {
state.blockNum = (&big.Int{}).SetUint64(*blockNum + 1)

Expand All @@ -102,7 +124,9 @@ func NewState(db *DB, initialBalances []BalanceEntry, chainID int64) (*State, er
return nil, err
}

tx.Commit()
if err = tx.Commit(); err != nil {
return nil, err
}
}

return state, err
Expand Down

0 comments on commit 73759a4

Please sign in to comment.