Skip to content

Commit

Permalink
gossamer_storage_tries_cached_total metric
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Feb 18, 2022
1 parent f4f7465 commit 2210f11
Show file tree
Hide file tree
Showing 13 changed files with 427 additions and 73 deletions.
25 changes: 23 additions & 2 deletions dot/state/block_finalisation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
"testing"

"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/golang/mock/gomock"

"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -61,7 +64,16 @@ func TestHighestRoundAndSetID(t *testing.T) {
}

func TestBlockState_SetFinalisedHash(t *testing.T) {
bs := newTestBlockState(t, testGenesisHeader, newTriesEmpty())
ctrl := gomock.NewController(t)

triesGauge := NewMockGauge(ctrl)
triesGauge.EXPECT().Set(0.00)
tries := &Tries{
rootToTrie: make(map[common.Hash]*trie.Trie),
triesGauge: triesGauge,
}

bs := newTestBlockState(t, testGenesisHeader, tries)
h, err := bs.GetFinalisedHash(0, 0)
require.NoError(t, err)
require.Equal(t, testGenesisHeader.Hash(), h)
Expand Down Expand Up @@ -97,7 +109,16 @@ func TestBlockState_SetFinalisedHash(t *testing.T) {
}

func TestSetFinalisedHash_setFirstSlotOnFinalisation(t *testing.T) {
bs := newTestBlockState(t, testGenesisHeader, newTriesEmpty())
ctrl := gomock.NewController(t)

triesGauge := NewMockGauge(ctrl)
triesGauge.EXPECT().Set(0.00).Times(2)
tries := &Tries{
rootToTrie: make(map[common.Hash]*trie.Trie),
triesGauge: triesGauge,
}

bs := newTestBlockState(t, testGenesisHeader, tries)
firstSlot := uint64(42069)

digest := types.NewDigest()
Expand Down
34 changes: 27 additions & 7 deletions dot/state/block_notify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ import (
"time"

"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/runtime"
runtimemocks "github.com/ChainSafe/gossamer/lib/runtime/mocks"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
)

var testMessageTimeout = time.Second * 3

func TestImportChannel(t *testing.T) {
bs := newTestBlockState(t, testGenesisHeader, NewTries(trie.NewEmptyTrie()))
bs := newTestBlockState(t, testGenesisHeader, newTriesEmpty())
ch := bs.GetImportedBlockNotifierChannel()

defer bs.FreeImportedBlockNotifierChannel(ch)
Expand All @@ -36,7 +38,7 @@ func TestImportChannel(t *testing.T) {
}

func TestFreeImportedBlockNotifierChannel(t *testing.T) {
bs := newTestBlockState(t, testGenesisHeader, NewTries(trie.NewEmptyTrie()))
bs := newTestBlockState(t, testGenesisHeader, newTriesEmpty())
ch := bs.GetImportedBlockNotifierChannel()
require.Equal(t, 1, len(bs.imported))

Expand All @@ -45,7 +47,16 @@ func TestFreeImportedBlockNotifierChannel(t *testing.T) {
}

func TestFinalizedChannel(t *testing.T) {
bs := newTestBlockState(t, testGenesisHeader, NewTries(trie.NewEmptyTrie()))
ctrl := gomock.NewController(t)

triesGauge := NewMockGauge(ctrl)
triesGauge.EXPECT().Set(0.00).Times(3)
tries := &Tries{
rootToTrie: make(map[common.Hash]*trie.Trie),
triesGauge: triesGauge,
}

bs := newTestBlockState(t, testGenesisHeader, tries)

ch := bs.GetFinalisedNotifierChannel()

Expand All @@ -67,7 +78,7 @@ func TestFinalizedChannel(t *testing.T) {
}

func TestImportChannel_Multi(t *testing.T) {
bs := newTestBlockState(t, testGenesisHeader, NewTries(trie.NewEmptyTrie()))
bs := newTestBlockState(t, testGenesisHeader, newTriesEmpty())

num := 5
chs := make([]chan *types.Block, num)
Expand Down Expand Up @@ -100,7 +111,16 @@ func TestImportChannel_Multi(t *testing.T) {
}

func TestFinalizedChannel_Multi(t *testing.T) {
bs := newTestBlockState(t, testGenesisHeader, NewTries(trie.NewEmptyTrie()))
ctrl := gomock.NewController(t)

triesGauge := NewMockGauge(ctrl)
triesGauge.EXPECT().Set(0.00)
tries := &Tries{
rootToTrie: make(map[common.Hash]*trie.Trie),
triesGauge: triesGauge,
}

bs := newTestBlockState(t, testGenesisHeader, tries)

num := 5
chs := make([]chan *types.FinalisationInfo, num)
Expand Down Expand Up @@ -137,7 +157,7 @@ func TestFinalizedChannel_Multi(t *testing.T) {
}

func TestService_RegisterUnRegisterRuntimeUpdatedChannel(t *testing.T) {
bs := newTestBlockState(t, testGenesisHeader, NewTries(trie.NewEmptyTrie()))
bs := newTestBlockState(t, testGenesisHeader, newTriesEmpty())
ch := make(chan<- runtime.Version)
chID, err := bs.RegisterRuntimeUpdatedChannel(ch)
require.NoError(t, err)
Expand All @@ -148,7 +168,7 @@ func TestService_RegisterUnRegisterRuntimeUpdatedChannel(t *testing.T) {
}

func TestService_RegisterUnRegisterConcurrentCalls(t *testing.T) {
bs := newTestBlockState(t, testGenesisHeader, NewTries(trie.NewEmptyTrie()))
bs := newTestBlockState(t, testGenesisHeader, newTriesEmpty())

go func() {
for i := 0; i < 100; i++ {
Expand Down
2 changes: 1 addition & 1 deletion dot/state/block_race_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestConcurrencySetHeader(t *testing.T) {
dbs[i] = NewInMemoryDB(t)
}

tries := NewTries(trie.NewEmptyTrie()) // not used in this test
tries := (*Tries)(nil) // not used in this test

pend := new(sync.WaitGroup)
pend.Add(threads)
Expand Down
22 changes: 20 additions & 2 deletions dot/state/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,16 @@ func TestAddBlock_BlockNumberToHash(t *testing.T) {
}

func TestFinalization_DeleteBlock(t *testing.T) {
bs := newTestBlockState(t, testGenesisHeader, newTriesEmpty())
ctrl := gomock.NewController(t)

triesGauge := NewMockGauge(ctrl)
triesGauge.EXPECT().Set(0.00).Times(5)
tries := &Tries{
rootToTrie: make(map[common.Hash]*trie.Trie),
triesGauge: triesGauge,
}

bs := newTestBlockState(t, testGenesisHeader, tries)
AddBlocksToState(t, bs, 5, false)

btBefore := bs.bt.DeepCopy()
Expand Down Expand Up @@ -473,7 +482,16 @@ func TestAddBlockToBlockTree(t *testing.T) {
}

func TestNumberIsFinalised(t *testing.T) {
bs := newTestBlockState(t, testGenesisHeader, newTriesEmpty())
ctrl := gomock.NewController(t)

triesGauge := NewMockGauge(ctrl)
triesGauge.EXPECT().Set(0.00).Times(2)
tries := &Tries{
rootToTrie: make(map[common.Hash]*trie.Trie),
triesGauge: triesGauge,
}

bs := newTestBlockState(t, testGenesisHeader, tries)
fin, err := bs.NumberIsFinalised(big.NewInt(0))
require.NoError(t, err)
require.True(t, fin)
Expand Down
5 changes: 2 additions & 3 deletions dot/state/epoch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/crypto/sr25519"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/ChainSafe/gossamer/pkg/scale"

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

func newEpochStateFromGenesis(t *testing.T) *EpochState {
db := NewInMemoryDB(t)
blockState := newTestBlockState(t, nil, NewTries(trie.NewEmptyTrie()))
blockState := newTestBlockState(t, nil, newTriesEmpty())
s, err := NewEpochStateFromGenesis(db, blockState, genesisBABEConfig)
require.NoError(t, err)
return s
Expand Down Expand Up @@ -186,7 +185,7 @@ func TestEpochState_SetAndGetSlotDuration(t *testing.T) {

func TestEpochState_GetEpochFromTime(t *testing.T) {
s := newEpochStateFromGenesis(t)
s.blockState = newTestBlockState(t, testGenesisHeader, NewTries(trie.NewEmptyTrie()))
s.blockState = newTestBlockState(t, testGenesisHeader, newTriesEmpty())

epochDuration, err := time.ParseDuration(
fmt.Sprintf("%dms",
Expand Down
5 changes: 4 additions & 1 deletion dot/state/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ func (s *Service) Initialise(gen *genesis.Genesis, header *types.Header, t *trie
return fmt.Errorf("failed to write genesis values to database: %s", err)
}

tries := NewTries(t)
tries, err := NewTries(t)
if err != nil {
return fmt.Errorf("cannot setup tries: %w", err)
}

// create block state from genesis block
blockState, err := NewBlockStateFromGenesis(db, tries, header, s.Telemetry)
Expand Down
160 changes: 160 additions & 0 deletions dot/state/mock_gauge_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion dot/state/offline_pruner.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ func NewOfflinePruner(inputDBPath, prunedDBPath string, bloomSize uint64,
return nil, fmt.Errorf("failed to load DB %w", err)
}

tries := NewTries(trie.NewEmptyTrie())
tries, err := NewTries(trie.NewEmptyTrie())
if err != nil {
return nil, fmt.Errorf("cannot setup tries: %w", err)
}

// create blockState state
// NewBlockState on pruner execution does not use telemetry
Expand Down
Loading

0 comments on commit 2210f11

Please sign in to comment.