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.

101 lines
2.0 KiB

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