github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/consensus/dpos/manager_test.go (about) 1 // Copyright 2019 The go-vnt Authors 2 // This file is part of the go-vnt library. 3 // 4 // The go-vnt library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-vnt library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-vnt library. If not, see <http://www.gnu.org/licenses/>. 16 17 package dpos 18 19 import ( 20 "bytes" 21 "crypto/ecdsa" 22 "github.com/stretchr/testify/assert" 23 "github.com/vntchain/go-vnt/common" 24 "github.com/vntchain/go-vnt/crypto" 25 "github.com/vntchain/go-vnt/params" 26 "testing" 27 ) 28 29 func TestManager(t *testing.T) { 30 tests := []struct { 31 initWitness []string 32 result []string 33 }{ 34 // Just Create a Manager, and test sort 35 { 36 initWitness: []string{"A", "B", "C", "D", "E"}, 37 result: []string{"A", "B", "C", "D", "E"}, 38 }, 39 } 40 41 for i, tt := range tests { 42 ap := newTesterAccountPool() 43 44 ws := ap.stringToAddressSorted(tt.initWitness) 45 assert.Equal(t, len(tt.initWitness), len(ws)) 46 47 conf := ¶ms.DposConfig{ 48 Period: 2, 49 WitnessesNum: 3, 50 } 51 52 m := NewManager(conf.Period, ws) 53 witness := m.Witnesses 54 55 // results address 56 results := ap.stringToAddressSorted(tt.result) 57 58 if len(witness) != len(results) { 59 t.Errorf("test:%d, witness lens != result", i) 60 } 61 62 for j := 0; j < len(results); j++ { 63 if !bytes.Equal(witness[j][:], results[j][:]) { 64 t.Errorf("test:%d, witness mismatch result[%d]: %x, %x", i, j, witness[j], results[j]) 65 } 66 } 67 } 68 } 69 70 // testerAccountPool maintains current active address 71 type testerAccountPool struct { 72 accounts map[string]*ecdsa.PrivateKey 73 } 74 75 func newTesterAccountPool() *testerAccountPool { 76 return &testerAccountPool{ 77 accounts: make(map[string]*ecdsa.PrivateKey), 78 } 79 } 80 81 func (ap *testerAccountPool) address(account string) common.Address { 82 // Ensure we have a persistent key for the account 83 if ap.accounts[account] == nil { 84 ap.accounts[account], _ = crypto.GenerateKey() 85 } 86 // Resolve and return the VNT address 87 return crypto.PubkeyToAddress(ap.accounts[account].PublicKey) 88 } 89 90 func (ap *testerAccountPool) stringToAddress(accounts []string) []common.Address { 91 var address []common.Address 92 for _, acc := range accounts { 93 address = append(address, ap.address(acc)) 94 } 95 return address 96 } 97 98 func (ap *testerAccountPool) stringToAddressSorted(accounts []string) []common.Address { 99 address := ap.stringToAddress(accounts) 100 // sort 101 for i := 0; i < len(address); i++ { 102 for j := i + 1; j < len(address); j++ { 103 if bytes.Compare(address[i][:], address[j][:]) > 0 { 104 address[i], address[j] = address[j], address[i] 105 } 106 } 107 } 108 109 return address 110 }