Skip to content

Commit

Permalink
Add BlockManifest type and assosciated methods
Browse files Browse the repository at this point in the history
  • Loading branch information
wizeguyy committed Jan 25, 2023
1 parent d69dc87 commit 5406607
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
42 changes: 42 additions & 0 deletions core/headerchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,48 @@ func NewHeaderChain(db ethdb.Database, engine consensus.Engine, chainConfig *par
return hc, nil
}

// CollectBlockManifest gathers the manifest of ancestor block hashes since the
// last coincident block.
func (hc *HeaderChain) CollectBlockManifest(h *types.Header) (types.BlockManifest, error) {
if h.NumberU64() == 0 && h.Hash() == hc.config.GenesisHash {
return types.BlockManifest{}, nil
}
parent := hc.GetHeader(h.ParentHash(), h.NumberU64()-1)
if parent == nil {
return types.BlockManifest{}, errors.New("ancestor not found")
} else {
return hc.collectBlockManifest(parent)
}
}

func (hc *HeaderChain) collectBlockManifest(h *types.Header) (types.BlockManifest, error) {
// Intialize manifest with this block's hash
manifest := types.BlockManifest{h.Hash()}
// Terminate the search if we reached genesis
if h.NumberU64() == 0 {
if h.Hash() != hc.config.GenesisHash {
return nil, fmt.Errorf("manifest builds on incorrect genesis, block0 hash: %s", h.Hash().String())
} else {
return manifest, nil
}
}
// Terminate the search on coincidence
if hc.engine.IsDomCoincident(h) {
return manifest, nil
}
// Recursively get the ancestor manifest, until a coincident ancestor is found
ancestor := hc.GetHeader(h.ParentHash(), h.NumberU64()-1)
if ancestor == nil {
return types.BlockManifest{}, errors.New("ancestor not found")
}
ancManifest, err := hc.collectBlockManifest(ancestor)
if err != nil {
return nil, errors.New("unable to get manifest for ancestor")
}
manifest = append(ancManifest, manifest...)
return manifest, nil
}

// Append
func (hc *HeaderChain) Append(batch ethdb.Batch, block *types.Block) error {

Expand Down
12 changes: 12 additions & 0 deletions core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package types

import (
"bytes"
"encoding/binary"
"fmt"
"io"
Expand Down Expand Up @@ -747,3 +748,14 @@ type HeaderRoots struct {
TxsRoot common.Hash
ReceiptsRoot common.Hash
}

// BlockManifest is a list of block hashes, which implements DerivableList
type BlockManifest []common.Hash

// Len returns the length of s.
func (m BlockManifest) Len() int { return len(m) }

// EncodeIndex encodes the i'th blockhash to w.
func (m BlockManifest) EncodeIndex(i int, w *bytes.Buffer) {
rlp.Encode(w, m[i])
}

0 comments on commit 5406607

Please sign in to comment.