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

WIP: Shard chain transition functions #3396

Closed
wants to merge 12 commits into from
30 changes: 4 additions & 26 deletions beacon-chain/core/epoch/epoch_processing.go
Original file line number Diff line number Diff line change
Expand Up @@ -663,28 +663,6 @@ func winningCrosslink(state *pb.BeaconState, shard uint64, epoch uint64) (*ethpb
return winnerCrosslink, crosslinkIndices, nil
}

// baseReward takes state and validator index and calculate
// individual validator's base reward quotient.
//
// Note: Adjusted quotient is calculated of base reward because it's too inefficient
// to repeat the same calculation for every validator versus just doing it once.
//
// Spec pseudocode definition:
// def get_base_reward(state: BeaconState, index: ValidatorIndex) -> Gwei:
// total_balance = get_total_active_balance(state)
// effective_balance = state.validator_registry[index].effective_balance
// return effective_balance * BASE_REWARD_FACTOR // integer_squareroot(total_balance) // BASE_REWARDS_PER_EPOCH
func baseReward(state *pb.BeaconState, index uint64) (uint64, error) {
totalBalance, err := helpers.TotalActiveBalance(state)
if err != nil {
return 0, errors.Wrap(err, "could not calculate active balance")
}
effectiveBalance := state.Validators[index].EffectiveBalance
baseReward := effectiveBalance * params.BeaconConfig().BaseRewardFactor /
mathutil.IntegerSquareRoot(totalBalance) / params.BeaconConfig().BaseRewardsPerEpoch
return baseReward, 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 Expand Up @@ -801,7 +779,7 @@ func attestationDelta(state *pb.BeaconState) ([]uint64, []uint64, error) {

// Update rewards and penalties to each eligible validator index.
for _, index := range eligible {
base, err := baseReward(state, index)
base, err := helpers.BaseReward(state, index)
if err != nil {
return nil, nil, errors.Wrap(err, "could not get base reward")
}
Expand Down Expand Up @@ -832,7 +810,7 @@ func attestationDelta(state *pb.BeaconState) ([]uint64, []uint64, error) {
for i, a := range attestersVotedSoruce {
slotsPerEpoch := params.BeaconConfig().SlotsPerEpoch

baseReward, err := baseReward(state, i)
baseReward, err := helpers.BaseReward(state, i)
if err != nil {
return nil, nil, errors.Wrap(err, "could not get proposer reward")
}
Expand All @@ -857,7 +835,7 @@ func attestationDelta(state *pb.BeaconState) ([]uint64, []uint64, error) {
attestedTarget[index] = true
}
for _, index := range eligible {
base, err := baseReward(state, index)
base, err := helpers.BaseReward(state, index)
if err != nil {
return nil, nil, errors.Wrap(err, "could not get base reward")
}
Expand Down Expand Up @@ -928,7 +906,7 @@ func crosslinkDelta(state *pb.BeaconState) ([]uint64, []uint64, error) {
attestingBalance := helpers.TotalBalance(state, attestingIndices)

for _, index := range committee {
base, err := baseReward(state, index)
base, err := helpers.BaseReward(state, index)
if err != nil {
return nil, nil, errors.Wrap(err, "could not get base reward")
}
Expand Down
18 changes: 9 additions & 9 deletions beacon-chain/core/epoch/epoch_processing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,12 +533,12 @@ func TestBaseReward_AccurateRewards(t *testing.T) {
{ExitEpoch: params.BeaconConfig().FarFutureEpoch, EffectiveBalance: tt.b}},
Balances: []uint64{tt.a},
}
c, err := baseReward(state, 0)
c, err := helpers.BaseReward(state, 0)
if err != nil {
t.Fatal(err)
}
if c != tt.c {
t.Errorf("baseReward(%d) = %d, want = %d",
t.Errorf("helpers.BaseReward(%d) = %d, want = %d",
tt.a, c, tt.c)
}
}
Expand Down Expand Up @@ -922,7 +922,7 @@ func TestCrosslinkDelta_NoOneAttested(t *testing.T) {
t.Errorf("Wanted reward balance 0, got %d", rewards[i])
}
// Since no one attested, all the validators should get penalized the same
base, err := baseReward(state, i)
base, err := helpers.BaseReward(state, i)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1007,7 +1007,7 @@ func TestCrosslinkDelta_SomeAttested(t *testing.T) {

nonAttestedIndices := []uint64{12, 23, 45, 79}
for _, i := range nonAttestedIndices {
base, err := baseReward(state, i)
base, err := helpers.BaseReward(state, i)
if err != nil {
t.Errorf("Could not get base reward: %v", err)
}
Expand Down Expand Up @@ -1115,7 +1115,7 @@ func TestAttestationDelta_NoOneAttested(t *testing.T) {
}
// Since no one attested, all the validators should get penalized the same
// it's 3 times the penalized amount because source, target and head.
base, err := baseReward(state, i)
base, err := helpers.BaseReward(state, i)
if err != nil {
t.Errorf("Could not get base reward: %v", err)
}
Expand Down Expand Up @@ -1172,7 +1172,7 @@ func TestAttestationDelta_SomeAttested(t *testing.T) {

attestedIndices := []uint64{5, 754, 797, 1637, 1770, 1862, 1192}
for _, i := range attestedIndices {
base, err := baseReward(state, i)
base, err := helpers.BaseReward(state, i)
if err != nil {
t.Errorf("Could not get base reward: %v", err)
}
Expand All @@ -1193,7 +1193,7 @@ func TestAttestationDelta_SomeAttested(t *testing.T) {

nonAttestedIndices := []uint64{12, 23, 45, 79}
for _, i := range nonAttestedIndices {
base, err := baseReward(state, i)
base, err := helpers.BaseReward(state, i)
if err != nil {
t.Errorf("Could not get base reward: %v", err)
}
Expand Down Expand Up @@ -1255,7 +1255,7 @@ func TestAttestationDelta_SomeAttestedFinalityDelay(t *testing.T) {

attestedIndices := []uint64{5, 754, 797, 1637, 1770, 1862, 1192}
for _, i := range attestedIndices {
base, err := baseReward(state, i)
base, err := helpers.BaseReward(state, i)
if err != nil {
t.Errorf("Could not get base reward: %v", err)
}
Expand All @@ -1276,7 +1276,7 @@ func TestAttestationDelta_SomeAttestedFinalityDelay(t *testing.T) {

nonAttestedIndices := []uint64{12, 23, 45, 79}
for _, i := range nonAttestedIndices {
base, err := baseReward(state, i)
base, err := helpers.BaseReward(state, i)
if err != nil {
t.Errorf("Could not get base reward: %v", err)
}
Expand Down
1 change: 1 addition & 0 deletions beacon-chain/core/helpers/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ go_library(
"//shared/bls:go_default_library",
"//shared/bytesutil:go_default_library",
"//shared/hashutil:go_default_library",
"//shared/mathutil:go_default_library",
"//shared/params:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@com_github_prysmaticlabs_go_bitfield//:go_default_library",
Expand Down
23 changes: 23 additions & 0 deletions beacon-chain/core/helpers/rewards_penalties.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/beacon-chain/cache"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/mathutil"
"github.com/prysmaticlabs/prysm/shared/params"
)

Expand Down Expand Up @@ -96,3 +97,25 @@ func DecreaseBalance(state *pb.BeaconState, idx uint64, delta uint64) *pb.Beacon
state.Balances[idx] -= delta
return state
}

// BaseReward takes state and validator index and calculate
// individual validator's base reward quotient.
//
// Note: Adjusted quotient is calculated of base reward because it's too inefficient
// to repeat the same calculation for every validator versus just doing it once.
//
// Spec pseudocode definition:
// def get_base_reward(state: BeaconState, index: ValidatorIndex) -> Gwei:
// total_balance = get_total_active_balance(state)
// effective_balance = state.validator_registry[index].effective_balance
// return effective_balance * BASE_REWARD_FACTOR // integer_squareroot(total_balance) // BASE_REWARDS_PER_EPOCH
func BaseReward(state *pb.BeaconState, index uint64) (uint64, error) {
totalBalance, err := TotalActiveBalance(state)
if err != nil {
return 0, errors.Wrap(err, "could not calculate active balance")
}
effectiveBalance := state.Validators[index].EffectiveBalance
baseReward := effectiveBalance * params.BeaconConfig().BaseRewardFactor /
mathutil.IntegerSquareRoot(totalBalance) / params.BeaconConfig().BaseRewardsPerEpoch
return baseReward, nil
}
5 changes: 2 additions & 3 deletions proto/beacon/p2p/v1/messages.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading