Skip to content

Commit

Permalink
chore(dot/core, dot/sync, lib/babe): refactor handling on block impor…
Browse files Browse the repository at this point in the history
…t to reduce duplicate code (#1645)
  • Loading branch information
noot authored Jun 17, 2021
1 parent c6686f0 commit 1c27575
Show file tree
Hide file tree
Showing 38 changed files with 932 additions and 904 deletions.
49 changes: 31 additions & 18 deletions dot/core/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,45 @@ import (
"fmt"
)

// ErrNilBlockState is returned when BlockState is nil
var ErrNilBlockState = errors.New("cannot have nil BlockState")
var (
// ErrNilBlockState is returned when BlockState is nil
ErrNilBlockState = errors.New("cannot have nil BlockState")

// ErrNilStorageState is returned when StorageState is nil
var ErrNilStorageState = errors.New("cannot have nil StorageState")
// ErrNilStorageState is returned when StorageState is nil
ErrNilStorageState = errors.New("cannot have nil StorageState")

// ErrNilKeystore is returned when keystore is nil
var ErrNilKeystore = errors.New("cannot have nil keystore")
// ErrNilKeystore is returned when keystore is nil
ErrNilKeystore = errors.New("cannot have nil keystore")

// ErrServiceStopped is returned when the service has been stopped
var ErrServiceStopped = errors.New("service has been stopped")
// ErrServiceStopped is returned when the service has been stopped
ErrServiceStopped = errors.New("service has been stopped")

// ErrInvalidBlock is returned when a block cannot be verified
var ErrInvalidBlock = errors.New("could not verify block")
// ErrInvalidBlock is returned when a block cannot be verified
ErrInvalidBlock = errors.New("could not verify block")

// ErrNilVerifier is returned when trying to instantiate a Syncer without a Verifier
var ErrNilVerifier = errors.New("cannot have nil Verifier")
// ErrNilVerifier is returned when trying to instantiate a Syncer without a Verifier
ErrNilVerifier = errors.New("cannot have nil Verifier")

// ErrNilRuntime is returned when trying to instantiate a Service or Syncer without a runtime
var ErrNilRuntime = errors.New("cannot have nil runtime")
// ErrNilRuntime is returned when trying to instantiate a Service or Syncer without a runtime
ErrNilRuntime = errors.New("cannot have nil runtime")

// ErrNilBlockProducer is returned when trying to instantiate a block producing Service without a block producer
var ErrNilBlockProducer = errors.New("cannot have nil BlockProducer")
// ErrNilBlockProducer is returned when trying to instantiate a block producing Service without a block producer
ErrNilBlockProducer = errors.New("cannot have nil BlockProducer")

// ErrNilConsensusMessageHandler is returned when trying to instantiate a Service without a FinalityMessageHandler
var ErrNilConsensusMessageHandler = errors.New("cannot have nil ErrNilFinalityMessageHandler")
// ErrNilConsensusMessageHandler is returned when trying to instantiate a Service without a FinalityMessageHandler
ErrNilConsensusMessageHandler = errors.New("cannot have nil ErrNilFinalityMessageHandler")

// ErrNilNetwork is returned when the Network interface is nil
ErrNilNetwork = errors.New("cannot have nil Network")

// ErrEmptyRuntimeCode is returned when the storage :code is empty
ErrEmptyRuntimeCode = errors.New("new :code is empty")

// ErrNilDigestHandler is returned when the DigestHandler interface is nil
ErrNilDigestHandler = errors.New("cannot have nil DigestHandler")

errNilCodeSubstitutedState = errors.New("cannot have nil CodeSubstitutedStat")
)

// ErrNilChannel is returned if a channel is nil
func ErrNilChannel(s string) error {
Expand Down
31 changes: 10 additions & 21 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 @@ -56,6 +55,7 @@ type StorageState interface {
LoadCode(root *common.Hash) ([]byte, error)
LoadCodeHash(root *common.Hash) (common.Hash, error)
TrieState(root *common.Hash) (*rtstorage.TrieState, error)
StoreTrie(*rtstorage.TrieState, *types.Header) error
GetStateRootFromBlock(bhash *common.Hash) (*common.Hash, error)
}

Expand All @@ -68,17 +68,6 @@ type TransactionState interface {
PendingInPool() []*transaction.ValidTransaction
}

// BlockProducer is the interface that a block production service must implement
type BlockProducer interface {
GetBlockChannel() <-chan types.Block
SetOnDisabled(authorityIndex uint32)
}

// Verifier is the interface for the block verifier
type Verifier interface {
SetOnDisabled(authorityIndex uint32, block *types.Header) error
}

// Network is the interface for the network service
type Network interface {
SendMessage(network.NotificationsMessage)
Expand All @@ -87,17 +76,17 @@ 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)
// CodeSubstitutedState interface to handle storage of code substitute state
type CodeSubstitutedState interface {
LoadCodeSubstitutedBlockHash() common.Hash
StoreCodeSubstitutedBlockHash(hash common.Hash) error
}

// DigestHandler is the interface for the consensus digest handler
type DigestHandler interface {
HandleDigests(header *types.Header)
}
3 changes: 0 additions & 3 deletions dot/core/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ import (
// adds valid transactions to the transaction queue of the BABE session
func (s *Service) HandleTransactionMessage(msg *network.TransactionMessage) error {
logger.Debug("received TransactionMessage")
if !s.isBlockProducer {
return nil
}

// get transactions from message extrinsics
txs := msg.Extrinsics
Expand Down
22 changes: 11 additions & 11 deletions dot/core/messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/scale"

"github.com/centrifuge/go-substrate-rpc-client/v2/signature"
ctypes "github.com/centrifuge/go-substrate-rpc-client/v2/types"
"github.com/stretchr/testify/require"
Expand All @@ -39,24 +40,21 @@ func TestService_ProcessBlockAnnounceMessage(t *testing.T) {
// TODO: move to sync package
net := new(MockNetwork)

newBlocks := make(chan types.Block)

cfg := &Config{
Network: net,
Keystore: keystore.NewGlobalKeystore(),
NewBlocks: newBlocks,
IsBlockProducer: false,
Network: net,
Keystore: keystore.NewGlobalKeystore(),
}

s := NewTestService(t, cfg)
err := s.Start()
require.Nil(t, err)

// simulate block sent from BABE session
newBlock := types.Block{
newBlock := &types.Block{
Header: &types.Header{
Number: big.NewInt(1),
ParentHash: s.blockState.BestBlockHash(),
Digest: types.Digest{types.NewBabeSecondaryPlainPreDigest(0, 1).ToPreRuntimeDigest()},
},
Body: types.NewBody([]byte{}),
}
Expand All @@ -72,10 +70,14 @@ func TestService_ProcessBlockAnnounceMessage(t *testing.T) {

//setup the SendMessage function
net.On("SendMessage", expected)
newBlocks <- newBlock

time.Sleep(time.Second * 2)
state, err := s.storageState.TrieState(nil)
require.NoError(t, err)

err = s.HandleBlockProduced(newBlock, state)
require.NoError(t, err)

time.Sleep(time.Second)
net.AssertCalled(t, "SendMessage", expected)
}

Expand Down Expand Up @@ -141,8 +143,6 @@ func TestService_HandleTransactionMessage(t *testing.T) {
cfg := &Config{
Keystore: ks,
TransactionState: state.NewTransactionState(),
IsBlockProducer: true,
BlockProducer: bp,
}

s := NewTestService(t, cfg)
Expand Down
18 changes: 18 additions & 0 deletions dot/core/mocks/digest_handler.go

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

Loading

0 comments on commit 1c27575

Please sign in to comment.