Skip to content

Commit

Permalink
Safeguard efficiency score calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
Djadih committed Oct 14, 2024
1 parent 2a7b069 commit b55fcac
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 8 deletions.
5 changes: 4 additions & 1 deletion consensus/blake3pow/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,10 @@ func (blake3pow *Blake3pow) verifyHeader(chain consensus.ChainHeaderReader, head
return fmt.Errorf("invalid threshold count: have %v, want %v", header.ThresholdCount(), 0)
}
} else {
expectedEfficiencyScore := chain.ComputeEfficiencyScore(parent)
expectedEfficiencyScore, err := chain.ComputeEfficiencyScore(parent)
if err != nil {
return err
}
if header.EfficiencyScore() != expectedEfficiencyScore {
return fmt.Errorf("invalid efficiency score: have %v, want %v", header.EfficiencyScore(), expectedEfficiencyScore)
}
Expand Down
2 changes: 1 addition & 1 deletion consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ type ChainHeaderReader interface {
ProcessingState() bool

// ComputeEfficiencyScore returns the efficiency score computed at each prime block
ComputeEfficiencyScore(header *types.WorkObject) uint16
ComputeEfficiencyScore(header *types.WorkObject) (uint16, error)

// ComputeExpansionNumber returns the expansion number of the block
ComputeExpansionNumber(parent *types.WorkObject) (uint8, error)
Expand Down
5 changes: 4 additions & 1 deletion consensus/progpow/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,10 @@ func (progpow *Progpow) verifyHeader(chain consensus.ChainHeaderReader, header,
return fmt.Errorf("invalid threshold count: have %v, want %v", header.ThresholdCount(), 0)
}
} else {
expectedEfficiencyScore := chain.ComputeEfficiencyScore(parent)
expectedEfficiencyScore, err := chain.ComputeEfficiencyScore(parent)
if err != nil {
return err
}
if header.EfficiencyScore() != expectedEfficiencyScore {
return fmt.Errorf("invalid efficiency score: have %v, want %v", header.EfficiencyScore(), expectedEfficiencyScore)
}
Expand Down
2 changes: 1 addition & 1 deletion core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,7 @@ func (c *Core) SubscribeChainSideEvent(ch chan<- ChainSideEvent) event.Subscript
// ComputeEfficiencyScore computes the efficiency score for the given prime
// block This data is is only valid if called from Prime context, otherwise
// there is no guarantee for this data to be accurate
func (c *Core) ComputeEfficiencyScore(header *types.WorkObject) uint16 {
func (c *Core) ComputeEfficiencyScore(header *types.WorkObject) (uint16, error) {
return c.sl.hc.ComputeEfficiencyScore(header)
}

Expand Down
12 changes: 10 additions & 2 deletions core/headerchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ const (
c_calcOrderCacheLimit = 10000
)

var (
errInvalidEfficiencyScore = errors.New("unable to compute efficiency score")
)

type calcOrderResponse struct {
intrinsicEntropy *big.Int
order int
Expand Down Expand Up @@ -1225,17 +1229,21 @@ func (hc *HeaderChain) ComputeExpansionNumber(parent *types.WorkObject) (uint8,
}

// ComputeEfficiencyScore calculates the efficiency score for the given header
func (hc *HeaderChain) ComputeEfficiencyScore(parent *types.WorkObject) uint16 {
func (hc *HeaderChain) ComputeEfficiencyScore(parent *types.WorkObject) (uint16, error) {
deltaEntropy := new(big.Int).Add(parent.ParentDeltaEntropy(common.REGION_CTX), parent.ParentDeltaEntropy(common.ZONE_CTX))
uncledDeltaEntropy := new(big.Int).Add(parent.ParentUncledDeltaEntropy(common.REGION_CTX), parent.ParentUncledDeltaEntropy(common.ZONE_CTX))

// Take the ratio of deltaEntropy to the uncledDeltaEntropy in percentage
efficiencyScore := uncledDeltaEntropy.Mul(uncledDeltaEntropy, big.NewInt(100))
if deltaEntropy.Cmp(common.Big0) == 0 {
hc.logger.Error(errInvalidEfficiencyScore)
return 0, errInvalidEfficiencyScore
}
efficiencyScore.Div(efficiencyScore, deltaEntropy)

// Calculate the exponential moving average
ewma := (uint16(efficiencyScore.Uint64()) + parent.EfficiencyScore()*params.TREE_EXPANSION_FILTER_ALPHA) / 10
return ewma
return ewma, nil
}

// UpdateEtxEligibleSlices returns the updated etx eligible slices field
Expand Down
5 changes: 4 additions & 1 deletion core/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -1461,7 +1461,10 @@ func (w *worker) prepareWork(genParams *generateParams, wo *types.WorkObject) (*
newWo.Header().SetThresholdCount(0)
} else {
// compute the efficiency score at each prime block
efficiencyScore := w.hc.ComputeEfficiencyScore(parent)
efficiencyScore, err := w.hc.ComputeEfficiencyScore(parent)
if err != nil {
return nil, err
}
newWo.Header().SetEfficiencyScore(efficiencyScore)

// If the threshold count is zero we have not started considering for the
Expand Down
2 changes: 1 addition & 1 deletion quai/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ func (b *QuaiAPIBackend) IsBlockHashABadHash(hash common.Hash) bool {
return b.quai.core.IsBlockHashABadHash(hash)
}

func (b *QuaiAPIBackend) ComputeEfficiencyScore(header *types.WorkObject) uint16 {
func (b *QuaiAPIBackend) ComputeEfficiencyScore(header *types.WorkObject) (uint16, error) {
return b.quai.core.ComputeEfficiencyScore(header)
}

Expand Down

0 comments on commit b55fcac

Please sign in to comment.