github.com/pokt-network/tendermint@v0.32.11-0.20230426215212-59310158d3e9/lite/helpers.go (about)

     1  package lite
     2  
     3  import (
     4  	"github.com/tendermint/tendermint/crypto"
     5  	"github.com/tendermint/tendermint/crypto/ed25519"
     6  	"github.com/tendermint/tendermint/crypto/secp256k1"
     7  
     8  	"github.com/tendermint/tendermint/types"
     9  	tmtime "github.com/tendermint/tendermint/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  	blockID := types.BlockID{
    79  		Hash:        header.Hash(),
    80  		PartsHeader: types.PartSetHeader{Total: 1, Hash: crypto.CRandBytes(32)},
    81  	}
    82  
    83  	// Fill in the votes we want.
    84  	for i := first; i < last && i < len(pkz); i++ {
    85  		vote := makeVote(header, vset, pkz[i], blockID)
    86  		commitSigs[vote.ValidatorIndex] = vote.CommitSig()
    87  	}
    88  	//blockID := types.BlockID{Hash: header.Hash()}
    89  	return types.NewCommit(blockID, commitSigs)
    90  }
    91  
    92  func makeVote(header *types.Header, valset *types.ValidatorSet, key crypto.PrivKey, blockID types.BlockID) *types.Vote {
    93  	addr := key.PubKey().Address()
    94  	idx, _ := valset.GetByAddress(addr)
    95  	vote := &types.Vote{
    96  		ValidatorAddress: addr,
    97  		ValidatorIndex:   idx,
    98  		Height:           header.Height,
    99  		Round:            1,
   100  		Timestamp:        tmtime.Now(),
   101  		Type:             types.PrecommitType,
   102  		BlockID:          blockID,
   103  	}
   104  	// Sign it
   105  	signBytes := vote.SignBytes(header.ChainID)
   106  	// TODO Consider reworking makeVote API to return an error
   107  	sig, err := key.Sign(signBytes)
   108  	if err != nil {
   109  		panic(err)
   110  	}
   111  	vote.Signature = sig
   112  
   113  	return vote
   114  }
   115  
   116  func genHeader(chainID string, height int64, txs types.Txs,
   117  	valset, nextValset *types.ValidatorSet, appHash, consHash, resHash []byte) *types.Header {
   118  
   119  	return &types.Header{
   120  		ChainID:  chainID,
   121  		Height:   height,
   122  		Time:     tmtime.Now(),
   123  		NumTxs:   int64(len(txs)),
   124  		TotalTxs: int64(len(txs)),
   125  		// LastBlockID
   126  		// LastCommitHash
   127  		ValidatorsHash:     valset.Hash(),
   128  		NextValidatorsHash: nextValset.Hash(),
   129  		DataHash:           txs.Hash(),
   130  		AppHash:            appHash,
   131  		ConsensusHash:      consHash,
   132  		LastResultsHash:    resHash,
   133  	}
   134  }
   135  
   136  // GenSignedHeader calls genHeader and signHeader and combines them into a SignedHeader.
   137  func (pkz privKeys) GenSignedHeader(chainID string, height int64, txs types.Txs,
   138  	valset, nextValset *types.ValidatorSet, appHash, consHash, resHash []byte, first, last int) types.SignedHeader {
   139  
   140  	header := genHeader(chainID, height, txs, valset, nextValset, appHash, consHash, resHash)
   141  	check := types.SignedHeader{
   142  		Header: header,
   143  		Commit: pkz.signHeader(header, first, last),
   144  	}
   145  	return check
   146  }
   147  
   148  // GenFullCommit calls genHeader and signHeader and combines them into a FullCommit.
   149  func (pkz privKeys) GenFullCommit(chainID string, height int64, txs types.Txs,
   150  	valset, nextValset *types.ValidatorSet, appHash, consHash, resHash []byte, first, last int) FullCommit {
   151  
   152  	header := genHeader(chainID, height, txs, valset, nextValset, appHash, consHash, resHash)
   153  	commit := types.SignedHeader{
   154  		Header: header,
   155  		Commit: pkz.signHeader(header, first, last),
   156  	}
   157  	return NewFullCommit(commit, valset, nextValset)
   158  }