github.com/iotexproject/iotex-core@v1.14.1-rc1/consensus/scheme/standalone.go (about) 1 // Copyright (c) 2019 IoTeX Foundation 2 // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability 3 // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. 4 // This source code is governed by Apache License 2.0 that can be found in the LICENSE file. 5 6 package scheme 7 8 import ( 9 "context" 10 "time" 11 12 "github.com/pkg/errors" 13 "go.uber.org/zap" 14 15 "github.com/iotexproject/iotex-core/blockchain" 16 "github.com/iotexproject/iotex-core/blockchain/block" 17 "github.com/iotexproject/iotex-core/pkg/log" 18 "github.com/iotexproject/iotex-core/pkg/routine" 19 "github.com/iotexproject/iotex-proto/golang/iotextypes" 20 ) 21 22 // Standalone is the consensus scheme that periodically create blocks 23 type Standalone struct { 24 task *routine.RecurringTask 25 } 26 27 type standaloneHandler struct { 28 bc blockchain.Blockchain 29 createCb CreateBlockCB 30 commitCb ConsensusDoneCB 31 pubCb BroadcastCB 32 } 33 34 func (s *standaloneHandler) Run() { 35 blk, err := s.createCb() 36 if err != nil { 37 log.L().Error("Failed to create.", zap.Error(err)) 38 return 39 } 40 41 if err := s.commitCb(blk); err != nil { 42 log.L().Error("Failed to commit.", zap.Error(err)) 43 return 44 } 45 if err := s.pubCb(blk); err != nil { 46 log.L().Error("Failed to publish event.", zap.Error(err)) 47 return 48 } 49 } 50 51 // NewStandalone creates a Standalone struct. 52 func NewStandalone(create CreateBlockCB, commit ConsensusDoneCB, pub BroadcastCB, bc blockchain.Blockchain, interval time.Duration) Scheme { 53 h := &standaloneHandler{ 54 bc: bc, 55 createCb: create, 56 commitCb: commit, 57 pubCb: pub, 58 } 59 return &Standalone{ 60 task: routine.NewRecurringTask(h.Run, interval), 61 } 62 } 63 64 // Start starts the service for a standalone 65 func (s *Standalone) Start(ctx context.Context) error { 66 return s.task.Start(ctx) 67 } 68 69 // Stop stops the service for a standalone 70 func (s *Standalone) Stop(ctx context.Context) error { 71 return s.task.Stop(ctx) 72 } 73 74 // HandleConsensusMsg handles incoming consensus message 75 func (s *Standalone) HandleConsensusMsg(msg *iotextypes.ConsensusMessage) error { 76 log.L().Warn("Standalone scheme does not handle incoming block propose requests.") 77 return nil 78 } 79 80 // Calibrate triggers an event to calibrate consensus context 81 func (s *Standalone) Calibrate(uint64) {} 82 83 // ValidateBlockFooter validates signatures in block footer 84 func (s *Standalone) ValidateBlockFooter(*block.Block) error { 85 log.L().Warn("Standalone scheme always return true for block footer validation") 86 return nil 87 } 88 89 // Metrics is not implemented for standalone scheme 90 func (s *Standalone) Metrics() (ConsensusMetrics, error) { 91 return ConsensusMetrics{}, errors.Wrapf( 92 ErrNotImplemented, 93 "standalone scheme does not supported metrics yet", 94 ) 95 } 96 97 // Activate is not implemented for standalone scheme 98 func (s *Standalone) Activate(_ bool) { 99 log.S().Warn("Standalone scheme could not support activate") 100 } 101 102 // Active is always true for standalone scheme 103 func (s *Standalone) Active() bool { return true }