diff --git a/beacon-chain/blockchain/forkchoice/process_block.go b/beacon-chain/blockchain/forkchoice/process_block.go index 5afee6a05..6a0338742 100644 --- a/beacon-chain/blockchain/forkchoice/process_block.go +++ b/beacon-chain/blockchain/forkchoice/process_block.go @@ -87,8 +87,34 @@ func (s *Store) OnBlock(ctx context.Context, b *ethpb.BeaconBlock) error { return errors.Wrap(err, "could not save state") } - if err := s.updateCheckpoints(ctx, postState); err != nil { - return errors.Wrap(err, "could not update checkpoint") + // Update justified check point. + if postState.CurrentJustifiedCheckpoint.Epoch > s.JustifiedCheckpt().Epoch { + s.justifiedCheckpt = postState.CurrentJustifiedCheckpoint + if err := s.db.SaveJustifiedCheckpoint(ctx, postState.CurrentJustifiedCheckpoint); err != nil { + return errors.Wrap(err, "could not save justified checkpoint") + } + } + + // Update finalized check point. + // Prune the block cache and helper caches on every new finalized epoch. + if postState.FinalizedCheckpoint.Epoch > s.finalizedCheckpt.Epoch { + s.clearSeenAtts() + helpers.ClearAllCaches() + if err := s.db.SaveFinalizedCheckpoint(ctx, postState.FinalizedCheckpoint); err != nil { + return errors.Wrap(err, "could not save finalized checkpoint") + } + + startSlot := helpers.StartSlot(s.prevFinalizedCheckpt.Epoch) + 1 + endSlot := helpers.StartSlot(s.finalizedCheckpt.Epoch) + if endSlot > startSlot { + if err := s.rmStatesOlderThanLastFinalized(ctx, startSlot, endSlot); err != nil { + return errors.Wrapf(err, "could not delete states prior to finalized check point, range: %d, %d", + startSlot, endSlot) + } + } + + s.prevFinalizedCheckpt = s.finalizedCheckpt + s.finalizedCheckpt = postState.FinalizedCheckpoint } // Update validator indices in database as needed. @@ -148,8 +174,35 @@ func (s *Store) OnBlockNoVerifyStateTransition(ctx context.Context, b *ethpb.Bea return errors.Wrap(err, "could not save state") } - if err := s.updateCheckpoints(ctx, postState); err != nil { - return errors.Wrap(err, "could not update checkpoint") + // Update justified check point. + if postState.CurrentJustifiedCheckpoint.Epoch > s.JustifiedCheckpt().Epoch { + s.justifiedCheckpt = postState.CurrentJustifiedCheckpoint + if err := s.db.SaveJustifiedCheckpoint(ctx, postState.CurrentJustifiedCheckpoint); err != nil { + return errors.Wrap(err, "could not save justified checkpoint") + } + } + + // Update finalized check point. + // Prune the block cache and helper caches on every new finalized epoch. + if postState.FinalizedCheckpoint.Epoch > s.finalizedCheckpt.Epoch { + s.clearSeenAtts() + helpers.ClearAllCaches() + + startSlot := helpers.StartSlot(s.prevFinalizedCheckpt.Epoch) + 1 + endSlot := helpers.StartSlot(s.finalizedCheckpt.Epoch) + if endSlot > startSlot { + if err := s.rmStatesOlderThanLastFinalized(ctx, startSlot, endSlot); err != nil { + return errors.Wrapf(err, "could not delete states prior to finalized check point, range: %d, %d", + startSlot, endSlot) + } + } + + if err := s.db.SaveFinalizedCheckpoint(ctx, postState.FinalizedCheckpoint); err != nil { + return errors.Wrap(err, "could not save finalized checkpoint") + } + + s.prevFinalizedCheckpt = s.finalizedCheckpt + s.finalizedCheckpt = postState.FinalizedCheckpoint } // Update validator indices in database as needed. @@ -261,43 +314,6 @@ func (s *Store) updateBlockAttestationVote(ctx context.Context, att *ethpb.Attes return nil } -func (s *Store) updateCheckpoints(ctx context.Context, postState *pb.BeaconState) error { - s.checkPointLock.Lock() - defer s.checkPointLock.Unlock() - // Update justified check point. - if postState.CurrentJustifiedCheckpoint.Epoch > s.justifiedCheckpt.Epoch { - s.justifiedCheckpt = postState.CurrentJustifiedCheckpoint - if err := s.db.SaveJustifiedCheckpoint(ctx, postState.CurrentJustifiedCheckpoint); err != nil { - return errors.Wrap(err, "could not save justified checkpoint") - } - } - - // Update finalized check point. - // Prune the block cache and helper caches on every new finalized epoch. - if postState.FinalizedCheckpoint.Epoch > s.finalizedCheckpt.Epoch { - s.clearSeenAtts() - helpers.ClearAllCaches() - if err := s.db.SaveFinalizedCheckpoint(ctx, postState.FinalizedCheckpoint); err != nil { - return errors.Wrap(err, "could not save finalized checkpoint") - } - - startSlot := helpers.StartSlot(s.prevFinalizedCheckpt.Epoch) + 1 - endSlot := helpers.StartSlot(s.finalizedCheckpt.Epoch) - if endSlot > startSlot { - if err := s.rmStatesOlderThanLastFinalized(ctx, startSlot, endSlot); err != nil { - return errors.Wrapf(err, "could not delete states prior to finalized check point, range: %d, %d", - startSlot, endSlot) - } - } - - s.prevFinalizedCheckpt = s.finalizedCheckpt - s.finalizedCheckpt = postState.FinalizedCheckpoint - cacheFinalizedEpoch.Set(float64(s.finalizedCheckpt.Epoch)) - cacheFinalizedRoot.Set(float64(bytesutil.ToLowInt64(s.finalizedCheckpt.Root))) - } - return nil -} - // verifyBlkPreState validates input block has a valid pre-state. func (s *Store) verifyBlkPreState(ctx context.Context, b *ethpb.BeaconBlock) (*pb.BeaconState, error) { preState, err := s.db.State(ctx, bytesutil.ToBytes32(b.ParentRoot)) diff --git a/beacon-chain/blockchain/forkchoice/service.go b/beacon-chain/blockchain/forkchoice/service.go index 3a158ddaf..5ff0511da 100644 --- a/beacon-chain/blockchain/forkchoice/service.go +++ b/beacon-chain/blockchain/forkchoice/service.go @@ -36,7 +36,6 @@ type Store struct { db db.Database justifiedCheckpt *ethpb.Checkpoint finalizedCheckpt *ethpb.Checkpoint - checkPointLock sync.RWMutex prevFinalizedCheckpt *ethpb.Checkpoint checkpointState *cache.CheckpointStateCache checkpointStateLock sync.Mutex @@ -79,11 +78,9 @@ func (s *Store) GenesisStore( justifiedCheckpoint *ethpb.Checkpoint, finalizedCheckpoint *ethpb.Checkpoint) error { - s.checkPointLock.Lock() s.justifiedCheckpt = proto.Clone(justifiedCheckpoint).(*ethpb.Checkpoint) s.finalizedCheckpt = proto.Clone(finalizedCheckpoint).(*ethpb.Checkpoint) s.prevFinalizedCheckpt = proto.Clone(finalizedCheckpoint).(*ethpb.Checkpoint) - s.checkPointLock.Unlock() justifiedState, err := s.db.State(ctx, bytesutil.ToBytes32(s.justifiedCheckpt.Root)) if err != nil { @@ -91,7 +88,7 @@ func (s *Store) GenesisStore( } if err := s.checkpointState.AddCheckpointState(&cache.CheckpointState{ - Checkpoint: s.JustifiedCheckpt(), + Checkpoint: s.justifiedCheckpt, State: justifiedState, }); err != nil { return errors.Wrap(err, "could not save genesis state in check point cache") @@ -248,14 +245,10 @@ func (s *Store) Head(ctx context.Context) ([]byte, error) { // JustifiedCheckpt returns the latest justified check point from fork choice store. func (s *Store) JustifiedCheckpt() *ethpb.Checkpoint { - s.checkPointLock.RLock() - defer s.checkPointLock.RUnlock() return proto.Clone(s.justifiedCheckpt).(*ethpb.Checkpoint) } // FinalizedCheckpt returns the latest finalized check point from fork choice store. func (s *Store) FinalizedCheckpt() *ethpb.Checkpoint { - s.checkPointLock.RLock() - defer s.checkPointLock.RUnlock() return proto.Clone(s.finalizedCheckpt).(*ethpb.Checkpoint) } diff --git a/proto/beacon/db/attestation_container.pb.go b/proto/beacon/db/attestation_container.pb.go index 6185c0ae3..5e6b890cf 100755 --- a/proto/beacon/db/attestation_container.pb.go +++ b/proto/beacon/db/attestation_container.pb.go @@ -5,13 +5,12 @@ package db import ( fmt "fmt" - io "io" - math "math" - _ "github.com/gogo/protobuf/gogoproto" proto "github.com/gogo/protobuf/proto" github_com_prysmaticlabs_go_bitfield "github.com/prysmaticlabs/go-bitfield" v1alpha1 "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" + io "io" + math "math" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/proto/beacon/db/finalized_block_root_container.pb.go b/proto/beacon/db/finalized_block_root_container.pb.go index ec2d6ffa7..a7eae5c80 100755 --- a/proto/beacon/db/finalized_block_root_container.pb.go +++ b/proto/beacon/db/finalized_block_root_container.pb.go @@ -5,10 +5,9 @@ package db import ( fmt "fmt" + proto "github.com/gogo/protobuf/proto" io "io" math "math" - - proto "github.com/gogo/protobuf/proto" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/proto/beacon/p2p/v1/messages.pb.go b/proto/beacon/p2p/v1/messages.pb.go index 429de7fc2..c49daf0c2 100755 --- a/proto/beacon/p2p/v1/messages.pb.go +++ b/proto/beacon/p2p/v1/messages.pb.go @@ -5,11 +5,10 @@ package ethereum_beacon_p2p_v1 import ( fmt "fmt" - io "io" - math "math" - _ "github.com/gogo/protobuf/gogoproto" proto "github.com/gogo/protobuf/proto" + io "io" + math "math" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/proto/beacon/p2p/v1/types.pb.go b/proto/beacon/p2p/v1/types.pb.go index 74ff5e6ce..3ff88f859 100755 --- a/proto/beacon/p2p/v1/types.pb.go +++ b/proto/beacon/p2p/v1/types.pb.go @@ -5,13 +5,12 @@ package ethereum_beacon_p2p_v1 import ( fmt "fmt" - io "io" - math "math" - _ "github.com/gogo/protobuf/gogoproto" proto "github.com/gogo/protobuf/proto" github_com_prysmaticlabs_go_bitfield "github.com/prysmaticlabs/go-bitfield" v1alpha1 "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" + io "io" + math "math" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/proto/beacon/rpc/v1/services.pb.go b/proto/beacon/rpc/v1/services.pb.go index 7318fb79c..aa39236d0 100755 --- a/proto/beacon/rpc/v1/services.pb.go +++ b/proto/beacon/rpc/v1/services.pb.go @@ -7,13 +7,12 @@ import ( context "context" encoding_binary "encoding/binary" fmt "fmt" - io "io" - math "math" - proto "github.com/gogo/protobuf/proto" types "github.com/gogo/protobuf/types" v1alpha1 "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" grpc "google.golang.org/grpc" + io "io" + math "math" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/proto/beacon/rpc/v1_gateway/services.pb.go b/proto/beacon/rpc/v1_gateway/services.pb.go index 03608afaa..dde8cd261 100755 --- a/proto/beacon/rpc/v1_gateway/services.pb.go +++ b/proto/beacon/rpc/v1_gateway/services.pb.go @@ -6,14 +6,13 @@ package ethereum_beacon_rpc_v1 import ( context "context" fmt "fmt" - math "math" - proto "github.com/golang/protobuf/proto" empty "github.com/golang/protobuf/ptypes/empty" v1alpha1 "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" grpc "google.golang.org/grpc" codes "google.golang.org/grpc/codes" status "google.golang.org/grpc/status" + math "math" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/proto/eth/v1alpha1/archive.pb.go b/proto/eth/v1alpha1/archive.pb.go index 4067734cc..382fc1dd2 100755 --- a/proto/eth/v1alpha1/archive.pb.go +++ b/proto/eth/v1alpha1/archive.pb.go @@ -5,11 +5,10 @@ package eth import ( fmt "fmt" - io "io" - math "math" - _ "github.com/gogo/protobuf/gogoproto" proto "github.com/gogo/protobuf/proto" + io "io" + math "math" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/proto/eth/v1alpha1/attestation.pb.go b/proto/eth/v1alpha1/attestation.pb.go index 008971640..b68d1b3d0 100755 --- a/proto/eth/v1alpha1/attestation.pb.go +++ b/proto/eth/v1alpha1/attestation.pb.go @@ -5,12 +5,11 @@ package eth import ( fmt "fmt" - io "io" - math "math" - _ "github.com/gogo/protobuf/gogoproto" proto "github.com/gogo/protobuf/proto" github_com_prysmaticlabs_go_bitfield "github.com/prysmaticlabs/go-bitfield" + io "io" + math "math" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/proto/eth/v1alpha1/beacon_block.pb.go b/proto/eth/v1alpha1/beacon_block.pb.go index 552ea64b0..f9eb15a78 100755 --- a/proto/eth/v1alpha1/beacon_block.pb.go +++ b/proto/eth/v1alpha1/beacon_block.pb.go @@ -5,11 +5,10 @@ package eth import ( fmt "fmt" - io "io" - math "math" - _ "github.com/gogo/protobuf/gogoproto" proto "github.com/gogo/protobuf/proto" + io "io" + math "math" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/proto/eth/v1alpha1/beacon_chain.pb.go b/proto/eth/v1alpha1/beacon_chain.pb.go index b9b34139d..9b10c7a36 100755 --- a/proto/eth/v1alpha1/beacon_chain.pb.go +++ b/proto/eth/v1alpha1/beacon_chain.pb.go @@ -6,14 +6,13 @@ package eth import ( context "context" fmt "fmt" - io "io" - math "math" - _ "github.com/gogo/protobuf/gogoproto" proto "github.com/gogo/protobuf/proto" types "github.com/gogo/protobuf/types" _ "google.golang.org/genproto/googleapis/api/annotations" grpc "google.golang.org/grpc" + io "io" + math "math" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/proto/eth/v1alpha1/node.pb.go b/proto/eth/v1alpha1/node.pb.go index 647805e30..2ba95ac9b 100755 --- a/proto/eth/v1alpha1/node.pb.go +++ b/proto/eth/v1alpha1/node.pb.go @@ -6,13 +6,12 @@ package eth import ( context "context" fmt "fmt" - io "io" - math "math" - proto "github.com/gogo/protobuf/proto" types "github.com/gogo/protobuf/types" _ "google.golang.org/genproto/googleapis/api/annotations" grpc "google.golang.org/grpc" + io "io" + math "math" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/proto/eth/v1alpha1/slasher.pb.go b/proto/eth/v1alpha1/slasher.pb.go index d185fe887..08faf1541 100755 --- a/proto/eth/v1alpha1/slasher.pb.go +++ b/proto/eth/v1alpha1/slasher.pb.go @@ -6,12 +6,11 @@ package eth import ( context "context" fmt "fmt" - io "io" - math "math" - proto "github.com/gogo/protobuf/proto" types "github.com/gogo/protobuf/types" grpc "google.golang.org/grpc" + io "io" + math "math" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/proto/eth/v1alpha1/validator.pb.go b/proto/eth/v1alpha1/validator.pb.go index 4fb145895..d4f83587b 100755 --- a/proto/eth/v1alpha1/validator.pb.go +++ b/proto/eth/v1alpha1/validator.pb.go @@ -7,14 +7,13 @@ import ( context "context" encoding_binary "encoding/binary" fmt "fmt" - io "io" - math "math" - _ "github.com/gogo/protobuf/gogoproto" proto "github.com/gogo/protobuf/proto" types "github.com/gogo/protobuf/types" _ "google.golang.org/genproto/googleapis/api/annotations" grpc "google.golang.org/grpc" + io "io" + math "math" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/proto/sharding/p2p/v1/messages.pb.go b/proto/sharding/p2p/v1/messages.pb.go index 61228b93d..ba268c5f4 100644 --- a/proto/sharding/p2p/v1/messages.pb.go +++ b/proto/sharding/p2p/v1/messages.pb.go @@ -5,10 +5,9 @@ package ethereum_sharding_p2p_v1 import ( fmt "fmt" + proto "github.com/gogo/protobuf/proto" io "io" math "math" - - proto "github.com/gogo/protobuf/proto" ) // Reference imports to suppress errors if they are not otherwise used.