Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/addBetterPruning' into addBetter…
Browse files Browse the repository at this point in the history
…Pruning
  • Loading branch information
nisdas committed Feb 24, 2021
2 parents 6a4e287 + 5cda038 commit b47ef68
Show file tree
Hide file tree
Showing 28 changed files with 298 additions and 67 deletions.
4 changes: 2 additions & 2 deletions beacon-chain/blockchain/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type Service struct {
depositCache *depositcache.DepositCache
chainStartFetcher powchain.ChainStartFetcher
attPool attestations.Pool
slashingPool *slashings.Pool
slashingPool slashings.PoolManager
exitPool *voluntaryexits.Pool
genesisTime time.Time
p2p p2p.Broadcaster
Expand Down Expand Up @@ -89,7 +89,7 @@ type Config struct {
DepositCache *depositcache.DepositCache
AttPool attestations.Pool
ExitPool *voluntaryexits.Pool
SlashingPool *slashings.Pool
SlashingPool slashings.PoolManager
P2p p2p.Broadcaster
MaxRoutines int
StateNotifier statefeed.Notifier
Expand Down
15 changes: 7 additions & 8 deletions beacon-chain/db/kv/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"reflect"
"testing"

"github.com/gogo/protobuf/proto"
types "github.com/prysmaticlabs/eth2-types"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/state"
Expand Down Expand Up @@ -57,7 +56,7 @@ func TestGenesisState_CanSaveRetrieve(t *testing.T) {

savedGenesisS, err := db.GenesisState(context.Background())
require.NoError(t, err)
assert.DeepEqual(t, st.InnerStateUnsafe(), savedGenesisS.InnerStateUnsafe(), "Did not retrieve saved state")
assert.DeepSSZEqual(t, st.InnerStateUnsafe(), savedGenesisS.InnerStateUnsafe(), "Did not retrieve saved state")
require.NoError(t, db.SaveGenesisBlockRoot(context.Background(), [32]byte{'C'}))
}

Expand Down Expand Up @@ -197,15 +196,15 @@ func TestStore_SaveDeleteState_CanGetHighestBelow(t *testing.T) {

highest, err := db.HighestSlotStatesBelow(context.Background(), 2)
require.NoError(t, err)
assert.Equal(t, true, proto.Equal(highest[0].InnerStateUnsafe(), s0), "Did not retrieve saved state: %v != %v", highest, s0)
assert.DeepSSZEqual(t, highest[0].InnerStateUnsafe(), s0)

highest, err = db.HighestSlotStatesBelow(context.Background(), 101)
require.NoError(t, err)
assert.Equal(t, true, proto.Equal(highest[0].InnerStateUnsafe(), s1), "Did not retrieve saved state: %v != %v", highest, s1)
assert.DeepSSZEqual(t, highest[0].InnerStateUnsafe(), s1)

highest, err = db.HighestSlotStatesBelow(context.Background(), 1001)
require.NoError(t, err)
assert.Equal(t, true, proto.Equal(highest[0].InnerStateUnsafe(), s2), "Did not retrieve saved state: %v != %v", highest, s2)
assert.DeepSSZEqual(t, highest[0].InnerStateUnsafe(), s2)
}

func TestStore_GenesisState_CanGetHighestBelow(t *testing.T) {
Expand All @@ -230,14 +229,14 @@ func TestStore_GenesisState_CanGetHighestBelow(t *testing.T) {

highest, err := db.HighestSlotStatesBelow(context.Background(), 2)
require.NoError(t, err)
assert.Equal(t, true, proto.Equal(highest[0].InnerStateUnsafe(), st.InnerStateUnsafe()))
assert.DeepSSZEqual(t, highest[0].InnerStateUnsafe(), st.InnerStateUnsafe())

highest, err = db.HighestSlotStatesBelow(context.Background(), 1)
require.NoError(t, err)
assert.Equal(t, true, proto.Equal(highest[0].InnerStateUnsafe(), genesisState.InnerStateUnsafe()))
assert.DeepSSZEqual(t, highest[0].InnerStateUnsafe(), genesisState.InnerStateUnsafe())
highest, err = db.HighestSlotStatesBelow(context.Background(), 0)
require.NoError(t, err)
assert.Equal(t, true, proto.Equal(highest[0].InnerStateUnsafe(), genesisState.InnerStateUnsafe()))
assert.DeepSSZEqual(t, highest[0].InnerStateUnsafe(), genesisState.InnerStateUnsafe())
}

func TestStore_CleanUpDirtyStates_AboveThreshold(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion beacon-chain/db/restore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path"
"testing"

types "github.com/prysmaticlabs/eth2-types"
"github.com/prysmaticlabs/prysm/beacon-chain/db/kv"
"github.com/prysmaticlabs/prysm/shared/cmd"
"github.com/prysmaticlabs/prysm/shared/testutil"
Expand Down Expand Up @@ -65,7 +66,7 @@ func TestRestore(t *testing.T) {
require.NoError(t, err)
headBlock, err := restoredDb.HeadBlock(ctx)
require.NoError(t, err)
assert.Equal(t, uint64(5000), headBlock.Block.Slot, "Restored database has incorrect data")
assert.Equal(t, types.Slot(5000), headBlock.Block.Slot, "Restored database has incorrect data")
assert.LogsContain(t, logHook, "Restore completed successfully")

}
2 changes: 1 addition & 1 deletion beacon-chain/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ type BeaconNode struct {
db db.Database
attestationPool attestations.Pool
exitPool *voluntaryexits.Pool
slashingsPool *slashings.Pool
slashingsPool slashings.PoolManager
depositCache *depositcache.DepositCache
stateFeed *event.Feed
blockFeed *event.Feed
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/operations/attestations/kv/aggregated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,6 @@ func TestKV_Aggregated_DuplicateAggregatedAttestations(t *testing.T) {
returned := cache.AggregatedAttestations()

// It should have only returned att2.
assert.DeepEqual(t, att2, returned[0], "Did not receive correct aggregated atts")
assert.DeepSSZEqual(t, att2, returned[0], "Did not receive correct aggregated atts")
assert.Equal(t, 1, len(returned), "Did not receive correct aggregated atts")
}
1 change: 1 addition & 0 deletions beacon-chain/operations/slashings/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ go_library(
"doc.go",
"log.go",
"metrics.go",
"mock.go",
"service.go",
"types.go",
],
Expand Down
43 changes: 43 additions & 0 deletions beacon-chain/operations/slashings/mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package slashings

import (
"context"

ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/state"
)

// PoolMock is a fake implementation of PoolManager.
type PoolMock struct {
PendingAttSlashings []*ethpb.AttesterSlashing
}

// PendingAttesterSlashings --
func (m *PoolMock) PendingAttesterSlashings(ctx context.Context, state *state.BeaconState, noLimit bool) []*ethpb.AttesterSlashing {
return m.PendingAttSlashings
}

// PendingProposerSlashings --
func (m *PoolMock) PendingProposerSlashings(ctx context.Context, state *state.BeaconState, noLimit bool) []*ethpb.ProposerSlashing {
panic("implement me")
}

// InsertAttesterSlashing --
func (m *PoolMock) InsertAttesterSlashing(ctx context.Context, state *state.BeaconState, slashing *ethpb.AttesterSlashing) error {
panic("implement me")
}

// InsertProposerSlashing --
func (m *PoolMock) InsertProposerSlashing(ctx context.Context, state *state.BeaconState, slashing *ethpb.ProposerSlashing) error {
panic("implement me")
}

// MarkIncludedAttesterSlashing --
func (m *PoolMock) MarkIncludedAttesterSlashing(as *ethpb.AttesterSlashing) {
panic("implement me")
}

// MarkIncludedProposerSlashing --
func (m *PoolMock) MarkIncludedProposerSlashing(ps *ethpb.ProposerSlashing) {
panic("implement me")
}
24 changes: 22 additions & 2 deletions beacon-chain/operations/slashings/types.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
package slashings

import (
"context"
"sync"

types "github.com/prysmaticlabs/eth2-types"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/state"
)

// Pool implements a struct to maintain pending and recently included attester and
// proposer slashings. This pool is used by proposers to insert into new blocks.
// PoolManager maintains a pool of pending and recently included attester and proposer slashings.
// This pool is used by proposers to insert data into new blocks.
type PoolManager interface {
PendingAttesterSlashings(ctx context.Context, state *state.BeaconState, noLimit bool) []*ethpb.AttesterSlashing
PendingProposerSlashings(ctx context.Context, state *state.BeaconState, noLimit bool) []*ethpb.ProposerSlashing
InsertAttesterSlashing(
ctx context.Context,
state *state.BeaconState,
slashing *ethpb.AttesterSlashing,
) error
InsertProposerSlashing(
ctx context.Context,
state *state.BeaconState,
slashing *ethpb.ProposerSlashing,
) error
MarkIncludedAttesterSlashing(as *ethpb.AttesterSlashing)
MarkIncludedProposerSlashing(ps *ethpb.ProposerSlashing)
}

// Pool is a concrete implementation of PoolManager.
type Pool struct {
lock sync.RWMutex
pendingProposerSlashing []*ethpb.ProposerSlashing
Expand Down
7 changes: 6 additions & 1 deletion beacon-chain/rpc/beacon/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ func TestServer_ListBlocks_Genesis_MultiBlocks(t *testing.T) {
}

func TestServer_ListBlocks_Pagination(t *testing.T) {
params.UseMinimalConfig()
defer params.UseMainnetConfig()

db := dbTest.SetupDB(t)
chain := &chainMock.ChainService{
CanonicalRoots: map[[32]byte]bool{},
Expand Down Expand Up @@ -357,8 +360,10 @@ func TestServer_GetChainHead_NoHeadBlock(t *testing.T) {
}

func TestServer_GetChainHead(t *testing.T) {
db := dbTest.SetupDB(t)
params.UseMinimalConfig()
defer params.UseMainnetConfig()

db := dbTest.SetupDB(t)
genBlock := testutil.NewBeaconBlock()
genBlock.Block.ParentRoot = bytesutil.PadTo([]byte{'G'}, 32)
require.NoError(t, db.SaveBlock(context.Background(), genBlock))
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/rpc/beacon/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type Server struct {
AttestationNotifier operation.Notifier
Broadcaster p2p.Broadcaster
AttestationsPool attestations.Pool
SlashingsPool *slashings.Pool
SlashingsPool slashings.PoolManager
CanonicalStateChan chan *pbp2p.BeaconState
ChainStartChan chan time.Time
ReceivedAttestationsBuffer chan *ethpb.Attestation
Expand Down
10 changes: 8 additions & 2 deletions beacon-chain/rpc/beacon/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1556,8 +1556,10 @@ func TestServer_GetValidatorParticipation_CurrentAndPrevEpoch(t *testing.T) {
}

func TestServer_GetValidatorParticipation_OrphanedUntilGenesis(t *testing.T) {
beaconDB := dbTest.SetupDB(t)
helpers.ClearCache()
params.UseMainnetConfig()

beaconDB := dbTest.SetupDB(t)
ctx := context.Background()
validatorCount := uint64(100)

Expand Down Expand Up @@ -1644,6 +1646,10 @@ func TestGetValidatorPerformance_Syncing(t *testing.T) {
}

func TestGetValidatorPerformance_OK(t *testing.T) {
helpers.ClearCache()
params.UseMinimalConfig()
defer params.UseMainnetConfig()

ctx := context.Background()
epoch := types.Epoch(1)
headState, err := testutil.NewBeaconState()
Expand Down Expand Up @@ -1916,9 +1922,9 @@ func TestServer_GetIndividualVotes_RequestFutureSlot(t *testing.T) {
}

func TestServer_GetIndividualVotes_ValidatorsDontExist(t *testing.T) {

params.UseMinimalConfig()
defer params.UseMainnetConfig()

beaconDB := dbTest.SetupDB(t)
ctx := context.Background()

Expand Down
2 changes: 2 additions & 0 deletions beacon-chain/rpc/beaconv1/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ go_test(
srcs = [
"blocks_test.go",
"config_test.go",
"pool_test.go",
"server_test.go",
"state_test.go",
],
Expand All @@ -61,6 +62,7 @@ go_test(
"//beacon-chain/blockchain/testing:go_default_library",
"//beacon-chain/db:go_default_library",
"//beacon-chain/db/testing:go_default_library",
"//beacon-chain/operations/slashings:go_default_library",
"//beacon-chain/p2p/testing:go_default_library",
"//beacon-chain/powchain/testing:go_default_library",
"//beacon-chain/state/stategen:go_default_library",
Expand Down
23 changes: 21 additions & 2 deletions beacon-chain/rpc/beaconv1/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import (
"errors"

ptypes "github.com/gogo/protobuf/types"

ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1"
"github.com/prysmaticlabs/prysm/proto/migration"
"go.opencensus.io/trace"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

// ListPoolAttestations retrieves attestations known by the node but
Expand All @@ -24,7 +27,23 @@ func (bs *Server) SubmitAttestation(ctx context.Context, req *ethpb.Attestation)
// ListPoolAttesterSlashings retrieves attester slashings known by the node but
// not necessarily incorporated into any block.
func (bs *Server) ListPoolAttesterSlashings(ctx context.Context, req *ptypes.Empty) (*ethpb.AttesterSlashingsPoolResponse, error) {
return nil, errors.New("unimplemented")
ctx, span := trace.StartSpan(ctx, "beaconv1.ListPoolAttesterSlashings")
defer span.End()

headState, err := bs.ChainInfoFetcher.HeadState(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not get head state: %v", err)
}
sourceSlashings := bs.SlashingsPool.PendingAttesterSlashings(ctx, headState, true)

slashings := make([]*ethpb.AttesterSlashing, len(sourceSlashings))
for i, s := range sourceSlashings {
slashings[i] = migration.V1Alpha1AttSlashingToV1(s)
}

return &ethpb.AttesterSlashingsPoolResponse{
Data: slashings,
}, nil
}

// SubmitAttesterSlashing submits AttesterSlashing object to node's pool and
Expand Down
Loading

0 comments on commit b47ef68

Please sign in to comment.