github.com/annchain/OG@v0.0.9/engine_test/annsensus_dummy_test.go (about)

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