diff --git a/beacon-chain/blockchain/store/BUILD.bazel b/beacon-chain/blockchain/store/BUILD.bazel new file mode 100644 index 0000000000..e59d020d5c --- /dev/null +++ b/beacon-chain/blockchain/store/BUILD.bazel @@ -0,0 +1,23 @@ +load("@prysm//tools/go:def.bzl", "go_library", "go_test") + +go_library( + name = "go_default_library", + srcs = [ + "doc.go", + "setter_getter.go", + "type.go", + ], + importpath = "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/store", + visibility = ["//beacon-chain:__subpackages__"], + deps = ["//proto/prysm/v1alpha1:go_default_library"], +) + +go_test( + name = "go_default_test", + srcs = ["setter_getter_test.go"], + embed = [":go_default_library"], + deps = [ + "//proto/prysm/v1alpha1:go_default_library", + "//testing/require:go_default_library", + ], +) diff --git a/beacon-chain/blockchain/store/doc.go b/beacon-chain/blockchain/store/doc.go new file mode 100644 index 0000000000..9124857683 --- /dev/null +++ b/beacon-chain/blockchain/store/doc.go @@ -0,0 +1,4 @@ +// Package store implements the store object defined in the phase0 fork choice spec. +// It serves as a helpful middleware layer in between blockchain pkg and fork choice protoarray pkg. +// All the getters and setters are concurrent thread safe +package store diff --git a/beacon-chain/blockchain/store/setter_getter.go b/beacon-chain/blockchain/store/setter_getter.go new file mode 100644 index 0000000000..dac5a8e84f --- /dev/null +++ b/beacon-chain/blockchain/store/setter_getter.go @@ -0,0 +1,73 @@ +package store + +import ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" + +// PrevJustifiedCheckpt returns the previous justified checkpoint in the store. +func (s *store) PrevJustifiedCheckpt() *ethpb.Checkpoint { + s.RLock() + defer s.RUnlock() + return s.prevJustifiedCheckpt +} + +// BestJustifiedCheckpt returns the best justified checkpoint in the store. +func (s *store) BestJustifiedCheckpt() *ethpb.Checkpoint { + s.RLock() + defer s.RUnlock() + return s.bestJustifiedCheckpt +} + +// JustifiedCheckpt returns the justified checkpoint in the store. +func (s *store) JustifiedCheckpt() *ethpb.Checkpoint { + s.RLock() + defer s.RUnlock() + return s.justifiedCheckpt +} + +// PrevFinalizedCheckpt returns the previous finalized checkpoint in the store. +func (s *store) PrevFinalizedCheckpt() *ethpb.Checkpoint { + s.RLock() + defer s.RUnlock() + return s.prevFinalizedCheckpt +} + +// FinalizedCheckpt returns the finalized checkpoint in the store. +func (s *store) FinalizedCheckpt() *ethpb.Checkpoint { + s.RLock() + defer s.RUnlock() + return s.finalizedCheckpt +} + +// SetPrevJustifiedCheckpt sets the previous justified checkpoint in the store. +func (s *store) SetPrevJustifiedCheckpt(cp *ethpb.Checkpoint) { + s.Lock() + defer s.Unlock() + s.prevJustifiedCheckpt = cp +} + +// SetBestJustifiedCheckpt sets the best justified checkpoint in the store. +func (s *store) SetBestJustifiedCheckpt(cp *ethpb.Checkpoint) { + s.Lock() + defer s.Unlock() + s.bestJustifiedCheckpt = cp +} + +// SetJustifiedCheckpt sets the justified checkpoint in the store. +func (s *store) SetJustifiedCheckpt(cp *ethpb.Checkpoint) { + s.Lock() + defer s.Unlock() + s.justifiedCheckpt = cp +} + +// SetFinalizedCheckpt sets the finalized checkpoint in the store. +func (s *store) SetFinalizedCheckpt(cp *ethpb.Checkpoint) { + s.Lock() + defer s.Unlock() + s.finalizedCheckpt = cp +} + +// SetPrevFinalizedCheckpt sets the previous finalized checkpoint in the store. +func (s *store) SetPrevFinalizedCheckpt(cp *ethpb.Checkpoint) { + s.Lock() + defer s.Unlock() + s.prevFinalizedCheckpt = cp +} diff --git a/beacon-chain/blockchain/store/setter_getter_test.go b/beacon-chain/blockchain/store/setter_getter_test.go new file mode 100644 index 0000000000..00a36b96cc --- /dev/null +++ b/beacon-chain/blockchain/store/setter_getter_test.go @@ -0,0 +1,53 @@ +package store + +import ( + "testing" + + ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" + "github.com/prysmaticlabs/prysm/testing/require" +) + +func Test_store_PrevJustifiedCheckpt(t *testing.T) { + s := &store{} + var cp *ethpb.Checkpoint + require.Equal(t, cp, s.PrevJustifiedCheckpt()) + cp = ðpb.Checkpoint{Epoch: 1, Root: []byte{'a'}} + s.SetPrevJustifiedCheckpt(cp) + require.Equal(t, cp, s.PrevJustifiedCheckpt()) +} + +func Test_store_BestJustifiedCheckpt(t *testing.T) { + s := &store{} + var cp *ethpb.Checkpoint + require.Equal(t, cp, s.BestJustifiedCheckpt()) + cp = ðpb.Checkpoint{Epoch: 1, Root: []byte{'a'}} + s.SetBestJustifiedCheckpt(cp) + require.Equal(t, cp, s.BestJustifiedCheckpt()) +} + +func Test_store_JustifiedCheckpt(t *testing.T) { + s := &store{} + var cp *ethpb.Checkpoint + require.Equal(t, cp, s.JustifiedCheckpt()) + cp = ðpb.Checkpoint{Epoch: 1, Root: []byte{'a'}} + s.SetJustifiedCheckpt(cp) + require.Equal(t, cp, s.JustifiedCheckpt()) +} + +func Test_store_FinalizedCheckpt(t *testing.T) { + s := &store{} + var cp *ethpb.Checkpoint + require.Equal(t, cp, s.FinalizedCheckpt()) + cp = ðpb.Checkpoint{Epoch: 1, Root: []byte{'a'}} + s.SetFinalizedCheckpt(cp) + require.Equal(t, cp, s.FinalizedCheckpt()) +} + +func Test_store_PrevFinalizedCheckpt(t *testing.T) { + s := &store{} + var cp *ethpb.Checkpoint + require.Equal(t, cp, s.PrevFinalizedCheckpt()) + cp = ðpb.Checkpoint{Epoch: 1, Root: []byte{'a'}} + s.SetPrevFinalizedCheckpt(cp) + require.Equal(t, cp, s.PrevFinalizedCheckpt()) +} diff --git a/beacon-chain/blockchain/store/type.go b/beacon-chain/blockchain/store/type.go new file mode 100644 index 0000000000..f8f24f9503 --- /dev/null +++ b/beacon-chain/blockchain/store/type.go @@ -0,0 +1,28 @@ +package store + +import ( + "sync" + + ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" +) + +// store is defined in the fork choice consensus spec for tracking current time and various versions of checkpoints. +// +// Spec code: +// class Store(object): +// time: uint64 +// genesis_time: uint64 +// justified_checkpoint: Checkpoint +// finalized_checkpoint: Checkpoint +// best_justified_checkpoint: Checkpoint +// proposerBoostRoot: Root +type store struct { + justifiedCheckpt *ethpb.Checkpoint + finalizedCheckpt *ethpb.Checkpoint + bestJustifiedCheckpt *ethpb.Checkpoint + sync.RWMutex + // These are not part of the consensus spec, but we do use them to return gRPC API requests. + // TODO(10094): Consider removing in v3. + prevFinalizedCheckpt *ethpb.Checkpoint + prevJustifiedCheckpt *ethpb.Checkpoint +}