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

Fix validator prev balance calculation #2992

Merged
merged 9 commits into from
Jul 19, 2019
5 changes: 3 additions & 2 deletions beacon-chain/rpc/attester_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ func (as *AttesterServer) RequestAttestation(ctx context.Context, req *pb.Attest
if err != nil {
return nil, fmt.Errorf("could not fetch head state: %v", err)
}
headState, err = state.ProcessSlots(ctx, headState, headBlock.Slot)

headState, err = state.ProcessSlots(ctx, headState, req.Slot)
if err != nil {
return nil, fmt.Errorf("could not process slot: %v", err)
}
Expand All @@ -140,7 +141,7 @@ func (as *AttesterServer) RequestAttestation(ctx context.Context, req *pb.Attest
}
}

startEpoch := headState.PreviousCrosslinks[req.Shard].EndEpoch
startEpoch := headState.CurrentCrosslinks[req.Shard].EndEpoch
endEpoch := startEpoch + params.BeaconConfig().MaxEpochsPerCrosslink
if endEpoch > targetEpoch {
endEpoch = targetEpoch
Expand Down
2 changes: 2 additions & 0 deletions beacon-chain/rpc/attester_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ func TestRequestAttestation_OK(t *testing.T) {
}
req := &pb.AttestationRequest{
Shard: 0,
Slot: 3*params.BeaconConfig().SlotsPerEpoch + 1,
}
res, err := attesterServer.RequestAttestation(context.Background(), req)
if err != nil {
Expand Down Expand Up @@ -266,6 +267,7 @@ func TestAttestationDataAtSlot_handlesFarAwayJustifiedEpoch(t *testing.T) {
}
req := &pb.AttestationRequest{
Shard: 0,
Slot: 10000,
}
res, err := attesterServer.RequestAttestation(context.Background(), req)
if err != nil {
Expand Down
7 changes: 5 additions & 2 deletions beacon-chain/rpc/proposer_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,11 @@ func (ps *ProposerServer) attestations(ctx context.Context, expectedSlot uint64)
return nil, fmt.Errorf("could not retrieve pending attest ations from operations service: %v", err)
}
// advance slot, if it is behind
for beaconState.Slot < expectedSlot {
beaconState.Slot++
if beaconState.Slot < expectedSlot {
beaconState, err = state.ProcessSlots(ctx, beaconState, expectedSlot)
if err != nil {
return nil, err
}
}

var attsReadyForInclusion []*pbp2p.Attestation
Expand Down
7 changes: 5 additions & 2 deletions beacon-chain/rpc/proposer_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,11 @@ func TestPendingAttestations_FiltersExpiredAttestations(t *testing.T) {
StartEpoch: 9,
DataRoot: params.BeaconConfig().ZeroHash[:],
}},
RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),
ActiveIndexRoots: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),
RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),
ActiveIndexRoots: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),
StateRoots: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),
BlockRoots: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),
LatestBlockHeader: &pbp2p.BeaconBlockHeader{StateRoot: []byte{}},
}

if err := db.SaveState(ctx, beaconState); err != nil {
Expand Down
1 change: 1 addition & 0 deletions validator/client/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func (v *ValidatorService) Start() {
keys: v.keys,
pubkeys: pubkeys,
logValidatorBalances: v.logValidatorBalances,
prevBalance: make(map[[48]byte]uint64),
}
go run(v.ctx, v.validator)
}
Expand Down
2 changes: 1 addition & 1 deletion validator/client/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type validator struct {
attesterClient pb.AttesterServiceClient
keys map[string]*keystore.Key
pubkeys [][]byte
prevBalance uint64
prevBalance map[[48]byte]uint64
logValidatorBalances bool
}

Expand Down
18 changes: 9 additions & 9 deletions validator/client/validator_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

pb "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/sirupsen/logrus"
)
Expand All @@ -24,13 +25,13 @@ func (v *validator) LogValidatorGainsAndLosses(ctx context.Context, slot uint64)
return nil
}

epoch := slot / params.BeaconConfig().SlotsPerEpoch
if epoch == 0 {
v.prevBalance = params.BeaconConfig().MaxEffectiveBalance
}
var totalPrevBalance uint64
reported := false
for _, pkey := range v.pubkeys {

if slot < params.BeaconConfig().SlotsPerEpoch {
v.prevBalance[bytesutil.ToBytes48(pkey)] = params.BeaconConfig().MaxEffectiveBalance
}

req := &pb.ValidatorPerformanceRequest{
Slot: slot,
PublicKey: pkey,
Expand Down Expand Up @@ -61,8 +62,8 @@ func (v *validator) LogValidatorGainsAndLosses(ctx context.Context, slot uint64)
}
newBalance := float64(resp.Balance) / float64(params.BeaconConfig().GweiPerEth)

if v.prevBalance > 0 {
prevBalance := float64(v.prevBalance) / float64(params.BeaconConfig().GweiPerEth)
if v.prevBalance[bytesutil.ToBytes48(pkey)] > 0 {
prevBalance := float64(v.prevBalance[bytesutil.ToBytes48(pkey)]) / float64(params.BeaconConfig().GweiPerEth)
percentNet := (newBalance - prevBalance) / prevBalance
log.WithFields(logrus.Fields{
"prevBalance": prevBalance,
Expand All @@ -72,9 +73,8 @@ func (v *validator) LogValidatorGainsAndLosses(ctx context.Context, slot uint64)
"pubKey": tpk,
}).Info("Net gains/losses in eth")
}
totalPrevBalance += resp.Balance
v.prevBalance[bytesutil.ToBytes48(pkey)] = resp.Balance
}

v.prevBalance = totalPrevBalance
return nil
}