Skip to content

Commit

Permalink
Expose fork choice node (#4819)
Browse files Browse the repository at this point in the history
* Expose node

* Comments

* Extra line

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
  • Loading branch information
terencechain and prylabs-bulldozer[bot] committed Feb 10, 2020
1 parent 4598344 commit 52524d5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions beacon-chain/forkchoice/interfaces.go
Expand Up @@ -38,5 +38,6 @@ type Pruner interface {
// Getter returns fork choice related information.
type Getter interface {
Nodes() []*protoarray.Node
Node([32]byte) *protoarray.Node
HasNode([32]byte) bool
}
21 changes: 21 additions & 0 deletions beacon-chain/forkchoice/protoarray/helpers.go
Expand Up @@ -70,3 +70,24 @@ func computeDeltas(

return deltas, votes, nil
}

// This return a copy of the proto array node object.
func copyNode(node *Node) *Node {
if node == nil {
return &Node{}
}

copiedRoot := [32]byte{}
copy(copiedRoot[:], node.root[:])

return &Node{
Slot: node.Slot,
root: copiedRoot,
Parent: node.Parent,
justifiedEpoch: node.justifiedEpoch,
finalizedEpoch: node.finalizedEpoch,
Weight: node.Weight,
bestChild: node.bestChild,
BestDescendent: node.BestDescendent,
}
}
13 changes: 13 additions & 0 deletions beacon-chain/forkchoice/protoarray/store.go
Expand Up @@ -106,6 +106,19 @@ func (f *ForkChoice) Nodes() []*Node {
return cpy
}

// Node returns the copied node in the fork choice store.
func (f *ForkChoice) Node(root [32]byte) *Node {
f.store.nodeIndicesLock.RLock()
defer f.store.nodeIndicesLock.RUnlock()

index, ok := f.store.nodeIndices[root]
if !ok {
return nil
}

return copyNode(f.store.nodes[index])
}

// HasNode returns true if the node exists in fork choice store,
// false else wise.
func (f *ForkChoice) HasNode(root [32]byte) bool {
Expand Down

0 comments on commit 52524d5

Please sign in to comment.