MCPcopy Create free account
hub / github.com/DNAProject/DNA / constructBlock

Method constructBlock

consensus/vbft/msg_builder.go:189–225  ·  view source on GitHub ↗
(blkNum uint32, prevBlkHash common.Uint256, txs []*types.Transaction, consensusPayload []byte, blocktimestamp uint32)

Source from the content-addressed store, hash-verified

187}
188
189func (self *Server) constructBlock(blkNum uint32, prevBlkHash common.Uint256, txs []*types.Transaction, consensusPayload []byte, blocktimestamp uint32) (*types.Block, error) {
190 txHash := []common.Uint256{}
191 for _, t := range txs {
192 txHash = append(txHash, t.Hash())
193 }
194 lastBlock, _ := self.blockPool.getSealedBlock(blkNum - 1)
195 if lastBlock == nil {
196 log.Errorf("constructBlock getlastblock failed blknum:%d", blkNum-1)
197 return nil, fmt.Errorf("constructBlock getlastblock failed blknum:%d", blkNum-1)
198 }
199
200 txRoot := common.ComputeMerkleRoot(txHash)
201 blockRoot := ledger.DefLedger.GetBlockRootWithNewTxRoots(lastBlock.Block.Header.Height, []common.Uint256{lastBlock.Block.Header.TransactionsRoot, txRoot})
202
203 blkHeader := &types.Header{
204 PrevBlockHash: prevBlkHash,
205 TransactionsRoot: txRoot,
206 BlockRoot: blockRoot,
207 Timestamp: blocktimestamp,
208 Height: uint32(blkNum),
209 ConsensusData: common.GetNonce(),
210 ConsensusPayload: consensusPayload,
211 }
212 blk := &types.Block{
213 Header: blkHeader,
214 Transactions: txs,
215 }
216 blkHash := blk.Hash()
217 sig, err := signature.Sign(self.account, blkHash[:])
218 if err != nil {
219 return nil, fmt.Errorf("sign block failed, block hash:%s, error: %s", blkHash.ToHexString(), err)
220 }
221 blkHeader.Bookkeepers = []keypair.PublicKey{self.account.PublicKey}
222 blkHeader.SigData = [][]byte{sig}
223
224 return blk, nil
225}
226
227func (self *Server) constructProposalMsg(blkNum uint32, sysTxs, userTxs []*types.Transaction, chainconfig *vconfig.ChainConfig) (*blockProposalMsg, error) {
228

Callers 1

constructProposalMsgMethod · 0.95

Calls 10

HashMethod · 0.95
ErrorfFunction · 0.92
ComputeMerkleRootFunction · 0.92
GetNonceFunction · 0.92
SignFunction · 0.92
getSealedBlockMethod · 0.80
ErrorfMethod · 0.80
HashMethod · 0.65
ToHexStringMethod · 0.45

Tested by

no test coverage detected