Skip to content

Commit

Permalink
consensu/parlia: optimize prepareTurnTerm and verifyTurnTerm
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanBSC committed Jun 6, 2024
1 parent 7cfe893 commit 7fe13c8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 41 deletions.
35 changes: 33 additions & 2 deletions consensus/parlia/bohrFork.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package parlia

import (
"context"
"errors"
"math/big"
mrand "math/rand"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/core/systemcontracts"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/internal/ethapi"
Expand All @@ -16,9 +18,38 @@ import (
"github.com/ethereum/go-ethereum/rpc"
)

func (p *Parlia) getTurnTerm(header *types.Header) (turnTerm *big.Int, err error) {
func (p *Parlia) getTurnTerm(chain consensus.ChainHeaderReader, header *types.Header) (*uint8, error) {
if header.Number.Uint64()%p.config.Epoch != 0 ||
!p.chainConfig.IsBohr(header.Number, header.Time) {
return nil, nil
}

parent := chain.GetHeaderByHash(header.ParentHash)
if parent == nil {
return nil, errors.New("parent not found")
}

var turnTerm uint8
if p.chainConfig.IsBohr(parent.Number, parent.Time) {
turnTermFromContract, err := p.getTurnTermFromContract(parent)
if err != nil {
return nil, err
}
if turnTermFromContract == nil {
return nil, errors.New("unexpected error when getTurnTermFromContract")
}
turnTerm = uint8(turnTermFromContract.Int64())
} else {
turnTerm = uint8(defaultTurnTerm)
}
log.Debug("getTurnTerm", "turnTerm", turnTerm)

return &turnTerm, nil
}

func (p *Parlia) getTurnTermFromContract(header *types.Header) (turnTerm *big.Int, err error) {
if params.UseRandTurnTerm {
return p.getRandTurnTerm(header)
return p.getRandTurnTerm(header) // used as a mock to get turnTerm from the contract
}

ctx, cancel := context.WithCancel(context.Background())
Expand Down
50 changes: 11 additions & 39 deletions consensus/parlia/parlia.go
Original file line number Diff line number Diff line change
Expand Up @@ -903,29 +903,13 @@ func (p *Parlia) prepareValidators(header *types.Header) error {
}

func (p *Parlia) prepareTurnTerm(chain consensus.ChainHeaderReader, header *types.Header) error {
if header.Number.Uint64()%p.config.Epoch != 0 ||
!p.chainConfig.IsBohr(header.Number, header.Time) {
return nil
turnTerm, err := p.getTurnTerm(chain, header)
if err != nil {
return err
}

parent := chain.GetHeaderByHash(header.ParentHash)
if parent == nil {
return errors.New("parent not found")
}

if p.chainConfig.IsBohr(parent.Number, parent.Time) {
turnTerm, err := p.getTurnTerm(parent)
if err != nil {
return err
}
if turnTerm == nil {
return errors.New("unexpected error when getTurnTerm")
}
header.Extra = append(header.Extra, byte(int(turnTerm.Int64())))
log.Debug("prepareTurnTerm", "turnTerm", turnTerm.Int64())
} else {
log.Debug("prepareTurnTerm", "turnTerm", "defaultTurnTerm")
header.Extra = append(header.Extra, byte(int(defaultTurnTerm)))
if turnTerm != nil {
header.Extra = append(header.Extra, *turnTerm)
}

return nil
Expand Down Expand Up @@ -1126,25 +1110,13 @@ func (p *Parlia) verifyTurnTerm(chain consensus.ChainHeaderReader, header *types
return err
}
if turnTermFromHeader != nil {
parent := chain.GetHeaderByHash(header.ParentHash)
if parent == nil {
return errors.New("parent not found")
turnTerm, err := p.getTurnTerm(chain, header)
if err != nil {
return err
}

if p.chainConfig.IsBohr(parent.Number, parent.Time) {
turnTermFromContract, err := p.getTurnTerm(parent)
if err != nil {
return err
}
if turnTermFromContract != nil && uint8(turnTermFromContract.Int64()) == *turnTermFromHeader {
log.Debug("verifyTurnTerm", "turnTerm", turnTermFromContract.Int64())
return nil
}
} else {
if uint8(defaultTurnTerm) == *turnTermFromHeader {
log.Debug("verifyTurnTerm", "turnTerm", "defaultTurnTerm")
return nil
}
if turnTerm != nil && *turnTerm == *turnTermFromHeader {
log.Debug("verifyTurnTerm", "turnTerm", *turnTerm)
return nil
}
}

Expand Down

0 comments on commit 7fe13c8

Please sign in to comment.