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