Skip to content

Commit

Permalink
Implement process_rewards_and_penalties for 0.6 (#2665)
Browse files Browse the repository at this point in the history
* update process crosslink and update existing tests

* added a test case to cover no crosslink changes

* more test

* preston's feedback

* spellings

* ValidatorStatus Estimating Activation RPC Server (#2469)

* fix spacing

* working on position in queue

* fmt

* spacing

* feedback

* tests

* rename

* Only Perform Initial Sync With a Single Peer (#2471)

* fix spacing

* use send instead of broadcast in initial sync

* Fix Estimation of Deposit Inclusion Slot in ValidatorActivationStatus (#2472)

* fix spacing

* fix time estimates

* correct slot estimation

* naming

* Update beacon-chain/rpc/validator_server.go

Co-Authored-By: rauljordan <raul@prysmaticlabs.com>

* SSZ web api for decoding input data (#2473)

* first pass ssz server for decoding deposit input data

* fix decoding

* revert viz change on helper

* add image target

* use /api prefix, add deployment for cluster

* fix lint

* standardize slot numbers (#2475)

* Add CORS for ssz api (#2476)

* first pass ssz server for decoding deposit input data

* fix decoding

* revert viz change on helper

* add image target

* use /api prefix, add deployment for cluster

* fix lint

* needed CORS

* Allow Client to Retrieve Multiple Validator Statuses (#2474)

* multiple validator statuses

* gazelle

* context

* fixing bugs

* remove old way of checking

* fix logging

* make activation queue more accurate

* fix rpc test

* add test

* fix remaining tests

* lint

* comment

* review comments

* Update Prysm README (#2477)

* README updated

* readme updates

* no err throw (#2479)

* Fix Status Nil Pointer Error (#2480)

* no err throw

* nil errors

* 3.175 (#2482)

* Better Error Message if Failing to Exit Initial Sync (#2483)

* no err throw

* nil errors

* better error on init sync

* Only Log Active Balances (#2485)

* only log active balance

* dont need ()

* change logging (#2487)

* fix chainstart waiting on rpc server (#2488)

* shift ticker to after activation (#2489)

* Add drain script (#2418)

* Add drain script

* Fix script to drain contracts from newest to oldest

* Add README

* remove comments

* Only after block 400k, look up by deposit event

* issue warn log on disconnecting peer instead of error (#2491)

* Display Only Active Validator Data (#2490)

* Fix Validator Status Field in RPC Server (#2492)

* fix status of key

* status test fix

* fmt

* Estimate the Time Till Follow Distance Is Completed (#2486)

* use estimation instead

* fix test

* fixing another test

* fix tests and preston's comments

* remove unused var

* fix condition

* Revert "fix condition"

This reverts commit dee0e31.

* dont return error

* add production config for testnet release (#2493)

* Lookup Validator Index in State in Status Check (#2494)

* state lookup

* refactor duplicate code

* refactor with mapping

* fix broken tests

* finish refactor

* merged master

* Starting, I need get_epoch_start_shard

* updated EpochCommitteeCount and fixed tests

* implemented ShardDelta

* test for ShardDelta

* implemented EpochStartShard

* added epoch out of bound test

* test for accurate start shard

* lint

* need to use changes from latest crosslinks

* added BaseReward and totalActiveBalance

* added test for base reward

* implemented process_attestation_delta

* comments

* comments

* merged master

* all tests passing

* start testing

* done

* merged master

* fixed tests

* tests, more to come

* tests done

* lint

* spaces over tabs

* addressed shay's feedback

* starting but need to merge a few things...

* tests

* fmt
  • Loading branch information
terencechain authored and nisdas committed Jun 9, 2019
1 parent f747de5 commit 5c1410b
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
32 changes: 32 additions & 0 deletions beacon-chain/core/epoch/epoch_processing.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,38 @@ func ProcessSlashings(state *pb.BeaconState) *pb.BeaconState {
return state
}

// ProcessRewardsAndPenalties processes the rewards and penalties of individual validator.
//
// Spec pseudocode definition:
// def process_rewards_and_penalties(state: BeaconState) -> None:
// if get_current_epoch(state) == GENESIS_EPOCH:
// return
//
// rewards1, penalties1 = get_attestation_deltas(state)
// rewards2, penalties2 = get_crosslink_deltas(state)
// for i in range(len(state.validator_registry)):
// increase_balance(state, i, rewards1[i] + rewards2[i])
// decrease_balance(state, i, penalties1[i] + penalties2[i])
func ProcessRewardsAndPenalties(state *pb.BeaconState) (*pb.BeaconState, error) {
// Can't process rewards and penalties in genesis epoch.
if helpers.CurrentEpoch(state) == 0 {
return state, nil
}
attsRewards, attsPenalties, err := AttestationDelta(state)
if err != nil {
return nil, fmt.Errorf("could not get attestation delta: %v ", err)
}
clRewards, clPenalties, err := CrosslinkDelta(state)
if err != nil {
return nil, fmt.Errorf("could not get crosslink delta: %v ", err)
}
for i := 0; i < len(state.ValidatorRegistry); i++ {
state = helpers.IncreaseBalance(state, uint64(i), attsRewards[i]+clRewards[i])
state = helpers.DecreaseBalance(state, uint64(i), attsPenalties[i]+clPenalties[i])
}
return state, nil
}

// AttestationDelta calculates the rewards and penalties of individual
// validator for voting the correct FFG source, FFG target, and head. It
// also calculates proposer delay inclusion and inactivity rewards
Expand Down
53 changes: 51 additions & 2 deletions beacon-chain/core/epoch/epoch_processing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1307,8 +1307,7 @@ func TestAttestationDelta_SomeAttested(t *testing.T) {
t.Fatal(err)
}

//attestedIndices := []uint64{1932, 500, 1790, 1015, 1477, 1211, 69}
attestedIndices := []uint64{500}
attestedIndices := []uint64{1932, 500, 1790, 1015, 1477, 1211, 69}

attestedBalance, err := AttestingBalance(state, atts)
totalBalance := totalActiveBalance(state)
Expand Down Expand Up @@ -1415,6 +1414,56 @@ func TestProcessRegistryUpdates_CanExits(t *testing.T) {
}
}

func TestProcessRewardsAndPenalties_GenesisEpoch(t *testing.T) {
state := &pb.BeaconState{Slot: params.BeaconConfig().SlotsPerEpoch - 1, LatestStartShard: 999}
newState, err := ProcessRewardsAndPenalties(state)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(state, newState) {
t.Error("genesis state mutated")
}
}

func TestProcessRewardsAndPenalties_SomeAttested(t *testing.T) {
e := params.BeaconConfig().SlotsPerEpoch
validatorCount := params.BeaconConfig().DepositsForChainStart / 8
state := buildState(e+2, validatorCount)
startShard := uint64(960)
atts := make([]*pb.PendingAttestation, 3)
for i := 0; i < len(atts); i++ {
atts[i] = &pb.PendingAttestation{
Data: &pb.AttestationData{
Slot: uint64(i),
CrosslinkDataRoot: []byte{'A'},
Shard: startShard + 1,
},
AggregationBitfield: []byte{0xC0, 0xC0, 0xC0, 0xC0},
InclusionDelay: 1,
}
}
state.PreviousEpochAttestations = atts
state.CurrentCrosslinks[startShard] = &pb.Crosslink{
CrosslinkDataRootHash32: []byte{'A'},
}
state.CurrentCrosslinks[startShard+1] = &pb.Crosslink{
CrosslinkDataRootHash32: []byte{'A'},
}

state, err := ProcessRewardsAndPenalties(state)
if err != nil {
t.Fatal(err)
}
if state.Balances[0] != params.BeaconConfig().MaxDepositAmount {
t.Errorf("wanted balance: %d, got: %d",
params.BeaconConfig().MaxDepositAmount, state.Balances[0])
}
if state.Balances[1] == params.BeaconConfig().MaxDepositAmount {
t.Errorf("validator balance %d can't equal to %d",
state.Balances[0], params.BeaconConfig().MaxDepositAmount)
}
}

func buildState(slot uint64, validatorCount uint64) *pb.BeaconState {
validators := make([]*pb.Validator, validatorCount)
for i := 0; i < len(validators); i++ {
Expand Down

0 comments on commit 5c1410b

Please sign in to comment.