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 blockEvents chan *coreb.Block
lastProposal []util.Uint256 lastProposal []util.Uint256
wallet *wallet.Wallet 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 // started is a flag set with Start method that runs an event handling
// goroutine. // goroutine.
started *atomic.Bool started *atomic.Bool
@ -97,6 +94,8 @@ type Config struct {
Broadcast func(p *npayload.Extensible) Broadcast func(p *npayload.Extensible)
// Chain is a core.Blockchainer instance. // Chain is a core.Blockchainer instance.
Chain blockchainer.Blockchainer Chain blockchainer.Blockchainer
// ProtocolConfiguration contains protocol settings.
ProtocolConfiguration config.ProtocolConfiguration
// RequestTx is a callback to which will be called // RequestTx is a callback to which will be called
// when a node lacks transactions present in a block. // when a node lacks transactions present in a block.
RequestTx func(h ...util.Uint256) RequestTx func(h ...util.Uint256)
@ -125,8 +124,6 @@ func NewService(cfg Config) (Service, error) {
transactions: make(chan *transaction.Transaction, 100), transactions: make(chan *transaction.Transaction, 100),
blockEvents: make(chan *coreb.Block, 1), blockEvents: make(chan *coreb.Block, 1),
network: cfg.Chain.GetConfig().Magic,
stateRootEnabled: cfg.Chain.GetConfig().StateRootInHeader,
started: atomic.NewBool(false), started: atomic.NewBool(false),
quit: make(chan struct{}), quit: make(chan struct{}),
finished: make(chan struct{}), finished: make(chan struct{}),
@ -182,7 +179,7 @@ func NewService(cfg Config) (Service, error) {
dbft.WithNewCommit(func() payload.Commit { return new(commit) }), dbft.WithNewCommit(func() payload.Commit { return new(commit) }),
dbft.WithNewRecoveryRequest(func() payload.RecoveryRequest { return new(recoveryRequest) }), dbft.WithNewRecoveryRequest(func() payload.RecoveryRequest { return new(recoveryRequest) }),
dbft.WithNewRecoveryMessage(func() payload.RecoveryMessage { dbft.WithNewRecoveryMessage(func() payload.RecoveryMessage {
return &recoveryMessage{stateRootEnabled: srv.stateRootEnabled} return &recoveryMessage{stateRootEnabled: srv.ProtocolConfiguration.StateRootInHeader}
}), }),
dbft.WithVerifyPrepareRequest(srv.verifyRequest), dbft.WithVerifyPrepareRequest(srv.verifyRequest),
dbft.WithVerifyPrepareResponse(func(_ payload.ConsensusPayload) error { return nil }), 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 { 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.SetHeight(c.BlockIndex)
cp.SetValidatorIndex(uint16(c.MyIndex)) cp.SetValidatorIndex(uint16(c.MyIndex))
cp.SetViewNumber(c.ViewNumber) 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 { func (s *service) newPrepareRequest() payload.PrepareRequest {
r := new(prepareRequest) r := new(prepareRequest)
if s.stateRootEnabled { if s.ProtocolConfiguration.StateRootInHeader {
r.stateRootEnabled = true r.stateRootEnabled = true
if sr, err := s.Chain.GetStateModule().GetStateRoot(s.dbft.BlockIndex - 1); err == nil { if sr, err := s.Chain.GetStateModule().GetStateRoot(s.dbft.BlockIndex - 1); err == nil {
r.stateRoot = sr.Root r.stateRoot = sr.Root
@ -364,7 +361,7 @@ func (s *service) payloadFromExtensible(ep *npayload.Extensible) *Payload {
return &Payload{ return &Payload{
Extensible: *ep, Extensible: *ep,
message: message{ 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 { if req.version != s.dbft.Version {
return errInvalidVersion return errInvalidVersion
} }
if s.stateRootEnabled { if s.ProtocolConfiguration.StateRootInHeader {
sr, err := s.Chain.GetStateModule().GetStateRoot(s.dbft.BlockIndex - 1) sr, err := s.Chain.GetStateModule().GetStateRoot(s.dbft.BlockIndex - 1)
if err != nil { if err != nil {
return err return err
@ -633,10 +630,10 @@ func (s *service) newBlockFromContext(ctx *dbft.Context) block.Block {
return nil return nil
} }
block.Block.Network = s.network block.Block.Network = s.ProtocolConfiguration.Magic
block.Block.Timestamp = ctx.Timestamp / nsInMs block.Block.Timestamp = ctx.Timestamp / nsInMs
block.Block.Index = ctx.BlockIndex block.Block.Index = ctx.BlockIndex
if s.stateRootEnabled { if s.ProtocolConfiguration.StateRootInHeader {
sr, err := s.Chain.GetStateModule().GetStateRoot(ctx.BlockIndex - 1) sr, err := s.Chain.GetStateModule().GetStateRoot(ctx.BlockIndex - 1)
if err != nil { if err != nil {
return nil return nil

View file

@ -447,6 +447,7 @@ func newTestServiceWithChain(t *testing.T, bc *core.Blockchain) *service {
Logger: zaptest.NewLogger(t), Logger: zaptest.NewLogger(t),
Broadcast: func(*npayload.Extensible) {}, Broadcast: func(*npayload.Extensible) {},
Chain: bc, Chain: bc,
ProtocolConfiguration: bc.GetConfig(),
RequestTx: func(...util.Uint256) {}, RequestTx: func(...util.Uint256) {},
TimePerBlock: time.Duration(bc.GetConfig().SecondsPerBlock) * time.Second, TimePerBlock: time.Duration(bc.GetConfig().SecondsPerBlock) * time.Second,
Wallet: &config.Wallet{ Wallet: &config.Wallet{

View file

@ -209,6 +209,7 @@ func newServerFromConstructors(config ServerConfig, chain blockchainer.Blockchai
Logger: log, Logger: log,
Broadcast: s.handleNewPayload, Broadcast: s.handleNewPayload,
Chain: chain, Chain: chain,
ProtocolConfiguration: chain.GetConfig(),
RequestTx: s.requestTx, RequestTx: s.requestTx,
Wallet: config.Wallet, Wallet: config.Wallet,