Skip to content

Commit 48b5524

Browse files
committed
Inject tries to BlockState and StorageState
1 parent 4170052 commit 48b5524

18 files changed

+111
-87
lines changed

dot/rpc/modules/dev_integration_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ func newState(t *testing.T) (*state.BlockState, *state.EpochState) {
4343
db := state.NewInMemoryDB(t)
4444

4545
_, genesisTrie, genesisHeader := genesis.NewTestGenesisWithTrieAndHeader(t)
46-
bs, err := state.NewBlockStateFromGenesis(db, genesisTrie, genesisHeader, telemetryMock)
46+
tries := state.NewTries(genesisTrie)
47+
bs, err := state.NewBlockStateFromGenesis(db, tries, genesisHeader, telemetryMock)
4748
require.NoError(t, err)
4849
es, err := state.NewEpochStateFromGenesis(db, bs, genesisBABEConfig)
4950
require.NoError(t, err)

dot/state/block.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"github.com/ChainSafe/gossamer/lib/blocktree"
1919
"github.com/ChainSafe/gossamer/lib/common"
2020
"github.com/ChainSafe/gossamer/lib/runtime"
21-
"github.com/ChainSafe/gossamer/lib/trie"
2221
"github.com/ChainSafe/gossamer/pkg/scale"
2322
"github.com/prometheus/client_golang/prometheus"
2423
"github.com/prometheus/client_golang/prometheus/promauto"
@@ -60,7 +59,7 @@ type BlockState struct {
6059
genesisHash common.Hash
6160
lastFinalised common.Hash
6261
unfinalisedBlocks *sync.Map // map[common.Hash]*types.Block
63-
tries *tries
62+
tries *Tries
6463

6564
// block notifiers
6665
imported map[chan *types.Block]struct{}
@@ -74,7 +73,7 @@ type BlockState struct {
7473
}
7574

7675
// NewBlockState will create a new BlockState backed by the database located at basePath
77-
func NewBlockState(db chaindb.Database, trs *tries, telemetry telemetry.Client) (*BlockState, error) {
76+
func NewBlockState(db chaindb.Database, trs *Tries, telemetry telemetry.Client) (*BlockState, error) {
7877
bs := &BlockState{
7978
dbPath: db.Path(),
8079
baseState: NewBaseState(db),
@@ -106,14 +105,14 @@ func NewBlockState(db chaindb.Database, trs *tries, telemetry telemetry.Client)
106105

107106
// NewBlockStateFromGenesis initialises a BlockState from a genesis header,
108107
// saving it to the database located at basePath
109-
func NewBlockStateFromGenesis(db chaindb.Database, trie *trie.Trie, header *types.Header,
110-
telemetryMailer telemetry.Client) (*BlockState, error) {
108+
func NewBlockStateFromGenesis(db chaindb.Database, trs *Tries, header *types.Header,
109+
telemetryMailer telemetry.Client) (*BlockState, error) { // TODO CHECKTEST
111110
bs := &BlockState{
112111
bt: blocktree.NewBlockTreeFromRoot(header),
113112
baseState: NewBaseState(db),
114113
db: chaindb.NewTable(db, blockPrefix),
115114
unfinalisedBlocks: new(sync.Map),
116-
tries: newTries(trie),
115+
tries: trs,
117116
imported: make(map[chan *types.Block]struct{}),
118117
finalised: make(map[chan *types.FinalisationInfo]struct{}),
119118
runtimeUpdateSubscriptions: make(map[uint32]chan<- runtime.Version),

dot/state/block_data_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
)
1616

1717
func TestGetSet_ReceiptMessageQueue_Justification(t *testing.T) {
18-
s := newTestBlockState(t, nil)
18+
s := newTestBlockState(t, nil, newTriesEmpty())
1919
require.NotNil(t, s)
2020

2121
var genesisHeader = &types.Header{

dot/state/block_finalisation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func (bs *BlockState) SetFinalisedHash(hash common.Hash, round, setID uint64) er
144144
bs.notifyFinalized(hash, round, setID)
145145
}
146146

147-
pruned := bs.bt.Prune(hash) // TODO this is always 0
147+
pruned := bs.bt.Prune(hash)
148148
for _, hash := range pruned {
149149
block, has := bs.getAndDeleteUnfinalisedBlock(hash)
150150
if !has {

dot/state/block_finalisation_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
func TestHighestRoundAndSetID(t *testing.T) {
16-
bs := newTestBlockState(t, testGenesisHeader)
16+
bs := newTestBlockState(t, testGenesisHeader, newTriesEmpty())
1717
round, setID, err := bs.GetHighestRoundAndSetID()
1818
require.NoError(t, err)
1919
require.Equal(t, uint64(0), round)
@@ -61,7 +61,7 @@ func TestHighestRoundAndSetID(t *testing.T) {
6161
}
6262

6363
func TestBlockState_SetFinalisedHash(t *testing.T) {
64-
bs := newTestBlockState(t, testGenesisHeader)
64+
bs := newTestBlockState(t, testGenesisHeader, newTriesEmpty())
6565
h, err := bs.GetFinalisedHash(0, 0)
6666
require.NoError(t, err)
6767
require.Equal(t, testGenesisHeader.Hash(), h)
@@ -97,7 +97,7 @@ func TestBlockState_SetFinalisedHash(t *testing.T) {
9797
}
9898

9999
func TestSetFinalisedHash_setFirstSlotOnFinalisation(t *testing.T) {
100-
bs := newTestBlockState(t, testGenesisHeader)
100+
bs := newTestBlockState(t, testGenesisHeader, newTriesEmpty())
101101
firstSlot := uint64(42069)
102102

103103
digest := types.NewDigest()

dot/state/block_notify_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ import (
1212
"github.com/ChainSafe/gossamer/dot/types"
1313
"github.com/ChainSafe/gossamer/lib/runtime"
1414
runtimemocks "github.com/ChainSafe/gossamer/lib/runtime/mocks"
15+
"github.com/ChainSafe/gossamer/lib/trie"
1516
"github.com/stretchr/testify/require"
1617
)
1718

1819
var testMessageTimeout = time.Second * 3
1920

2021
func TestImportChannel(t *testing.T) {
21-
bs := newTestBlockState(t, testGenesisHeader)
22+
bs := newTestBlockState(t, testGenesisHeader, NewTries(trie.NewEmptyTrie()))
2223
ch := bs.GetImportedBlockNotifierChannel()
2324

2425
defer bs.FreeImportedBlockNotifierChannel(ch)
@@ -35,7 +36,7 @@ func TestImportChannel(t *testing.T) {
3536
}
3637

3738
func TestFreeImportedBlockNotifierChannel(t *testing.T) {
38-
bs := newTestBlockState(t, testGenesisHeader)
39+
bs := newTestBlockState(t, testGenesisHeader, NewTries(trie.NewEmptyTrie()))
3940
ch := bs.GetImportedBlockNotifierChannel()
4041
require.Equal(t, 1, len(bs.imported))
4142

@@ -44,7 +45,7 @@ func TestFreeImportedBlockNotifierChannel(t *testing.T) {
4445
}
4546

4647
func TestFinalizedChannel(t *testing.T) {
47-
bs := newTestBlockState(t, testGenesisHeader)
48+
bs := newTestBlockState(t, testGenesisHeader, NewTries(trie.NewEmptyTrie()))
4849

4950
ch := bs.GetFinalisedNotifierChannel()
5051

@@ -66,7 +67,7 @@ func TestFinalizedChannel(t *testing.T) {
6667
}
6768

6869
func TestImportChannel_Multi(t *testing.T) {
69-
bs := newTestBlockState(t, testGenesisHeader)
70+
bs := newTestBlockState(t, testGenesisHeader, NewTries(trie.NewEmptyTrie()))
7071

7172
num := 5
7273
chs := make([]chan *types.Block, num)
@@ -99,7 +100,7 @@ func TestImportChannel_Multi(t *testing.T) {
99100
}
100101

101102
func TestFinalizedChannel_Multi(t *testing.T) {
102-
bs := newTestBlockState(t, testGenesisHeader)
103+
bs := newTestBlockState(t, testGenesisHeader, NewTries(trie.NewEmptyTrie()))
103104

104105
num := 5
105106
chs := make([]chan *types.FinalisationInfo, num)
@@ -136,7 +137,7 @@ func TestFinalizedChannel_Multi(t *testing.T) {
136137
}
137138

138139
func TestService_RegisterUnRegisterRuntimeUpdatedChannel(t *testing.T) {
139-
bs := newTestBlockState(t, testGenesisHeader)
140+
bs := newTestBlockState(t, testGenesisHeader, NewTries(trie.NewEmptyTrie()))
140141
ch := make(chan<- runtime.Version)
141142
chID, err := bs.RegisterRuntimeUpdatedChannel(ch)
142143
require.NoError(t, err)
@@ -147,7 +148,7 @@ func TestService_RegisterUnRegisterRuntimeUpdatedChannel(t *testing.T) {
147148
}
148149

149150
func TestService_RegisterUnRegisterConcurrentCalls(t *testing.T) {
150-
bs := newTestBlockState(t, testGenesisHeader)
151+
bs := newTestBlockState(t, testGenesisHeader, NewTries(trie.NewEmptyTrie()))
151152

152153
go func() {
153154
for i := 0; i < 100; i++ {

dot/state/block_race_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ func TestConcurrencySetHeader(t *testing.T) {
2828
dbs[i] = NewInMemoryDB(t)
2929
}
3030

31-
someTrie := trie.NewEmptyTrie() // not used in this test
31+
tries := NewTries(trie.NewEmptyTrie()) // not used in this test
3232

3333
pend := new(sync.WaitGroup)
3434
pend.Add(threads)
3535
for i := 0; i < threads; i++ {
3636
go func(index int) {
3737
defer pend.Done()
3838

39-
bs, err := NewBlockStateFromGenesis(dbs[index], someTrie, testGenesisHeader, telemetryMock)
39+
bs, err := NewBlockStateFromGenesis(dbs[index], tries, testGenesisHeader, telemetryMock)
4040
require.NoError(t, err)
4141

4242
header := &types.Header{

dot/state/block_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var testGenesisHeader = &types.Header{
2525
Digest: types.NewDigest(),
2626
}
2727

28-
func newTestBlockState(t *testing.T, header *types.Header) *BlockState {
28+
func newTestBlockState(t *testing.T, header *types.Header, tries *Tries) *BlockState {
2929
ctrl := gomock.NewController(t)
3030
telemetryMock := NewMockClient(ctrl)
3131
telemetryMock.EXPECT().SendMessage(gomock.Any()).AnyTimes()
@@ -35,13 +35,13 @@ func newTestBlockState(t *testing.T, header *types.Header) *BlockState {
3535
header = testGenesisHeader
3636
}
3737

38-
bs, err := NewBlockStateFromGenesis(db, trie.NewEmptyTrie(), header, telemetryMock)
38+
bs, err := NewBlockStateFromGenesis(db, tries, header, telemetryMock)
3939
require.NoError(t, err)
4040
return bs
4141
}
4242

4343
func TestSetAndGetHeader(t *testing.T) {
44-
bs := newTestBlockState(t, nil)
44+
bs := newTestBlockState(t, nil, newTriesEmpty())
4545

4646
header := &types.Header{
4747
Number: big.NewInt(0),
@@ -58,7 +58,7 @@ func TestSetAndGetHeader(t *testing.T) {
5858
}
5959

6060
func TestHasHeader(t *testing.T) {
61-
bs := newTestBlockState(t, nil)
61+
bs := newTestBlockState(t, nil, newTriesEmpty())
6262

6363
header := &types.Header{
6464
Number: big.NewInt(0),
@@ -75,7 +75,7 @@ func TestHasHeader(t *testing.T) {
7575
}
7676

7777
func TestGetBlockByNumber(t *testing.T) {
78-
bs := newTestBlockState(t, testGenesisHeader)
78+
bs := newTestBlockState(t, testGenesisHeader, newTriesEmpty())
7979

8080
blockHeader := &types.Header{
8181
ParentHash: testGenesisHeader.Hash(),
@@ -97,7 +97,7 @@ func TestGetBlockByNumber(t *testing.T) {
9797
}
9898

9999
func TestAddBlock(t *testing.T) {
100-
bs := newTestBlockState(t, testGenesisHeader)
100+
bs := newTestBlockState(t, testGenesisHeader, newTriesEmpty())
101101

102102
// Create header
103103
header0 := &types.Header{
@@ -160,7 +160,7 @@ func TestAddBlock(t *testing.T) {
160160
}
161161

162162
func TestGetSlotForBlock(t *testing.T) {
163-
bs := newTestBlockState(t, testGenesisHeader)
163+
bs := newTestBlockState(t, testGenesisHeader, newTriesEmpty())
164164
expectedSlot := uint64(77)
165165

166166
babeHeader := types.NewBabeDigest()
@@ -191,7 +191,7 @@ func TestGetSlotForBlock(t *testing.T) {
191191
}
192192

193193
func TestIsBlockOnCurrentChain(t *testing.T) {
194-
bs := newTestBlockState(t, testGenesisHeader)
194+
bs := newTestBlockState(t, testGenesisHeader, newTriesEmpty())
195195
currChain, branchChains := AddBlocksToState(t, bs, 3, false)
196196

197197
for _, header := range currChain {
@@ -214,7 +214,7 @@ func TestIsBlockOnCurrentChain(t *testing.T) {
214214
}
215215

216216
func TestAddBlock_BlockNumberToHash(t *testing.T) {
217-
bs := newTestBlockState(t, testGenesisHeader)
217+
bs := newTestBlockState(t, testGenesisHeader, newTriesEmpty())
218218
currChain, branchChains := AddBlocksToState(t, bs, 8, false)
219219

220220
bestHash := bs.BestBlockHash()
@@ -262,7 +262,7 @@ func TestAddBlock_BlockNumberToHash(t *testing.T) {
262262
}
263263

264264
func TestFinalization_DeleteBlock(t *testing.T) {
265-
bs := newTestBlockState(t, testGenesisHeader)
265+
bs := newTestBlockState(t, testGenesisHeader, newTriesEmpty())
266266
AddBlocksToState(t, bs, 5, false)
267267

268268
btBefore := bs.bt.DeepCopy()
@@ -317,7 +317,7 @@ func TestFinalization_DeleteBlock(t *testing.T) {
317317
}
318318

319319
func TestGetHashByNumber(t *testing.T) {
320-
bs := newTestBlockState(t, testGenesisHeader)
320+
bs := newTestBlockState(t, testGenesisHeader, newTriesEmpty())
321321

322322
res, err := bs.GetHashByNumber(big.NewInt(0))
323323
require.NoError(t, err)
@@ -344,7 +344,7 @@ func TestGetHashByNumber(t *testing.T) {
344344

345345
func TestAddBlock_WithReOrg(t *testing.T) {
346346
t.Skip() // TODO: this should be fixed after state refactor PR
347-
bs := newTestBlockState(t, testGenesisHeader)
347+
bs := newTestBlockState(t, testGenesisHeader, newTriesEmpty())
348348

349349
header1a := &types.Header{
350350
Number: big.NewInt(1),
@@ -453,7 +453,7 @@ func TestAddBlock_WithReOrg(t *testing.T) {
453453
}
454454

455455
func TestAddBlockToBlockTree(t *testing.T) {
456-
bs := newTestBlockState(t, testGenesisHeader)
456+
bs := newTestBlockState(t, testGenesisHeader, newTriesEmpty())
457457

458458
header := &types.Header{
459459
Number: big.NewInt(1),
@@ -473,7 +473,7 @@ func TestAddBlockToBlockTree(t *testing.T) {
473473
}
474474

475475
func TestNumberIsFinalised(t *testing.T) {
476-
bs := newTestBlockState(t, testGenesisHeader)
476+
bs := newTestBlockState(t, testGenesisHeader, newTriesEmpty())
477477
fin, err := bs.NumberIsFinalised(big.NewInt(0))
478478
require.NoError(t, err)
479479
require.True(t, fin)

dot/state/epoch_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/ChainSafe/gossamer/dot/types"
1212
"github.com/ChainSafe/gossamer/lib/crypto/sr25519"
1313
"github.com/ChainSafe/gossamer/lib/keystore"
14+
"github.com/ChainSafe/gossamer/lib/trie"
1415
"github.com/ChainSafe/gossamer/pkg/scale"
1516

1617
"github.com/stretchr/testify/require"
@@ -28,7 +29,8 @@ var genesisBABEConfig = &types.BabeConfiguration{
2829

2930
func newEpochStateFromGenesis(t *testing.T) *EpochState {
3031
db := NewInMemoryDB(t)
31-
s, err := NewEpochStateFromGenesis(db, newTestBlockState(t, nil), genesisBABEConfig)
32+
blockState := newTestBlockState(t, nil, NewTries(trie.NewEmptyTrie()))
33+
s, err := NewEpochStateFromGenesis(db, blockState, genesisBABEConfig)
3234
require.NoError(t, err)
3335
return s
3436
}
@@ -184,7 +186,7 @@ func TestEpochState_SetAndGetSlotDuration(t *testing.T) {
184186

185187
func TestEpochState_GetEpochFromTime(t *testing.T) {
186188
s := newEpochStateFromGenesis(t)
187-
s.blockState = newTestBlockState(t, testGenesisHeader)
189+
s.blockState = newTestBlockState(t, testGenesisHeader, NewTries(trie.NewEmptyTrie()))
188190

189191
epochDuration, err := time.ParseDuration(
190192
fmt.Sprintf("%dms",

dot/state/helpers_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,17 @@ import (
88
"testing"
99
"time"
1010

11+
"github.com/ChainSafe/gossamer/lib/common"
12+
"github.com/ChainSafe/gossamer/lib/trie"
1113
"github.com/stretchr/testify/require"
1214
)
1315

16+
func newTriesEmpty() *Tries {
17+
return &Tries{
18+
rootToTrie: make(map[common.Hash]*trie.Trie),
19+
}
20+
}
21+
1422
// newGenerator creates a new PRNG seeded with the
1523
// unix nanoseconds value of the current time.
1624
func newGenerator() (prng *rand.Rand) {

0 commit comments

Comments
 (0)