github.com/ari-anchor/sei-tendermint@v0.0.0-20230519144642-dc826b7b56bb/internal/state/test/factory/block.go (about)

     1  package factory
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/require"
     9  
    10  	sm "github.com/ari-anchor/sei-tendermint/internal/state"
    11  	"github.com/ari-anchor/sei-tendermint/internal/test/factory"
    12  	"github.com/ari-anchor/sei-tendermint/types"
    13  )
    14  
    15  func MakeBlocks(ctx context.Context, t *testing.T, n int, state *sm.State, privVal types.PrivValidator) []*types.Block {
    16  	t.Helper()
    17  
    18  	blocks := make([]*types.Block, n)
    19  
    20  	var (
    21  		prevBlock     *types.Block
    22  		prevBlockMeta *types.BlockMeta
    23  	)
    24  
    25  	appHeight := byte(0x01)
    26  	for i := 0; i < n; i++ {
    27  		height := int64(i + 1)
    28  
    29  		block, parts := makeBlockAndPartSet(ctx, t, *state, prevBlock, prevBlockMeta, privVal, height)
    30  
    31  		blocks[i] = block
    32  
    33  		prevBlock = block
    34  		prevBlockMeta = types.NewBlockMeta(block, parts)
    35  
    36  		// update state
    37  		state.AppHash = []byte{appHeight}
    38  		appHeight++
    39  		state.LastBlockHeight = height
    40  	}
    41  
    42  	return blocks
    43  }
    44  
    45  func MakeBlock(state sm.State, height int64, c *types.Commit) *types.Block {
    46  	return state.MakeBlock(
    47  		height,
    48  		factory.MakeNTxs(state.LastBlockHeight, 10),
    49  		c,
    50  		nil,
    51  		state.Validators.GetProposer().Address,
    52  	)
    53  }
    54  
    55  func makeBlockAndPartSet(
    56  	ctx context.Context,
    57  	t *testing.T,
    58  	state sm.State,
    59  	lastBlock *types.Block,
    60  	lastBlockMeta *types.BlockMeta,
    61  	privVal types.PrivValidator,
    62  	height int64,
    63  ) (*types.Block, *types.PartSet) {
    64  	t.Helper()
    65  
    66  	lastCommit := &types.Commit{Height: height - 1}
    67  	if height > 1 {
    68  		vote, err := factory.MakeVote(
    69  			ctx,
    70  			privVal,
    71  			lastBlock.Header.ChainID,
    72  			1, lastBlock.Header.Height, 0, 2,
    73  			lastBlockMeta.BlockID,
    74  			time.Now())
    75  		require.NoError(t, err)
    76  		lastCommit = &types.Commit{
    77  			Height:     vote.Height,
    78  			Round:      vote.Round,
    79  			BlockID:    lastBlock.LastBlockID,
    80  			Signatures: []types.CommitSig{vote.CommitSig()},
    81  		}
    82  	}
    83  
    84  	block := state.MakeBlock(height, []types.Tx{}, lastCommit, nil, state.Validators.GetProposer().Address)
    85  	partSet, err := block.MakePartSet(types.BlockPartSizeBytes)
    86  	require.NoError(t, err)
    87  
    88  	return block, partSet
    89  }