github.com/annchain/OG@v0.0.9/consensus/annsensus_test/annsensus_test.go (about) 1 // Copyright © 2019 Annchain Authors <EMAIL ADDRESS> 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 package annsensus_test 15 16 import ( 17 "github.com/annchain/OG/common/crypto" 18 "github.com/annchain/OG/consensus/annsensus" 19 "github.com/annchain/OG/consensus/dkg" 20 "github.com/annchain/OG/consensus/term" 21 "github.com/annchain/kyber/v3/pairing/bn256" 22 "github.com/sirupsen/logrus" 23 "testing" 24 "time" 25 ) 26 27 //func genPublicKeys(num int) (accounts []ogcrypto.PublicKey) { 28 // signer := ogcrypto.NewSigner(ogcrypto.CryptoTypeSecp256k1) 29 // for i := 0; i < num; i++ { 30 // pub, _ := signer.RandomKeyPair() 31 // accounts = append(accounts, pub) 32 // } 33 // return accounts 34 //} 35 // 36 //func TestAnnSensus_GenerateDKgPublicKey(t *testing.T) { 37 // var as = NewAnnSensus(4, false, 1, true, 5, 38 // genPublicKeys(5), "test.json", false) 39 // 40 // pk := as.dkg.PublicKey() 41 // fmt.Println(hexutil.Encode(pk)) 42 // point, err := bn256.UnmarshalBinaryPointG2(pk) 43 // if err != nil { 44 // t.Fatal(err) 45 // } 46 // fmt.Println(point) 47 //} 48 49 func logInit() { 50 Formatter := new(logrus.TextFormatter) 51 Formatter.TimestampFormat = "15:04:05.000000" 52 Formatter.FullTimestamp = true 53 Formatter.ForceColors = true 54 logrus.SetFormatter(Formatter) 55 logrus.SetLevel(logrus.TraceLevel) 56 //logrus.SetReportCaller(true) 57 } 58 59 func init() { 60 logInit() 61 } 62 63 func generatePeers(suite *bn256.Suite, n int) []dkg.PartSec { 64 signer := crypto.NewSigner(crypto.CryptoTypeSecp256k1) 65 peerInfos := make([]dkg.PartSec, n) 66 for i := 0; i < n; i++ { 67 pubKey, privKey := signer.RandomKeyPair() 68 address := pubKey.Address() 69 // dkg kyber pub/priv key 70 dkgPrivKey, dkgPubKey := dkg.GenPartnerPair(suite) 71 72 peerInfos[i] = dkg.PartSec{ 73 PartPub: dkg.PartPub{ 74 Point: dkgPubKey, 75 Peer: dkg.DkgPeer{ 76 Id: i, 77 PublicKey: pubKey, 78 Address: address, 79 PublicKeyBytes: nil, 80 }, 81 }, 82 Scalar: dkgPrivKey, 83 PrivateKey: privKey, 84 } 85 } 86 return peerInfos 87 } 88 89 func TestAnnSensusFourNodesGenesisTerm(t *testing.T) { 90 nodes := 8 91 accounts := sampleAccounts(nodes) 92 93 suite := bn256.NewSuiteG2() 94 95 // build an annsensus 96 config := annsensus.AnnsensusProcessorConfig{ 97 DisableTermChange: false, 98 DisabledConsensus: false, 99 TermChangeInterval: 60 * 1000, 100 GenesisAccounts: nil, 101 } 102 103 // prepare message channel for each peer 104 // both bft and dkg messages will be adapted to AnnsensusMessage 105 peerChans := make([]chan *annsensus.AnnsensusMessageEvent, nodes) 106 for i := 0; i < nodes; i++ { 107 peerChans[i] = make(chan *annsensus.AnnsensusMessageEvent) 108 } 109 110 aps := make([]*annsensus.AnnsensusPartner, nodes) 111 termProviders := make([]annsensus.TermIdProvider, nodes) 112 113 for i := 0; i < nodes; i++ { 114 // init AnnsensusPeerCommunicator for each node 115 bftAdapter := &annsensus.PlainBftAdapter{} 116 dkgAdapter := &annsensus.PlainDkgAdapter{} 117 communicator := &LocalAnnsensusPeerCommunicator{ 118 Myid: i, 119 Peers: peerChans, 120 pipe: peerChans[i], 121 } 122 123 termProvider := NewDummyTermProvider() 124 termHolder := annsensus.NewAnnsensusTermHolder(termProvider) 125 126 defaultAnnsensusPartnerProvider := &annsensus.DefaultAnnsensusPartnerProvider{ 127 MyAccountProvider: &dummyAccountProvider{MyAccount: accounts[i]}, 128 ProposalGenerator: &dummyProposalGenerator{}, 129 ProposalValidator: &dummyProposalValidator{}, 130 DecisionMaker: &dummyDecisionMaker{}, 131 BftAdatper: bftAdapter, 132 DkgAdatper: dkgAdapter, 133 PeerOutgoing: communicator, 134 } 135 136 consensusContextProvider := dummyConsensusContextProivder{ 137 } 138 139 ann := &annsensus.AnnsensusPartner{ 140 Config: config, 141 BftAdapter: bftAdapter, 142 DkgAdapter: dkgAdapter, 143 TermProvider: termProvider, 144 TermHolder: termHolder, 145 BftPartnerProvider: defaultAnnsensusPartnerProvider, 146 DkgPartnerProvider: defaultAnnsensusPartnerProvider, 147 PeerOutgoing: communicator, 148 PeerIncoming: communicator, 149 ConsensusContextProvider: consensusContextProvider, 150 } 151 ann.InitDefault() 152 153 aps[i] = ann 154 termProviders[i] = termProvider 155 } 156 157 // genesis accounts for dkg. Only partpub is shared. 158 // message is encrypted using partpub. TODO: is that possible? or we need an account public key 159 peers := generatePeers(suite, nodes) 160 pubs := make([]dkg.PartPub, len(peers)) 161 162 for i, peer := range peers { 163 pubs[i] = peer.PartPub 164 } 165 166 // for consensus, all peers are participated in the consensus. 167 senators := make([]term.Senator, len(accounts)) 168 169 // build a public shared term 170 for i, account := range accounts { 171 senators[i] = term.Senator{ 172 Id: i, 173 Address: account.Address, 174 PublicKey: account.PublicKey, 175 BlsPublicKey: pubs[i], 176 } 177 } 178 179 // recover the public key now since everyone (including light peers) needs this info to verify txs. 180 for i := 0; i < nodes; i++ { 181 aps[i].Start() 182 c := termProviders[i].GetTermChangeEventChannel() 183 184 genesisTerm := &term.Term{ 185 Id: 0, 186 PartsNum: len(peers), 187 Threshold: len(peers)*2/3 + 1, 188 Senators: senators, 189 AllPartPublicKeys: pubs, 190 PublicKey: peers[i].PartPub.Point, 191 ActivateHeight: 0, 192 Suite: suite, 193 } 194 195 //tm := term.NewTerm(1,nodes,0) 196 contextProvider := dummyConsensusContext{ 197 term: genesisTerm, 198 MyBftId: i, 199 MyPartSec: peers[i], 200 } 201 202 c <- contextProvider 203 } 204 205 time.Sleep(time.Hour * 1) 206 }