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