github.com/annchain/OG@v0.0.9/consensus/annsensus_test/stub_test.go (about)

     1  package annsensus_test
     2  
     3  import (
     4  	"github.com/annchain/OG/account"
     5  	"github.com/annchain/OG/consensus/annsensus"
     6  	"github.com/annchain/OG/consensus/bft"
     7  	"github.com/annchain/OG/consensus/dkg"
     8  	"github.com/annchain/OG/consensus/term"
     9  	"github.com/sirupsen/logrus"
    10  	"time"
    11  )
    12  
    13  type dummyAccountProvider struct {
    14  	MyAccount *account.Account
    15  }
    16  
    17  func (d dummyAccountProvider) Account() *account.Account {
    18  	return d.MyAccount
    19  }
    20  
    21  type dummySignatureProvider struct {
    22  }
    23  
    24  func (s dummySignatureProvider) Sign(data []byte) []byte {
    25  	// no sign
    26  	return data
    27  }
    28  
    29  type dummyConsensusContext struct {
    30  	term      *term.Term
    31  	MyBftId   int
    32  	MyPartSec dkg.PartSec
    33  	blockTime time.Duration
    34  }
    35  
    36  func (d dummyConsensusContext) GetTerm() *term.Term {
    37  	return d.term
    38  }
    39  
    40  func (d dummyConsensusContext) GetMyBftId() int {
    41  	return d.MyBftId
    42  }
    43  
    44  func (d dummyConsensusContext) GetMyPartSec() dkg.PartSec {
    45  	return d.MyPartSec
    46  }
    47  
    48  func (d dummyConsensusContext) GetBlockTime() time.Duration {
    49  	return d.blockTime
    50  }
    51  
    52  type dummyConsensusContextProivder struct {
    53  	Template annsensus.ConsensusContext
    54  }
    55  
    56  func (d dummyConsensusContextProivder) GetConsensusContext(newTerm *term.Term) annsensus.ConsensusContext {
    57  	return &dummyConsensusContext{
    58  		term:      newTerm,
    59  		MyBftId:   d.Template.GetMyBftId(),
    60  		MyPartSec: d.Template.GetMyPartSec(),
    61  		blockTime: d.Template.GetBlockTime(),
    62  	}
    63  }
    64  
    65  type dummyProposalGenerator struct {
    66  	CurrentHeight uint64
    67  }
    68  
    69  func (d dummyProposalGenerator) ProduceProposal() (proposal bft.Proposal, validCondition bft.ProposalCondition) {
    70  	time.Sleep(1000 * time.Millisecond)
    71  	currentTime := time.Now()
    72  	p := bft.StringProposal{Content: currentTime.Format("2006-01-02 15:04:05")}
    73  
    74  	return &p, bft.ProposalCondition{ValidHeight: d.CurrentHeight}
    75  }
    76  
    77  type dummyProposalValidator struct {
    78  }
    79  
    80  func (d dummyProposalValidator) ValidateProposal(proposal bft.Proposal) error {
    81  	return nil
    82  }
    83  
    84  type dummyDecisionMaker struct {
    85  }
    86  
    87  func (d dummyDecisionMaker) MakeDecision(proposal bft.Proposal, state *bft.HeightRoundState) (bft.ConsensusDecision, error) {
    88  	return proposal, nil
    89  }
    90  
    91  type dummyTermProvider struct {
    92  	termChangeEventChan chan annsensus.ConsensusContext
    93  }
    94  
    95  func NewDummyTermProvider() *dummyTermProvider {
    96  	return &dummyTermProvider{termChangeEventChan: make(chan annsensus.ConsensusContext)}
    97  }
    98  
    99  func (d dummyTermProvider) HeightTerm(height uint64) (termId uint32) {
   100  	// currently always return 0 as a genesis term.
   101  	return 0
   102  	// return uint32(height / 10)
   103  }
   104  
   105  func (d dummyTermProvider) CurrentTerm() (termId uint32) {
   106  	panic("implement me")
   107  }
   108  
   109  func (d dummyTermProvider) Peers(termId uint32) ([]bft.BftPeer, error) {
   110  	panic("implement me")
   111  }
   112  
   113  func (d dummyTermProvider) GetTermChangeEventChannel() chan annsensus.ConsensusContext {
   114  	return d.termChangeEventChan
   115  }
   116  
   117  type dummyAnnsensusPartnerProvider struct {
   118  	peerChans []chan *annsensus.AnnsensusMessageEvent
   119  }
   120  
   121  func NewDummyAnnsensusPartnerProivder(peerChans []chan *annsensus.AnnsensusMessageEvent) *dummyAnnsensusPartnerProvider {
   122  	dapp := &dummyAnnsensusPartnerProvider{
   123  		peerChans: peerChans,
   124  	}
   125  	return dapp
   126  }
   127  
   128  func (d *dummyAnnsensusPartnerProvider) GetDkgPartnerInstance(context annsensus.ConsensusContext) (dkgPartner dkg.DkgPartner, err error) {
   129  	myId := context.GetMyBftId()
   130  
   131  	localAnnsensusPeerCommunicator := &LocalAnnsensusPeerCommunicator{
   132  		Myid:  myId,
   133  		Peers: d.peerChans,
   134  		pipe:  d.peerChans[myId],
   135  	}
   136  
   137  	dkgMessageAdapter := &annsensus.PlainDkgAdapter{
   138  		DkgMessageUnmarshaller: &annsensus.DkgMessageUnmarshaller{},
   139  	}
   140  
   141  	commuicatorDkg := annsensus.NewProxyDkgPeerCommunicator(dkgMessageAdapter, localAnnsensusPeerCommunicator)
   142  
   143  	term := context.GetTerm()
   144  	dkgPartner, err = dkg.NewDefaultDkgPartner(
   145  		term.Suite,
   146  		term.Id,
   147  		term.PartsNum,
   148  		term.Threshold,
   149  		term.AllPartPublicKeys,
   150  		context.GetMyPartSec(),
   151  		commuicatorDkg,
   152  		commuicatorDkg,
   153  	)
   154  	return
   155  
   156  }
   157  
   158  func (d *dummyAnnsensusPartnerProvider) GetBftPartnerInstance(context annsensus.ConsensusContext) bft.BftPartner {
   159  	myId := context.GetMyBftId()
   160  
   161  	bftMessageAdapter := &annsensus.PlainBftAdapter{
   162  		BftMessageUnmarshaller: &annsensus.BftMessageUnmarshaller{},
   163  	}
   164  
   165  	localAnnsensusPeerCommunicator := &LocalAnnsensusPeerCommunicator{
   166  		Myid:  myId,
   167  		Peers: d.peerChans,
   168  		pipe:  d.peerChans[myId],
   169  	}
   170  	commuicatorBft := annsensus.NewProxyBftPeerCommunicator(bftMessageAdapter, localAnnsensusPeerCommunicator)
   171  
   172  	currentTerm := context.GetTerm()
   173  
   174  	peerInfos := annsensus.DkgToBft(currentTerm.AllPartPublicKeys)
   175  
   176  	bftPartner := bft.NewDefaultBFTPartner(
   177  		currentTerm.PartsNum,
   178  		context.GetMyBftId(),
   179  		context.GetBlockTime(),
   180  		commuicatorBft,
   181  		commuicatorBft,
   182  		&dummyProposalGenerator{},
   183  		&dummyProposalValidator{},
   184  		&dummyDecisionMaker{},
   185  		peerInfos,
   186  	)
   187  	return bftPartner
   188  }
   189  
   190  type LocalAnnsensusPeerCommunicator struct {
   191  	Myid  int
   192  	Peers []chan *annsensus.AnnsensusMessageEvent
   193  	pipe  chan *annsensus.AnnsensusMessageEvent
   194  }
   195  
   196  func (d *LocalAnnsensusPeerCommunicator) Broadcast(msg annsensus.AnnsensusMessage, peers []annsensus.AnnsensusPeer) {
   197  	for _, peer := range peers {
   198  		logrus.WithField("peer", peer.Id).WithField("IM", d.Myid).
   199  			WithField("msg", msg).Debug("local broadcasting annsensus message")
   200  		go func(peer annsensus.AnnsensusPeer) {
   201  			//ffchan.NewTimeoutSenderShort(d.Peers[peer.Id], msg, "annsensus")
   202  			d.Peers[peer.Id] <- &annsensus.AnnsensusMessageEvent{
   203  				Message: msg,
   204  				Peer:    peer,
   205  			}
   206  		}(peer)
   207  	}
   208  }
   209  
   210  func (d *LocalAnnsensusPeerCommunicator) Unicast(msg annsensus.AnnsensusMessage, peer annsensus.AnnsensusPeer) {
   211  	logrus.Debug("local unicasting by dummyBftPeerCommunicator")
   212  	go func() {
   213  		//ffchan.NewTimeoutSenderShort(d.PeerPipeIns[peer.Id], msg, "bft")
   214  		d.Peers[peer.Id] <- &annsensus.AnnsensusMessageEvent{
   215  			Message: msg,
   216  			Peer:    peer,
   217  		}
   218  	}()
   219  }
   220  
   221  func (d *LocalAnnsensusPeerCommunicator) GetPipeIn() chan *annsensus.AnnsensusMessageEvent {
   222  	return d.pipe
   223  }
   224  
   225  func (d *LocalAnnsensusPeerCommunicator) GetPipeOut() chan *annsensus.AnnsensusMessageEvent {
   226  	return d.pipe
   227  }