Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
[Blockchain/IBFT] Update snapshot after processBlock ends (#482)
Browse files Browse the repository at this point in the history
* implement ProcessHeaders as interface method

* move ProcessHeaders out of VerifyHeaders
  • Loading branch information
dbrajovic authored Apr 27, 2022
1 parent 538ea6e commit 805f9ef
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 5 deletions.
6 changes: 6 additions & 0 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type gasPriceAverage struct {

type Verifier interface {
VerifyHeader(parent, header *types.Header) error
ProcessHeaders(headers []*types.Header) error
GetBlockCreator(header *types.Header) (types.Address, error)
PreStateCommit(header *types.Header, txn *state.Transition) error
}
Expand Down Expand Up @@ -700,6 +701,11 @@ func (b *Blockchain) WriteBlock(block *types.Block) error {
return err
}

// update snapshot
if err := b.consensus.ProcessHeaders([]*types.Header{header}); err != nil {
return err
}

b.dispatchEvent(evnt)

// Update the average gas price
Expand Down
4 changes: 4 additions & 0 deletions blockchain/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ func (m *MockVerifier) VerifyHeader(parent, header *types.Header) error {
return nil
}

func (m *MockVerifier) ProcessHeaders(headers []*types.Header) error {
return nil
}

func (m *MockVerifier) GetBlockCreator(header *types.Header) (types.Address, error) {
return header.Miner, nil
}
Expand Down
3 changes: 3 additions & 0 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ type Consensus interface {
// VerifyHeader verifies the header is correct
VerifyHeader(parent, header *types.Header) error

// ProcessHeaders updates the snapshot based on the verified headers
ProcessHeaders(headers []*types.Header) error

// GetBlockCreator retrieves the block creator (or signer) given the block header
GetBlockCreator(header *types.Header) (types.Address, error)

Expand Down
4 changes: 4 additions & 0 deletions consensus/dev/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ func (d *Dev) VerifyHeader(parent *types.Header, header *types.Header) error {
return nil
}

func (d *Dev) ProcessHeaders(headers []*types.Header) error {
return nil
}

func (d *Dev) GetBlockCreator(header *types.Header) (types.Address, error) {
return header.Miner, nil
}
Expand Down
4 changes: 4 additions & 0 deletions consensus/dummy/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func (d *Dummy) VerifyHeader(parent *types.Header, header *types.Header) error {
return nil
}

func (d *Dummy) ProcessHeaders(headers []*types.Header) error {
return nil
}

func (d *Dummy) GetBlockCreator(header *types.Header) (types.Address, error) {
return header.Miner, nil
}
Expand Down
10 changes: 5 additions & 5 deletions consensus/ibft/ibft.go
Original file line number Diff line number Diff line change
Expand Up @@ -1255,14 +1255,14 @@ func (i *Ibft) VerifyHeader(parent, header *types.Header) error {
return err
}

// process the new block in order to update the snapshot
if err := i.processHeaders([]*types.Header{header}); err != nil {
return err
}

return nil
}

// ProcessHeaders updates the snapshot based on previously verified headers
func (i *Ibft) ProcessHeaders(headers []*types.Header) error {
return i.processHeaders(headers)
}

// GetBlockCreator retrieves the block signer from the extra data field
func (i *Ibft) GetBlockCreator(header *types.Header) (types.Address, error) {
return ecrecoverFromHeader(header)
Expand Down

0 comments on commit 805f9ef

Please sign in to comment.