diff --git a/beacon-chain/blockchain/service.go b/beacon-chain/blockchain/service.go index 223c43dd5c..9d14a0e575 100644 --- a/beacon-chain/blockchain/service.go +++ b/beacon-chain/blockchain/service.go @@ -212,14 +212,3 @@ func (c *ChainService) ChainHeadRoot() ([32]byte, error) { } return root, nil } - -// doesPoWBlockExist checks if the referenced PoW block exists. -func (c *ChainService) doesPoWBlockExist(hash [32]byte) bool { - powBlock, err := c.web3Service.Client().BlockByHash(c.ctx, hash) - if err != nil { - log.Debugf("fetching PoW block corresponding to mainchain reference failed: %v", err) - return false - } - - return powBlock != nil -} diff --git a/beacon-chain/core/attestations/BUILD.bazel b/beacon-chain/core/attestations/BUILD.bazel deleted file mode 100644 index 51322fbad6..0000000000 --- a/beacon-chain/core/attestations/BUILD.bazel +++ /dev/null @@ -1,19 +0,0 @@ -load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") - -go_library( - name = "go_default_library", - srcs = ["attestation.go"], - importpath = "github.com/prysmaticlabs/prysm/beacon-chain/core/attestations", - visibility = ["//beacon-chain:__subpackages__"], - deps = [ - "//beacon-chain/core/helpers:go_default_library", - "//proto/beacon/p2p/v1:go_default_library", - ], -) - -go_test( - name = "go_default_test", - srcs = ["attestation_test.go"], - embed = [":go_default_library"], - deps = ["//proto/beacon/p2p/v1:go_default_library"], -) diff --git a/beacon-chain/core/attestations/attestation.go b/beacon-chain/core/attestations/attestation.go deleted file mode 100644 index 7ad8bbec9d..0000000000 --- a/beacon-chain/core/attestations/attestation.go +++ /dev/null @@ -1,48 +0,0 @@ -// Package attestations tracks the life-cycle of the latest attestations -// from each validator. It also contains libraries to create attestation -// message, verify attestation correctness and slashing conditions. -package attestations - -import ( - "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" - pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" -) - -// IsDoubleVote checks if both of the attestations have been used to vote for the same slot. -// Spec: -// def is_double_vote(attestation_data_1: AttestationData, -// attestation_data_2: AttestationData) -> bool -// """ -// Checks if the two ``AttestationData`` have the same target. -// """ -// target_epoch_1 = slot_to_epoch(attestation_data_1.slot) -// target_epoch_2 = slot_to_epoch(attestation_data_2.slot) -// return target_epoch_1 == target_epoch_2 -func IsDoubleVote(attestation1 *pb.AttestationData, attestation2 *pb.AttestationData) bool { - targetEpoch1 := helpers.SlotToEpoch(attestation1.Slot) - targetEpoch2 := helpers.SlotToEpoch(attestation2.Slot) - return targetEpoch1 == targetEpoch2 -} - -// IsSurroundVote checks if the data provided by the attestations fulfill the conditions for -// a surround vote. -// Spec: -// def is_surround_vote(attestation_data_1: AttestationData, -// attestation_data_2: AttestationData) -> bool: -// """ -// Checks if ``attestation_data_1`` surrounds ``attestation_data_2``. -// """ -// source_epoch_1 = attestation_data_1.justified_epoch -// source_epoch_2 = attestation_data_2.justified_epoch -// target_epoch_1 = slot_to_epoch(attestation_data_1.slot) -// target_epoch_2 = slot_to_epoch(attestation_data_2.slot) -// -// return source_epoch_1 < source_epoch_2 and target_epoch_2 < target_epoch_1 -func IsSurroundVote(attestation1 *pb.AttestationData, attestation2 *pb.AttestationData) bool { - sourceEpoch1 := attestation1.JustifiedEpoch - sourceEpoch2 := attestation2.JustifiedEpoch - targetEpoch1 := helpers.SlotToEpoch(attestation1.Slot) - targetEpoch2 := helpers.SlotToEpoch(attestation2.Slot) - - return sourceEpoch1 < sourceEpoch2 && targetEpoch2 < targetEpoch1 -} diff --git a/beacon-chain/core/attestations/attestation_test.go b/beacon-chain/core/attestations/attestation_test.go deleted file mode 100644 index a92621143f..0000000000 --- a/beacon-chain/core/attestations/attestation_test.go +++ /dev/null @@ -1,51 +0,0 @@ -package attestations - -import ( - "testing" - - pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" -) - -func TestIsDoubleVote_SameAndDifferentEpochs(t *testing.T) { - att1 := &pb.AttestationData{ - Slot: 0, - } - - att2 := &pb.AttestationData{ - Slot: 64, - } - - if IsDoubleVote(att1, att2) { - t.Error("It is a double vote despite the attestations being on different epochs") - } - - att2.Slot = 1 - - if !IsDoubleVote(att1, att2) { - t.Error("It is not a double vote despite the attestations being on the same epoch") - } -} - -func TestIsSurroundVote_SameAndDifferentEpochs(t *testing.T) { - att1 := &pb.AttestationData{ - Slot: 0, - JustifiedEpoch: 0, - } - - att2 := &pb.AttestationData{ - Slot: 0, - JustifiedEpoch: 0, - } - - if IsSurroundVote(att1, att2) { - t.Error("It is a surround vote despite both attestations having the same epoch") - } - - att1.Slot = 192 - att2.JustifiedEpoch = 1 - att2.Slot = 128 - - if !IsSurroundVote(att1, att2) { - t.Error("It is not a surround vote despite all the surround conditions being fulfilled") - } -} diff --git a/beacon-chain/core/blocks/block.go b/beacon-chain/core/blocks/block.go index e6bf0021c6..846e0bd0f7 100644 --- a/beacon-chain/core/blocks/block.go +++ b/beacon-chain/core/blocks/block.go @@ -4,7 +4,6 @@ package blocks import ( - "bytes" "context" "fmt" @@ -60,23 +59,3 @@ func ProcessBlockRoots(ctx context.Context, state *pb.BeaconState, parentRoot [3 } return state } - -// BlockChildren obtains the blocks in a list of observed blocks which have the current -// beacon block's hash as their parent root hash. -// -// Spec pseudocode definition: -// Let get_children(store: Store, block: BeaconBlock) -> -// List[BeaconBlock] returns the child blocks of the given block. -func BlockChildren(block *pb.BeaconBlock, observedBlocks []*pb.BeaconBlock) ([]*pb.BeaconBlock, error) { - var children []*pb.BeaconBlock - root, err := hashutil.HashBeaconBlock(block) - if err != nil { - return nil, fmt.Errorf("could not hash block: %v", err) - } - for _, observed := range observedBlocks { - if bytes.Equal(observed.ParentRootHash32, root[:]) { - children = append(children, observed) - } - } - return children, nil -} diff --git a/beacon-chain/core/epoch/epoch_operations.go b/beacon-chain/core/epoch/epoch_operations.go index 083f197af5..6f18a63810 100644 --- a/beacon-chain/core/epoch/epoch_operations.go +++ b/beacon-chain/core/epoch/epoch_operations.go @@ -95,33 +95,6 @@ func PrevAttestations(ctx context.Context, state *pb.BeaconState) []*pb.PendingA return prevEpochAttestations } -// PrevJustifiedAttestations returns the justified attestations -// of the previous 2 epochs. -// -// Spec pseudocode definition: -// return [a for a in current_epoch_attestations + previous_epoch_attestations -// if a.data.justified_epoch == state.previous_justified_epoch] -func PrevJustifiedAttestations( - ctx context.Context, - state *pb.BeaconState, - currentEpochAttestations []*pb.PendingAttestation, - prevEpochAttestations []*pb.PendingAttestation, -) []*pb.PendingAttestation { - - ctx, span := trace.StartSpan(ctx, "beacon-chain.ChainService.state.ProcessEpoch.PrevJustifiedAttestations") - defer span.End() - - var prevJustifiedAttestations []*pb.PendingAttestation - epochAttestations := append(currentEpochAttestations, prevEpochAttestations...) - - for _, attestation := range epochAttestations { - if attestation.Data.JustifiedEpoch == state.PreviousJustifiedEpoch { - prevJustifiedAttestations = append(prevJustifiedAttestations, attestation) - } - } - return prevJustifiedAttestations -} - // PrevEpochBoundaryAttestations returns the boundary attestations // at the start of the previous epoch. // diff --git a/beacon-chain/core/epoch/epoch_operations_test.go b/beacon-chain/core/epoch/epoch_operations_test.go index d996c87d6f..e9aac01c1a 100644 --- a/beacon-chain/core/epoch/epoch_operations_test.go +++ b/beacon-chain/core/epoch/epoch_operations_test.go @@ -212,39 +212,6 @@ func TestPrevEpochAttestations_AccurateAttestationSlots(t *testing.T) { } } -func TestPrevJustifiedAttestations_AccurateShardsAndEpoch(t *testing.T) { - prevEpochAttestations := []*pb.PendingAttestation{ - {Data: &pb.AttestationData{JustifiedEpoch: 0}}, - {Data: &pb.AttestationData{JustifiedEpoch: 0}}, - {Data: &pb.AttestationData{JustifiedEpoch: 0}}, - {Data: &pb.AttestationData{Shard: 2, JustifiedEpoch: 1}}, - {Data: &pb.AttestationData{Shard: 3, JustifiedEpoch: 1}}, - {Data: &pb.AttestationData{JustifiedEpoch: 15}}, - } - - thisEpochAttestations := []*pb.PendingAttestation{ - {Data: &pb.AttestationData{JustifiedEpoch: 0}}, - {Data: &pb.AttestationData{JustifiedEpoch: 0}}, - {Data: &pb.AttestationData{JustifiedEpoch: 0}}, - {Data: &pb.AttestationData{JustifiedEpoch: 1}}, - {Data: &pb.AttestationData{Shard: 1, JustifiedEpoch: 1}}, - {Data: &pb.AttestationData{JustifiedEpoch: 13}}, - } - - state := &pb.BeaconState{PreviousJustifiedEpoch: 1} - - prevJustifiedAttestations := PrevJustifiedAttestations(context.Background(), state, thisEpochAttestations, prevEpochAttestations) - - for i, attestation := range prevJustifiedAttestations { - if attestation.Data.Shard != uint64(i) { - t.Errorf("Wanted shard %d, got %d", i, attestation.Data.Shard) - } - if attestation.Data.JustifiedEpoch != 1 { - t.Errorf("Wanted justified epoch 0, got %d", attestation.Data.JustifiedEpoch) - } - } -} - func TestPrevEpochBoundaryAttestations_AccurateAttestationData(t *testing.T) { if params.BeaconConfig().SlotsPerEpoch != 64 { t.Errorf("SlotsPerEpoch should be 64 for these tests to pass") diff --git a/beacon-chain/core/helpers/slot_epoch.go b/beacon-chain/core/helpers/slot_epoch.go index 4800f5ec92..23f3f845b1 100644 --- a/beacon-chain/core/helpers/slot_epoch.go +++ b/beacon-chain/core/helpers/slot_epoch.go @@ -56,11 +56,6 @@ func StartSlot(epoch uint64) uint64 { return epoch * params.BeaconConfig().SlotsPerEpoch } -// AttestationCurrentEpoch returns the current epoch referenced by the attestation. -func AttestationCurrentEpoch(att *pb.AttestationData) uint64 { - return SlotToEpoch(att.Slot) -} - // IsEpochStart returns true if the given slot number is an epoch starting slot // number. func IsEpochStart(slot uint64) bool { diff --git a/beacon-chain/core/helpers/slot_epoch_test.go b/beacon-chain/core/helpers/slot_epoch_test.go index 6047eab06a..9f72f3fbd3 100644 --- a/beacon-chain/core/helpers/slot_epoch_test.go +++ b/beacon-chain/core/helpers/slot_epoch_test.go @@ -97,23 +97,6 @@ func TestEpochStartSlot_OK(t *testing.T) { } } -func TestAttestationCurrentEpoch_OK(t *testing.T) { - tests := []struct { - slot uint64 - epoch uint64 - }{ - {slot: 0 * params.BeaconConfig().SlotsPerEpoch, epoch: 0}, - {slot: 1 * params.BeaconConfig().SlotsPerEpoch, epoch: 1}, - {slot: 10 * params.BeaconConfig().SlotsPerEpoch, epoch: 10}, - } - for _, tt := range tests { - attData := &pb.AttestationData{Slot: tt.slot} - if tt.epoch != AttestationCurrentEpoch(attData) { - t.Errorf("AttestationEpoch(%d) = %d, wanted: %d", attData.Slot, AttestationCurrentEpoch(attData), tt.epoch) - } - } -} - func TestIsEpochStart(t *testing.T) { epochLength := params.BeaconConfig().SlotsPerEpoch diff --git a/beacon-chain/db/block.go b/beacon-chain/db/block.go index b1582b121d..d26a540c21 100644 --- a/beacon-chain/db/block.go +++ b/beacon-chain/db/block.go @@ -246,34 +246,6 @@ func (db *BeaconDB) BlockBySlot(ctx context.Context, slot uint64) (*pb.BeaconBlo return block, err } -// HasBlockBySlot returns a boolean, and if the block exists, it returns the block. -func (db *BeaconDB) HasBlockBySlot(slot uint64) (bool, *pb.BeaconBlock, error) { - var block *pb.BeaconBlock - var exists bool - slotEnc := encodeSlotNumber(slot) - - err := db.view(func(tx *bolt.Tx) error { - mainChain := tx.Bucket(mainChainBucket) - blockBkt := tx.Bucket(blockBucket) - - blockRoot := mainChain.Get(slotEnc) - if blockRoot == nil { - return nil - } - - enc := blockBkt.Get(blockRoot) - if enc == nil { - return nil - } - exists = true - - var err error - block, err = createBlock(enc) - return err - }) - return exists, block, err -} - // HighestBlockSlot returns the in-memory value for the highest block we've // seen in the database. func (db *BeaconDB) HighestBlockSlot() uint64 { diff --git a/beacon-chain/db/block_test.go b/beacon-chain/db/block_test.go index 6083c55a67..147b0ee5db 100644 --- a/beacon-chain/db/block_test.go +++ b/beacon-chain/db/block_test.go @@ -238,46 +238,6 @@ func TestChainProgress_OK(t *testing.T) { } } -func TestHasBlockBySlot_OK(t *testing.T) { - db := setupDB(t) - defer teardownDB(t, db) - ctx := context.Background() - - blkSlot := uint64(10) - block1 := &pb.BeaconBlock{ - Slot: blkSlot, - } - - exists, _, err := db.HasBlockBySlot(blkSlot) - if err != nil { - t.Fatalf("failed to get block: %v", err) - } - if exists { - t.Error("Block exists despite being not being saved") - } - - if err := db.SaveBlock(block1); err != nil { - t.Fatalf("save block failed: %v", err) - } - - if err := db.UpdateChainHead(ctx, block1, &pb.BeaconState{}); err != nil { - t.Fatalf("Unable to save block and state in db %v", err) - } - - exists, blk, err := db.HasBlockBySlot(blkSlot) - if err != nil { - t.Fatalf("failed to get block: %v", err) - } - if !exists { - t.Error("Block does not exist in db") - } - - if blk.Slot != blkSlot { - t.Errorf("Saved block does not have the slot from which it was requested") - } - -} - func TestJustifiedBlock_NoneExists(t *testing.T) { db := setupDB(t) defer teardownDB(t, db) diff --git a/beacon-chain/db/state.go b/beacon-chain/db/state.go index 92cc499dcc..beb91ae4f3 100644 --- a/beacon-chain/db/state.go +++ b/beacon-chain/db/state.go @@ -5,8 +5,6 @@ import ( "encoding/binary" "errors" "fmt" - "time" - "github.com/boltdb/bolt" "github.com/gogo/protobuf/proto" "github.com/prometheus/client_golang/prometheus" @@ -318,19 +316,6 @@ func createState(enc []byte) (*pb.BeaconState, error) { return protoState, nil } -// GenesisTime returns the genesis timestamp for the state. -func (db *BeaconDB) GenesisTime(ctx context.Context) (time.Time, error) { - state, err := db.HeadState(ctx) - if err != nil { - return time.Time{}, fmt.Errorf("could not retrieve state: %v", err) - } - if state == nil { - return time.Time{}, fmt.Errorf("state not found: %v", err) - } - genesisTime := time.Unix(int64(state.GenesisTime), int64(0)) - return genesisTime, nil -} - func (db *BeaconDB) deleteHistoricalStates(slot uint64) error { if !featureconfig.FeatureConfig().EnableHistoricalStatePruning { return nil diff --git a/beacon-chain/db/state_test.go b/beacon-chain/db/state_test.go index bdc27eed85..29e547ac75 100644 --- a/beacon-chain/db/state_test.go +++ b/beacon-chain/db/state_test.go @@ -88,35 +88,6 @@ func TestInitializeState_OK(t *testing.T) { } } -func TestGenesisTime_OK(t *testing.T) { - db := setupDB(t) - defer teardownDB(t, db) - ctx := context.Background() - - genesisTime, err := db.GenesisTime(ctx) - if err == nil { - t.Fatal("Expected GenesisTime to fail") - } - - deposits, _ := setupInitialDeposits(t, 10) - if err := db.InitializeState(uint64(genesisTime.Unix()), deposits, &pb.Eth1Data{}); err != nil { - t.Fatalf("Failed to initialize state: %v", err) - } - - time1, err := db.GenesisTime(ctx) - if err != nil { - t.Fatalf("GenesisTime failed on second attempt: %v", err) - } - time2, err := db.GenesisTime(ctx) - if err != nil { - t.Fatalf("GenesisTime failed on second attempt: %v", err) - } - - if time1 != time2 { - t.Fatalf("Expected %v and %v to be equal", time1, time2) - } -} - func TestFinalizeState_OK(t *testing.T) { db := setupDB(t) defer teardownDB(t, db)