Skip to content

Commit

Permalink
Added PrimeEntropyThreshold into the header
Browse files Browse the repository at this point in the history
  • Loading branch information
gameofpointers committed Aug 22, 2023
1 parent b8f3921 commit df8814c
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 130 deletions.
8 changes: 7 additions & 1 deletion core/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ func (sl *Slice) Append(header *types.Header, domPendingHeader *types.Header, do
if err != nil {
return nil, false, err
}

time7 := common.PrettyDuration(time.Since(start))
time8 := common.PrettyDuration(time.Since(start))
var subPendingEtxs types.Transactions
Expand Down Expand Up @@ -796,6 +797,12 @@ func (sl *Slice) combinePendingHeader(header *types.Header, slPendingHeader *typ
combinedPendingHeader.SetParentEntropy(header.ParentEntropy(index), index)
combinedPendingHeader.SetParentDeltaS(header.ParentDeltaS(index), index)

if index == common.REGION_CTX {
for i := 0; i < common.NumZonesInRegion; i++ {
combinedPendingHeader.SetPrimeEntropyThreshold(header.PrimeEntropyThreshold(i), i)
}
}

if inSlice {
combinedPendingHeader.SetEtxRollupHash(header.EtxRollupHash())
combinedPendingHeader.SetDifficulty(header.Difficulty())
Expand Down Expand Up @@ -823,7 +830,6 @@ func (sl *Slice) NewGenesisPendingHeader(domPendingHeader *types.Header) {
if err != nil {
return
}

if nodeCtx == common.PRIME_CTX {
domPendingHeader = types.CopyHeader(localPendingHeader)
} else {
Expand Down
205 changes: 120 additions & 85 deletions core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,27 +80,28 @@ func (n *BlockNonce) UnmarshalText(input []byte) error {

// Header represents a block header in the Quai blockchain.
type Header struct {
parentHash []common.Hash `json:"parentHash" gencodec:"required"`
uncleHash common.Hash `json:"sha3Uncles" gencodec:"required"`
coinbase common.Address `json:"miner" gencodec:"required"`
root common.Hash `json:"stateRoot" gencodec:"required"`
txHash common.Hash `json:"transactionsRoot" gencodec:"required"`
etxHash common.Hash `json:"extTransactionsRoot" gencodec:"required"`
etxRollupHash common.Hash `json:"extRollupRoot" gencodec:"required"`
manifestHash []common.Hash `json:"manifestHash" gencodec:"required"`
receiptHash common.Hash `json:"receiptsRoot" 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"`
gasLimit uint64 `json:"gasLimit" gencodec:"required"`
gasUsed uint64 `json:"gasUsed" gencodec:"required"`
baseFee *big.Int `json:"baseFeePerGas" gencodec:"required"`
location common.Location `json:"location" gencodec:"required"`
time uint64 `json:"timestamp" gencodec:"required"`
extra []byte `json:"extraData" gencodec:"required"`
mixHash common.Hash `json:"mixHash" gencodec:"required"`
nonce BlockNonce `json:"nonce"`
parentHash []common.Hash `json:"parentHash" gencodec:"required"`
uncleHash common.Hash `json:"sha3Uncles" gencodec:"required"`
coinbase common.Address `json:"miner" gencodec:"required"`
root common.Hash `json:"stateRoot" gencodec:"required"`
txHash common.Hash `json:"transactionsRoot" gencodec:"required"`
etxHash common.Hash `json:"extTransactionsRoot" gencodec:"required"`
etxRollupHash common.Hash `json:"extRollupRoot" gencodec:"required"`
manifestHash []common.Hash `json:"manifestHash" gencodec:"required"`
receiptHash common.Hash `json:"receiptsRoot" gencodec:"required"`
difficulty *big.Int `json:"difficulty" gencodec:"required"`
primeEntropyThreshold []*big.Int `json:"primeEntropyThreshold" gencodec:"required"`
parentEntropy []*big.Int `json:"parentEntropy" gencodec:"required"`
parentDeltaS []*big.Int `json:"parentDeltaS" gencodec:"required"`
number []*big.Int `json:"number" gencodec:"required"`
gasLimit uint64 `json:"gasLimit" gencodec:"required"`
gasUsed uint64 `json:"gasUsed" gencodec:"required"`
baseFee *big.Int `json:"baseFeePerGas" gencodec:"required"`
location common.Location `json:"location" gencodec:"required"`
time uint64 `json:"timestamp" gencodec:"required"`
extra []byte `json:"extraData" gencodec:"required"`
mixHash common.Hash `json:"mixHash" gencodec:"required"`
nonce BlockNonce `json:"nonce"`

// caches
hash atomic.Value
Expand All @@ -111,41 +112,43 @@ type Header struct {

// field type overrides for gencodec
type headerMarshaling struct {
Difficulty *hexutil.Big
Number []*hexutil.Big
GasLimit hexutil.Uint64
GasUsed hexutil.Uint64
BaseFee *hexutil.Big
ParentEntropy []*hexutil.Big
ParentDeltaS []*hexutil.Big
Time hexutil.Uint64
Extra hexutil.Bytes
Hash common.Hash `json:"hash"` // adds call to Hash() in MarshalJSON
Difficulty *hexutil.Big
PrimeEntropyThreshold []*hexutil.Big
Number []*hexutil.Big
GasLimit hexutil.Uint64
GasUsed hexutil.Uint64
BaseFee *hexutil.Big
ParentEntropy []*hexutil.Big
ParentDeltaS []*hexutil.Big
Time hexutil.Uint64
Extra hexutil.Bytes
Hash common.Hash `json:"hash"` // adds call to Hash() in MarshalJSON
}

// "external" header encoding. used for eth protocol, etc.
type extheader struct {
ParentHash []common.Hash
UncleHash common.Hash
Coinbase common.Address
Root common.Hash
TxHash common.Hash
EtxHash common.Hash
EtxRollupHash common.Hash
ManifestHash []common.Hash
ReceiptHash common.Hash
Difficulty *big.Int
ParentEntropy []*big.Int
ParentDeltaS []*big.Int
Number []*big.Int
GasLimit uint64
GasUsed uint64
BaseFee *big.Int
Location common.Location
Time uint64
Extra []byte
MixHash common.Hash
Nonce BlockNonce
ParentHash []common.Hash
UncleHash common.Hash
Coinbase common.Address
Root common.Hash
TxHash common.Hash
EtxHash common.Hash
EtxRollupHash common.Hash
ManifestHash []common.Hash
ReceiptHash common.Hash
Difficulty *big.Int
PrimeEntropyThreshold []*big.Int
ParentEntropy []*big.Int
ParentDeltaS []*big.Int
Number []*big.Int
GasLimit uint64
GasUsed uint64
BaseFee *big.Int
Location common.Location
Time uint64
Extra []byte
MixHash common.Hash
Nonce BlockNonce
}

// Construct an empty header
Expand All @@ -157,6 +160,7 @@ func EmptyHeader() *Header {
h.parentDeltaS = make([]*big.Int, common.HierarchyDepth)
h.number = make([]*big.Int, common.HierarchyDepth)
h.difficulty = big.NewInt(0)
h.primeEntropyThreshold = make([]*big.Int, common.NumZonesInRegion)
h.root = EmptyRootHash
h.mixHash = EmptyRootHash
h.txHash = EmptyRootHash
Expand All @@ -171,6 +175,9 @@ func EmptyHeader() *Header {
h.parentDeltaS[i] = big.NewInt(0)
h.number[i] = big.NewInt(0)
}
for i := 0; i < common.NumZonesInRegion; i++ {
h.primeEntropyThreshold[i] = big.NewInt(0)
}
return h
}

Expand All @@ -190,6 +197,7 @@ func (h *Header) DecodeRLP(s *rlp.Stream) error {
h.manifestHash = eh.ManifestHash
h.receiptHash = eh.ReceiptHash
h.difficulty = eh.Difficulty
h.primeEntropyThreshold = eh.PrimeEntropyThreshold
h.parentEntropy = eh.ParentEntropy
h.parentDeltaS = eh.ParentDeltaS
h.number = eh.Number
Expand All @@ -208,27 +216,28 @@ func (h *Header) DecodeRLP(s *rlp.Stream) error {
// EncodeRLP serializes h into the Quai RLP block format.
func (h *Header) EncodeRLP(w io.Writer) error {
return rlp.Encode(w, extheader{
ParentHash: h.parentHash,
UncleHash: h.uncleHash,
Coinbase: h.coinbase,
Root: h.root,
TxHash: h.txHash,
EtxHash: h.etxHash,
EtxRollupHash: h.etxRollupHash,
ManifestHash: h.manifestHash,
ReceiptHash: h.receiptHash,
Difficulty: h.difficulty,
ParentEntropy: h.parentEntropy,
ParentDeltaS: h.parentDeltaS,
Number: h.number,
GasLimit: h.gasLimit,
GasUsed: h.gasUsed,
BaseFee: h.baseFee,
Location: h.location,
Time: h.time,
Extra: h.extra,
MixHash: h.mixHash,
Nonce: h.nonce,
ParentHash: h.parentHash,
UncleHash: h.uncleHash,
Coinbase: h.coinbase,
Root: h.root,
TxHash: h.txHash,
EtxHash: h.etxHash,
EtxRollupHash: h.etxRollupHash,
ManifestHash: h.manifestHash,
ReceiptHash: h.receiptHash,
Difficulty: h.difficulty,
PrimeEntropyThreshold: h.primeEntropyThreshold,
ParentEntropy: h.parentEntropy,
ParentDeltaS: h.parentDeltaS,
Number: h.number,
GasLimit: h.gasLimit,
GasUsed: h.gasUsed,
BaseFee: h.baseFee,
Location: h.location,
Time: h.time,
Extra: h.extra,
MixHash: h.mixHash,
Nonce: h.nonce,
})
}

Expand Down Expand Up @@ -259,12 +268,15 @@ func (h *Header) RPCMarshalHeader() map[string]interface{} {
number := make([]*hexutil.Big, common.HierarchyDepth)
parentEntropy := make([]*hexutil.Big, common.HierarchyDepth)
parentDeltaS := make([]*hexutil.Big, common.HierarchyDepth)
primeEntropyThreshold := make([]*hexutil.Big, common.HierarchyDepth)
for i := 0; i < common.HierarchyDepth; i++ {
number[i] = (*hexutil.Big)(h.Number(i))
parentEntropy[i] = (*hexutil.Big)(h.ParentEntropy(i))
parentDeltaS[i] = (*hexutil.Big)(h.ParentDeltaS(i))
primeEntropyThreshold[i] = (*hexutil.Big)(h.PrimeEntropyThreshold(i))
}
result["number"] = number
result["primeEntropyThreshold"] = primeEntropyThreshold
result["parentEntropy"] = parentEntropy
result["parentDeltaS"] = parentDeltaS

Expand Down Expand Up @@ -315,6 +327,16 @@ func (h *Header) ParentDeltaS(args ...int) *big.Int {
}
return h.parentDeltaS[nodeCtx]
}
func (h *Header) PrimeEntropyThreshold(args ...int) *big.Int {
nodeCtx := common.NodeLocation.Context()
if len(args) > 0 {
nodeCtx = args[0]
}
if args[0] < 0 { //GENESIS ESCAPE
nodeCtx = 0
}
return h.primeEntropyThreshold[nodeCtx]
}
func (h *Header) ManifestHash(args ...int) common.Hash {
nodeCtx := common.NodeLocation.Context()
if len(args) > 0 {
Expand Down Expand Up @@ -437,6 +459,14 @@ func (h *Header) SetDifficulty(val *big.Int) {
h.sealHash = atomic.Value{} // clear sealHash cache
h.difficulty = new(big.Int).Set(val)
}
func (h *Header) SetPrimeEntropyThreshold(val *big.Int, args ...int) {
h.hash = atomic.Value{} // clear hash cache
h.sealHash = atomic.Value{} // clear sealHash cache
if len(args) == 0 {
panic("have to provide index for setting the prime difficulty")
}
h.primeEntropyThreshold[args[0]] = new(big.Int).Set(val)
}
func (h *Header) SetNumber(val *big.Int, args ...int) {
h.hash = atomic.Value{} // clear hash cache
h.sealHash = atomic.Value{} // clear sealHash cache
Expand Down Expand Up @@ -489,9 +519,10 @@ func (h *Header) SetNonce(val BlockNonce) {
}

// Array accessors
func (h *Header) ParentHashArray() []common.Hash { return h.parentHash }
func (h *Header) ManifestHashArray() []common.Hash { return h.manifestHash }
func (h *Header) NumberArray() []*big.Int { return h.number }
func (h *Header) ParentHashArray() []common.Hash { return h.parentHash }
func (h *Header) ManifestHashArray() []common.Hash { return h.manifestHash }
func (h *Header) NumberArray() []*big.Int { return h.number }
func (h *Header) PrimeEntropyThresholdArray() []*big.Int { return h.primeEntropyThreshold }

// headerData comprises all data fields of the header, excluding the nonce, so
// that the nonce may be independently adjusted in the work algorithm.
Expand Down Expand Up @@ -780,13 +811,17 @@ func CopyHeader(h *Header) *Header {
cpy.parentEntropy = make([]*big.Int, common.HierarchyDepth)
cpy.parentDeltaS = make([]*big.Int, common.HierarchyDepth)
cpy.number = make([]*big.Int, common.HierarchyDepth)
cpy.primeEntropyThreshold = make([]*big.Int, common.NumZonesInRegion)
for i := 0; i < common.HierarchyDepth; i++ {
cpy.SetParentHash(h.ParentHash(i), i)
cpy.SetManifestHash(h.ManifestHash(i), i)
cpy.SetParentEntropy(h.ParentEntropy(i), i)
cpy.SetParentDeltaS(h.ParentDeltaS(i), i)
cpy.SetNumber(h.Number(i), i)
}
for i := 0; i < common.NumZonesInRegion; i++ {
cpy.SetPrimeEntropyThreshold(h.PrimeEntropyThreshold(i), i)
}
cpy.SetUncleHash(h.UncleHash())
cpy.SetCoinbase(h.Coinbase())
cpy.SetRoot(h.Root())
Expand Down Expand Up @@ -1018,15 +1053,6 @@ type extPendingHeader struct {
Termini Termini
}

func (t Termini) RPCMarshalTermini() map[string]interface{} {
result := map[string]interface{}{
"domTerminus": t.DomTerminus(),
"subTermini": t.SubTermini(),
"primeTermini": t.PrimeTermini(),
}
return result
}

// DecodeRLP decodes the Quai RLP encoding into pending header format.
func (p *PendingHeader) DecodeRLP(s *rlp.Stream) error {
var eb extPendingHeader
Expand Down Expand Up @@ -1153,6 +1179,15 @@ type extTermini struct {
PrimeTermini []common.Hash
}

func (t Termini) RPCMarshalTermini() map[string]interface{} {
result := map[string]interface{}{
"domTerminus": t.DomTerminus(),
"subTermini": t.SubTermini(),
"primeTermini": t.PrimeTermini(),
}
return result
}

// DecodeRLP decodes the Quai RLP encoding into pending header format.
func (t *Termini) DecodeRLP(s *rlp.Stream) error {
var et extTermini
Expand Down
Loading

0 comments on commit df8814c

Please sign in to comment.