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

Function buildTestBlock

consensus/vbft/block_pool_test.go:42–89  ·  view source on GitHub ↗
(t *testing.T, lastBlock *types.Block, lgr *ledger.Ledger)

Source from the content-addressed store, hash-verified

40}
41
42func buildTestBlock(t *testing.T, lastBlock *types.Block, lgr *ledger.Ledger) (*Block, error) {
43 timestamp := uint32(time.Now().Unix())
44 if timestamp <= lastBlock.Header.Timestamp {
45 timestamp = lastBlock.Header.Timestamp + 1
46 }
47 txs := []*types.Transaction{}
48 txHash := []common.Uint256{}
49 txRoot := common.ComputeMerkleRoot(txHash)
50 blockRoot := lgr.GetBlockRootWithNewTxRoots(lastBlock.Header.Height, []common.Uint256{lastBlock.Header.TransactionsRoot, txRoot})
51
52 consensusPayload, err := json.Marshal(&vconfig.VbftBlockInfo{})
53 if err != nil {
54 t.Fatalf("failed to build consensus payload: %s", err)
55 }
56
57 blkHeader := &types.Header{
58 PrevBlockHash: lastBlock.Header.Hash(),
59 TransactionsRoot: txRoot,
60 BlockRoot: blockRoot,
61 Timestamp: timestamp,
62 Height: lastBlock.Header.Height + 1,
63 ConsensusData: common.GetNonce(),
64 ConsensusPayload: consensusPayload,
65 Bookkeepers: make([]keypair.PublicKey, 0),
66 SigData: make([][]byte, 0),
67 }
68
69 // add sigs
70 hash := blkHeader.Hash()
71 for i := 0; i < 5; i++ {
72 acc := testBookkeeperAccounts[i]
73 sig, err := signature.Sign(acc, hash[:])
74 if err != nil {
75 t.Fatalf("bookkeeper %d sign block: %s", i, err)
76 }
77 blkHeader.Bookkeepers = append(blkHeader.Bookkeepers, acc.PublicKey)
78 blkHeader.SigData = append(blkHeader.SigData, sig)
79 }
80
81 blk := &types.Block{
82 Header: blkHeader,
83 Transactions: txs,
84 }
85 block := &Block{
86 Block: blk,
87 }
88 return block, nil
89}
90func TestAddBlock(t *testing.T) {
91 blockpool, err := buildTestBlockPool(t)
92 if err != nil {

Callers 1

TestAddBlockFunction · 0.85

Calls 8

HashMethod · 0.95
ComputeMerkleRootFunction · 0.92
GetNonceFunction · 0.92
SignFunction · 0.92
MarshalMethod · 0.80
FatalfMethod · 0.80
HashMethod · 0.65

Tested by

no test coverage detected