Skip to content

Commit

Permalink
Added primeTermini array to Termini struct with helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
gameofpointers committed Aug 22, 2023
1 parent fce724a commit b8f3921
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 14 deletions.
57 changes: 48 additions & 9 deletions core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -1020,8 +1020,9 @@ type extPendingHeader struct {

func (t Termini) RPCMarshalTermini() map[string]interface{} {
result := map[string]interface{}{
"domTerminus": t.DomTerminus(),
"subTermini": t.SubTermini(),
"domTerminus": t.DomTerminus(),
"subTermini": t.SubTermini(),
"primeTermini": t.PrimeTermini(),
}
return result
}
Expand All @@ -1046,9 +1047,13 @@ func (p PendingHeader) EncodeRLP(w io.Writer) error {

// Termini stores the dom terminus (i.e the previous dom block) and
// subTermini(i.e the dom blocks that have occured in the subordinate chains)
// primeTermini(i.e the prime blocks have occured in a region for each of the zones in region)
// - prime termini is used to control the liveliness of the primes in slices
// using the PrimeEntropyThreshold
type Termini struct {
domTerminus common.Hash `json:"domTerminus"`
subTermini []common.Hash `json:"subTermini"`
domTerminus common.Hash `json:"domTerminus"`
subTermini []common.Hash `json:"subTermini"`
primeTermini []common.Hash `json:"primeTermini"`
}

func CopyTermini(termini Termini) Termini {
Expand All @@ -1057,12 +1062,16 @@ func CopyTermini(termini Termini) Termini {
for i, t := range termini.subTermini {
newTermini.SetSubTerminiAtIndex(t, i)
}
for i, t := range termini.primeTermini {
newTermini.SetPrimeTerminiAtIndex(t, i)
}
return newTermini
}

func EmptyTermini() Termini {
termini := Termini{}
termini.subTermini = make([]common.Hash, common.HierarchyDepth)
termini.primeTermini = make([]common.Hash, common.NumZonesInRegion)
return termini
}

Expand All @@ -1074,13 +1083,24 @@ func (t Termini) SubTermini() []common.Hash {
return t.subTermini
}

func (t Termini) PrimeTermini() []common.Hash {
return t.primeTermini
}

func (t Termini) SubTerminiAtIndex(args ...int) common.Hash {
if len(args) == 0 {
panic("cannot access sub termini at index with the index")
}
return t.subTermini[args[0]]
}

func (t Termini) PrimeTerminiAtIndex(args ...int) common.Hash {
if len(args) == 0 {
panic("cannot access prime termini at index with the index")
}
return t.primeTermini[args[0]]
}

func (t *Termini) SetDomTerminus(domTerminus common.Hash) {
t.domTerminus = domTerminus
}
Expand All @@ -1092,27 +1112,45 @@ func (t *Termini) SetSubTermini(subTermini []common.Hash) {
}
}

func (t *Termini) SetPrimeTermini(primeTermini []common.Hash) {
t.primeTermini = make([]common.Hash, len(primeTermini))
for i := 0; i < len(primeTermini); i++ {
t.primeTermini[i] = primeTermini[i]
}
}

func (t *Termini) SetSubTerminiAtIndex(val common.Hash, args ...int) {
if len(args) == 0 {
panic("index cannot be empty for the sub termini")
}
t.subTermini[args[0]] = val
}

func (t *Termini) SetPrimeTerminiAtIndex(val common.Hash, args ...int) {
if len(args) == 0 {
panic("index cannot be empty for the prime termini")
}
t.primeTermini[args[0]] = val
}

func (t *Termini) IsValid() bool {
if t == nil {
return false
}
if len(t.subTermini) != common.HierarchyDepth {
return false
}
if len(t.primeTermini) != common.NumZonesInRegion {
return false
}
return true
}

// "external termini" pending header encoding. used for rlp
type extTermini struct {
DomTerminus common.Hash
SubTermini []common.Hash
DomTerminus common.Hash
SubTermini []common.Hash
PrimeTermini []common.Hash
}

// DecodeRLP decodes the Quai RLP encoding into pending header format.
Expand All @@ -1121,15 +1159,16 @@ func (t *Termini) DecodeRLP(s *rlp.Stream) error {
if err := s.Decode(&et); err != nil {
return err
}
t.domTerminus, t.subTermini = et.DomTerminus, et.SubTermini
t.domTerminus, t.subTermini, t.primeTermini = et.DomTerminus, et.SubTermini, et.PrimeTermini
return nil
}

// EncodeRLP serializes b into the Quai RLP format.
func (t Termini) EncodeRLP(w io.Writer) error {
return rlp.Encode(w, extTermini{
DomTerminus: t.domTerminus,
SubTermini: t.subTermini,
DomTerminus: t.domTerminus,
SubTermini: t.subTermini,
PrimeTermini: t.primeTermini,
})
}

Expand Down
17 changes: 12 additions & 5 deletions core/types/gen_header_json.go

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

0 comments on commit b8f3921

Please sign in to comment.