consensus: store ProtocolConfiguration in consensus config

This commit is contained in:
Anna Shaleva 2021-03-15 12:25:52 +03:00
parent f7d74190f5
commit 23a3514cc0
3 changed files with 26 additions and 27 deletions

View file

@ -73,9 +73,6 @@ type service struct {
blockEvents chan *coreb.Block
lastProposal []util.Uint256
wallet *wallet.Wallet
network netmode.Magic
// stateRootEnabled specifies if state root should be exchanged and checked during consensus.
stateRootEnabled bool
// started is a flag set with Start method that runs an event handling
// goroutine.
started *atomic.Bool
@ -97,6 +94,8 @@ type Config struct {
Broadcast func(p *npayload.Extensible)
// Chain is a core.Blockchainer instance.
Chain blockchainer.Blockchainer
// ProtocolConfiguration contains protocol settings.
ProtocolConfiguration config.ProtocolConfiguration
// RequestTx is a callback to which will be called
// when a node lacks transactions present in a block.
RequestTx func(h ...util.Uint256)
@ -123,13 +122,11 @@ func NewService(cfg Config) (Service, error) {
txx: newFIFOCache(cacheMaxCapacity),
messages: make(chan Payload, 100),
transactions: make(chan *transaction.Transaction, 100),
blockEvents: make(chan *coreb.Block, 1),
network: cfg.Chain.GetConfig().Magic,
stateRootEnabled: cfg.Chain.GetConfig().StateRootInHeader,
started: atomic.NewBool(false),
quit: make(chan struct{}),
finished: make(chan struct{}),
transactions: make(chan *transaction.Transaction, 100),
blockEvents: make(chan *coreb.Block, 1),
started: atomic.NewBool(false),
quit: make(chan struct{}),
finished: make(chan struct{}),
}
if cfg.Wallet == nil {
@ -182,7 +179,7 @@ func NewService(cfg Config) (Service, error) {
dbft.WithNewCommit(func() payload.Commit { return new(commit) }),
dbft.WithNewRecoveryRequest(func() payload.RecoveryRequest { return new(recoveryRequest) }),
dbft.WithNewRecoveryMessage(func() payload.RecoveryMessage {
return &recoveryMessage{stateRootEnabled: srv.stateRootEnabled}
return &recoveryMessage{stateRootEnabled: srv.ProtocolConfiguration.StateRootInHeader}
}),
dbft.WithVerifyPrepareRequest(srv.verifyRequest),
dbft.WithVerifyPrepareResponse(func(_ payload.ConsensusPayload) error { return nil }),
@ -214,7 +211,7 @@ func NewPayload(m netmode.Magic, stateRootEnabled bool) *Payload {
}
func (s *service) newPayload(c *dbft.Context, t payload.MessageType, msg interface{}) payload.ConsensusPayload {
cp := NewPayload(s.network, s.stateRootEnabled)
cp := NewPayload(s.ProtocolConfiguration.Magic, s.ProtocolConfiguration.StateRootInHeader)
cp.SetHeight(c.BlockIndex)
cp.SetValidatorIndex(uint16(c.MyIndex))
cp.SetViewNumber(c.ViewNumber)
@ -234,7 +231,7 @@ func (s *service) newPayload(c *dbft.Context, t payload.MessageType, msg interfa
func (s *service) newPrepareRequest() payload.PrepareRequest {
r := new(prepareRequest)
if s.stateRootEnabled {
if s.ProtocolConfiguration.StateRootInHeader {
r.stateRootEnabled = true
if sr, err := s.Chain.GetStateModule().GetStateRoot(s.dbft.BlockIndex - 1); err == nil {
r.stateRoot = sr.Root
@ -364,7 +361,7 @@ func (s *service) payloadFromExtensible(ep *npayload.Extensible) *Payload {
return &Payload{
Extensible: *ep,
message: message{
stateRootEnabled: s.stateRootEnabled,
stateRootEnabled: s.ProtocolConfiguration.StateRootInHeader,
},
}
}
@ -482,7 +479,7 @@ func (s *service) verifyRequest(p payload.ConsensusPayload) error {
if req.version != s.dbft.Version {
return errInvalidVersion
}
if s.stateRootEnabled {
if s.ProtocolConfiguration.StateRootInHeader {
sr, err := s.Chain.GetStateModule().GetStateRoot(s.dbft.BlockIndex - 1)
if err != nil {
return err
@ -633,10 +630,10 @@ func (s *service) newBlockFromContext(ctx *dbft.Context) block.Block {
return nil
}
block.Block.Network = s.network
block.Block.Network = s.ProtocolConfiguration.Magic
block.Block.Timestamp = ctx.Timestamp / nsInMs
block.Block.Index = ctx.BlockIndex
if s.stateRootEnabled {
if s.ProtocolConfiguration.StateRootInHeader {
sr, err := s.Chain.GetStateModule().GetStateRoot(ctx.BlockIndex - 1)
if err != nil {
return nil

View file

@ -444,11 +444,12 @@ func newTestService(t *testing.T) *service {
func newTestServiceWithChain(t *testing.T, bc *core.Blockchain) *service {
srv, err := NewService(Config{
Logger: zaptest.NewLogger(t),
Broadcast: func(*npayload.Extensible) {},
Chain: bc,
RequestTx: func(...util.Uint256) {},
TimePerBlock: time.Duration(bc.GetConfig().SecondsPerBlock) * time.Second,
Logger: zaptest.NewLogger(t),
Broadcast: func(*npayload.Extensible) {},
Chain: bc,
ProtocolConfiguration: bc.GetConfig(),
RequestTx: func(...util.Uint256) {},
TimePerBlock: time.Duration(bc.GetConfig().SecondsPerBlock) * time.Second,
Wallet: &config.Wallet{
Path: "./testdata/wallet1.json",
Password: "one",

View file

@ -206,11 +206,12 @@ func newServerFromConstructors(config ServerConfig, chain blockchainer.Blockchai
}
srv, err := newConsensus(consensus.Config{
Logger: log,
Broadcast: s.handleNewPayload,
Chain: chain,
RequestTx: s.requestTx,
Wallet: config.Wallet,
Logger: log,
Broadcast: s.handleNewPayload,
Chain: chain,
ProtocolConfiguration: chain.GetConfig(),
RequestTx: s.requestTx,
Wallet: config.Wallet,
TimePerBlock: config.TimePerBlock,
})