github.com/adoriasoft/tendermint@v0.34.0-dev1.0.20200722151356-96d84601a75a/light/helpers_test.go (about) 1 package light_test 2 3 import ( 4 "time" 5 6 "github.com/tendermint/tendermint/crypto" 7 "github.com/tendermint/tendermint/crypto/ed25519" 8 "github.com/tendermint/tendermint/crypto/tmhash" 9 tmproto "github.com/tendermint/tendermint/proto/tendermint/types" 10 "github.com/tendermint/tendermint/types" 11 tmtime "github.com/tendermint/tendermint/types/time" 12 ) 13 14 // privKeys is a helper type for testing. 15 // 16 // It lets us simulate signing with many keys. The main use case is to create 17 // a set, and call GenSignedHeader to get properly signed header for testing. 18 // 19 // You can set different weights of validators each time you call ToValidators, 20 // and can optionally extend the validator set later with Extend. 21 type privKeys []crypto.PrivKey 22 23 // genPrivKeys produces an array of private keys to generate commits. 24 func genPrivKeys(n int) privKeys { 25 res := make(privKeys, n) 26 for i := range res { 27 res[i] = ed25519.GenPrivKey() 28 } 29 return res 30 } 31 32 // // Change replaces the key at index i. 33 // func (pkz privKeys) Change(i int) privKeys { 34 // res := make(privKeys, len(pkz)) 35 // copy(res, pkz) 36 // res[i] = ed25519.GenPrivKey() 37 // return res 38 // } 39 40 // Extend adds n more keys (to remove, just take a slice). 41 func (pkz privKeys) Extend(n int) privKeys { 42 extra := genPrivKeys(n) 43 return append(pkz, extra...) 44 } 45 46 // // GenSecpPrivKeys produces an array of secp256k1 private keys to generate commits. 47 // func GenSecpPrivKeys(n int) privKeys { 48 // res := make(privKeys, n) 49 // for i := range res { 50 // res[i] = secp256k1.GenPrivKey() 51 // } 52 // return res 53 // } 54 55 // // ExtendSecp adds n more secp256k1 keys (to remove, just take a slice). 56 // func (pkz privKeys) ExtendSecp(n int) privKeys { 57 // extra := GenSecpPrivKeys(n) 58 // return append(pkz, extra...) 59 // } 60 61 // ToValidators produces a valset from the set of keys. 62 // The first key has weight `init` and it increases by `inc` every step 63 // so we can have all the same weight, or a simple linear distribution 64 // (should be enough for testing). 65 func (pkz privKeys) ToValidators(init, inc int64) *types.ValidatorSet { 66 res := make([]*types.Validator, len(pkz)) 67 for i, k := range pkz { 68 res[i] = types.NewValidator(k.PubKey(), init+int64(i)*inc) 69 } 70 return types.NewValidatorSet(res) 71 } 72 73 // signHeader properly signs the header with all keys from first to last exclusive. 74 func (pkz privKeys) signHeader(header *types.Header, first, last int) *types.Commit { 75 commitSigs := make([]types.CommitSig, len(pkz)) 76 for i := 0; i < len(pkz); i++ { 77 commitSigs[i] = types.NewCommitSigAbsent() 78 } 79 80 // We need this list to keep the ordering. 81 vset := pkz.ToValidators(1, 1) 82 83 blockID := types.BlockID{ 84 Hash: header.Hash(), 85 PartSetHeader: types.PartSetHeader{Total: 1, Hash: crypto.CRandBytes(32)}, 86 } 87 88 // Fill in the votes we want. 89 for i := first; i < last && i < len(pkz); i++ { 90 vote := makeVote(header, vset, pkz[i], blockID) 91 commitSigs[vote.ValidatorIndex] = vote.CommitSig() 92 } 93 94 return types.NewCommit(header.Height, 1, blockID, commitSigs) 95 } 96 97 func makeVote(header *types.Header, valset *types.ValidatorSet, 98 key crypto.PrivKey, blockID types.BlockID) *types.Vote { 99 100 addr := key.PubKey().Address() 101 idx, _ := valset.GetByAddress(addr) 102 vote := &types.Vote{ 103 ValidatorAddress: addr, 104 ValidatorIndex: idx, 105 Height: header.Height, 106 Round: 1, 107 Timestamp: tmtime.Now(), 108 Type: tmproto.PrecommitType, 109 BlockID: blockID, 110 } 111 112 v := vote.ToProto() 113 // Sign it 114 signBytes := types.VoteSignBytes(header.ChainID, v) 115 sig, err := key.Sign(signBytes) 116 if err != nil { 117 panic(err) 118 } 119 120 vote.Signature = sig 121 122 return vote 123 } 124 125 func genHeader(chainID string, height int64, bTime time.Time, txs types.Txs, 126 valset, nextValset *types.ValidatorSet, appHash, consHash, resHash []byte) *types.Header { 127 128 return &types.Header{ 129 ChainID: chainID, 130 Height: height, 131 Time: bTime, 132 // LastBlockID 133 // LastCommitHash 134 ValidatorsHash: valset.Hash(), 135 NextValidatorsHash: nextValset.Hash(), 136 DataHash: txs.Hash(), 137 AppHash: appHash, 138 ConsensusHash: consHash, 139 LastResultsHash: resHash, 140 ProposerAddress: valset.Validators[0].Address, 141 } 142 } 143 144 // GenSignedHeader calls genHeader and signHeader and combines them into a SignedHeader. 145 func (pkz privKeys) GenSignedHeader(chainID string, height int64, bTime time.Time, txs types.Txs, 146 valset, nextValset *types.ValidatorSet, appHash, consHash, resHash []byte, first, last int) *types.SignedHeader { 147 148 header := genHeader(chainID, height, bTime, txs, valset, nextValset, appHash, consHash, resHash) 149 return &types.SignedHeader{ 150 Header: header, 151 Commit: pkz.signHeader(header, first, last), 152 } 153 } 154 155 // GenSignedHeaderLastBlockID calls genHeader and signHeader and combines them into a SignedHeader. 156 func (pkz privKeys) GenSignedHeaderLastBlockID(chainID string, height int64, bTime time.Time, txs types.Txs, 157 valset, nextValset *types.ValidatorSet, appHash, consHash, resHash []byte, first, last int, 158 lastBlockID types.BlockID) *types.SignedHeader { 159 160 header := genHeader(chainID, height, bTime, txs, valset, nextValset, appHash, consHash, resHash) 161 header.LastBlockID = lastBlockID 162 return &types.SignedHeader{ 163 Header: header, 164 Commit: pkz.signHeader(header, first, last), 165 } 166 } 167 168 func (pkz privKeys) ChangeKeys(delta int) privKeys { 169 newKeys := pkz[delta:] 170 return newKeys.Extend(delta) 171 } 172 173 // Generates the header and validator set to create a full entire mock node with blocks to height ( 174 // blockSize) and with variation in validator sets. BlockIntervals are in per minute. 175 // NOTE: Expected to have a large validator set size ~ 100 validators. 176 func GenMockNode( 177 chainID string, 178 blockSize int64, 179 valSize int, 180 valVariation float32, 181 bTime time.Time) ( 182 string, 183 map[int64]*types.SignedHeader, 184 map[int64]*types.ValidatorSet) { 185 186 var ( 187 headers = make(map[int64]*types.SignedHeader, blockSize) 188 valset = make(map[int64]*types.ValidatorSet, blockSize) 189 keys = genPrivKeys(valSize) 190 totalVariation = valVariation 191 valVariationInt int 192 newKeys privKeys 193 ) 194 195 valVariationInt = int(totalVariation) 196 totalVariation = -float32(valVariationInt) 197 newKeys = keys.ChangeKeys(valVariationInt) 198 199 // genesis header and vals 200 lastHeader := keys.GenSignedHeader(chainID, 1, bTime.Add(1*time.Minute), nil, 201 keys.ToValidators(2, 2), newKeys.ToValidators(2, 2), hash("app_hash"), hash("cons_hash"), 202 hash("results_hash"), 0, len(keys)) 203 currentHeader := lastHeader 204 headers[1] = currentHeader 205 valset[1] = keys.ToValidators(2, 2) 206 keys = newKeys 207 208 for height := int64(2); height <= blockSize; height++ { 209 totalVariation += valVariation 210 valVariationInt = int(totalVariation) 211 totalVariation = -float32(valVariationInt) 212 newKeys = keys.ChangeKeys(valVariationInt) 213 currentHeader = keys.GenSignedHeaderLastBlockID(chainID, height, bTime.Add(time.Duration(height)*time.Minute), 214 nil, 215 keys.ToValidators(2, 2), newKeys.ToValidators(2, 2), hash("app_hash"), hash("cons_hash"), 216 hash("results_hash"), 0, len(keys), types.BlockID{Hash: lastHeader.Hash()}) 217 headers[height] = currentHeader 218 valset[height] = keys.ToValidators(2, 2) 219 lastHeader = currentHeader 220 keys = newKeys 221 } 222 223 return chainID, headers, valset 224 } 225 226 func hash(s string) []byte { 227 return tmhash.Sum([]byte(s)) 228 }