MCPcopy Create free account
hub / github.com/CPChain/chain / ValidateState

Method ValidateState

core/block_validator.go:78–101  ·  view source on GitHub ↗

ValidateState validates the various changes that happen after a state transition, such as amount of used gas, the receipt roots and the state root itself. ValidateState returns a database batch if the validation was a success otherwise nil and an error is returned.

(block, parent *types.Block, statedb *state.StateDB, receipts types.Receipts, usedGas uint64)

Source from the content-addressed store, hash-verified

76// itself. ValidateState returns a database batch if the validation was a success
77// otherwise nil and an error is returned.
78func (v *BlockValidator) ValidateState(block, parent *types.Block, statedb *state.StateDB, receipts types.Receipts, usedGas uint64) error {
79 // this is a copy of the block header.
80 header := block.Header()
81 if block.GasUsed() != usedGas {
82 return fmt.Errorf("invalid gas used (remote: %d local: %d)", block.GasUsed(), usedGas)
83 }
84 // Validate the received block's bloom with the one derived from the generated receipts.
85 // For valid blocks this should always validate to true.
86 rbloom := types.CreateBloom(receipts)
87 if rbloom != header.LogsBloom {
88 return fmt.Errorf("invalid bloom (remote: %x local: %x)", header.LogsBloom, rbloom)
89 }
90 // Tre receipt Trie's root (R = (Tr [[H1, R1], ... [Hn, R1]]))
91 receiptSha := types.DeriveSha(receipts)
92 if receiptSha != header.ReceiptsRoot {
93 return fmt.Errorf("invalid receipt root hash (remote: %x local: %x)", header.ReceiptsRoot, receiptSha)
94 }
95 // Validate the state root against the received state root and throw
96 // an error if they don't match.
97 if root := statedb.IntermediateRoot(true); header.StateRoot != root {
98 return fmt.Errorf("invalid merkle root (remote: %x local: %x)", header.StateRoot, root)
99 }
100 return nil
101}
102
103// CalcGasLimit computes the gas limit of the next block after parent.
104// This is miner strategy, not consensus protocol.

Callers

nothing calls this directly

Calls 3

HeaderMethod · 0.80
GasUsedMethod · 0.80
IntermediateRootMethod · 0.80

Tested by

no test coverage detected