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.

92 lines
1.8 KiB

  1. package factory
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/require"
  6. "github.com/tendermint/tendermint/crypto"
  7. "github.com/tendermint/tendermint/crypto/tmhash"
  8. "github.com/tendermint/tendermint/types"
  9. "github.com/tendermint/tendermint/version"
  10. )
  11. const (
  12. DefaultTestChainID = "test-chain"
  13. )
  14. var (
  15. DefaultTestTime = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
  16. )
  17. func RandomAddress() []byte {
  18. return crypto.CRandBytes(crypto.AddressSize)
  19. }
  20. func RandomHash() []byte {
  21. return crypto.CRandBytes(tmhash.Size)
  22. }
  23. func MakeBlockID() types.BlockID {
  24. return MakeBlockIDWithHash(RandomHash())
  25. }
  26. func MakeBlockIDWithHash(hash []byte) types.BlockID {
  27. return types.BlockID{
  28. Hash: hash,
  29. PartSetHeader: types.PartSetHeader{
  30. Total: 100,
  31. Hash: RandomHash(),
  32. },
  33. }
  34. }
  35. // MakeHeader fills the rest of the contents of the header such that it passes
  36. // validate basic
  37. func MakeHeader(t *testing.T, h *types.Header) *types.Header {
  38. t.Helper()
  39. if h.Version.Block == 0 {
  40. h.Version.Block = version.BlockProtocol
  41. }
  42. if h.Height == 0 {
  43. h.Height = 1
  44. }
  45. if h.LastBlockID.IsNil() {
  46. h.LastBlockID = MakeBlockID()
  47. }
  48. if h.ChainID == "" {
  49. h.ChainID = DefaultTestChainID
  50. }
  51. if len(h.LastCommitHash) == 0 {
  52. h.LastCommitHash = RandomHash()
  53. }
  54. if len(h.DataHash) == 0 {
  55. h.DataHash = RandomHash()
  56. }
  57. if len(h.ValidatorsHash) == 0 {
  58. h.ValidatorsHash = RandomHash()
  59. }
  60. if len(h.NextValidatorsHash) == 0 {
  61. h.NextValidatorsHash = RandomHash()
  62. }
  63. if len(h.ConsensusHash) == 0 {
  64. h.ConsensusHash = RandomHash()
  65. }
  66. if len(h.AppHash) == 0 {
  67. h.AppHash = RandomHash()
  68. }
  69. if len(h.LastResultsHash) == 0 {
  70. h.LastResultsHash = RandomHash()
  71. }
  72. if len(h.EvidenceHash) == 0 {
  73. h.EvidenceHash = RandomHash()
  74. }
  75. if len(h.ProposerAddress) == 0 {
  76. h.ProposerAddress = RandomAddress()
  77. }
  78. require.NoError(t, h.ValidateBasic())
  79. return h
  80. }