github.com/evdatsion/aphelion-dpos-bft@v0.32.1/lite/helpers.go (about) 1 package lite 2 3 import ( 4 "github.com/evdatsion/aphelion-dpos-bft/crypto" 5 "github.com/evdatsion/aphelion-dpos-bft/crypto/ed25519" 6 "github.com/evdatsion/aphelion-dpos-bft/crypto/secp256k1" 7 8 "github.com/evdatsion/aphelion-dpos-bft/types" 9 tmtime "github.com/evdatsion/aphelion-dpos-bft/types/time" 10 ) 11 12 // privKeys is a helper type for testing. 13 // 14 // It lets us simulate signing with many keys. The main use case is to create 15 // a set, and call GenSignedHeader to get properly signed header for testing. 16 // 17 // You can set different weights of validators each time you call ToValidators, 18 // and can optionally extend the validator set later with Extend. 19 type privKeys []crypto.PrivKey 20 21 // genPrivKeys produces an array of private keys to generate commits. 22 func genPrivKeys(n int) privKeys { 23 res := make(privKeys, n) 24 for i := range res { 25 res[i] = ed25519.GenPrivKey() 26 } 27 return res 28 } 29 30 // Change replaces the key at index i. 31 func (pkz privKeys) Change(i int) privKeys { 32 res := make(privKeys, len(pkz)) 33 copy(res, pkz) 34 res[i] = ed25519.GenPrivKey() 35 return res 36 } 37 38 // Extend adds n more keys (to remove, just take a slice). 39 func (pkz privKeys) Extend(n int) privKeys { 40 extra := genPrivKeys(n) 41 return append(pkz, extra...) 42 } 43 44 // GenSecpPrivKeys produces an array of secp256k1 private keys to generate commits. 45 func GenSecpPrivKeys(n int) privKeys { 46 res := make(privKeys, n) 47 for i := range res { 48 res[i] = secp256k1.GenPrivKey() 49 } 50 return res 51 } 52 53 // ExtendSecp adds n more secp256k1 keys (to remove, just take a slice). 54 func (pkz privKeys) ExtendSecp(n int) privKeys { 55 extra := GenSecpPrivKeys(n) 56 return append(pkz, extra...) 57 } 58 59 // ToValidators produces a valset from the set of keys. 60 // The first key has weight `init` and it increases by `inc` every step 61 // so we can have all the same weight, or a simple linear distribution 62 // (should be enough for testing). 63 func (pkz privKeys) ToValidators(init, inc int64) *types.ValidatorSet { 64 res := make([]*types.Validator, len(pkz)) 65 for i, k := range pkz { 66 res[i] = types.NewValidator(k.PubKey(), init+int64(i)*inc) 67 } 68 return types.NewValidatorSet(res) 69 } 70 71 // signHeader properly signs the header with all keys from first to last exclusive. 72 func (pkz privKeys) signHeader(header *types.Header, first, last int) *types.Commit { 73 commitSigs := make([]*types.CommitSig, len(pkz)) 74 75 // We need this list to keep the ordering. 76 vset := pkz.ToValidators(1, 0) 77 78 // Fill in the votes we want. 79 for i := first; i < last && i < len(pkz); i++ { 80 vote := makeVote(header, vset, pkz[i]) 81 commitSigs[vote.ValidatorIndex] = vote.CommitSig() 82 } 83 blockID := types.BlockID{Hash: header.Hash()} 84 return types.NewCommit(blockID, commitSigs) 85 } 86 87 func makeVote(header *types.Header, valset *types.ValidatorSet, key crypto.PrivKey) *types.Vote { 88 addr := key.PubKey().Address() 89 idx, _ := valset.GetByAddress(addr) 90 vote := &types.Vote{ 91 ValidatorAddress: addr, 92 ValidatorIndex: idx, 93 Height: header.Height, 94 Round: 1, 95 Timestamp: tmtime.Now(), 96 Type: types.PrecommitType, 97 BlockID: types.BlockID{Hash: header.Hash()}, 98 } 99 // Sign it 100 signBytes := vote.SignBytes(header.ChainID) 101 // TODO Consider reworking makeVote API to return an error 102 sig, err := key.Sign(signBytes) 103 if err != nil { 104 panic(err) 105 } 106 vote.Signature = sig 107 108 return vote 109 } 110 111 func genHeader(chainID string, height int64, txs types.Txs, 112 valset, nextValset *types.ValidatorSet, appHash, consHash, resHash []byte) *types.Header { 113 114 return &types.Header{ 115 ChainID: chainID, 116 Height: height, 117 Time: tmtime.Now(), 118 NumTxs: int64(len(txs)), 119 TotalTxs: int64(len(txs)), 120 // LastBlockID 121 // LastCommitHash 122 ValidatorsHash: valset.Hash(), 123 NextValidatorsHash: nextValset.Hash(), 124 DataHash: txs.Hash(), 125 AppHash: appHash, 126 ConsensusHash: consHash, 127 LastResultsHash: resHash, 128 } 129 } 130 131 // GenSignedHeader calls genHeader and signHeader and combines them into a SignedHeader. 132 func (pkz privKeys) GenSignedHeader(chainID string, height int64, txs types.Txs, 133 valset, nextValset *types.ValidatorSet, appHash, consHash, resHash []byte, first, last int) types.SignedHeader { 134 135 header := genHeader(chainID, height, txs, valset, nextValset, appHash, consHash, resHash) 136 check := types.SignedHeader{ 137 Header: header, 138 Commit: pkz.signHeader(header, first, last), 139 } 140 return check 141 } 142 143 // GenFullCommit calls genHeader and signHeader and combines them into a FullCommit. 144 func (pkz privKeys) GenFullCommit(chainID string, height int64, txs types.Txs, 145 valset, nextValset *types.ValidatorSet, appHash, consHash, resHash []byte, first, last int) FullCommit { 146 147 header := genHeader(chainID, height, txs, valset, nextValset, appHash, consHash, resHash) 148 commit := types.SignedHeader{ 149 Header: header, 150 Commit: pkz.signHeader(header, first, last), 151 } 152 return NewFullCommit(commit, valset, nextValset) 153 }