github.com/annchain/OG@v0.0.9/plugin/annsensus/annsensus.go (about) 1 package annsensus 2 3 import ( 4 "github.com/annchain/OG/communication" 5 "github.com/annchain/OG/consensus/annsensus" 6 "github.com/annchain/OG/consensus/bft" 7 "github.com/annchain/OG/eventbus" 8 "github.com/annchain/OG/message" 9 "github.com/annchain/OG/og/account" 10 ) 11 12 var supportedMessageTypes = []message.GeneralMessageType{ 13 MessageTypeAnnsensus, 14 } 15 16 type AnnsensusPlugin struct { 17 messageHandler communication.GeneralMessageEventHandler 18 Communicator *ProxyAnnsensusPeerCommunicator 19 AnnsensusPartner *annsensus.AnnsensusPartner 20 } 21 22 func (a *AnnsensusPlugin) SupportedEventHandlers() []eventbus.EventHandlerRegisterInfo { 23 return []eventbus.EventHandlerRegisterInfo{} 24 } 25 26 func (a *AnnsensusPlugin) Start() { 27 a.AnnsensusPartner.Start() 28 } 29 30 func (a *AnnsensusPlugin) Stop() { 31 a.AnnsensusPartner.Stop() 32 } 33 34 func (a *AnnsensusPlugin) GetMessageEventHandler() communication.GeneralMessageEventHandler { 35 return a.messageHandler 36 } 37 38 func (o *AnnsensusPlugin) SetOutgoing(outgoing communication.GeneralPeerCommunicatorOutgoing) { 39 o.Communicator.GeneralOutgoing = outgoing 40 } 41 42 func NewAnnsensusPlugin(termProvider annsensus.TermIdProvider, 43 myAccountProvider account.AccountProvider, 44 proposalGenerator bft.ProposalGenerator, 45 proposalValidator bft.ProposalValidator, 46 decisionMaker bft.DecisionMaker) *AnnsensusPlugin { 47 // load config first. 48 config := annsensus.AnnsensusProcessorConfig{ 49 DisableTermChange: false, 50 DisabledConsensus: false, 51 TermChangeInterval: 60 * 1000, 52 GenesisAccounts: nil, 53 } 54 annsensusMessageAdapter := &DefaultAnnsensusMessageAdapter{ 55 unmarshaller: AnnsensusMessageUnmarshaller{}, 56 } 57 58 communicator := &ProxyAnnsensusPeerCommunicator{ 59 AnnsensusMessageAdapter: annsensusMessageAdapter, 60 GeneralOutgoing: nil, // place for p2p peer outgoing 61 } 62 communicator.InitDefault() 63 64 bftAdapter := &annsensus.PlainBftAdapter{} 65 dkgAdapter := &annsensus.PlainDkgAdapter{} 66 67 annsensusPartnerProvider := &annsensus.DefaultAnnsensusPartnerProvider{ 68 MyAccountProvider: myAccountProvider, 69 ProposalGenerator: proposalGenerator, 70 ProposalValidator: proposalValidator, 71 DecisionMaker: decisionMaker, 72 BftAdatper: bftAdapter, // for outgoing message only 73 DkgAdatper: dkgAdapter, // for outgoing message only 74 PeerOutgoing: communicator, 75 } 76 termHolder := annsensus.NewAnnsensusTermHolder(termProvider) 77 78 annsensusPartner := &annsensus.AnnsensusPartner{ 79 Config: config, 80 BftAdapter: bftAdapter, // for incoming message only 81 DkgAdapter: dkgAdapter, // for incoming message only 82 TermProvider: termProvider, 83 TermHolder: termHolder, 84 BftPartnerProvider: annsensusPartnerProvider, 85 DkgPartnerProvider: annsensusPartnerProvider, 86 PeerOutgoing: communicator, 87 PeerIncoming: communicator, 88 } 89 90 return &AnnsensusPlugin{ 91 messageHandler: &AnnsensusMessageMessageHandler{ 92 AnnsensusPartner: annsensusPartner, 93 AnnsensusMessageAdapter: annsensusMessageAdapter, 94 }, 95 Communicator: communicator, 96 AnnsensusPartner: annsensusPartner, 97 } 98 } 99 100 func (a AnnsensusPlugin) SupportedMessageTypes() []message.GeneralMessageType { 101 return supportedMessageTypes 102 } 103 104 type AnnsensusMessageAdapter interface { 105 AdaptGeneralMessage(incomingMsg message.GeneralMessage) (annMessage annsensus.AnnsensusMessage, err error) 106 AdaptGeneralPeer(gnrPeer message.GeneralPeer) (annsensus.AnnsensusPeer, error) 107 108 AdaptAnnsensusMessage(outgoingMsg annsensus.AnnsensusMessage) (msg message.GeneralMessage, err error) 109 AdaptAnnsensusPeer(annPeer annsensus.AnnsensusPeer) (message.GeneralPeer, error) 110 }