Skip to content

Commit

Permalink
PreChainStart Activation Fix (#2544)
Browse files Browse the repository at this point in the history
* fix activation

* remove logs

* remove logs

* revert change

* fix test
  • Loading branch information
Nishant Das authored and rauljordan committed May 9, 2019
1 parent 729c45d commit 5fc6f2d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
59 changes: 54 additions & 5 deletions beacon-chain/rpc/validator_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,17 @@ func (vs *ValidatorServer) ValidatorStatus(
if err != nil {
return nil, fmt.Errorf("could not fetch beacon state: %v", err)
}
chainStarted, _, err := vs.powChainService.HasChainStartLogOccurred()
if err != nil {
return nil, err
}

chainStartKeys, err := vs.chainStartPubkeys()
if err != nil {
return nil, err
}
validatorIndexMap := stateutils.ValidatorIndexMap(beaconState)
return vs.validatorStatus(ctx, req.PublicKey, validatorIndexMap, beaconState), nil
return vs.validatorStatus(ctx, req.PublicKey, chainStarted, chainStartKeys, validatorIndexMap, beaconState), nil
}

// MultipleValidatorStatus returns the validator status response for the set of validators
Expand All @@ -235,13 +244,22 @@ func (vs *ValidatorServer) MultipleValidatorStatus(
if err != nil {
return false, nil, err
}
chainStarted, _, err := vs.powChainService.HasChainStartLogOccurred()
if err != nil {
return false, nil, err
}

chainStartKeys, err := vs.chainStartPubkeys()
if err != nil {
return false, nil, err
}

validatorIndexMap := stateutils.ValidatorIndexMap(beaconState)
for i, key := range pubkeys {
if ctx.Err() != nil {
return false, nil, ctx.Err()
}

status := vs.validatorStatus(ctx, key, validatorIndexMap, beaconState)
status := vs.validatorStatus(ctx, key, chainStarted, chainStartKeys, validatorIndexMap, beaconState)
resp := &pb.ValidatorActivationResponse_Status{
Status: status,
PublicKey: key,
Expand All @@ -256,8 +274,9 @@ func (vs *ValidatorServer) MultipleValidatorStatus(
}

func (vs *ValidatorServer) validatorStatus(
ctx context.Context, pubKey []byte, idxMap map[[32]byte]int, beaconState *pbp2p.BeaconState,
) *pb.ValidatorStatusResponse {
ctx context.Context, pubKey []byte, chainStarted bool,
chainStartKeys map[[96]byte]bool, idxMap map[[32]byte]int,
beaconState *pbp2p.BeaconState) *pb.ValidatorStatusResponse {
pk := bytesutil.ToBytes32(pubKey)
valIdx, ok := idxMap[pk]
_, eth1BlockNumBigInt := vs.beaconDB.DepositByPubkey(ctx, pubKey)
Expand All @@ -277,6 +296,23 @@ func (vs *ValidatorServer) validatorStatus(
}
}

if !chainStarted {
return &pb.ValidatorStatusResponse{
Status: pb.ValidatorStatus_UNKNOWN_STATUS,
ActivationEpoch: params.BeaconConfig().FarFutureEpoch - params.BeaconConfig().GenesisEpoch,
Eth1DepositBlockNumber: eth1BlockNumBigInt.Uint64(),
}
}

if exists := chainStartKeys[bytesutil.ToBytes96(pubKey)]; exists {
return &pb.ValidatorStatusResponse{
Status: pb.ValidatorStatus_ACTIVE,
ActivationEpoch: 0,
Eth1DepositBlockNumber: eth1BlockNumBigInt.Uint64(),
DepositInclusionSlot: 0,
}
}

depositBlockSlot, err := vs.depositBlockSlot(ctx, beaconState.Slot, eth1BlockNumBigInt, beaconState)
if err != nil {
return &pb.ValidatorStatusResponse{
Expand Down Expand Up @@ -433,3 +469,16 @@ func (vs *ValidatorServer) depositBlockSlot(ctx context.Context, currentSlot uin

return depositBlockSlot, nil
}

func (vs *ValidatorServer) chainStartPubkeys() (map[[96]byte]bool, error) {
pubkeys := make(map[[96]byte]bool)
deposits := vs.powChainService.ChainStartDeposits()
for _, dep := range deposits {
depInput, err := helpers.DecodeDepositInput(dep)
if err != nil {
return nil, err
}
pubkeys[bytesutil.ToBytes96(depInput.Pubkey)] = true
}
return pubkeys, nil
}
1 change: 1 addition & 0 deletions beacon-chain/rpc/validator_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,7 @@ func TestWaitForActivation_ContextClosed(t *testing.T) {
beaconDB: db,
ctx: ctx,
chainService: newMockChainService(),
powChainService: &mockPOWChainService{},
canonicalStateChan: make(chan *pbp2p.BeaconState, 1),
}
req := &pb.ValidatorActivationRequest{
Expand Down

0 comments on commit 5fc6f2d

Please sign in to comment.