Skip to content

Commit

Permalink
Dependency inject to NewBlockStateFromGenesis
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Dec 6, 2022
1 parent 92aed5e commit 8f14659
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 12 deletions.
8 changes: 7 additions & 1 deletion dot/rpc/modules/dev_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"encoding/binary"
"testing"

"github.com/ChainSafe/chaindb"
"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/babe"
Expand Down Expand Up @@ -43,7 +44,12 @@ func newState(t *testing.T) (*state.BlockState, *state.EpochState) {
_, genesisTrie, genesisHeader := newTestGenesisWithTrieAndHeader(t)
tries := state.NewTries()
tries.SetTrie(&genesisTrie)
bs, err := state.NewBlockStateFromGenesis(db, tries, &genesisHeader, telemetryMock)

baseState := state.NewBaseState(db)
const blockPrefix = "block"
blockStateDatabase := chaindb.NewTable(db, blockPrefix)
bs, err := state.NewBlockStateFromGenesis(blockStateDatabase,
baseState, tries, &genesisHeader, telemetryMock)
require.NoError(t, err)
es, err := state.NewEpochStateFromGenesis(db, bs, genesisBABEConfig)
require.NoError(t, err)
Expand Down
12 changes: 7 additions & 5 deletions dot/state/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
)

const (
// blockPrefix is the prefix to use for the block state
// key value database.
blockPrefix = "block"
)

Expand Down Expand Up @@ -101,13 +103,13 @@ func NewBlockState(db BlockStateDatabase, baseState *BaseState,
}

// NewBlockStateFromGenesis initialises a BlockState from a genesis header,
// saving it to the database located at basePath
func NewBlockStateFromGenesis(db *chaindb.BadgerDB, trs *Tries, header *types.Header,
telemetryMailer Telemetry) (*BlockState, error) {
// saving it to the database.
func NewBlockStateFromGenesis(db BlockStateDatabase, baseState *BaseState,
trs *Tries, header *types.Header, telemetryMailer Telemetry) (*BlockState, error) {
bs := &BlockState{
bt: blocktree.NewBlockTreeFromRoot(header),
baseState: NewBaseState(db),
db: chaindb.NewTable(db, blockPrefix),
baseState: baseState,
db: db,
unfinalisedBlocks: newHashToBlockMap(),
tries: trs,
imported: make(map[chan *types.Block]struct{}),
Expand Down
6 changes: 5 additions & 1 deletion dot/state/block_race_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ func TestConcurrencySetHeader(t *testing.T) {
go func(index int) {
defer pend.Done()

bs, err := NewBlockStateFromGenesis(dbs[index], tries, testGenesisHeader, telemetryMock)
db := dbs[index]
baseState := NewBaseState(db)
blockStateDatabase := chaindb.NewTable(db, blockPrefix)
bs, err := NewBlockStateFromGenesis(blockStateDatabase,
baseState, tries, testGenesisHeader, telemetryMock)
require.NoError(t, err)

header := &types.Header{
Expand Down
6 changes: 5 additions & 1 deletion dot/state/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"
"time"

"github.com/ChainSafe/chaindb"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/trie"
Expand All @@ -32,7 +33,10 @@ func newTestBlockState(t *testing.T, tries *Tries) *BlockState {
db := NewInMemoryDB(t)
header := testGenesisHeader

bs, err := NewBlockStateFromGenesis(db, tries, header, telemetryMock)
baseState := NewBaseState(db)
blockStateDatabase := chaindb.NewTable(db, blockPrefix)
bs, err := NewBlockStateFromGenesis(blockStateDatabase,
baseState, tries, header, telemetryMock)
require.NoError(t, err)

// loads in-memory tries with genesis state root, should be deleted
Expand Down
4 changes: 3 additions & 1 deletion dot/state/grandpa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ func testBlockState(t *testing.T, db *chaindb.BadgerDB) *BlockState {
telemetryMock.EXPECT().SendMessage(gomock.Any()).AnyTimes()
header := testGenesisHeader

bs, err := NewBlockStateFromGenesis(db, newTriesEmpty(), header, telemetryMock)
baseState := NewBaseState(db)
blockStateDatabase := chaindb.NewTable(db, blockPrefix)
bs, err := NewBlockStateFromGenesis(blockStateDatabase, baseState, newTriesEmpty(), header, telemetryMock)
require.NoError(t, err)

// loads in-memory tries with genesis state root, should be deleted
Expand Down
5 changes: 3 additions & 2 deletions dot/state/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ func (s *Service) Initialise(gen *genesis.Genesis, header *types.Header, t *trie
tries := NewTries()
tries.SetTrie(t)

// create block state from genesis block
blockState, err := NewBlockStateFromGenesis(db, tries, header, s.Telemetry)
baseState := NewBaseState(db)
blockStateDatabase := chaindb.NewTable(db, blockPrefix)
blockState, err := NewBlockStateFromGenesis(blockStateDatabase, baseState, tries, header, s.Telemetry)
if err != nil {
return fmt.Errorf("failed to create block state from genesis: %s", err)
}
Expand Down
5 changes: 4 additions & 1 deletion dot/state/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"
"time"

"github.com/ChainSafe/chaindb"
"github.com/ChainSafe/gossamer/dot/state/pruner"
"github.com/ChainSafe/gossamer/dot/telemetry"
"github.com/ChainSafe/gossamer/dot/types"
Expand Down Expand Up @@ -194,7 +195,9 @@ func TestGetStorageChildAndGetStorageFromChild(t *testing.T) {

tries := newTriesEmpty()

blockState, err := NewBlockStateFromGenesis(db, tries, &genHeader, telemetryMock)
baseState := NewBaseState(db)
blockStateDatabase := chaindb.NewTable(db, blockPrefix)
blockState, err := NewBlockStateFromGenesis(blockStateDatabase, baseState, tries, &genHeader, telemetryMock)
require.NoError(t, err)

storage, err := NewStorageState(db, blockState, tries, pruner.Config{})
Expand Down

0 comments on commit 8f14659

Please sign in to comment.