Skip to content

Commit

Permalink
cleanup core interfaces, fix digest package tests
Browse files Browse the repository at this point in the history
  • Loading branch information
noot committed Jun 11, 2021
1 parent cdee399 commit 46dbf3b
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 65 deletions.
12 changes: 0 additions & 12 deletions dot/core/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/ChainSafe/gossamer/dot/network"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/grandpa"
rtstorage "github.com/ChainSafe/gossamer/lib/runtime/storage"
"github.com/ChainSafe/gossamer/lib/transaction"
)
Expand Down Expand Up @@ -81,17 +80,6 @@ type Network interface {
// EpochState is the interface for state.EpochState
type EpochState interface {
GetEpochForBlock(header *types.Header) (uint64, error)
SetEpochData(epoch uint64, info *types.EpochData) error
SetConfigData(epoch uint64, info *types.ConfigData) error
SetCurrentEpoch(epoch uint64) error
GetCurrentEpoch() (uint64, error)
}

// GrandpaState is the interface for the state.GrandpaState
type GrandpaState interface {
SetNextChange(authorities []*grandpa.Voter, number *big.Int) error
IncrementSetID() error
SetNextPause(number *big.Int) error
SetNextResume(number *big.Int) error
GetCurrentSetID() (uint64, error)
}
114 changes: 79 additions & 35 deletions dot/digest/digest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,65 @@ import (

"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/crypto/ed25519"
"github.com/ChainSafe/gossamer/lib/crypto/sr25519"
"github.com/ChainSafe/gossamer/lib/genesis"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/trie"

log "github.com/ChainSafe/log15"
"github.com/stretchr/testify/require"
)

func newTestDigestHandler(t *testing.T, withBABE, withGrandpa bool) *DigestHandler { //nolint
// TODO: use these from core?
func addTestBlocksToState(t *testing.T, depth int, blockState BlockState) []*types.Header {
return addTestBlocksToStateWithParent(t, blockState.(*state.BlockState).BestBlockHash(), depth, blockState)
}

func addTestBlocksToStateWithParent(t *testing.T, previousHash common.Hash, depth int, blockState BlockState) []*types.Header {
prevHeader, err := blockState.(*state.BlockState).GetHeader(previousHash)
require.NoError(t, err)
previousNum := prevHeader.Number

headers := []*types.Header{}

for i := 1; i <= depth; i++ {
block := &types.Block{
Header: &types.Header{
ParentHash: previousHash,
Number: big.NewInt(int64(i)).Add(previousNum, big.NewInt(int64(i))),
Digest: types.Digest{},
},
Body: &types.Body{},
}

previousHash = block.Header.Hash()

err := blockState.(*state.BlockState).AddBlock(block)
require.NoError(t, err)
headers = append(headers, block.Header)
}

return headers
}

func newTestGenesisWithTrieAndHeader(t *testing.T) (*genesis.Genesis, *trie.Trie, *types.Header) {
gen, err := genesis.NewGenesisFromJSONRaw("../../chain/gssmr/genesis.json")
if err != nil {
gen, err = genesis.NewGenesisFromJSONRaw("../../../chain/gssmr/genesis.json")
require.NoError(t, err)
}

genTrie, err := genesis.NewTrieFromGenesis(gen)
require.NoError(t, err)

genesisHeader, err := types.NewHeader(common.NewHash([]byte{0}), genTrie.MustHash(), trie.EmptyHash, big.NewInt(0), types.Digest{})
require.NoError(t, err)
return gen, genTrie, genesisHeader
}

func newTestHandler(t *testing.T, withBABE, withGrandpa bool) *Handler { //nolint
testDatadirPath, err := ioutil.TempDir("/tmp", "test-datadir-*")
require.NoError(t, err)
stateSrvc := state.NewService(testDatadirPath, log.LvlInfo)
Expand All @@ -45,19 +95,14 @@ func newTestDigestHandler(t *testing.T, withBABE, withGrandpa bool) *DigestHandl
err = stateSrvc.Start()
require.NoError(t, err)

var bp BlockProducer
if withBABE {
bp = &mockBlockProducer{}
}

time.Sleep(time.Second)
dh, err := NewDigestHandler(stateSrvc.Block, stateSrvc.Epoch, stateSrvc.Grandpa, bp, &mockVerifier{})
dh, err := NewHandler(stateSrvc.Block, stateSrvc.Epoch, stateSrvc.Grandpa)
require.NoError(t, err)
return dh
}

func TestDigestHandler_GrandpaScheduledChange(t *testing.T) {
handler := newTestDigestHandler(t, false, true)
func TestHandler_GrandpaScheduledChange(t *testing.T) {
handler := newTestHandler(t, false, true)
handler.Start()
defer handler.Stop()

Expand All @@ -83,18 +128,18 @@ func TestDigestHandler_GrandpaScheduledChange(t *testing.T) {
Number: big.NewInt(1),
}

err = handler.HandleConsensusDigest(d, header)
err = handler.handleConsensusDigest(d, header)
require.NoError(t, err)

headers := addTestBlocksToState(t, 2, handler.blockState)
for _, h := range headers {
handler.blockState.SetFinalizedHash(h.Hash(), 0, 0)
handler.blockState.(*state.BlockState).SetFinalizedHash(h.Hash(), 0, 0)
}

// authorities should change on start of block 3 from start
headers = addTestBlocksToState(t, 1, handler.blockState)
for _, h := range headers {
handler.blockState.SetFinalizedHash(h.Hash(), 0, 0)
handler.blockState.(*state.BlockState).SetFinalizedHash(h.Hash(), 0, 0)
}

time.Sleep(time.Millisecond * 100)
Expand All @@ -109,8 +154,8 @@ func TestDigestHandler_GrandpaScheduledChange(t *testing.T) {
require.Equal(t, expected, auths)
}

func TestDigestHandler_GrandpaForcedChange(t *testing.T) {
handler := newTestDigestHandler(t, false, true)
func TestHandler_GrandpaForcedChange(t *testing.T) {
handler := newTestHandler(t, false, true)
handler.Start()
defer handler.Stop()

Expand All @@ -136,7 +181,7 @@ func TestDigestHandler_GrandpaForcedChange(t *testing.T) {
Number: big.NewInt(1),
}

err = handler.HandleConsensusDigest(d, header)
err = handler.handleConsensusDigest(d, header)
require.NoError(t, err)

addTestBlocksToState(t, 3, handler.blockState)
Expand All @@ -156,8 +201,8 @@ func TestDigestHandler_GrandpaForcedChange(t *testing.T) {
require.Equal(t, expected, auths)
}

func TestDigestHandler_GrandpaPauseAndResume(t *testing.T) {
handler := newTestDigestHandler(t, false, true)
func TestHandler_GrandpaPauseAndResume(t *testing.T) {
handler := newTestHandler(t, false, true)
handler.Start()
defer handler.Stop()

Expand All @@ -173,15 +218,15 @@ func TestDigestHandler_GrandpaPauseAndResume(t *testing.T) {
Data: data,
}

err = handler.HandleConsensusDigest(d, nil)
err = handler.handleConsensusDigest(d, nil)
require.NoError(t, err)
nextPause, err := handler.grandpaState.(*state.GrandpaState).GetNextPause()
require.NoError(t, err)
require.Equal(t, big.NewInt(int64(p.Delay)), nextPause)

headers := addTestBlocksToState(t, 3, handler.blockState)
for _, h := range headers {
handler.blockState.SetFinalizedHash(h.Hash(), 0, 0)
handler.blockState.(*state.BlockState).SetFinalizedHash(h.Hash(), 0, 0)
}

time.Sleep(time.Millisecond * 100)
Expand All @@ -199,7 +244,7 @@ func TestDigestHandler_GrandpaPauseAndResume(t *testing.T) {
Data: data,
}

err = handler.HandleConsensusDigest(d, nil)
err = handler.handleConsensusDigest(d, nil)
require.NoError(t, err)

addTestBlocksToState(t, 3, handler.blockState)
Expand All @@ -212,7 +257,7 @@ func TestDigestHandler_GrandpaPauseAndResume(t *testing.T) {
}

func TestNextGrandpaAuthorityChange_OneChange(t *testing.T) {
handler := newTestDigestHandler(t, false, true)
handler := newTestHandler(t, false, true)
handler.Start()
defer handler.Stop()

Expand All @@ -233,7 +278,7 @@ func TestNextGrandpaAuthorityChange_OneChange(t *testing.T) {
Number: big.NewInt(1),
}

err = handler.HandleConsensusDigest(d, header)
err = handler.handleConsensusDigest(d, header)
require.NoError(t, err)

next := handler.NextGrandpaAuthorityChange()
Expand All @@ -248,7 +293,7 @@ func TestNextGrandpaAuthorityChange_OneChange(t *testing.T) {
}

func TestNextGrandpaAuthorityChange_MultipleChanges(t *testing.T) {
handler := newTestDigestHandler(t, false, true)
handler := newTestHandler(t, false, true)
handler.Start()
defer handler.Stop()

Expand All @@ -273,7 +318,7 @@ func TestNextGrandpaAuthorityChange_MultipleChanges(t *testing.T) {
Number: big.NewInt(1),
}

err = handler.HandleConsensusDigest(d, header)
err = handler.handleConsensusDigest(d, header)
require.NoError(t, err)

nextSetID := uint64(1)
Expand All @@ -299,7 +344,7 @@ func TestNextGrandpaAuthorityChange_MultipleChanges(t *testing.T) {
Data: data,
}

err = handler.HandleConsensusDigest(d, header)
err = handler.handleConsensusDigest(d, header)
require.NoError(t, err)

next := handler.NextGrandpaAuthorityChange()
Expand All @@ -312,8 +357,8 @@ func TestNextGrandpaAuthorityChange_MultipleChanges(t *testing.T) {
require.Equal(t, expected, auths)
}

func TestDigestHandler_HandleBABEOnDisabled(t *testing.T) {
handler := newTestDigestHandler(t, true, false)
func TestHandler_HandleBABEOnDisabled(t *testing.T) {
handler := newTestHandler(t, true, false)
handler.Start()
defer handler.Stop()

Expand All @@ -329,9 +374,8 @@ func TestDigestHandler_HandleBABEOnDisabled(t *testing.T) {
Data: data,
}

err = handler.HandleConsensusDigest(d, nil)
err = handler.handleConsensusDigest(d, nil)
require.NoError(t, err)
require.Equal(t, uint32(7), handler.babe.(*mockBlockProducer).disabled)
}

func createHeaderWithPreDigest(slotNumber uint64) *types.Header {
Expand All @@ -347,8 +391,8 @@ func createHeaderWithPreDigest(slotNumber uint64) *types.Header {
}
}

func TestDigestHandler_HandleNextEpochData(t *testing.T) {
handler := newTestDigestHandler(t, true, false)
func TestHandler_HandleNextEpochData(t *testing.T) {
handler := newTestHandler(t, true, false)
handler.Start()
defer handler.Stop()

Expand Down Expand Up @@ -380,7 +424,7 @@ func TestDigestHandler_HandleNextEpochData(t *testing.T) {

header := createHeaderWithPreDigest(10)

err = handler.HandleConsensusDigest(d, header)
err = handler.handleConsensusDigest(d, header)
require.NoError(t, err)

stored, err := handler.epochState.(*state.EpochState).GetEpochData(1)
Expand All @@ -390,8 +434,8 @@ func TestDigestHandler_HandleNextEpochData(t *testing.T) {
require.Equal(t, res, stored)
}

func TestDigestHandler_HandleNextConfigData(t *testing.T) {
handler := newTestDigestHandler(t, true, false)
func TestHandler_HandleNextConfigData(t *testing.T) {
handler := newTestHandler(t, true, false)
handler.Start()
defer handler.Stop()

Expand All @@ -411,7 +455,7 @@ func TestDigestHandler_HandleNextConfigData(t *testing.T) {

header := createHeaderWithPreDigest(10)

err = handler.HandleConsensusDigest(d, header)
err = handler.handleConsensusDigest(d, header)
require.NoError(t, err)

stored, err := handler.epochState.(*state.EpochState).GetConfigData(1)
Expand Down
34 changes: 16 additions & 18 deletions dot/digest/interface.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,42 @@
// Copyright 2019 ChainSafe Systems (ON) Corp.
// This file is part of gossamer.
//
// The gossamer library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The gossamer library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the gossamer library. If not, see <http://www.gnu.org/licenses/>.

package digest

import (
"math/big"

"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/grandpa"
)

// BlockState interface for block state methods
type BlockState interface {
BestBlockHash() common.Hash
BestBlockHeader() (*types.Header, error)
BestBlockNumber() (*big.Int, error)
BestBlockStateRoot() (common.Hash, error)
BestBlock() (*types.Block, error)
AddBlock(*types.Block) error
GetAllBlocksAtDepth(hash common.Hash) []common.Hash
GetBlockByHash(common.Hash) (*types.Block, error)
GenesisHash() common.Hash
GetSlotForBlock(common.Hash) (uint64, error)
GetFinalizedHeader(uint64, uint64) (*types.Header, error)
GetFinalizedHash(uint64, uint64) (common.Hash, error)
SetFinalizedHash(common.Hash, uint64, uint64) error
RegisterImportedChannel(ch chan<- *types.Block) (byte, error)
UnregisterImportedChannel(id byte)
RegisterFinalizedChannel(ch chan<- *types.FinalisationInfo) (byte, error)
UnregisterFinalizedChannel(id byte)
HighestCommonAncestor(a, b common.Hash) (common.Hash, error)
SubChain(start, end common.Hash) ([]common.Hash, error)
GetBlockBody(hash common.Hash) (*types.Body, error)
}

// EpochState is the interface for state.EpochState
type EpochState interface {
GetEpochForBlock(header *types.Header) (uint64, error)
SetEpochData(epoch uint64, info *types.EpochData) error
SetConfigData(epoch uint64, info *types.ConfigData) error
SetCurrentEpoch(epoch uint64) error
GetCurrentEpoch() (uint64, error)
}

// GrandpaState is the interface for the state.GrandpaState
Expand Down

0 comments on commit 46dbf3b

Please sign in to comment.