Skip to content

Commit

Permalink
Changed Difficulty to a single value instead of slice
Browse files Browse the repository at this point in the history
  • Loading branch information
gameofpointers committed Apr 11, 2023
1 parent 24fe690 commit ceea6da
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 104 deletions.
29 changes: 16 additions & 13 deletions consensus/blake3pow/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/dominant-strategies/go-quai/consensus/misc"
"github.com/dominant-strategies/go-quai/core/state"
"github.com/dominant-strategies/go-quai/core/types"
"github.com/dominant-strategies/go-quai/log"
"github.com/dominant-strategies/go-quai/params"
"github.com/dominant-strategies/go-quai/rlp"
"github.com/dominant-strategies/go-quai/trie"
Expand Down Expand Up @@ -244,14 +245,13 @@ func (blake3pow *Blake3pow) verifyHeader(chain consensus.ChainHeaderReader, head
if header.Time() < parent.Time() {
return errOlderBlockTime
}
// Make sure subordinate difficulty does not exceed local difficulty
if nodeCtx < common.HierarchyDepth-1 && header.Difficulty().Cmp(header.Difficulty(nodeCtx+1)) < 0 {
return errDifficultyCrossover
}
// Verify the block's difficulty based on its timestamp and parent's difficulty
expected := blake3pow.CalcDifficulty(chain, parent)
if expected.Cmp(header.Difficulty()) != 0 {
return fmt.Errorf("invalid difficulty: have %v, want %v", header.Difficulty(), expected)
// difficulty adjustment can only be checked in zone
if nodeCtx == common.ZONE_CTX {
expected := blake3pow.CalcDifficulty(chain, parent)
if expected.Cmp(header.Difficulty()) != 0 {
return fmt.Errorf("invalid difficulty: have %v, want %v", header.Difficulty(), expected)
}
}

order, err := header.CalcOrder()
Expand Down Expand Up @@ -328,6 +328,10 @@ func (blake3pow *Blake3pow) verifyHeader(chain consensus.ChainHeaderReader, head
func (blake3pow *Blake3pow) CalcDifficulty(chain consensus.ChainHeaderReader, parent *types.Header) *big.Int {
nodeCtx := common.NodeLocation.Context()

if nodeCtx != common.ZONE_CTX {
log.Error("Cannot CalcDifficulty for", "context", nodeCtx)
return nil
}
// https://github.com/ethereum/EIPs/issues/100.
// algorithm:
// diff = (parent_diff +
Expand Down Expand Up @@ -367,8 +371,8 @@ func (blake3pow *Blake3pow) CalcDifficulty(chain consensus.ChainHeaderReader, pa
x.Add(parent.Difficulty(), x)

// minimum difficulty can ever be (before exponential factor)
if x.Cmp(params.MinimumDifficulty[nodeCtx]) < 0 {
x.Set(params.MinimumDifficulty[nodeCtx])
if x.Cmp(params.MinimumDifficulty) < 0 {
x.Set(params.MinimumDifficulty)
}

return x
Expand Down Expand Up @@ -399,7 +403,7 @@ func (blake3pow *Blake3pow) verifySeal(chain consensus.ChainHeaderReader, header
return blake3pow.shared.verifySeal(chain, header, fulldag)
}
// Ensure that we have a valid difficulty for the block
if header.Difficulty(common.ZONE_CTX).Sign() <= 0 {
if header.Difficulty().Sign() <= 0 {
return errInvalidDifficulty
}
// Check for valid zone share and order matches context
Expand Down Expand Up @@ -453,7 +457,7 @@ type headerData struct {
ManifestHash []common.Hash
ReceiptHash []common.Hash
Bloom []types.Bloom
Difficulty []*big.Int
Difficulty *big.Int
Number []*big.Int
GasLimit []uint64
GasUsed []uint64
Expand All @@ -479,7 +483,7 @@ func (blake3pow *Blake3pow) SealHash(header *types.Header) (hash common.Hash) {
ManifestHash: make([]common.Hash, common.HierarchyDepth),
ReceiptHash: make([]common.Hash, common.HierarchyDepth),
Bloom: make([]types.Bloom, common.HierarchyDepth),
Difficulty: make([]*big.Int, common.HierarchyDepth),
Difficulty: header.Difficulty(),
Number: make([]*big.Int, common.HierarchyDepth),
GasLimit: make([]uint64, common.HierarchyDepth),
GasUsed: make([]uint64, common.HierarchyDepth),
Expand All @@ -499,7 +503,6 @@ func (blake3pow *Blake3pow) SealHash(header *types.Header) (hash common.Hash) {
hdata.ManifestHash[i] = header.ManifestHash(i)
hdata.ReceiptHash[i] = header.ReceiptHash(i)
hdata.Bloom[i] = header.Bloom(i)
hdata.Difficulty[i] = header.Difficulty(i)
hdata.Number[i] = header.Number(i)
hdata.GasLimit[i] = header.GasLimit(i)
hdata.GasUsed[i] = header.GasUsed(i)
Expand Down
2 changes: 1 addition & 1 deletion consensus/blake3pow/sealer.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (blake3pow *Blake3pow) Seal(header *types.Header, results chan<- *types.Hea
func (blake3pow *Blake3pow) mine(header *types.Header, id int, seed uint64, abort chan struct{}, found chan *types.Header) {
// Extract some data from the header
var (
target = new(big.Int).Div(big2e256, header.Difficulty(common.ZONE_CTX))
target = new(big.Int).Div(big2e256, header.Difficulty())
)
// Start generating random nonces until we abort or find a good one
var (
Expand Down
23 changes: 11 additions & 12 deletions core/gen_genesis.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 8 additions & 12 deletions core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type Genesis struct {
Timestamp uint64 `json:"timestamp"`
ExtraData []byte `json:"extraData"`
GasLimit []uint64 `json:"gasLimit" gencodec:"required"`
Difficulty []*big.Int `json:"difficulty" gencodec:"required"`
Difficulty *big.Int `json:"difficulty" gencodec:"required"`
Mixhash common.Hash `json:"mixHash"`
Coinbase []common.Address `json:"coinbase"`
Alloc GenesisAlloc `json:"alloc" gencodec:"required"`
Expand Down Expand Up @@ -288,20 +288,17 @@ func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
head.SetRoot(primeRoot, common.PRIME_CTX)
head.SetRoot(types.EmptyRootHash, common.REGION_CTX) // Not genesis allocs allowed
head.SetRoot(types.EmptyRootHash, common.ZONE_CTX) // Not genesis allocs allowed
head.SetDifficulty(g.Difficulty)
for i := 0; i < common.HierarchyDepth; i++ {
head.SetNumber(big.NewInt(0), i)
head.SetParentHash(common.Hash{}, i)
head.SetGasLimit(g.GasLimit[i], i)
head.SetGasUsed(0, i)
head.SetBaseFee(new(big.Int).SetUint64(params.InitialBaseFee), i)
head.SetDifficulty(g.Difficulty[i], i)
head.SetCoinbase(common.ZeroAddr, i)
if g.GasLimit[i] == 0 {
head.SetGasLimit(params.GenesisGasLimit, i)
}
if g.Difficulty[i] == nil {
head.SetDifficulty(params.GenesisDifficulty[i], i)
}
}

// If we are a prime node, commit the Prime state. If not, create a new
Expand All @@ -325,7 +322,6 @@ func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
// Commit writes the block and state of a genesis specification to the database.
// The block is committed as the canonical head block.
func (g *Genesis) Commit(db ethdb.Database) (*types.Block, error) {
nodeCtx := common.NodeLocation.Context()
block := g.ToBlock(db)
if block.Number().Sign() != 0 {
return nil, fmt.Errorf("can't commit genesis block with number > 0")
Expand All @@ -337,7 +333,7 @@ func (g *Genesis) Commit(db ethdb.Database) (*types.Block, error) {
if err := config.CheckConfigForkOrder(); err != nil {
return nil, err
}
rawdb.WriteTd(db, block.Hash(), block.NumberU64(), g.Difficulty[nodeCtx])
rawdb.WriteTd(db, block.Hash(), block.NumberU64(), g.Difficulty)
rawdb.WriteBlock(db, block)
rawdb.WriteReceipts(db, block.Hash(), block.NumberU64(), nil)
rawdb.WriteCanonicalHash(db, block.Hash(), block.NumberU64())
Expand Down Expand Up @@ -379,7 +375,7 @@ func DefaultColosseumGenesisBlock() *Genesis {
Nonce: 66,
ExtraData: hexutil.MustDecode("0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa"),
GasLimit: []uint64{1000000, 1000000, 1000000},
Difficulty: []*big.Int{big.NewInt(32048576), big.NewInt(8048576), big.NewInt(2048576)},
Difficulty: big.NewInt(2048576),
Alloc: decodePrealloc(colosseumAllocData),
}
}
Expand All @@ -391,7 +387,7 @@ func DefaultGardenGenesisBlock() *Genesis {
Nonce: 67,
ExtraData: hexutil.MustDecode("0x3535353535353535353535353535353535353535353535353535353535353535"),
GasLimit: []uint64{1000000, 1000000, 1000000},
Difficulty: []*big.Int{big.NewInt(32048576), big.NewInt(8048576), big.NewInt(441092)},
Difficulty: big.NewInt(441092),
Alloc: decodePrealloc(gardenAllocData),
}
}
Expand All @@ -403,7 +399,7 @@ func DefaultOrchardGenesisBlock() *Genesis {
Nonce: 68,
ExtraData: hexutil.MustDecode("0x3535353535353535353535353535353535353535353535353535353535353535"),
GasLimit: []uint64{1000000, 1000000, 1000000},
Difficulty: []*big.Int{big.NewInt(32048576), big.NewInt(8048576), big.NewInt(2048576)},
Difficulty: big.NewInt(2048576),
Alloc: decodePrealloc(orchardAllocData),
}
}
Expand All @@ -415,7 +411,7 @@ func DefaultLocalGenesisBlock() *Genesis {
Nonce: 67,
ExtraData: hexutil.MustDecode("0x3535353535353535353535353535353535353535353535353535353535353535"),
GasLimit: []uint64{1000000, 1000000, 1000000},
Difficulty: []*big.Int{big.NewInt(1600000), big.NewInt(800000), big.NewInt(300000)},
Difficulty: big.NewInt(300000),
Alloc: decodePrealloc(localAllocData),
}
}
Expand All @@ -430,7 +426,7 @@ func DeveloperGenesisBlock(period uint64, faucet common.Address) *Genesis {
ExtraData: append(append(make([]byte, 32), faucet.Bytes()[:]...), make([]byte, crypto.SignatureLength)...),
GasLimit: []uint64{0x47b760, 0x47b760, 0x47b760},
BaseFee: []*big.Int{big.NewInt(params.InitialBaseFee), big.NewInt(params.InitialBaseFee), big.NewInt(params.InitialBaseFee)},
Difficulty: []*big.Int{big.NewInt(1), big.NewInt(1), big.NewInt(1)},
Difficulty: big.NewInt(1),
Alloc: map[common.Address]GenesisAccount{
common.BytesToAddress([]byte{1}): {Balance: big.NewInt(1)}, // ECRecover
common.BytesToAddress([]byte{2}): {Balance: big.NewInt(1)}, // SHA256
Expand Down
8 changes: 6 additions & 2 deletions core/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ func (sl *Slice) computePendingHeader(localPendingHeaderWithTermini types.Pendin
// updatePhCacheFromDom combines the recieved pending header with the pending header stored locally at a given terminus for specified context
func (sl *Slice) updatePhCacheFromDom(pendingHeader types.PendingHeader, terminiIndex int, indices []int) error {

nodeCtx := common.NodeLocation.Context()
hash := pendingHeader.Termini[terminiIndex]
localPendingHeader, exists := sl.phCache[hash]

Expand All @@ -560,7 +561,10 @@ func (sl *Slice) updatePhCacheFromDom(pendingHeader types.PendingHeader, termini
for _, i := range indices {
combinedPendingHeader = sl.combinePendingHeader(pendingHeader.Header, combinedPendingHeader, i)
}
combinedPendingHeader.SetLocation(common.NodeLocation)
if nodeCtx == common.ZONE_CTX {
combinedPendingHeader.SetDifficulty(localPendingHeader.Header.Difficulty())
combinedPendingHeader.SetLocation(common.NodeLocation)
}

sl.writeToPhCacheAndPickPhHead(types.PendingHeader{Header: combinedPendingHeader, Termini: localPendingHeader.Termini})

Expand Down Expand Up @@ -742,11 +746,11 @@ func (sl *Slice) combinePendingHeader(header *types.Header, slPendingHeader *typ
combinedPendingHeader.SetManifestHash(header.ManifestHash(index), index)
combinedPendingHeader.SetReceiptHash(header.ReceiptHash(index), index)
combinedPendingHeader.SetRoot(header.Root(index), index)
combinedPendingHeader.SetDifficulty(header.Difficulty(index), index)
combinedPendingHeader.SetParentEntropy(header.ParentEntropy(index), index)
combinedPendingHeader.SetParentDeltaS(header.ParentDeltaS(index), index)
combinedPendingHeader.SetCoinbase(header.Coinbase(index), index)
combinedPendingHeader.SetBloom(header.Bloom(index), index)
combinedPendingHeader.SetDifficulty(header.Difficulty())

return combinedPendingHeader
}
Expand Down
Loading

0 comments on commit ceea6da

Please sign in to comment.