Skip to content

Commit

Permalink
Fix the Calcorder based on the starting expansion number
Browse files Browse the repository at this point in the history
  • Loading branch information
gameofpointers authored and kiltsonfire committed May 21, 2024
1 parent 6e47250 commit 51726ea
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
9 changes: 7 additions & 2 deletions consensus/blake3pow/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,9 @@ func (blake3pow *Blake3pow) verifyHeader(chain consensus.ChainHeaderReader, head
if header.ThresholdCount() != 0 {
return fmt.Errorf("invalid threshold count: have %v, want %v", header.ThresholdCount(), 0)
}
if header.ExpansionNumber() != 0 {
return fmt.Errorf("invalid expansion number: have %v, want %v", header.ExpansionNumber(), 0)
genesisHeader := chain.GetHeaderByNumber(0)
if header.ExpansionNumber() != genesisHeader.ExpansionNumber() {
return fmt.Errorf("invalid expansion number: have %v, want %v", header.ExpansionNumber(), genesisHeader.ExpansionNumber())
}
} else {
expectedEfficiencyScore := chain.ComputeEfficiencyScore(parent)
Expand Down Expand Up @@ -503,6 +504,10 @@ func (blake3pow *Blake3pow) CalcDifficulty(chain consensus.ChainHeaderReader, pa
// Base case: expansion number is 0 and the parent is the actual genesis block
return parent.Difficulty()
}
genesisBlock := chain.GetHeaderByHash(parent.Hash())
if genesisBlock.ExpansionNumber() > 0 && parent.Hash() == chain.Config().DefaultGenesisHash {
return parent.Difficulty()
}
genesis := chain.GetHeaderByHash(parent.Hash())
genesisTotalLogS := blake3pow.TotalLogS(chain, genesis)
if genesisTotalLogS.Cmp(genesis.ParentEntropy(common.PRIME_CTX)) < 0 { // prevent negative difficulty
Expand Down
9 changes: 7 additions & 2 deletions consensus/progpow/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,9 @@ func (progpow *Progpow) verifyHeader(chain consensus.ChainHeaderReader, header,
if header.ThresholdCount() != 0 {
return fmt.Errorf("invalid threshold count: have %v, want %v", header.ThresholdCount(), 0)
}
if header.ExpansionNumber() != 0 {
return fmt.Errorf("invalid expansion number: have %v, want %v", header.ExpansionNumber(), 0)
genesisHeader := chain.GetHeaderByNumber(0)
if header.ExpansionNumber() != genesisHeader.ExpansionNumber() {
return fmt.Errorf("invalid expansion number: have %v, want %v", header.ExpansionNumber(), genesisHeader.ExpansionNumber())
}
} else {
expectedEfficiencyScore := chain.ComputeEfficiencyScore(parent)
Expand Down Expand Up @@ -501,6 +502,10 @@ func (progpow *Progpow) CalcDifficulty(chain consensus.ChainHeaderReader, parent
// Base case: expansion number is 0 and the parent is the actual genesis block
return parent.Difficulty()
}
genesisBlock := chain.GetHeaderByHash(parent.Hash())
if genesisBlock.ExpansionNumber() > 0 && parent.Hash() == chain.Config().DefaultGenesisHash {
return parent.Difficulty()
}
genesis := chain.GetHeaderByHash(parent.Hash())
genesisTotalLogS := progpow.TotalLogS(chain, genesis)
if genesisTotalLogS.Cmp(genesis.ParentEntropy(common.PRIME_CTX)) < 0 { // prevent negative difficulty
Expand Down
1 change: 1 addition & 0 deletions core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ func (g *Genesis) ToBlock(startingExpansionNumber uint64) *types.WorkObject {
head.Header().SetExtra(g.ExtraData)
head.Header().SetGasLimit(g.GasLimit)
head.Header().SetGasUsed(0)
head.Header().SetExpansionNumber(uint8(startingExpansionNumber))
if startingExpansionNumber > 0 {
// Fill each byte with 0xFF to set all bits to 1
var etxEligibleSlices common.Hash
Expand Down
6 changes: 5 additions & 1 deletion core/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,11 @@ func (w *worker) prepareWork(genParams *generateParams, wo *types.WorkObject) (*
if parent.NumberU64(common.PRIME_CTX) == 0 {
newWo.Header().SetEfficiencyScore(0)
newWo.Header().SetThresholdCount(0)
newWo.Header().SetExpansionNumber(0)
// get the genesis expansion number, since in the normal expansion
// scenario this is only triggered in the case of [0, 0] we can just read
// the genesis block by number 0
genesisHeader := w.hc.GetBlockByNumber(0)
newWo.Header().SetExpansionNumber(genesisHeader.ExpansionNumber())
} else {
// compute the efficiency score at each prime block
efficiencyScore := w.hc.ComputeEfficiencyScore(parent)
Expand Down

0 comments on commit 51726ea

Please sign in to comment.