diff --git a/consensus/blake3pow/consensus.go b/consensus/blake3pow/consensus.go index a70cbfea1b..a04e0ae4ea 100644 --- a/consensus/blake3pow/consensus.go +++ b/consensus/blake3pow/consensus.go @@ -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" @@ -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() @@ -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 + @@ -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 @@ -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 @@ -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 @@ -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), @@ -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) diff --git a/consensus/blake3pow/sealer.go b/consensus/blake3pow/sealer.go index 232977709b..96b1d959e2 100644 --- a/consensus/blake3pow/sealer.go +++ b/consensus/blake3pow/sealer.go @@ -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 ( diff --git a/core/gen_genesis.go b/core/gen_genesis.go index bf63ccbea3..bf8bc263d9 100644 --- a/core/gen_genesis.go +++ b/core/gen_genesis.go @@ -22,15 +22,15 @@ func (g Genesis) MarshalJSON() ([]byte, error) { Nonce math.HexOrDecimal64 `json:"nonce"` Timestamp math.HexOrDecimal64 `json:"timestamp"` ExtraData hexutil.Bytes `json:"extraData"` - GasLimit []math.HexOrDecimal64 `json:"gasLimit" gencodec:"required"` - Difficulty []*math.HexOrDecimal256 `json:"difficulty" gencodec:"required"` + GasLimit []math.HexOrDecimal64 `json:"gasLimit" gencodec:"required"` + Difficulty *math.HexOrDecimal256 `json:"difficulty" gencodec:"required"` Mixhash common.Hash `json:"mixHash"` - Coinbase []common.Address `json:"coinbase"` + Coinbase []common.Address `json:"coinbase"` Alloc map[common.UnprefixedAddress]GenesisAccount `json:"alloc" gencodec:"required"` - Number []math.HexOrDecimal64 `json:"number"` - GasUsed []math.HexOrDecimal64 `json:"gasUsed"` - ParentHash []common.Hash `json:"parentHash"` - BaseFee []*math.HexOrDecimal256 `json:"baseFeePerGas"` + Number []math.HexOrDecimal64 `json:"number"` + GasUsed []math.HexOrDecimal64 `json:"gasUsed"` + ParentHash []common.Hash `json:"parentHash"` + BaseFee []*math.HexOrDecimal256 `json:"baseFeePerGas"` } var enc Genesis enc.Config = g.Config @@ -39,7 +39,6 @@ func (g Genesis) MarshalJSON() ([]byte, error) { enc.ExtraData = g.ExtraData enc.Mixhash = g.Mixhash enc.GasLimit = make([]math.HexOrDecimal64, common.HierarchyDepth) - enc.Difficulty = make([]*math.HexOrDecimal256, common.HierarchyDepth) enc.Coinbase = make([]common.Address, common.HierarchyDepth) enc.Number = make([]math.HexOrDecimal64, common.HierarchyDepth) enc.GasUsed = make([]math.HexOrDecimal64, common.HierarchyDepth) @@ -57,13 +56,13 @@ func (g Genesis) MarshalJSON() ([]byte, error) { } for i := 0; i < common.HierarchyDepth; i++ { enc.GasLimit[i] = math.HexOrDecimal64(g.GasLimit[i]) - enc.Difficulty[i] = (*math.HexOrDecimal256)(g.Difficulty[i]) enc.Coinbase[i] = g.Coinbase[i] enc.Number[i] = math.HexOrDecimal64(g.Number[i]) enc.GasUsed[i] = math.HexOrDecimal64(g.GasUsed[i]) enc.ParentHash[i] = g.ParentHash[i] enc.BaseFee[i] = (*math.HexOrDecimal256)(g.BaseFee[i]) } + enc.Difficulty = (*math.HexOrDecimal256)(g.Difficulty) return json.Marshal(&enc) } @@ -75,7 +74,7 @@ func (g *Genesis) UnmarshalJSON(input []byte) error { Timestamp *math.HexOrDecimal64 `json:"timestamp"` ExtraData *hexutil.Bytes `json:"extraData"` GasLimit []*math.HexOrDecimal64 `json:"gasLimit" gencodec:"required"` - Difficulty []*math.HexOrDecimal256 `json:"difficulty" gencodec:"required"` + Difficulty *math.HexOrDecimal256 `json:"difficulty" gencodec:"required"` Mixhash *common.Hash `json:"mixHash"` Coinbase []*common.Address `json:"coinbase"` Alloc map[common.UnprefixedAddress]GenesisAccount `json:"alloc" gencodec:"required"` @@ -117,10 +116,10 @@ func (g *Genesis) UnmarshalJSON(input []byte) error { return errors.New("missing required field 'gasLimit' for Genesis") } g.GasLimit[i] = uint64(*dec.GasLimit[i]) - if dec.Difficulty[i] == nil { + if dec.Difficulty == nil { return errors.New("missing required field 'difficulty' for Genesis") } - g.Difficulty[i] = (*big.Int)(dec.Difficulty[i]) + g.Difficulty = (*big.Int)(dec.Difficulty) if dec.Coinbase[i] != nil { g.Coinbase[i] = *dec.Coinbase[i] } diff --git a/core/genesis.go b/core/genesis.go index 33f119caec..008cef1238 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -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"` @@ -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 @@ -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") @@ -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()) @@ -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), } } @@ -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), } } @@ -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), } } @@ -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), } } @@ -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 diff --git a/core/slice.go b/core/slice.go index 256663e7c0..180efe3465 100644 --- a/core/slice.go +++ b/core/slice.go @@ -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] @@ -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}) @@ -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 } diff --git a/core/types/block.go b/core/types/block.go index 0ce285aec1..a88b17fe5d 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -88,7 +88,7 @@ type Header struct { manifestHash []common.Hash `json:"manifestHash" gencodec:"required"` receiptHash []common.Hash `json:"receiptsRoot" gencodec:"required"` bloom []Bloom `json:"logsBloom" gencodec:"required"` - difficulty []*big.Int `json:"difficulty" gencodec:"required"` + difficulty *big.Int `json:"difficulty" gencodec:"required"` parentEntropy []*big.Int `json:"parentEntropy" gencodec:"required"` parentDeltaS []*big.Int `json:"parentDeltaS" gencodec:"required"` number []*big.Int `json:"number" gencodec:"required"` @@ -103,7 +103,7 @@ type Header struct { // field type overrides for gencodec type headerMarshaling struct { - Difficulty []*hexutil.Big + Difficulty *hexutil.Big Number []*hexutil.Big GasLimit []hexutil.Uint64 GasUsed []hexutil.Uint64 @@ -127,7 +127,7 @@ type extheader struct { ManifestHash []common.Hash ReceiptHash []common.Hash Bloom []Bloom - Difficulty []*big.Int + Difficulty *big.Int ParentEntropy []*big.Int ParentDeltaS []*big.Int Number []*big.Int @@ -153,13 +153,13 @@ func EmptyHeader() *Header { h.manifestHash = make([]common.Hash, common.HierarchyDepth) h.receiptHash = make([]common.Hash, common.HierarchyDepth) h.bloom = make([]Bloom, common.HierarchyDepth) - h.difficulty = make([]*big.Int, common.HierarchyDepth) h.parentEntropy = make([]*big.Int, common.HierarchyDepth) h.parentDeltaS = make([]*big.Int, common.HierarchyDepth) h.number = make([]*big.Int, common.HierarchyDepth) h.gasLimit = make([]uint64, common.HierarchyDepth) h.gasUsed = make([]uint64, common.HierarchyDepth) h.baseFee = make([]*big.Int, common.HierarchyDepth) + h.difficulty = big.NewInt(0) for i := 0; i < common.HierarchyDepth; i++ { h.root[i] = EmptyRootHash @@ -169,7 +169,6 @@ func EmptyHeader() *Header { h.etxRollupHash[i] = EmptyRootHash h.manifestHash[i] = EmptyRootHash h.uncleHash[i] = EmptyUncleHash - h.difficulty[i] = big.NewInt(0) h.parentEntropy[i] = big.NewInt(0) h.parentDeltaS[i] = big.NewInt(0) h.number[i] = big.NewInt(0) @@ -324,12 +323,8 @@ func (h *Header) Bloom(args ...int) Bloom { } return h.bloom[nodeCtx] } -func (h *Header) Difficulty(args ...int) *big.Int { - nodeCtx := common.NodeLocation.Context() - if len(args) > 0 { - nodeCtx = args[0] - } - return h.difficulty[nodeCtx] +func (h *Header) Difficulty() *big.Int { + return h.difficulty } func (h *Header) Number(args ...int) *big.Int { nodeCtx := common.NodeLocation.Context() @@ -459,12 +454,8 @@ func (h *Header) SetBloom(val Bloom, args ...int) { } h.bloom[nodeCtx] = val } -func (h *Header) SetDifficulty(val *big.Int, args ...int) { - nodeCtx := common.NodeLocation.Context() - if len(args) > 0 { - nodeCtx = args[0] - } - h.difficulty[nodeCtx] = new(big.Int).Set(val) +func (h *Header) SetDifficulty(val *big.Int) { + h.difficulty = new(big.Int).Set(val) } func (h *Header) SetNumber(val *big.Int, args ...int) { nodeCtx := common.NodeLocation.Context() @@ -515,7 +506,6 @@ func (h *Header) EtxRollupHashArray() []common.Hash { return h.etxRollupHash } func (h *Header) ManifestHashArray() []common.Hash { return h.manifestHash } func (h *Header) ReceiptHashArray() []common.Hash { return h.receiptHash } func (h *Header) BloomArray() []Bloom { return h.bloom } -func (h *Header) DifficultyArray() []*big.Int { return h.difficulty } func (h *Header) NumberArray() []*big.Int { return h.number } func (h *Header) GasLimitArray() []uint64 { return h.gasLimit } func (h *Header) GasUsedArray() []uint64 { return h.gasUsed } @@ -543,7 +533,7 @@ var headerSize = common.StorageSize(reflect.TypeOf(Header{}).Size()) // Size returns the approximate memory used by all internal contents. It is used // to approximate and limit the memory consumption of various caches. func (h *Header) Size() common.StorageSize { - return headerSize + common.StorageSize(len(h.extra)+(totalBitLen(h.difficulty)+totalBitLen(h.number))/8) + return headerSize + common.StorageSize(len(h.extra)+(h.difficulty.BitLen()+totalBitLen(h.number))/8) } // SanityCheck checks a few basic things -- these checks are way beyond what @@ -578,7 +568,7 @@ func (h *Header) SanityCheck() error { if h.bloom == nil || len(h.bloom) != common.HierarchyDepth { return fmt.Errorf("field cannot be `nil`: bloom") } - if h.difficulty == nil || len(h.difficulty) != common.HierarchyDepth { + if h.difficulty == nil { return fmt.Errorf("field cannot be `nil`: difficulty") } if h.number == nil || len(h.number) != common.HierarchyDepth { @@ -612,17 +602,15 @@ func (h *Header) SanityCheck() error { if h.number[i] != nil && !h.number[i].IsUint64() { return fmt.Errorf("too large block number[%d]: bitlen %d", i, h.number[i].BitLen()) } - if h.difficulty[i] != nil { - if diffLen := h.difficulty[i].BitLen(); diffLen > 80 { - return fmt.Errorf("too large block difficulty[%d]: bitlen %d", i, diffLen) - } - } if h.baseFee[i] != nil { if bfLen := h.baseFee[i].BitLen(); bfLen > 256 { return fmt.Errorf("too large base fee: bitlen %d", bfLen) } } } + if diffLen := h.difficulty.BitLen(); diffLen > 80 { + return fmt.Errorf("too large block difficulty: bitlen %d", diffLen) + } if eLen := len(h.extra); eLen > 100*1024 { return fmt.Errorf("too large block extradata: size %d", eLen) } @@ -896,7 +884,6 @@ func CopyHeader(h *Header) *Header { cpy.manifestHash = make([]common.Hash, common.HierarchyDepth) cpy.receiptHash = make([]common.Hash, common.HierarchyDepth) cpy.bloom = make([]Bloom, common.HierarchyDepth) - cpy.difficulty = make([]*big.Int, common.HierarchyDepth) cpy.parentEntropy = make([]*big.Int, common.HierarchyDepth) cpy.parentDeltaS = make([]*big.Int, common.HierarchyDepth) cpy.number = make([]*big.Int, common.HierarchyDepth) @@ -914,7 +901,6 @@ func CopyHeader(h *Header) *Header { cpy.SetManifestHash(h.ManifestHash(i), i) cpy.SetReceiptHash(h.ReceiptHash(i), i) cpy.SetBloom(h.Bloom(i), i) - cpy.SetDifficulty(h.Difficulty(i), i) cpy.SetParentEntropy(h.ParentEntropy(i), i) cpy.SetParentDeltaS(h.ParentDeltaS(i), i) cpy.SetNumber(h.Number(i), i) @@ -926,6 +912,7 @@ func CopyHeader(h *Header) *Header { cpy.extra = make([]byte, len(h.extra)) copy(cpy.extra, h.extra) } + cpy.SetDifficulty(h.Difficulty()) cpy.SetLocation(h.location) cpy.SetTime(h.time) cpy.SetNonce(h.nonce) @@ -966,7 +953,7 @@ func (b *Block) EtxRollupHash(args ...int) common.Hash { return b.header.EtxRoll func (b *Block) ManifestHash(args ...int) common.Hash { return b.header.ManifestHash(args...) } func (b *Block) ReceiptHash(args ...int) common.Hash { return b.header.ReceiptHash(args...) } func (b *Block) Bloom(args ...int) Bloom { return b.header.Bloom(args...) } -func (b *Block) Difficulty(args ...int) *big.Int { return b.header.Difficulty(args...) } +func (b *Block) Difficulty(args ...int) *big.Int { return b.header.Difficulty() } func (b *Block) ParentEntropy(args ...int) *big.Int { return b.header.ParentEntropy(args...) } func (b *Block) ParentDeltaS(args ...int) *big.Int { return b.header.ParentDeltaS(args...) } func (b *Block) Number(args ...int) *big.Int { return b.header.Number(args...) } diff --git a/core/types/gen_header_json.go b/core/types/gen_header_json.go index 55b98e5789..759e7d576f 100644 --- a/core/types/gen_header_json.go +++ b/core/types/gen_header_json.go @@ -26,7 +26,7 @@ func (h Header) MarshalJSON() ([]byte, error) { ManifestHash []common.Hash `json:"manifestHash" gencodec:"required"` ReceiptHash []common.Hash `json:"receiptsRoot" gencodec:"required"` Bloom []Bloom `json:"logsBloom" gencodec:"required"` - Difficulty []*hexutil.Big `json:"difficulty" gencodec:"required"` + Difficulty *hexutil.Big `json:"difficulty" gencodec:"required"` ParentEntropy []*hexutil.Big `json:"parentEntropy" gencodec:"required"` ParentDeltaS []*hexutil.Big `json:"parentDeltaS" gencodec:"required"` Number []*hexutil.Big `json:"number" gencodec:"required"` @@ -40,7 +40,6 @@ func (h Header) MarshalJSON() ([]byte, error) { Hash common.Hash `json:"hash"` } // Initialize the enc struct - enc.Difficulty = make([]*hexutil.Big, common.HierarchyDepth) enc.ParentEntropy = make([]*hexutil.Big, common.HierarchyDepth) enc.ParentDeltaS = make([]*hexutil.Big, common.HierarchyDepth) enc.Number = make([]*hexutil.Big, common.HierarchyDepth) @@ -59,7 +58,6 @@ func (h Header) MarshalJSON() ([]byte, error) { copy(enc.ReceiptHash, h.ReceiptHashArray()) copy(enc.Bloom, h.BloomArray()) for i := 0; i < common.HierarchyDepth; i++ { - enc.Difficulty[i] = (*hexutil.Big)(h.Difficulty(i)) enc.ParentEntropy[i] = (*hexutil.Big)(h.ParentEntropy(i)) enc.ParentDeltaS[i] = (*hexutil.Big)(h.ParentDeltaS(i)) enc.Number[i] = (*hexutil.Big)(h.Number(i)) @@ -67,6 +65,7 @@ func (h Header) MarshalJSON() ([]byte, error) { enc.GasUsed[i] = hexutil.Uint64(h.GasUsed(i)) enc.BaseFee[i] = (*hexutil.Big)(h.BaseFee(i)) } + enc.Difficulty = (*hexutil.Big)(h.Difficulty()) enc.Location = h.Location() enc.Time = hexutil.Uint64(h.Time()) enc.Extra = hexutil.Bytes(h.Extra()) @@ -88,7 +87,7 @@ func (h *Header) UnmarshalJSON(input []byte) error { EtxRollupHash []common.Hash `json:"extRollupRoot" gencodec:"required"` ManifestHash []common.Hash `json:"manifestHash" gencodec:"required"` Bloom []Bloom `json:"logsBloom" gencodec:"required"` - Difficulty []*hexutil.Big `json:"difficulty" gencodec:"required"` + Difficulty *hexutil.Big `json:"difficulty" gencodec:"required"` ParentEntropy []*hexutil.Big `json:"parentEntropy" gencodec:"required"` ParentDeltaS []*hexutil.Big `json:"parentDeltaS" gencodec:"required"` Number []*hexutil.Big `json:"number" gencodec:"required"` @@ -168,7 +167,6 @@ func (h *Header) UnmarshalJSON(input []byte) error { h.manifestHash = make([]common.Hash, common.HierarchyDepth) h.receiptHash = make([]common.Hash, common.HierarchyDepth) h.bloom = make([]Bloom, common.HierarchyDepth) - h.difficulty = make([]*big.Int, common.HierarchyDepth) h.parentEntropy = make([]*big.Int, common.HierarchyDepth) h.parentDeltaS = make([]*big.Int, common.HierarchyDepth) h.number = make([]*big.Int, common.HierarchyDepth) @@ -187,10 +185,6 @@ func (h *Header) UnmarshalJSON(input []byte) error { h.SetEtxRollupHash(dec.EtxRollupHash[i], i) h.SetManifestHash(dec.ManifestHash[i], i) h.SetBloom(dec.Bloom[i], i) - if dec.Difficulty[i] == nil { - return errors.New("missing required field 'difficulty' for Header") - } - h.SetDifficulty((*big.Int)(dec.Difficulty[i]), i) if dec.ParentEntropy[i] == nil { return errors.New("missing required field 'parentEntropy' for Header") } @@ -210,6 +204,7 @@ func (h *Header) UnmarshalJSON(input []byte) error { } h.SetBaseFee((*big.Int)(dec.BaseFee[i]), i) } + h.SetDifficulty((*big.Int)(dec.Difficulty)) h.SetLocation(dec.Location) h.SetTime(uint64(dec.Time)) h.SetExtra(dec.Extra) diff --git a/core/worker.go b/core/worker.go index 1622ccad9f..a304624c02 100644 --- a/core/worker.go +++ b/core/worker.go @@ -861,10 +861,14 @@ func (w *worker) prepareWork(genParams *generateParams, block *types.Block) (*en header.SetCoinbase(w.coinbase) } - // Run the consensus preparation with the default or customized consensus engine. - if err := w.engine.Prepare(w.hc, header, block.Header()); err != nil { - log.Error("Failed to prepare header for sealing", "err", err) - return nil, err + // Only zone should calculate the difficulty + nodeCtx := common.NodeLocation.Context() + if nodeCtx == common.ZONE_CTX { + // Run the consensus preparation with the default or customized consensus engine. + if err := w.engine.Prepare(w.hc, header, block.Header()); err != nil { + log.Error("Failed to prepare header for sealing", "err", err) + return nil, err + } } env, err := w.makeEnv(parent, header, w.coinbase) diff --git a/eth/filters/api.go b/eth/filters/api.go index 89ac697125..57b54f0ad5 100644 --- a/eth/filters/api.go +++ b/eth/filters/api.go @@ -625,6 +625,7 @@ func RPCMarshalHeader(head *types.Header) map[string]interface{} { "sha3Uncles": head.UncleHashArray(), "logsBloom": head.BloomArray(), "stateRoot": head.RootArray(), + "difficulty": (*hexutil.Big)(head.Difficulty()), "miner": head.CoinbaseArray(), "extraData": hexutil.Bytes(head.Extra()), "size": hexutil.Uint64(head.Size()), @@ -638,21 +639,18 @@ func RPCMarshalHeader(head *types.Header) map[string]interface{} { } number := make([]*hexutil.Big, common.HierarchyDepth) - difficulty := make([]*hexutil.Big, common.HierarchyDepth) parentEntropy := make([]*hexutil.Big, common.HierarchyDepth) parentDeltaS := make([]*hexutil.Big, common.HierarchyDepth) gasLimit := make([]hexutil.Uint, common.HierarchyDepth) gasUsed := make([]hexutil.Uint, common.HierarchyDepth) for i := 0; i < common.HierarchyDepth; i++ { number[i] = (*hexutil.Big)(head.Number(i)) - difficulty[i] = (*hexutil.Big)(head.Difficulty(i)) parentEntropy[i] = (*hexutil.Big)(head.ParentEntropy(i)) parentDeltaS[i] = (*hexutil.Big)(head.ParentDeltaS(i)) gasLimit[i] = hexutil.Uint(head.GasLimit(i)) gasUsed[i] = hexutil.Uint(head.GasUsed(i)) } result["number"] = number - result["difficulty"] = difficulty result["parentEntropy"] = parentEntropy result["parentDeltaS"] = parentDeltaS result["gasLimit"] = gasLimit diff --git a/internal/quaiapi/quai_api.go b/internal/quaiapi/quai_api.go index 6877496724..f6ca804523 100644 --- a/internal/quaiapi/quai_api.go +++ b/internal/quaiapi/quai_api.go @@ -406,6 +406,7 @@ func RPCMarshalHeader(head *types.Header) map[string]interface{} { result := map[string]interface{}{ "hash": head.Hash(), "parentHash": head.ParentHashArray(), + "difficulty": (*hexutil.Big)(head.Difficulty()), "nonce": head.Nonce(), "sha3Uncles": head.UncleHashArray(), "logsBloom": head.BloomArray(), @@ -423,21 +424,18 @@ func RPCMarshalHeader(head *types.Header) map[string]interface{} { } number := make([]*hexutil.Big, common.HierarchyDepth) - difficulty := make([]*hexutil.Big, common.HierarchyDepth) parentEntropy := make([]*hexutil.Big, common.HierarchyDepth) parentDeltaS := make([]*hexutil.Big, common.HierarchyDepth) gasLimit := make([]hexutil.Uint, common.HierarchyDepth) gasUsed := make([]hexutil.Uint, common.HierarchyDepth) for i := 0; i < common.HierarchyDepth; i++ { number[i] = (*hexutil.Big)(head.Number(i)) - difficulty[i] = (*hexutil.Big)(head.Difficulty(i)) parentEntropy[i] = (*hexutil.Big)(head.ParentEntropy(i)) parentDeltaS[i] = (*hexutil.Big)(head.ParentDeltaS(i)) gasLimit[i] = hexutil.Uint(head.GasLimit(i)) gasUsed[i] = hexutil.Uint(head.GasUsed(i)) } result["number"] = number - result["difficulty"] = difficulty result["parentEntropy"] = parentEntropy result["parentDeltaS"] = parentDeltaS result["gasLimit"] = gasLimit diff --git a/params/config.go b/params/config.go index 7123bea2eb..abeeb15fd5 100644 --- a/params/config.go +++ b/params/config.go @@ -25,10 +25,10 @@ import ( // Genesis hashes to enforce below configs on. var ( - ColosseumGenesisHash = common.HexToHash("0x07107d5523286f6e9bda586242abaf77a262a6763b63969db7a3635679ce6b63") - GardenGenesisHash = common.HexToHash("0xe8c63ec6e47b9f9c126c505eaddc639165a3c952938f791705cb0ef4a7e3b184") - OrchardGenesisHash = common.HexToHash("0x23962df067cb177b33fcf58a3fd6e07a1cde9d2d856e944af82ef706ef4c7bf2") - LocalGenesisHash = common.HexToHash("0x1f3c8e8719fd838f09182d04acdefeb98a24232f0f0cf0f3fecfefbcfcf0c635") + ColosseumGenesisHash = common.HexToHash("0xa5951ca01396546d238c8c900c86bd10894905a04c3f6902142345aabbff7947") + GardenGenesisHash = common.HexToHash("0x3651b09b23cd8f76630d32ae2391d46dbd4d56f5d7b46e2787bf8aba47a2f74a") + OrchardGenesisHash = common.HexToHash("0xb49b67aa6d88002118176c6d4d779346d7aee59786829939ee6ac61cc6fb78eb") + LocalGenesisHash = common.HexToHash("0x4c4c6a48351375caa0009b9c36154a4c4b1a79c400464b98a7368dfd825b6b3e") ) var ( diff --git a/params/protocol_params.go b/params/protocol_params.go index 9c4084fa4c..898c984e9f 100644 --- a/params/protocol_params.go +++ b/params/protocol_params.go @@ -152,15 +152,13 @@ const ( ) var ( - DifficultyBoundDivisor = big.NewInt(2048) // The bound divisor of the difficulty, used in the update calculations. - ZoneMinDifficulty = big.NewInt(131072) // The minimum difficulty in a zone. Prime & regions should be multiples of this value - RegionMinDifficulty = new(big.Int).Mul(big.NewInt(10), ZoneMinDifficulty) - PrimeMinDifficulty = new(big.Int).Mul(big.NewInt(10), RegionMinDifficulty) - MinimumDifficulty = []*big.Int{PrimeMinDifficulty, RegionMinDifficulty, ZoneMinDifficulty} // The minimum that the difficulty may ever be. - GenesisDifficulty = []*big.Int{PrimeMinDifficulty, RegionMinDifficulty, ZoneMinDifficulty} // Difficulty of the Genesis block. - DurationLimit = []*big.Int{big.NewInt(1000), big.NewInt(100), big.NewInt(10)} // The decision boundary on the blocktime duration used to determine whether difficulty should go up or not. - GardenDurationLimit = []*big.Int{big.NewInt(150), big.NewInt(30), big.NewInt(3)} // The decision boundary on the blocktime duration used to determine whether difficulty should go up or not. - OrchardDurationLimit = []*big.Int{big.NewInt(150), big.NewInt(30), big.NewInt(3)} // The decision boundary on the blocktime duration used to determine whether difficulty should go up or not. - LocalDurationLimit = []*big.Int{big.NewInt(24), big.NewInt(7), big.NewInt(2)} // The decision boundary on the blocktime duration used to determine whether difficulty should go up or not. + DifficultyBoundDivisor = big.NewInt(2048) // The bound divisor of the difficulty, used in the update calculations. + ZoneMinDifficulty = big.NewInt(131072) // The minimum difficulty in a zone. Prime & regions should be multiples of this value + MinimumDifficulty = ZoneMinDifficulty // The minimum that the difficulty may ever be. + GenesisDifficulty = ZoneMinDifficulty // Difficulty of the Genesis block. + DurationLimit = []*big.Int{big.NewInt(1000), big.NewInt(100), big.NewInt(10)} // The decision boundary on the blocktime duration used to determine whether difficulty should go up or not. + GardenDurationLimit = []*big.Int{big.NewInt(150), big.NewInt(30), big.NewInt(3)} // The decision boundary on the blocktime duration used to determine whether difficulty should go up or not. + OrchardDurationLimit = []*big.Int{big.NewInt(150), big.NewInt(30), big.NewInt(3)} // The decision boundary on the blocktime duration used to determine whether difficulty should go up or not. + LocalDurationLimit = []*big.Int{big.NewInt(24), big.NewInt(7), big.NewInt(2)} // The decision boundary on the blocktime duration used to determine whether difficulty should go up or not. TimeFactor = big.NewInt(7) ) diff --git a/quaiclient/quaiclient.go b/quaiclient/quaiclient.go index 4470b89420..6c26709f75 100644 --- a/quaiclient/quaiclient.go +++ b/quaiclient/quaiclient.go @@ -88,6 +88,7 @@ func RPCMarshalHeader(head *types.Header) map[string]interface{} { "sha3Uncles": head.UncleHashArray(), "logsBloom": head.BloomArray(), "stateRoot": head.RootArray(), + "difficulty": (*hexutil.Big)(head.Difficulty()), "manifestHash": head.ManifestHashArray(), "extTransactionsRoot": head.EtxHashArray(), "extRollupRoot": head.EtxRollupHashArray(), @@ -101,21 +102,18 @@ func RPCMarshalHeader(head *types.Header) map[string]interface{} { } number := make([]*hexutil.Big, common.HierarchyDepth) - difficulty := make([]*hexutil.Big, common.HierarchyDepth) parentEntropy := make([]*hexutil.Big, common.HierarchyDepth) parentDeltaS := make([]*hexutil.Big, common.HierarchyDepth) gasLimit := make([]hexutil.Uint, common.HierarchyDepth) gasUsed := make([]hexutil.Uint, common.HierarchyDepth) for i := 0; i < common.HierarchyDepth; i++ { number[i] = (*hexutil.Big)(head.Number(i)) - difficulty[i] = (*hexutil.Big)(head.Difficulty(i)) parentEntropy[i] = (*hexutil.Big)(head.ParentEntropy(i)) parentDeltaS[i] = (*hexutil.Big)(head.ParentDeltaS(i)) gasLimit[i] = hexutil.Uint(head.GasLimit(i)) gasUsed[i] = hexutil.Uint(head.GasUsed(i)) } result["number"] = number - result["difficulty"] = difficulty result["parentEntropy"] = parentEntropy result["parentDeltaS"] = parentDeltaS result["gasLimit"] = gasLimit