Skip to content

Commit 367778b

Browse files
committed
terence's review #3
1 parent 0d9e8dc commit 367778b

File tree

4 files changed

+19
-17
lines changed

4 files changed

+19
-17
lines changed

beacon-chain/forkchoice/protoarray/optimistic_sync.go

+12-10
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,26 @@ func (f *ForkChoice) IsOptimistic(root [32]byte) (bool, error) {
1919
return false, ErrUnknownNodeRoot
2020
}
2121
node := f.store.nodes[index]
22-
return node.optimistic == SYNCING, nil
22+
return node.optimistic == syncing, nil
2323
}
2424

2525
// SetOptimisticToValid is called with the root of a block that was returned as
2626
// VALID by the EL.
2727
// WARNING: This method returns an error if the root is not found in forkchoice
2828
func (f *ForkChoice) SetOptimisticToValid(ctx context.Context, root [32]byte) error {
29-
f.store.nodesLock.RLock()
30-
defer f.store.nodesLock.RUnlock()
29+
f.store.nodesLock.Lock()
30+
defer f.store.nodesLock.Unlock()
3131
// We can only update if given root is in Fork Choice
3232
index, ok := f.store.nodesIndices[root]
3333
if !ok {
3434
return ErrUnknownNodeRoot
3535
}
3636

37-
for node := f.store.nodes[index]; node.optimistic == SYNCING; node = f.store.nodes[index] {
37+
for node := f.store.nodes[index]; node.optimistic == syncing; node = f.store.nodes[index] {
3838
if ctx.Err() != nil {
3939
return ctx.Err()
4040
}
41-
node.optimistic = VALID
41+
node.optimistic = valid
4242
index = node.parent
4343
if index == NonExistentNode {
4444
break
@@ -51,7 +51,7 @@ func (f *ForkChoice) SetOptimisticToValid(ctx context.Context, root [32]byte) er
5151
// SetOptimisticToInvalid updates the synced_tips map when the block with the given root becomes INVALID.
5252
// It takes two parameters: the root of the INVALID block and the payload Hash
5353
// of the last valid block.s
54-
func (f *ForkChoice) SetOptimisticToInvalid(ctx context.Context, root, payload [32]byte) ([][32]byte, error) {
54+
func (f *ForkChoice) SetOptimisticToInvalid(ctx context.Context, root, payloadHash [32]byte) ([][32]byte, error) {
5555
f.store.nodesLock.Lock()
5656
invalidRoots := make([][32]byte, 0)
5757
// We only support setting invalid a node existing in Forkchoice
@@ -62,7 +62,7 @@ func (f *ForkChoice) SetOptimisticToInvalid(ctx context.Context, root, payload [
6262
}
6363
node := f.store.nodes[invalidIndex]
6464

65-
lastValidIndex, ok := f.store.payloadIndices[payload]
65+
lastValidIndex, ok := f.store.payloadIndices[payloadHash]
6666
if !ok || lastValidIndex == NonExistentNode {
6767
f.store.nodesLock.Unlock()
6868
return invalidRoots, errInvalidFinalizedNode
@@ -77,6 +77,8 @@ func (f *ForkChoice) SetOptimisticToInvalid(ctx context.Context, root, payload [
7777
}
7878
node = f.store.nodes[firstInvalidIndex]
7979
}
80+
// if the last valid hash is not an ancestor of the invalid block, we
81+
// just remove the invalid block.
8082
if node.parent != lastValidIndex {
8183
node = f.store.nodes[invalidIndex]
8284
firstInvalidIndex = invalidIndex
@@ -120,7 +122,7 @@ func (f *ForkChoice) SetOptimisticToInvalid(ctx context.Context, root, payload [
120122
}
121123

122124
invalidIndices := map[uint64]bool{firstInvalidIndex: true}
123-
node.optimistic = INVALID
125+
node.optimistic = invalid
124126
node.weight = 0
125127
delete(f.store.nodesIndices, node.root)
126128
delete(f.store.canonicalNodes, node.root)
@@ -130,7 +132,7 @@ func (f *ForkChoice) SetOptimisticToInvalid(ctx context.Context, root, payload [
130132
if _, ok := invalidIndices[invalidNode.parent]; !ok {
131133
continue
132134
}
133-
if invalidNode.optimistic == VALID {
135+
if invalidNode.optimistic == valid {
134136
f.store.nodesLock.Unlock()
135137
return invalidRoots, errInvalidOptimisticStatus
136138
}
@@ -140,7 +142,7 @@ func (f *ForkChoice) SetOptimisticToInvalid(ctx context.Context, root, payload [
140142
if !previouslyBoosted && invalidNode.root == previousBoostRoot {
141143
previouslyBoosted = true
142144
}
143-
invalidNode.optimistic = INVALID
145+
invalidNode.optimistic = invalid
144146
invalidIndices[index] = true
145147
invalidNode.weight = 0
146148
delete(f.store.nodesIndices, invalidNode.root)

beacon-chain/forkchoice/protoarray/optimistic_sync_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func TestOptimistic_Outside_ForkChoice(t *testing.T) {
3535
slot: types.Slot(100),
3636
root: bytesutil.ToBytes32([]byte("helloA")),
3737
bestChild: 1,
38-
optimistic: VALID,
38+
optimistic: valid,
3939
}
4040
nodes := []*Node{
4141
nodeA,
@@ -291,8 +291,8 @@ func TestSetOptimisticToInvalid(t *testing.T) {
291291
require.Equal(t, tc.newBestChild, lvh.bestChild)
292292
require.Equal(t, tc.newBestDescendant, lvh.bestDescendant)
293293
require.Equal(t, tc.newParentWeight, lvh.weight)
294-
require.Equal(t, SYNCING, f.store.nodes[8].optimistic /* F */)
295-
require.Equal(t, VALID, f.store.nodes[5].optimistic /* E */)
294+
require.Equal(t, syncing, f.store.nodes[8].optimistic /* F */)
295+
require.Equal(t, valid, f.store.nodes[5].optimistic /* E */)
296296
f.store.nodesLock.RUnlock()
297297
}
298298
}

beacon-chain/forkchoice/protoarray/store.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ func (s *Store) prune(ctx context.Context, finalizedRoot [32]byte) error {
631631
// Any node with diff finalized or justified epoch than the ones in fork choice store
632632
// should not be viable to head.
633633
func (s *Store) leadsToViableHead(node *Node) (bool, error) {
634-
if node.optimistic == INVALID {
634+
if node.optimistic == invalid {
635635
return false, nil
636636
}
637637

beacon-chain/forkchoice/protoarray/types.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ type Node struct {
5151
type OptimisticStatus uint8
5252

5353
const (
54-
SYNCING OptimisticStatus = iota // the node is optimistic
55-
VALID //fully validated node
56-
INVALID // invalid execution payload
54+
syncing OptimisticStatus = iota // the node is optimistic
55+
valid //fully validated node
56+
invalid // invalid execution payload
5757
)
5858

5959
// Vote defines an individual validator's vote.

0 commit comments

Comments
 (0)