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.

95 lines
1.9 KiB

  1. package factory
  2. import (
  3. "github.com/tendermint/tendermint/crypto"
  4. "github.com/tendermint/tendermint/crypto/tmhash"
  5. "github.com/tendermint/tendermint/types"
  6. "github.com/tendermint/tendermint/version"
  7. )
  8. const (
  9. DefaultTestChainID = "test-chain"
  10. )
  11. func MakeVersion() version.Consensus {
  12. return version.Consensus{
  13. Block: version.BlockProtocol,
  14. App: 1,
  15. }
  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(h *types.Header) (*types.Header, error) {
  38. if h.Version.Block == 0 {
  39. h.Version.Block = version.BlockProtocol
  40. }
  41. if h.Height == 0 {
  42. h.Height = 1
  43. }
  44. if h.LastBlockID.IsZero() {
  45. h.LastBlockID = MakeBlockID()
  46. }
  47. if h.ChainID == "" {
  48. h.ChainID = DefaultTestChainID
  49. }
  50. if len(h.LastCommitHash) == 0 {
  51. h.LastCommitHash = RandomHash()
  52. }
  53. if len(h.DataHash) == 0 {
  54. h.DataHash = RandomHash()
  55. }
  56. if len(h.ValidatorsHash) == 0 {
  57. h.ValidatorsHash = RandomHash()
  58. }
  59. if len(h.NextValidatorsHash) == 0 {
  60. h.NextValidatorsHash = RandomHash()
  61. }
  62. if len(h.ConsensusHash) == 0 {
  63. h.ConsensusHash = RandomHash()
  64. }
  65. if len(h.AppHash) == 0 {
  66. h.AppHash = RandomHash()
  67. }
  68. if len(h.LastResultsHash) == 0 {
  69. h.LastResultsHash = RandomHash()
  70. }
  71. if len(h.EvidenceHash) == 0 {
  72. h.EvidenceHash = RandomHash()
  73. }
  74. if len(h.ProposerAddress) == 0 {
  75. h.ProposerAddress = RandomAddress()
  76. }
  77. return h, h.ValidateBasic()
  78. }
  79. func MakeRandomHeader() *types.Header {
  80. h, err := MakeHeader(&types.Header{})
  81. if err != nil {
  82. panic(err)
  83. }
  84. return h
  85. }