Skip to content

Commit 4f45785

Browse files
committed
core: use headers only where blocks are unnecessary
1 parent b87a684 commit 4f45785

5 files changed

Lines changed: 11 additions & 11 deletions

File tree

‎core/block_validator.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (v *BlockValidator) ValidateBody(block *types.Block) error {
7777
// transition, such as amount of used gas, the receipt roots and the state root
7878
// itself. ValidateState returns a database batch if the validation was a success
7979
// otherwise nil and an error is returned.
80-
func (v *BlockValidator) ValidateState(block, parent *types.Block, statedb *state.StateDB, receipts types.Receipts, usedGas uint64) error {
80+
func (v *BlockValidator) ValidateState(block *types.Block, statedb *state.StateDB, receipts types.Receipts, usedGas uint64) error {
8181
header := block.Header()
8282
if block.GasUsed() != usedGas {
8383
return fmt.Errorf("invalid gas used (remote: %d local: %d)", block.GasUsed(), usedGas)

‎core/blockchain.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,9 +1221,9 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, []
12211221

12221222
parent := it.previous()
12231223
if parent == nil {
1224-
parent = bc.GetBlock(block.ParentHash(), block.NumberU64()-1)
1224+
parent = bc.GetHeader(block.ParentHash(), block.NumberU64()-1)
12251225
}
1226-
state, err := state.New(parent.Root(), bc.stateCache)
1226+
state, err := state.New(parent.Root, bc.stateCache)
12271227
if err != nil {
12281228
return it.index, events, coalescedLogs, err
12291229
}
@@ -1236,7 +1236,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, []
12361236
return it.index, events, coalescedLogs, err
12371237
}
12381238
// Validate the state using the default validator
1239-
if err := bc.Validator().ValidateState(block, parent, state, receipts, usedGas); err != nil {
1239+
if err := bc.Validator().ValidateState(block, state, receipts, usedGas); err != nil {
12401240
bc.reportBlock(block, receipts, err)
12411241
return it.index, events, coalescedLogs, err
12421242
}
@@ -1368,15 +1368,15 @@ func (bc *BlockChain) insertSidechain(block *types.Block, it *insertIterator) (i
13681368
// blocks to regenerate the required state
13691369
localTd := bc.GetTd(current.Hash(), current.NumberU64())
13701370
if localTd.Cmp(externTd) > 0 {
1371-
log.Info("Sidechain written to disk", "start", it.first().NumberU64(), "end", it.previous().NumberU64(), "sidetd", externTd, "localtd", localTd)
1371+
log.Info("Sidechain written to disk", "start", it.first().NumberU64(), "end", it.previous().Number, "sidetd", externTd, "localtd", localTd)
13721372
return it.index, nil, nil, err
13731373
}
13741374
// Gather all the sidechain hashes (full blocks may be memory heavy)
13751375
var (
13761376
hashes []common.Hash
13771377
numbers []uint64
13781378
)
1379-
parent := bc.GetHeader(it.previous().Hash(), it.previous().NumberU64())
1379+
parent := it.previous()
13801380
for parent != nil && !bc.HasState(parent.Root) {
13811381
hashes = append(hashes, parent.Hash())
13821382
numbers = append(numbers, parent.Number.Uint64())

‎core/blockchain_insert.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,12 @@ func (it *insertIterator) next() (*types.Block, error) {
111111
return it.chain[it.index], it.validator.ValidateBody(it.chain[it.index])
112112
}
113113

114-
// previous returns the previous block was being processed, or nil
115-
func (it *insertIterator) previous() *types.Block {
114+
// previous returns the previous header that was being processed, or nil.
115+
func (it *insertIterator) previous() *types.Header {
116116
if it.index < 1 {
117117
return nil
118118
}
119-
return it.chain[it.index-1]
119+
return it.chain[it.index-1].Header()
120120
}
121121

122122
// first returns the first block in the it.

‎core/blockchain_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func testBlockChainImport(chain types.Blocks, blockchain *BlockChain) error {
149149
blockchain.reportBlock(block, receipts, err)
150150
return err
151151
}
152-
err = blockchain.validator.ValidateState(block, blockchain.GetBlockByHash(block.ParentHash()), statedb, receipts, usedGas)
152+
err = blockchain.validator.ValidateState(block, statedb, receipts, usedGas)
153153
if err != nil {
154154
blockchain.reportBlock(block, receipts, err)
155155
return err

‎core/types.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type Validator interface {
3232

3333
// ValidateState validates the given statedb and optionally the receipts and
3434
// gas used.
35-
ValidateState(block, parent *types.Block, state *state.StateDB, receipts types.Receipts, usedGas uint64) error
35+
ValidateState(block *types.Block, state *state.StateDB, receipts types.Receipts, usedGas uint64) error
3636
}
3737

3838
// Processor is an interface for processing blocks using a given initial state.

0 commit comments

Comments
 (0)