github.com/annchain/OG@v0.0.9/consensus/annsensus/archive/partner.go (about) 1 package archive 2 3 // 4 //var ( 5 // SequencerGenerationRetryTimes = 7 6 //) 7 // 8 //// partner is a participant in the consensus group. 9 //// partner does not care which consensus method is being used in the bottom layer. 10 //// it only provides necessary functions and infomation to support consensus module. 11 //// e.g., produce proposal, broadcast messages, receive message and update consensus state 12 //// When there comes a term change, reset bftPartnerMyself, dkg 13 //type AnnsensusPartner struct { 14 // accountNonceProvider AccountNonceProvider 15 // accountProvider ConsensusAccountProvider 16 // peerCommunicator bft.BftPeerCommunicator // AnnsensusPartner is a BftPeerCommunicator, bftPeerCommunicator is a peerCommunicator 17 // bftPartnerMyself *bft.BftPartner 18 // dkg *archive.DkgPartner 19 // termProvider TermIdProvider 20 // heightProvider HeightProvider 21 // sequencerProducer SequencerProducer 22 // consensusReachedChannel chan bft.ConsensusDecision 23 // quit chan bool 24 //} 25 // 26 //func NewAnnsensusPartner(accountNonceProvider AccountNonceProvider, peerCommunicator bft.BftPeerCommunicator, 27 // termProvider TermIdProvider, accountProvider ConsensusAccountProvider, sequencerProducer SequencerProducer) *AnnsensusPartner { 28 // // init bft related components 29 // 30 // // init dkg related components 31 // // init annsensus partner as an integration center 32 // ap := &AnnsensusPartner{ 33 // accountNonceProvider: accountNonceProvider, 34 // accountProvider: accountProvider, 35 // peerCommunicator: peerCommunicator, 36 // bftPartnerMyself: nil, 37 // dkg: nil, 38 // termProvider: termProvider, 39 // heightProvider: nil, 40 // sequencerProducer: sequencerProducer, 41 // consensusReachedChannel: make(chan bft.ConsensusDecision), 42 // quit: make(chan bool), 43 // } 44 // trustfulPeerCommunicator := communicator.NewTrustfulBftPeerCommunicator(signer, termProvider, accountProvider) 45 // 46 // ap := &AnnsensusPartner{ 47 // peerCommunicator: trustfulPeerCommunicator, 48 // bftPartnerMyself: bft.NewDefaultBFTPartner(nParticipants, id, blockTime), 49 // accountNonceProvider: accountNonceProvider, 50 // } 51 // ap.bftPartnerMyself.RegisterConsensusReachedListener(ap) 52 // 53 // return ap 54 //} 55 // 56 //func (o *AnnsensusPartner) Start() { 57 // // start loop 58 // goroutine.New(o.loop) 59 //} 60 // 61 //func (o *AnnsensusPartner) Stop() { 62 // panic("implement me") 63 //} 64 // 65 //func (o *AnnsensusPartner) Name() string { 66 // panic("implement me") 67 //} 68 // 69 //// MakeDecision here is the final validator for recovering BLS threshold signature for this Proposal. 70 //// It is not the same as the one in verifiers. Those are for normal tx validation for all nodes. 71 //func (o *AnnsensusPartner) MakeDecision(proposal bft.Proposal, state *bft.HeightRoundState) (bft.ConsensusDecision, error) { 72 // var sigShares [][]byte 73 // sequencerProposal := proposal.(*bft.SequencerProposal) 74 // // reform bls signature 75 // for i, commit := range state.PreCommits { 76 // if commit == nil { 77 // logrus.WithField("partner", i).WithField("hr", state.MessageProposal.HeightRound). 78 // Trace("parnter commit is nil") 79 // continue 80 // } 81 // //logrus.WithField("len", len(commit.BlsSignature)).WithField("sigs", hexutil.Encode(commit.BlsSignature)). 82 // // Trace("commit", commit) 83 // sigShares = append(sigShares, commit.BlsSignature) 84 // } 85 // // TODO: concurrency check for currentTerm 86 // currentTerm := o.termProvider.CurrentTerm() 87 // 88 // jointSig, err := o.dkg.RecoverAndVerifySignature(sigShares, sequencerProposal.GetId().ToBytes(), currentTerm) 89 // if err != nil { 90 // logrus.WithField("termId", currentTerm).WithError(err).Warn("joint sig verification failed") 91 // return nil, err 92 // } 93 // sequencerProposal.Signature = jointSig 94 // // TODO: may set the pubkey 95 // sequencerProposal.Proposing = false 96 // return sequencerProposal, nil 97 //} 98 // 99 //func (o *AnnsensusPartner) GetConsensusDecisionMadeEventChannel() chan bft.ConsensusDecision { 100 // return o.consensusReachedChannel 101 //} 102 // 103 //// ValidateProposal is called once a proposal is received from consensus peers 104 //// 105 //func (o *AnnsensusPartner) ValidateProposal(proposal bft.Proposal) error { 106 // // validate sequencer 107 // err := o.sequencerProducer.ValidateSequencer(proposal.(*bft.SequencerProposal).Sequencer) 108 // return err 109 //} 110 // 111 //func (o *AnnsensusPartner) ProduceProposal() (proposal bft.Proposal, validCondition bft.ProposalCondition) { 112 // me := o.accountProvider.Account() 113 // nonce := o.accountNonceProvider.GetNonce(me) 114 // logrus.WithField("nonce", nonce).Debug("gen seq") 115 // targetHeight := o.heightProvider.CurrentHeight() + 1 116 // targetTermId := o.termProvider.HeightTerm(targetHeight) 117 // blsPub, err := o.dkg.GetJoinPublicKey(targetTermId).MarshalBinary() 118 // 119 // if err != nil { 120 // logrus.WithField("term", targetTermId).WithField("height", targetHeight). 121 // WithError(err).Error("error on getting joint public key") 122 // panic(err) 123 // } 124 // var seq *tx_types.Sequencer 125 // 126 // for i := 0; i < SequencerGenerationRetryTimes; i++ { 127 // innerSequencer, err, genAgain := o.sequencerProducer.GenerateSequencer(me.Address, targetHeight, nonce, &me.PrivateKey, blsPub) 128 // if err != nil { 129 // logrus.WithError(err).WithField("times", i).Warn("gen sequencer failed") 130 // if !genAgain { 131 // break 132 // } 133 // logrus.WithField("retry", i).Warn("try to generate sequencer again") 134 // } else { 135 // seq = innerSequencer 136 // } 137 // } 138 // if seq == nil { 139 // logrus.WithField("height", targetHeight).WithField("term", targetTermId).Error("failed to generate sequencer") 140 // return 141 // } 142 // return &bft.SequencerProposal{Sequencer: *seq}, bft.ProposalCondition{ValidHeight: targetHeight} 143 //} 144 // 145 //func (o *AnnsensusPartner) loop() { 146 // // wait for term ready 147 // // Am I in the term and so that I should participate in the consensus? 148 // // if yes, init bft 149 // // once a decision is made, consume, broadcast, and wait for another term ready. 150 // // term will be ready once seq has been written into the ledger and next term is decided. 151 // for { 152 // select { 153 // case <-o.quit: 154 // break 155 // case newTerm := <-o.termProvider.GetTermChangeEventChannel(): 156 // // term changed, init term 157 // o.handleTermChanged(newTerm) 158 // case decision := <-o.consensusReachedChannel: 159 // o.handleConsensusReached(decision) 160 // } 161 // } 162 //} 163 // 164 //func (o *AnnsensusPartner) handleConsensusReached(decision bft.ConsensusDecision) { 165 // // decision is made, broadcast to others 166 // fmt.Println(decision) 167 // logrus.Warn("Here you need to broadcast decision to other non-consensus nodes") 168 //} 169 // 170 //func (o *AnnsensusPartner) handleTermChanged(term *term.Term) { 171 // // init a bft 172 // bft := bft.NewDefaultBFTPartner() 173 // fmt.Println(term) 174 //}