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.

88 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, error) {
  33. block, _, err := state.MakeBlock(
  34. height,
  35. factory.MakeTenTxs(state.LastBlockHeight),
  36. c,
  37. nil,
  38. state.Validators.GetProposer().Address,
  39. )
  40. if err != nil {
  41. return nil, err
  42. }
  43. return block, nil
  44. }
  45. func makeBlockAndPartSet(
  46. ctx context.Context,
  47. t *testing.T,
  48. state sm.State,
  49. lastBlock *types.Block,
  50. lastBlockMeta *types.BlockMeta,
  51. privVal types.PrivValidator,
  52. height int64,
  53. ) (*types.Block, *types.PartSet) {
  54. t.Helper()
  55. lastCommit := types.NewCommit(height-1, 0, types.BlockID{}, nil)
  56. if height > 1 {
  57. vote, err := factory.MakeVote(
  58. ctx,
  59. privVal,
  60. lastBlock.Header.ChainID,
  61. 1, lastBlock.Header.Height, 0, 2,
  62. lastBlockMeta.BlockID,
  63. time.Now())
  64. require.NoError(t, err)
  65. lastCommit = types.NewCommit(vote.Height, vote.Round,
  66. lastBlockMeta.BlockID, []types.CommitSig{vote.CommitSig()})
  67. }
  68. block, partSet, err := state.MakeBlock(height, []types.Tx{}, lastCommit, nil, state.Validators.GetProposer().Address)
  69. require.NoError(t, err)
  70. return block, partSet
  71. }