From 8d889f169ea9518e8974f2f9db4da9eca77cb7c6 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Wed, 22 Jan 2020 08:50:16 -0800 Subject: [PATCH] Part 1 of proto array fork choice - docs and interfaces (#4615) * Docs * Interface definitions * Fmt and gazelle * Rename interface to interfaces --- beacon-chain/cache/skip_slot_cache_test.go | 2 +- beacon-chain/forkchoice/BUILD.bazel | 11 +++++++ beacon-chain/forkchoice/doc.go | 7 ++++ beacon-chain/forkchoice/interfaces.go | 33 +++++++++++++++++++ .../forkchoice/protoarray/BUILD.bazel | 8 +++++ beacon-chain/forkchoice/protoarray/doc.go | 7 ++++ beacon-chain/sync/initial-sync/round_robin.go | 2 +- beacon-chain/sync/pending_blocks_queue.go | 2 +- shared/featureconfig/flags.go | 2 +- validator/db/attestation_history_test.go | 14 ++++---- 10 files changed, 77 insertions(+), 11 deletions(-) create mode 100644 beacon-chain/forkchoice/BUILD.bazel create mode 100644 beacon-chain/forkchoice/doc.go create mode 100644 beacon-chain/forkchoice/interfaces.go create mode 100644 beacon-chain/forkchoice/protoarray/BUILD.bazel create mode 100644 beacon-chain/forkchoice/protoarray/doc.go diff --git a/beacon-chain/cache/skip_slot_cache_test.go b/beacon-chain/cache/skip_slot_cache_test.go index 8edfef2ba1..004146a305 100644 --- a/beacon-chain/cache/skip_slot_cache_test.go +++ b/beacon-chain/cache/skip_slot_cache_test.go @@ -5,8 +5,8 @@ import ( "testing" "github.com/gogo/protobuf/proto" - pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/beacon-chain/cache" + pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/featureconfig" ) diff --git a/beacon-chain/forkchoice/BUILD.bazel b/beacon-chain/forkchoice/BUILD.bazel new file mode 100644 index 0000000000..305345ea60 --- /dev/null +++ b/beacon-chain/forkchoice/BUILD.bazel @@ -0,0 +1,11 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "go_default_library", + srcs = [ + "doc.go", + "interfaces.go", + ], + importpath = "github.com/prysmaticlabs/prysm/beacon-chain/forkchoice", + visibility = ["//beacon-chain:__subpackages__"], +) diff --git a/beacon-chain/forkchoice/doc.go b/beacon-chain/forkchoice/doc.go new file mode 100644 index 0000000000..506a489d7f --- /dev/null +++ b/beacon-chain/forkchoice/doc.go @@ -0,0 +1,7 @@ +/* +Package forkchoice implements the service to support fork choice for the eth2 beacon chain. This contains the +necessary components to track latest validators votes, and balances. Then a store object to be used +to calculate head. High level fork choice summary: +https://notes.ethereum.org/@vbuterin/rkhCgQteN?type=view#LMD-GHOST-fork-choice-rule +*/ +package forkchoice diff --git a/beacon-chain/forkchoice/interfaces.go b/beacon-chain/forkchoice/interfaces.go new file mode 100644 index 0000000000..7c83665a8e --- /dev/null +++ b/beacon-chain/forkchoice/interfaces.go @@ -0,0 +1,33 @@ +package forkchoice + +import ( + "context" +) + +// ForkChoice represents the full fork choice interface composed of all of the sub-interfaces. +type ForkChoice interface { + HeadRetriever // to compute head. + BlockProcessor // to track new block for fork choice. + AttestationProcessor // to track new attestation for fork choice. + Pruner // to clean old data for fork choice. +} + +// HeadRetriever retrieves head root of the current chain. +type HeadRetriever interface { + Head(context.Context, uint64, [32]byte, uint64, []uint64) ([32]byte, error) +} + +// BlockProcessor processes the block that's used for accounting fork choice. +type BlockProcessor interface { + ProcessBlock(context.Context, uint64, [32]byte, [32]byte, uint64, uint64) error +} + +// AttestationProcessor processes the attestation that's used for accounting fork choice. +type AttestationProcessor interface { + ProcessAttestation(context.Context, []uint64, [32]byte, uint64) +} + +// Pruner prunes the fork choice upon new finalization. This is used to keep fork choice sane. +type Pruner interface { + Prune(context.Context, [32]byte) +} diff --git a/beacon-chain/forkchoice/protoarray/BUILD.bazel b/beacon-chain/forkchoice/protoarray/BUILD.bazel new file mode 100644 index 0000000000..2dd98d6867 --- /dev/null +++ b/beacon-chain/forkchoice/protoarray/BUILD.bazel @@ -0,0 +1,8 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "go_default_library", + srcs = ["doc.go"], + importpath = "github.com/prysmaticlabs/prysm/beacon-chain/forkchoice/protoarray", + visibility = ["//beacon-chain:__subpackages__"], +) diff --git a/beacon-chain/forkchoice/protoarray/doc.go b/beacon-chain/forkchoice/protoarray/doc.go new file mode 100644 index 0000000000..78c932c6f6 --- /dev/null +++ b/beacon-chain/forkchoice/protoarray/doc.go @@ -0,0 +1,7 @@ +/* +Package protoarray implements proto array fork choice as outlined: +https://github.com/protolambda/lmd-ghost#array-based-stateful-dag-proto_array +This was motivated by the following light house implementation: +https://github.com/sigp/lighthouse/pull/804 +*/ +package protoarray diff --git a/beacon-chain/sync/initial-sync/round_robin.go b/beacon-chain/sync/initial-sync/round_robin.go index 928d51ebe5..bc5b6a9be3 100644 --- a/beacon-chain/sync/initial-sync/round_robin.go +++ b/beacon-chain/sync/initial-sync/round_robin.go @@ -9,12 +9,12 @@ import ( "sync/atomic" "time" - "github.com/prysmaticlabs/prysm/beacon-chain/flags" "github.com/libp2p/go-libp2p-core/peer" "github.com/paulbellamy/ratecounter" "github.com/pkg/errors" eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" + "github.com/prysmaticlabs/prysm/beacon-chain/flags" prysmsync "github.com/prysmaticlabs/prysm/beacon-chain/sync" p2ppb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/bytesutil" diff --git a/beacon-chain/sync/pending_blocks_queue.go b/beacon-chain/sync/pending_blocks_queue.go index a6c45abf14..459cf3a7c1 100644 --- a/beacon-chain/sync/pending_blocks_queue.go +++ b/beacon-chain/sync/pending_blocks_queue.go @@ -69,7 +69,7 @@ func (r *Service) processPendingBlocks(ctx context.Context) error { "parentRoot": hex.EncodeToString(b.Block.ParentRoot), }).Info("Requesting parent block") req := [][32]byte{bytesutil.ToBytes32(b.Block.ParentRoot)} - + // Start with a random peer to query, but choose the first peer in our unsorted list that claims to // have a head slot newer than the block slot we are requesting. pid := pids[rand.Int()%len(pids)] diff --git a/shared/featureconfig/flags.go b/shared/featureconfig/flags.go index a446bf74eb..608f283bf3 100644 --- a/shared/featureconfig/flags.go +++ b/shared/featureconfig/flags.go @@ -171,7 +171,7 @@ var ( Hidden: true, } deprecatedSaveDepositDataFlag = cli.BoolFlag{ - Name: "save-deposit-data", + Name: "save-deposit-data", Usage: deprecatedUsage, Hidden: true, } diff --git a/validator/db/attestation_history_test.go b/validator/db/attestation_history_test.go index 43ad82c618..9bc5ec7ebc 100644 --- a/validator/db/attestation_history_test.go +++ b/validator/db/attestation_history_test.go @@ -60,7 +60,7 @@ func TestSaveAttestationHistory_OK(t *testing.T) { newMap[1] = farFuture newMap[epoch] = 1 history := &slashpb.AttestationHistory{ - TargetToSource: newMap, + TargetToSource: newMap, LatestEpochWritten: 2, } @@ -75,7 +75,7 @@ func TestSaveAttestationHistory_OK(t *testing.T) { if savedHistory == nil || !reflect.DeepEqual(history, savedHistory) { t.Fatalf("Expected DB to keep object the same, received: %v", history) } - if savedHistory.TargetToSource[epoch] != newMap[epoch]{ + if savedHistory.TargetToSource[epoch] != newMap[epoch] { t.Fatalf("Expected target epoch %d to have the same marked source epoch, received %d", epoch, savedHistory.TargetToSource[epoch]) } if savedHistory.TargetToSource[epoch-1] != farFuture { @@ -111,7 +111,7 @@ func TestSaveAttestationHistory_Overwrites(t *testing.T) { pubkey: []byte{0}, epoch: uint64(1), history: &slashpb.AttestationHistory{ - TargetToSource: newMap1, + TargetToSource: newMap1, LatestEpochWritten: 1, }, }, @@ -119,7 +119,7 @@ func TestSaveAttestationHistory_Overwrites(t *testing.T) { pubkey: []byte{0}, epoch: uint64(2), history: &slashpb.AttestationHistory{ - TargetToSource: newMap2, + TargetToSource: newMap2, LatestEpochWritten: 2, }, }, @@ -145,11 +145,11 @@ func TestSaveAttestationHistory_Overwrites(t *testing.T) { if history == nil || !reflect.DeepEqual(history, tt.history) { t.Fatalf("Expected DB to keep object the same, received: %v", history) } - if history.TargetToSource[tt.epoch] != tt.epoch - 1 { + if history.TargetToSource[tt.epoch] != tt.epoch-1 { t.Fatalf("Expected target epoch %d to be marked with correct source epoch %d", tt.epoch, history.TargetToSource[tt.epoch]) } - if history.TargetToSource[tt.epoch - 1] != farFuture { - t.Fatalf("Expected target epoch %d to not be marked as attested for, received %d", tt.epoch-1, history.TargetToSource[tt.epoch - 1]) + if history.TargetToSource[tt.epoch-1] != farFuture { + t.Fatalf("Expected target epoch %d to not be marked as attested for, received %d", tt.epoch-1, history.TargetToSource[tt.epoch-1]) } } }