github.com/badrootd/nibiru-cometbft@v0.37.5-0.20240307173500-2a75559eee9b/internal/test/block.go (about)

     1  package test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/badrootd/nibiru-cometbft/crypto"
    10  	"github.com/badrootd/nibiru-cometbft/crypto/tmhash"
    11  	sm "github.com/badrootd/nibiru-cometbft/state"
    12  	"github.com/badrootd/nibiru-cometbft/types"
    13  	"github.com/badrootd/nibiru-cometbft/version"
    14  )
    15  
    16  const (
    17  	DefaultTestChainID = "test-chain"
    18  )
    19  
    20  var (
    21  	DefaultTestTime = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
    22  )
    23  
    24  func RandomAddress() []byte {
    25  	return crypto.CRandBytes(crypto.AddressSize)
    26  }
    27  
    28  func RandomHash() []byte {
    29  	return crypto.CRandBytes(tmhash.Size)
    30  }
    31  
    32  func MakeBlockID() types.BlockID {
    33  	return MakeBlockIDWithHash(RandomHash())
    34  }
    35  
    36  func MakeBlockIDWithHash(hash []byte) types.BlockID {
    37  	return types.BlockID{
    38  		Hash: hash,
    39  		PartSetHeader: types.PartSetHeader{
    40  			Total: 100,
    41  			Hash:  RandomHash(),
    42  		},
    43  	}
    44  }
    45  
    46  // MakeHeader fills the rest of the contents of the header such that it passes
    47  // validate basic
    48  func MakeHeader(t *testing.T, h *types.Header) *types.Header {
    49  	t.Helper()
    50  	if h.Version.Block == 0 {
    51  		h.Version.Block = version.BlockProtocol
    52  	}
    53  	if h.Height == 0 {
    54  		h.Height = 1
    55  	}
    56  	if h.LastBlockID.IsZero() {
    57  		h.LastBlockID = MakeBlockID()
    58  	}
    59  	if h.ChainID == "" {
    60  		h.ChainID = DefaultTestChainID
    61  	}
    62  	if len(h.LastCommitHash) == 0 {
    63  		h.LastCommitHash = RandomHash()
    64  	}
    65  	if len(h.DataHash) == 0 {
    66  		h.DataHash = RandomHash()
    67  	}
    68  	if len(h.ValidatorsHash) == 0 {
    69  		h.ValidatorsHash = RandomHash()
    70  	}
    71  	if len(h.NextValidatorsHash) == 0 {
    72  		h.NextValidatorsHash = RandomHash()
    73  	}
    74  	if len(h.ConsensusHash) == 0 {
    75  		h.ConsensusHash = RandomHash()
    76  	}
    77  	if len(h.AppHash) == 0 {
    78  		h.AppHash = RandomHash()
    79  	}
    80  	if len(h.LastResultsHash) == 0 {
    81  		h.LastResultsHash = RandomHash()
    82  	}
    83  	if len(h.EvidenceHash) == 0 {
    84  		h.EvidenceHash = RandomHash()
    85  	}
    86  	if len(h.ProposerAddress) == 0 {
    87  		h.ProposerAddress = RandomAddress()
    88  	}
    89  
    90  	require.NoError(t, h.ValidateBasic())
    91  
    92  	return h
    93  }
    94  
    95  func MakeBlock(state sm.State) *types.Block {
    96  	return state.MakeBlock(state.LastBlockHeight+1, MakeNTxs(state.LastBlockHeight+1, 10), new(types.Commit), nil, state.NextValidators.Proposer.Address)
    97  }
    98  
    99  func MakeBlocks(n int, state sm.State, privVals []types.PrivValidator) ([]*types.Block, error) {
   100  	blockID := MakeBlockID()
   101  	blocks := make([]*types.Block, n)
   102  
   103  	for i := 0; i < n; i++ {
   104  		height := state.LastBlockHeight + 1 + int64(i)
   105  		lastCommit, err := MakeCommit(blockID, height-1, 0, state.LastValidators, privVals, state.ChainID, state.LastBlockTime)
   106  		if err != nil {
   107  			return nil, err
   108  		}
   109  		block := state.MakeBlock(height, MakeNTxs(height, 10), lastCommit, nil, state.LastValidators.Proposer.Address)
   110  		blocks[i] = block
   111  		state.LastBlockID = blockID
   112  		state.LastBlockHeight = height
   113  		state.LastBlockTime = state.LastBlockTime.Add(1 * time.Second)
   114  		state.LastValidators = state.Validators.Copy()
   115  		state.Validators = state.NextValidators.Copy()
   116  		state.NextValidators = state.NextValidators.CopyIncrementProposerPriority(1)
   117  		state.AppHash = RandomHash()
   118  
   119  		blockID = MakeBlockIDWithHash(block.Hash())
   120  	}
   121  
   122  	return blocks, nil
   123  }