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)
| 162 | // |
| 163 | // TODO(bobg): rename to CommitAppliedBlock for clarity (deferred from https://github.com/chain/chain/pull/788) |
| 164 | func (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 | |
| 193 | func (c *Chain) queueSnapshot(ctx context.Context, height uint64, timestamp time.Time, s *state.Snapshot) { |
| 194 | // Non-blockingly queue the snapshot for storage. |