github.com/annchain/OG@v0.0.9/consensus/annsensus/providers.go (about)

     1  package annsensus
     2  
     3  import (
     4  	"github.com/annchain/OG/consensus/bft"
     5  	"github.com/annchain/OG/consensus/dkg"
     6  	"github.com/annchain/OG/og/account"
     7  )
     8  
     9  type BftPartnerProvider interface {
    10  	GetBftPartnerInstance(context ConsensusContext) bft.BftPartner
    11  }
    12  
    13  type DkgPartnerProvider interface {
    14  	GetDkgPartnerInstance(context ConsensusContext) (dkg.DkgPartner, error)
    15  }
    16  
    17  type DefaultAnnsensusPartnerProvider struct {
    18  	MyAccountProvider account.AccountProvider // interface to the ledger
    19  	ProposalGenerator bft.ProposalGenerator   // interface to the ledger
    20  	ProposalValidator bft.ProposalValidator   // interface to the ledger
    21  	DecisionMaker     bft.DecisionMaker       // interface to the ledger
    22  	BftAdatper        BftMessageAdapter
    23  	DkgAdatper        DkgMessageAdapter
    24  	PeerOutgoing      AnnsensusPeerCommunicatorOutgoing
    25  }
    26  
    27  func (d *DefaultAnnsensusPartnerProvider) GetDkgPartnerInstance(context ConsensusContext) (dkgPartner dkg.DkgPartner, err error) {
    28  	if d.PeerOutgoing == nil || d.DkgAdatper == nil {
    29  		panic("wrong init")
    30  	}
    31  	dkgComm := NewProxyDkgPeerCommunicator(d.DkgAdatper, d.PeerOutgoing)
    32  	currentTerm := context.GetTerm()
    33  	dkgPartner, err = dkg.NewDefaultDkgPartner(
    34  		currentTerm.Suite,
    35  		currentTerm.Id,
    36  		currentTerm.PartsNum,
    37  		currentTerm.Threshold,
    38  		currentTerm.AllPartPublicKeys,
    39  		context.GetMyPartSec(),
    40  		dkgComm,
    41  		dkgComm)
    42  	return
    43  }
    44  
    45  func (d *DefaultAnnsensusPartnerProvider) GetBftPartnerInstance(context ConsensusContext) bft.BftPartner {
    46  	if d.PeerOutgoing == nil || d.BftAdatper == nil {
    47  		panic("wrong init")
    48  	}
    49  
    50  	bftComm := NewProxyBftPeerCommunicator(d.BftAdatper, d.PeerOutgoing)
    51  
    52  	bftPartner := bft.NewDefaultBFTPartner(
    53  		context.GetTerm().PartsNum,
    54  		context.GetMyBftId(),
    55  		context.GetBlockTime(),
    56  		bftComm, //BftPeerCommunicatorIncoming
    57  		bftComm, //BftPeerCommunicatorOutgoing
    58  		d.ProposalGenerator,
    59  		d.ProposalValidator,
    60  		d.DecisionMaker,
    61  		DkgToBft(context.GetTerm().AllPartPublicKeys),
    62  	)
    63  	bftPartner.GetBftPeerCommunicatorIncoming()
    64  	return bftPartner
    65  }
    66  
    67  func DkgToBft(dkgInfo []dkg.PartPub) []bft.BftPeer {
    68  	var peerInfos []bft.BftPeer
    69  	for _, peer := range dkgInfo {
    70  		peerInfos = append(peerInfos, bft.BftPeer{
    71  			Id:             peer.Peer.Id,
    72  			PublicKey:      peer.Peer.PublicKey,
    73  			Address:        peer.Peer.Address,
    74  			PublicKeyBytes: peer.Peer.PublicKeyBytes,
    75  		})
    76  	}
    77  	return peerInfos
    78  }