MCPcopy Create free account
hub / github.com/chain/Core / GenerateBlock

Method GenerateBlock

protocol/block.go:51–121  ·  view source on GitHub ↗

GenerateBlock generates a valid, but unsigned, candidate block from the current pending transaction pool. It returns the new block and a snapshot of what the state snapshot is if the block is applied. After generating the block, the pending transaction pool will be empty.

(ctx context.Context, prev *legacy.Block, snapshot *state.Snapshot, now time.Time, txs []*legacy.Tx)

Source from the content-addressed store, hash-verified

49// After generating the block, the pending transaction pool will be
50// empty.
51func (c *Chain) GenerateBlock(ctx context.Context, prev *legacy.Block, snapshot *state.Snapshot, now time.Time, txs []*legacy.Tx) (*legacy.Block, *state.Snapshot, error) {
52 // TODO(kr): move this into a lower-level package (e.g. chain/protocol/bc)
53 // so that other packages (e.g. chain/protocol/validation) unit tests can
54 // call this function.
55
56 timestampMS := bc.Millis(now)
57 if timestampMS < prev.TimestampMS {
58 return nil, nil, fmt.Errorf("timestamp %d is earlier than prevblock timestamp %d", timestampMS, prev.TimestampMS)
59 }
60
61 // Make a copy of the snapshot that we can apply our changes to.
62 newSnapshot := state.Copy(c.state.snapshot)
63 newSnapshot.PruneNonces(timestampMS)
64
65 b := &legacy.Block{
66 BlockHeader: legacy.BlockHeader{
67 Version: 1,
68 Height: prev.Height + 1,
69 PreviousBlockHash: prev.Hash(),
70 TimestampMS: timestampMS,
71 BlockCommitment: legacy.BlockCommitment{
72 ConsensusProgram: prev.ConsensusProgram,
73 },
74 },
75 }
76
77 var txEntries []*bc.Tx
78
79 for _, tx := range txs {
80 if len(b.Transactions) >= maxBlockTxs {
81 break
82 }
83
84 // Filter out transactions that are not well-formed.
85 err := c.ValidateTx(tx.Tx)
86 if err != nil {
87 // TODO(bobg): log this?
88 continue
89 }
90
91 // Filter out transactions that are not yet valid, or no longer
92 // valid, per the block's timestamp.
93 if tx.Tx.MinTimeMs > 0 && tx.Tx.MinTimeMs > b.TimestampMS {
94 continue
95 }
96 if tx.Tx.MaxTimeMs > 0 && tx.Tx.MaxTimeMs < b.TimestampMS {
97 continue
98 }
99
100 // Filter out double-spends etc.
101 err = newSnapshot.ApplyTx(tx.Tx)
102 if err != nil {
103 // TODO(bobg): log this?
104 continue
105 }
106
107 b.Transactions = append(b.Transactions, tx)
108 txEntries = append(txEntries, tx.Tx)

Callers 10

generateBlockFunction · 0.95
TestGeneratorRecoveryFunction · 0.95
benchGenBlockFunction · 0.95
TestGenerateBlockFunction · 0.80
makeEmptyBlockFunction · 0.80
TestBadMaxIssuanceWindowFunction · 0.80
MakeBlockFunction · 0.80
makeBlockMethod · 0.80

Calls 5

ValidateTxMethod · 0.95
PruneNoncesMethod · 0.80
ApplyTxMethod · 0.80
RootHashMethod · 0.80
HashMethod · 0.45

Tested by 8

generateBlockFunction · 0.76
TestGeneratorRecoveryFunction · 0.76
benchGenBlockFunction · 0.76
TestGenerateBlockFunction · 0.64
makeEmptyBlockFunction · 0.64
TestBadMaxIssuanceWindowFunction · 0.64