Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(dot/core, dot/sync, lib/babe): refactor handling on block import to reduce duplicate code #1645

Merged
merged 53 commits into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
e92668d
add 3 auth genesis
noot Jun 9, 2021
8a67205
Merge branch 'noot/devnet-genesis' into noot/fix-babe-epoch
noot Jun 10, 2021
f893b82
improve epoch transition logic
noot Jun 10, 2021
37e457a
implement code substitutes
edwardmack Jun 10, 2021
a919188
fix babe tests
noot Jun 10, 2021
c23fee5
add wait until epoch starts
noot Jun 11, 2021
d23f2be
lint
noot Jun 11, 2021
52d0f73
merge w development
noot Jun 11, 2021
5cccb37
Merge branch 'development' into ed/code_substitutes
edwardmack Jun 11, 2021
b6c88b1
cleanup
noot Jun 11, 2021
578ff9c
restore genesis
noot Jun 11, 2021
6177b10
Merge branch 'development' of github.com:ChainSafe/gossamer into noot…
noot Jun 11, 2021
13cf25c
address comments
noot Jun 14, 2021
0b7e8f5
Merge branch 'noot/fix-babe-epoch' of github.com:ChainSafe/gossamer i…
noot Jun 14, 2021
e1ea906
increase test epoch length
noot Jun 14, 2021
da1d50e
Merge branch 'development' into ed/code_substitutes
edwardmack Jun 15, 2021
18460c8
Merge branch 'development' of github.com:ChainSafe/gossamer into noot…
noot Jun 15, 2021
af33101
add go.mod
noot Jun 15, 2021
d2f38b2
update b.Paused to close channel
noot Jun 15, 2021
d580158
address PR comments
edwardmack Jun 15, 2021
27dc09f
update babe.Pause
noot Jun 15, 2021
7a5ce9e
remove unneeded files
noot Jun 15, 2021
749da75
add pause/resume test
noot Jun 15, 2021
1058a9a
lint
noot Jun 15, 2021
df318d6
small cleanup
noot Jun 15, 2021
5716370
Merge branch 'ed/code_substitutes' into noot/block-handle-refactor
noot Jun 15, 2021
f474f14
begin core handle block import refactor
noot Jun 15, 2021
b7af203
move digest handler to its own package
noot Jun 15, 2021
ce94028
move AddBlock and StoreTrie logic to core.handleBlock
noot Jun 15, 2021
9e4364b
update babe to use core.HandleBlockImport
noot Jun 15, 2021
f36e546
merge w development
noot Jun 15, 2021
9984b89
add handler to babe cfg
noot Jun 15, 2021
7ef0e79
cleanup
noot Jun 15, 2021
ed5ad02
set core digestHandler
noot Jun 16, 2021
74a3d19
fix dot, core, sync tests
noot Jun 16, 2021
5c4dff5
lint
noot Jun 16, 2021
29a4f10
merge w development
noot Jun 16, 2021
034a685
cleanup
noot Jun 16, 2021
a0ac8e7
cleanup
noot Jun 16, 2021
d71ae70
fix unit tests
noot Jun 16, 2021
0f52ff1
fix
noot Jun 16, 2021
9bc76ee
add lock for closing/writing to core blockAddCh
noot Jun 16, 2021
4010684
Merge branch 'development' of github.com:ChainSafe/gossamer into noot…
noot Jun 16, 2021
7598f83
store trie before adding block
noot Jun 16, 2021
b6bd916
fix
noot Jun 16, 2021
786dc1c
fix unit tests
noot Jun 16, 2021
62b7277
fix babe to use right handler func
noot Jun 16, 2021
d1b23b1
fix
noot Jun 17, 2021
4d4942a
fix unit test
noot Jun 17, 2021
ff3d140
merge w development
noot Jun 17, 2021
68c130e
add go.sum
noot Jun 17, 2021
589380e
Merge branch 'development' of github.com:ChainSafe/gossamer into noot…
noot Jun 17, 2021
991d5e5
cleanup
noot Jun 17, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 29 additions & 18 deletions dot/core/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,43 @@ 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")
)

// 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) 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