github.com/badrootd/celestia-core@v0.0.0-20240305091328-aa4207a4b25d/state/test/factory/block.go (about) 1 package factory 2 3 import ( 4 "time" 5 6 sm "github.com/badrootd/celestia-core/state" 7 "github.com/badrootd/celestia-core/test/factory" 8 9 tmproto "github.com/badrootd/celestia-core/proto/tendermint/types" 10 "github.com/badrootd/celestia-core/types" 11 ) 12 13 func MakeBlocks(n int, state *sm.State, privVal types.PrivValidator) []*types.Block { 14 blocks := make([]*types.Block, 0) 15 16 var ( 17 prevBlock *types.Block 18 prevBlockMeta *types.BlockMeta 19 ) 20 21 appHeight := byte(0x01) 22 for i := 0; i < n; i++ { 23 height := int64(i + 1) 24 25 block, parts := makeBlockAndPartSet(*state, prevBlock, prevBlockMeta, privVal, height) 26 blocks = append(blocks, block) 27 28 prevBlock = block 29 prevBlockMeta = types.NewBlockMeta(block, parts) 30 31 // update state 32 state.AppHash = []byte{appHeight} 33 appHeight++ 34 state.LastBlockHeight = height 35 } 36 37 return blocks 38 } 39 40 func MakeBlock(state sm.State, height int64, c *types.Commit) *types.Block { 41 block, _ := state.MakeBlock( 42 height, 43 factory.MakeTenTxs(state.LastBlockHeight), 44 c, 45 nil, 46 state.Validators.GetProposer().Address, 47 ) 48 return block 49 } 50 51 func makeBlockAndPartSet(state sm.State, lastBlock *types.Block, lastBlockMeta *types.BlockMeta, 52 privVal types.PrivValidator, height int64) (*types.Block, *types.PartSet) { 53 54 lastCommit := types.NewCommit(height-1, 0, types.BlockID{}, nil) 55 if height > 1 { 56 vote, _ := MakeVote( 57 privVal, 58 lastBlock.Header.ChainID, 59 1, lastBlock.Header.Height, 0, 2, 60 lastBlockMeta.BlockID, 61 time.Now()) 62 lastCommit = types.NewCommit(vote.Height, vote.Round, 63 lastBlockMeta.BlockID, []types.CommitSig{vote.CommitSig()}) 64 } 65 66 return state.MakeBlock(height, []types.Tx{}, lastCommit, nil, state.Validators.GetProposer().Address) 67 } 68 69 func MakeVote( 70 val types.PrivValidator, 71 chainID string, 72 valIndex int32, 73 height int64, 74 round int32, 75 step int, 76 blockID types.BlockID, 77 time time.Time, 78 ) (*types.Vote, error) { 79 pubKey, err := val.GetPubKey() 80 if err != nil { 81 return nil, err 82 } 83 v := &types.Vote{ 84 ValidatorAddress: pubKey.Address(), 85 ValidatorIndex: valIndex, 86 Height: height, 87 Round: round, 88 Type: tmproto.SignedMsgType(step), 89 BlockID: blockID, 90 Timestamp: time, 91 } 92 93 vpb := v.ToProto() 94 err = val.SignVote(chainID, vpb) 95 if err != nil { 96 panic(err) 97 } 98 v.Signature = vpb.Signature 99 return v, nil 100 }