MCPcopy Index your code
hub / github.com/chain/Core / CommitAppliedBlock

Method CommitAppliedBlock

protocol/block.go:164–191  ·  view source on GitHub ↗

CommitBlock commits a block to the blockchain. The block must already have been applied with ApplyValidBlock or ApplyNewBlock, which will have produced the new snapshot that's required here. This function saves the block to the store and sometimes (not more often than saveSnapshotFrequency) saves t

(ctx context.Context, block *legacy.Block, snapshot *state.Snapshot)

Source from the content-addressed store, hash-verified

162//
163// TODO(bobg): rename to CommitAppliedBlock for clarity (deferred from https://github.com/chain/chain/pull/788)
164func (c *Chain) CommitAppliedBlock(ctx context.Context, block *legacy.Block, snapshot *state.Snapshot) error {
165 // SaveBlock is the linearization point. Once the block is committed
166 // to persistent storage, the block has been applied and everything
167 // else can be derived from that block.
168 err := c.store.SaveBlock(ctx, block)
169 if err != nil {
170 return errors.Wrap(err, "storing block")
171 }
172 if block.Time().After(c.lastQueuedSnapshot.Add(saveSnapshotFrequency)) {
173 c.queueSnapshot(ctx, block.Height, block.Time(), snapshot)
174 }
175
176 err = c.store.FinalizeBlock(ctx, block.Height)
177 if err != nil {
178 return errors.Wrap(err, "finalizing block")
179 }
180
181 // c.setState will update the local blockchain state and height.
182 // When c.store is a txdb.Store, and c has been initialized with a
183 // channel from txdb.ListenBlocks, then the above call to
184 // c.store.FinalizeBlock will have done a postgresql NOTIFY and
185 // that will wake up the goroutine in NewChain, which also calls
186 // setHeight. But duplicate calls with the same blockheight are
187 // harmless; and the following call is required in the cases where
188 // it's not redundant.
189 c.setState(block, snapshot)
190 return nil
191}
192
193func (c *Chain) queueSnapshot(ctx context.Context, height uint64, timestamp time.Time, s *state.Snapshot) {
194 // Non-blockingly queue the snapshot for storage.

Callers 10

RecoverMethod · 0.95
NewChainFunction · 0.95
generateBlockFunction · 0.95
ConfigureFunction · 0.95
newTestChainFunction · 0.80
makeEmptyBlockFunction · 0.80
MakeBlockFunction · 0.80
applyBlockFunction · 0.80
commitBlockMethod · 0.80

Calls 6

queueSnapshotMethod · 0.95
setStateMethod · 0.95
TimeMethod · 0.80
SaveBlockMethod · 0.65
FinalizeBlockMethod · 0.65
AddMethod · 0.45

Tested by 4

generateBlockFunction · 0.76
newTestChainFunction · 0.64
makeEmptyBlockFunction · 0.64