You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
1.9 KiB

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