Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wire voluntary exits pool #4613

Merged
merged 15 commits into from Jan 22, 2020
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions beacon-chain/blockchain/BUILD.bazel
Expand Up @@ -24,6 +24,7 @@ go_library(
"//beacon-chain/core/state:go_default_library",
"//beacon-chain/db:go_default_library",
"//beacon-chain/operations/attestations:go_default_library",
"//beacon-chain/operations/voluntaryexits:go_default_library",
"//beacon-chain/p2p:go_default_library",
"//beacon-chain/powchain:go_default_library",
"//proto/beacon/p2p/v1:go_default_library",
Expand Down
3 changes: 3 additions & 0 deletions beacon-chain/blockchain/receive_block.go
Expand Up @@ -125,6 +125,9 @@ func (s *Service) ReceiveBlockNoPubsub(ctx context.Context, block *ethpb.SignedB
log.Errorf("Could not save attestation for fork choice: %v", err)
return nil
}
for _, exit := range block.Block.Body.VoluntaryExits {
s.exitPool.MarkIncluded(exit)
}

// Reports on block and fork choice metrics.
s.reportSlotMetrics(blockCopy.Block.Slot)
Expand Down
4 changes: 4 additions & 0 deletions beacon-chain/blockchain/service.go
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
"github.com/prysmaticlabs/prysm/beacon-chain/db"
"github.com/prysmaticlabs/prysm/beacon-chain/operations/attestations"
"github.com/prysmaticlabs/prysm/beacon-chain/operations/voluntaryexits"
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
"github.com/prysmaticlabs/prysm/beacon-chain/powchain"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
Expand All @@ -43,6 +44,7 @@ type Service struct {
depositCache *depositcache.DepositCache
chainStartFetcher powchain.ChainStartFetcher
attPool attestations.Pool
exitPool *voluntaryexits.Pool
forkChoiceStore forkchoice.ForkChoicer
genesisTime time.Time
p2p p2p.Broadcaster
Expand All @@ -65,6 +67,7 @@ type Config struct {
BeaconDB db.HeadAccessDatabase
DepositCache *depositcache.DepositCache
AttPool attestations.Pool
ExitPool *voluntaryexits.Pool
P2p p2p.Broadcaster
MaxRoutines int64
StateNotifier statefeed.Notifier
Expand All @@ -82,6 +85,7 @@ func NewService(ctx context.Context, cfg *Config) (*Service, error) {
depositCache: cfg.DepositCache,
chainStartFetcher: cfg.ChainStartFetcher,
attPool: cfg.AttPool,
exitPool: cfg.ExitPool,
forkChoiceStore: store,
p2p: cfg.P2p,
canonicalRoots: make(map[uint64][]byte),
Expand Down
1 change: 1 addition & 0 deletions beacon-chain/node/BUILD.bazel
Expand Up @@ -17,6 +17,7 @@ go_library(
"//beacon-chain/gateway:go_default_library",
"//beacon-chain/interop-cold-start:go_default_library",
"//beacon-chain/operations/attestations:go_default_library",
"//beacon-chain/operations/voluntaryexits:go_default_library",
"//beacon-chain/p2p:go_default_library",
"//beacon-chain/powchain:go_default_library",
"//beacon-chain/rpc:go_default_library",
Expand Down
6 changes: 6 additions & 0 deletions beacon-chain/node/node.go
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/prysmaticlabs/prysm/beacon-chain/gateway"
interopcoldstart "github.com/prysmaticlabs/prysm/beacon-chain/interop-cold-start"
"github.com/prysmaticlabs/prysm/beacon-chain/operations/attestations"
"github.com/prysmaticlabs/prysm/beacon-chain/operations/voluntaryexits"
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
"github.com/prysmaticlabs/prysm/beacon-chain/powchain"
"github.com/prysmaticlabs/prysm/beacon-chain/rpc"
Expand Down Expand Up @@ -58,6 +59,7 @@ type BeaconNode struct {
stop chan struct{} // Channel to wait for termination notifications.
db db.Database
attestationPool attestations.Pool
exitPool *voluntaryexits.Pool
depositCache *depositcache.DepositCache
stateFeed *event.Feed
opFeed *event.Feed
Expand Down Expand Up @@ -101,6 +103,7 @@ func NewBeaconNode(ctx *cli.Context) (*BeaconNode, error) {
stateFeed: new(event.Feed),
opFeed: new(event.Feed),
attestationPool: attestations.NewPool(),
exitPool: voluntaryexits.NewPool(),
}

if err := beacon.startDB(ctx); err != nil {
Expand Down Expand Up @@ -303,6 +306,7 @@ func (b *BeaconNode) registerBlockchainService(ctx *cli.Context) error {
DepositCache: b.depositCache,
ChainStartFetcher: web3Service,
AttPool: b.attestationPool,
ExitPool: b.exitPool,
P2p: b.fetchP2P(ctx),
MaxRoutines: maxRoutines,
StateNotifier: b,
Expand Down Expand Up @@ -391,6 +395,7 @@ func (b *BeaconNode) registerSyncService(ctx *cli.Context) error {
InitialSync: initSync,
StateNotifier: b,
AttPool: b.attestationPool,
ExitPool: b.exitPool,
})

return b.services.RegisterService(rs)
Expand Down Expand Up @@ -470,6 +475,7 @@ func (b *BeaconNode) registerRPCService(ctx *cli.Context) error {
AttestationReceiver: chainService,
GenesisTimeFetcher: chainService,
AttestationsPool: b.attestationPool,
ExitPool: b.exitPool,
POWChainService: web3Service,
ChainStartFetcher: chainStartFetcher,
MockEth1Votes: mockEth1DataVotes,
Expand Down
3 changes: 0 additions & 3 deletions beacon-chain/operations/voluntaryexits/BUILD.bazel
Expand Up @@ -9,7 +9,6 @@ go_library(
importpath = "github.com/prysmaticlabs/prysm/beacon-chain/operations/voluntaryexits",
visibility = ["//beacon-chain:__subpackages__"],
deps = [
"//beacon-chain/blockchain:go_default_library",
"//beacon-chain/core/helpers:go_default_library",
"//shared/params:go_default_library",
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
Expand All @@ -22,8 +21,6 @@ go_test(
srcs = ["service_test.go"],
embed = [":go_default_library"],
deps = [
"//beacon-chain/blockchain/testing:go_default_library",
"//proto/beacon/p2p/v1:go_default_library",
"//shared/params:go_default_library",
"@com_github_gogo_protobuf//proto:go_default_library",
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
Expand Down
9 changes: 3 additions & 6 deletions beacon-chain/operations/voluntaryexits/service.go
Expand Up @@ -6,7 +6,6 @@ import (
"sync"

ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/blockchain"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/shared/params"
)
Expand All @@ -17,16 +16,14 @@ type Pool struct {
lock sync.RWMutex
pending []*ethpb.SignedVoluntaryExit
included map[uint64]bool
chain blockchain.HeadFetcher
}

// NewPool accepts a head fetcher (for reading the validator set) and returns an initialized
// voluntary exit pool.
func NewPool(chain blockchain.HeadFetcher) *Pool {
func NewPool() *Pool {
return &Pool{
pending: make([]*ethpb.SignedVoluntaryExit, 0),
included: make(map[uint64]bool),
chain: chain,
}
}

Expand All @@ -46,7 +43,7 @@ func (p *Pool) PendingExits(slot uint64) []*ethpb.SignedVoluntaryExit {

// InsertVoluntaryExit into the pool. This method is a no-op if the pending exit already exists,
// has been included recently, or the validator is already exited.
func (p *Pool) InsertVoluntaryExit(ctx context.Context, exit *ethpb.SignedVoluntaryExit) {
func (p *Pool) InsertVoluntaryExit(ctx context.Context, validators []*ethpb.Validator, exit *ethpb.SignedVoluntaryExit) {
p.lock.Lock()
defer p.lock.Unlock()

Expand All @@ -56,7 +53,7 @@ func (p *Pool) InsertVoluntaryExit(ctx context.Context, exit *ethpb.SignedVolunt
}

// Has the validator been exited already?
if h, _ := p.chain.HeadState(ctx); h == nil || len(h.Validators) <= int(exit.Exit.ValidatorIndex) || h.Validators[exit.Exit.ValidatorIndex].ExitEpoch != params.BeaconConfig().FarFutureEpoch {
if len(validators) <= int(exit.Exit.ValidatorIndex) || validators[exit.Exit.ValidatorIndex].ExitEpoch != params.BeaconConfig().FarFutureEpoch {
return
}

Expand Down
33 changes: 13 additions & 20 deletions beacon-chain/operations/voluntaryexits/service_test.go
Expand Up @@ -7,8 +7,6 @@ import (

"github.com/gogo/protobuf/proto"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/params"
)

Expand Down Expand Up @@ -193,32 +191,27 @@ func TestPool_InsertVoluntaryExit(t *testing.T) {
},
}
ctx := context.Background()
chain := &mock.ChainService{
State: &pb.BeaconState{
Validators: []*ethpb.Validator{
{ // 0
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
},
{ // 1
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
},
{ // 2 - Already exited.
ExitEpoch: 15,
},
{ // 3
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
},
},
validators := []*ethpb.Validator{
{ // 0
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
},
{ // 1
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
},
{ // 2 - Already exited.
ExitEpoch: 15,
},
{ // 3
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &Pool{
pending: tt.fields.pending,
included: tt.fields.included,
chain: chain,
}
p.InsertVoluntaryExit(ctx, tt.args.exit)
p.InsertVoluntaryExit(ctx, validators, tt.args.exit)
if len(p.pending) != len(tt.want) {
t.Fatalf("Mismatched lengths of pending list. Got %d, wanted %d.", len(p.pending), len(tt.want))
}
Expand Down
1 change: 1 addition & 0 deletions beacon-chain/rpc/BUILD.bazel
Expand Up @@ -13,6 +13,7 @@ go_library(
"//beacon-chain/core/feed/state:go_default_library",
"//beacon-chain/db:go_default_library",
"//beacon-chain/operations/attestations:go_default_library",
"//beacon-chain/operations/voluntaryexits:go_default_library",
"//beacon-chain/p2p:go_default_library",
"//beacon-chain/powchain:go_default_library",
"//beacon-chain/rpc/aggregator:go_default_library",
Expand Down
5 changes: 5 additions & 0 deletions beacon-chain/rpc/service.go
Expand Up @@ -20,6 +20,7 @@ import (
statefeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/state"
"github.com/prysmaticlabs/prysm/beacon-chain/db"
"github.com/prysmaticlabs/prysm/beacon-chain/operations/attestations"
"github.com/prysmaticlabs/prysm/beacon-chain/operations/voluntaryexits"
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
"github.com/prysmaticlabs/prysm/beacon-chain/powchain"
"github.com/prysmaticlabs/prysm/beacon-chain/rpc/aggregator"
Expand Down Expand Up @@ -64,6 +65,7 @@ type Service struct {
chainStartFetcher powchain.ChainStartFetcher
mockEth1Votes bool
attestationsPool attestations.Pool
exitPool *voluntaryexits.Pool
syncService sync.Checker
host string
port string
Expand Down Expand Up @@ -105,6 +107,7 @@ type Config struct {
GenesisTimeFetcher blockchain.GenesisTimeFetcher
MockEth1Votes bool
AttestationsPool attestations.Pool
ExitPool *voluntaryexits.Pool
SyncService sync.Checker
Broadcaster p2p.Broadcaster
PeersFetcher p2p.PeersProvider
Expand Down Expand Up @@ -137,6 +140,7 @@ func NewService(ctx context.Context, cfg *Config) *Service {
chainStartFetcher: cfg.ChainStartFetcher,
mockEth1Votes: cfg.MockEth1Votes,
attestationsPool: cfg.AttestationsPool,
exitPool: cfg.ExitPool,
syncService: cfg.SyncService,
host: cfg.Host,
port: cfg.Port,
Expand Down Expand Up @@ -202,6 +206,7 @@ func (s *Service) Start() {
BeaconDB: s.beaconDB,
AttestationCache: cache.NewAttestationCache(),
AttPool: s.attestationsPool,
ExitPool: s.exitPool,
HeadFetcher: s.headFetcher,
ForkFetcher: s.forkFetcher,
FinalizationFetcher: s.finalizationFetcher,
Expand Down
2 changes: 2 additions & 0 deletions beacon-chain/rpc/validator/BUILD.bazel
Expand Up @@ -25,6 +25,7 @@ go_library(
"//beacon-chain/core/state/interop:go_default_library",
"//beacon-chain/db:go_default_library",
"//beacon-chain/operations/attestations:go_default_library",
"//beacon-chain/operations/voluntaryexits:go_default_library",
"//beacon-chain/p2p:go_default_library",
"//beacon-chain/powchain:go_default_library",
"//beacon-chain/sync:go_default_library",
Expand Down Expand Up @@ -72,6 +73,7 @@ go_test(
"//beacon-chain/core/state:go_default_library",
"//beacon-chain/db/testing:go_default_library",
"//beacon-chain/operations/attestations:go_default_library",
"//beacon-chain/operations/voluntaryexits:go_default_library",
"//beacon-chain/p2p/testing:go_default_library",
"//beacon-chain/powchain/testing:go_default_library",
"//beacon-chain/rpc/testing:go_default_library",
Expand Down
4 changes: 3 additions & 1 deletion beacon-chain/rpc/validator/exit.go
Expand Up @@ -39,5 +39,7 @@ func (vs *Server) ProposeExit(ctx context.Context, req *ethpb.SignedVoluntaryExi
},
})

return nil, nil
vs.ExitPool.InsertVoluntaryExit(ctx, s.Validators, req)

return nil, vs.P2P.Broadcast(ctx, req)
}
4 changes: 4 additions & 0 deletions beacon-chain/rpc/validator/exit_test.go
Expand Up @@ -13,6 +13,8 @@ import (
opfeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/operation"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
dbutil "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
"github.com/prysmaticlabs/prysm/beacon-chain/operations/voluntaryexits"
mockp2p "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing"
mockSync "github.com/prysmaticlabs/prysm/beacon-chain/sync/initial-sync/testing"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil"
Expand Down Expand Up @@ -46,6 +48,8 @@ func TestSub(t *testing.T) {
GenesisTime: genesisTime,
StateNotifier: mockChainService.StateNotifier(),
OperationNotifier: mockChainService.OperationNotifier(),
ExitPool: voluntaryexits.NewPool(),
P2P: mockp2p.NewTestP2P(t),
}

// Subscribe to operation notifications.
Expand Down
2 changes: 2 additions & 0 deletions beacon-chain/rpc/validator/server.go
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/db"
"github.com/prysmaticlabs/prysm/beacon-chain/operations/attestations"
"github.com/prysmaticlabs/prysm/beacon-chain/operations/voluntaryexits"
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
"github.com/prysmaticlabs/prysm/beacon-chain/powchain"
"github.com/prysmaticlabs/prysm/beacon-chain/sync"
Expand Down Expand Up @@ -51,6 +52,7 @@ type Server struct {
StateNotifier statefeed.Notifier
P2P p2p.Broadcaster
AttPool attestations.Pool
ExitPool *voluntaryexits.Pool
BlockReceiver blockchain.BlockReceiver
MockEth1Votes bool
Eth1BlockFetcher powchain.POWBlockFetcher
Expand Down
1 change: 1 addition & 0 deletions beacon-chain/sync/BUILD.bazel
Expand Up @@ -43,6 +43,7 @@ go_library(
"//beacon-chain/db:go_default_library",
"//beacon-chain/db/filters:go_default_library",
"//beacon-chain/operations/attestations:go_default_library",
"//beacon-chain/operations/voluntaryexits:go_default_library",
"//beacon-chain/p2p:go_default_library",
"//beacon-chain/p2p/encoder:go_default_library",
"//proto/beacon/p2p/v1:go_default_library",
Expand Down
4 changes: 4 additions & 0 deletions beacon-chain/sync/service.go
Expand Up @@ -11,6 +11,7 @@ import (
statefeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/state"
"github.com/prysmaticlabs/prysm/beacon-chain/db"
"github.com/prysmaticlabs/prysm/beacon-chain/operations/attestations"
"github.com/prysmaticlabs/prysm/beacon-chain/operations/voluntaryexits"
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
"github.com/prysmaticlabs/prysm/shared"
)
Expand All @@ -25,6 +26,7 @@ type Config struct {
P2P p2p.P2P
DB db.NoHeadAccessDatabase
AttPool attestations.Pool
ExitPool *voluntaryexits.Pool
Chain blockchainService
InitialSync Checker
StateNotifier statefeed.Notifier
Expand All @@ -49,6 +51,7 @@ func NewRegularSync(cfg *Config) *Service {
db: cfg.DB,
p2p: cfg.P2P,
attPool: cfg.AttPool,
exitPool: cfg.ExitPool,
chain: cfg.Chain,
initialSync: cfg.InitialSync,
slotToPendingBlocks: make(map[uint64]*ethpb.SignedBeaconBlock),
Expand All @@ -71,6 +74,7 @@ type Service struct {
p2p p2p.P2P
db db.NoHeadAccessDatabase
attPool attestations.Pool
exitPool *voluntaryexits.Pool
chain blockchainService
slotToPendingBlocks map[uint64]*ethpb.SignedBeaconBlock
seenPendingBlocks map[[32]byte]bool
Expand Down
7 changes: 6 additions & 1 deletion beacon-chain/sync/subscriber_handlers.go
Expand Up @@ -4,10 +4,15 @@ import (
"context"

"github.com/gogo/protobuf/proto"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
)

func (r *Service) voluntaryExitSubscriber(ctx context.Context, msg proto.Message) error {
// TODO(#3259): Requires handlers in operations service to be implemented.
s, err := r.chain.HeadState(ctx)
if err != nil {
return err
}
r.exitPool.InsertVoluntaryExit(ctx, s.Validators, msg.(*ethpb.SignedVoluntaryExit))
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/sync/validate_voluntary_exit_test.go
Expand Up @@ -25,7 +25,7 @@ func setupValidExit(t *testing.T) (*ethpb.SignedVoluntaryExit, *pb.BeaconState)
exit := &ethpb.SignedVoluntaryExit{
Exit: &ethpb.VoluntaryExit{
ValidatorIndex: 0,
Epoch: 1+params.BeaconConfig().PersistentCommitteePeriod,
Epoch: 1 + params.BeaconConfig().PersistentCommitteePeriod,
},
}
registry := []*ethpb.Validator{
Expand Down