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

     1  package engine_test
     2  
     3  import (
     4  	"github.com/annchain/OG/arefactor/common/mylog"
     5  	"github.com/annchain/OG/common/crypto"
     6  	"github.com/annchain/OG/consensus/dkg"
     7  	"github.com/annchain/OG/consensus/term"
     8  	"github.com/annchain/OG/engine"
     9  	"github.com/annchain/OG/message"
    10  	"github.com/annchain/OG/plugin/annsensus"
    11  	"github.com/annchain/kyber/v3/pairing/bn256"
    12  	"github.com/sirupsen/logrus"
    13  	"testing"
    14  	"time"
    15  )
    16  
    17  func generatePeers(suite *bn256.Suite, n int) []dkg.PartSec {
    18  	signer := crypto.NewSigner(crypto.CryptoTypeSecp256k1)
    19  	peerInfos := make([]dkg.PartSec, n)
    20  	for i := 0; i < n; i++ {
    21  		pubKey, privKey := signer.RandomKeyPair()
    22  		address := pubKey.Address()
    23  		// dkg kyber pub/priv key
    24  		dkgPrivKey, dkgPubKey := dkg.GenPartnerPair(suite)
    25  
    26  		peerInfos[i] = dkg.PartSec{
    27  			PartPub: dkg.PartPub{
    28  				Point: dkgPubKey,
    29  				Peer: dkg.DkgPeer{
    30  					Id:             i,
    31  					PublicKey:      pubKey,
    32  					Address:        address,
    33  					PublicKeyBytes: nil,
    34  				},
    35  			},
    36  			Scalar:     dkgPrivKey,
    37  			PrivateKey: privKey,
    38  		}
    39  	}
    40  	return peerInfos
    41  }
    42  
    43  // TestAnnsensusBenchmark will start the blockchain from genesis
    44  func TestAnnsensusBenchmark(t *testing.T) {
    45  	mylog.LogInit(logrus.InfoLevel)
    46  	nodes := 4
    47  	plugins := make([]*annsensus.AnnsensusPlugin, nodes)
    48  	chans := make([]chan *message.GeneralMessageEvent, nodes)
    49  	communicators := make([]*LocalGeneralPeerCommunicator, nodes)
    50  
    51  	engines := make([]*engine.Engine, nodes)
    52  
    53  	for i := 0; i < nodes; i++ {
    54  		chans[i] = make(chan *message.GeneralMessageEvent)
    55  	}
    56  
    57  	for i := 0; i < nodes; i++ {
    58  		communicators[i] = NewLocalGeneralPeerCommunicator(i, chans[i], chans)
    59  	}
    60  
    61  	accounts := sampleAccounts(nodes)
    62  
    63  	for i := 0; i < nodes; i++ {
    64  		plugins[i] = annsensus.NewAnnsensusPlugin(
    65  			NewDummyTermProvider(),
    66  			&dummyAccountProvider{MyAccount: accounts[i]},
    67  			&dummyProposalGenerator{},
    68  			&dummyProposalValidator{},
    69  			&dummyDecisionMaker{})
    70  		plugins[i].SetOutgoing(communicators[i])
    71  	}
    72  
    73  	// init general processor
    74  	for i := 0; i < nodes; i++ {
    75  		eng := engine.Engine{
    76  			Config:       engine.EngineConfig{},
    77  			PeerOutgoing: communicators[i],
    78  			PeerIncoming: communicators[i],
    79  		}
    80  		eng.InitDefault()
    81  		eng.RegisterPlugin(plugins[i])
    82  		engines[i] = &eng
    83  		eng.Start()
    84  	}
    85  
    86  	suite := bn256.NewSuiteG2()
    87  	// genesis accounts for dkg. Only partpub is shared.
    88  	// message is encrypted using partpub. TODO: is that possible? or we need an account public key
    89  	peers := generatePeers(suite, nodes)
    90  	pubs := make([]dkg.PartPub, len(peers))
    91  
    92  	for i, peer := range peers {
    93  		pubs[i] = peer.PartPub
    94  	}
    95  	// for consensus, all peers are participated in the consensus.
    96  	senators := make([]term.Senator, len(accounts))
    97  	// build a public shared term
    98  	for i, account := range accounts {
    99  		senators[i] = term.Senator{
   100  			Id:           i,
   101  			Address:      account.Address,
   102  			PublicKey:    account.PublicKey,
   103  			BlsPublicKey: pubs[i],
   104  		}
   105  	}
   106  
   107  	for i := 0; i < nodes; i++ {
   108  		c := plugins[i].AnnsensusPartner.TermProvider.GetTermChangeEventChannel()
   109  		genesisTerm := &term.Term{
   110  			Id:                0,
   111  			PartsNum:          nodes,
   112  			Threshold:         nodes*2/3 + 1,
   113  			Senators:          senators,
   114  			AllPartPublicKeys: pubs,
   115  			PublicKey:         peers[i].PartPub.Point,
   116  			ActivateHeight:    0,
   117  			Suite:             suite,
   118  		}
   119  
   120  		//tm := term.NewTerm(1,nodes,0)
   121  		contextProvider := dummyContext{
   122  			term:      genesisTerm,
   123  			MyBftId:   i,
   124  			MyPartSec: peers[i],
   125  		}
   126  
   127  		c <- contextProvider
   128  	}
   129  	logrus.Info("Started")
   130  	//plugins[0].OgPartner.SendMessagePing(communication.OgPeer{Id: 1})
   131  
   132  	var lastValue uint = 0
   133  	for i := 0; i < 60; i++ {
   134  		v := engines[0].GetBenchmarks()["mps"].(uint)
   135  		if lastValue == 0 {
   136  			lastValue = v
   137  		} else {
   138  			logrus.WithField("mps", v-lastValue).Info("performance")
   139  		}
   140  		lastValue = v
   141  		time.Sleep(time.Second)
   142  	}
   143  }