2021-10-18 14:54:17 +00:00
|
|
|
# beacon_chain
|
2023-08-02 23:03:40 +00:00
|
|
|
# Copyright (c) 2020-2023 Status Research & Development GmbH
|
2021-10-18 14:54:17 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
|
|
|
# Beacon chain internals
|
|
|
|
../../beacon_chain/spec/[beaconstate, validator, helpers, state_transition_epoch],
|
2022-01-12 14:50:30 +00:00
|
|
|
../../beacon_chain/spec/datatypes/[altair, bellatrix],
|
2021-10-18 14:54:17 +00:00
|
|
|
# Test utilities
|
|
|
|
../../testutil,
|
2023-02-10 20:59:38 +00:00
|
|
|
../fixtures_utils, ../os_ops
|
2021-10-18 14:54:17 +00:00
|
|
|
|
|
|
|
const
|
2022-01-03 17:43:00 +00:00
|
|
|
RewardsDirBase = SszTestsDir/const_preset/"bellatrix"/"rewards"
|
2021-10-18 14:54:17 +00:00
|
|
|
RewardsDirBasic = RewardsDirBase/"basic"/"pyspec_tests"
|
|
|
|
RewardsDirLeak = RewardsDirBase/"leak"/"pyspec_tests"
|
|
|
|
RewardsDirRandom = RewardsDirBase/"random"/"pyspec_tests"
|
|
|
|
|
|
|
|
func init(T: type Deltas, len: int): T =
|
|
|
|
if not result.rewards.setLen(len):
|
|
|
|
raiseAssert "setLen"
|
|
|
|
if not result.penalties.setLen(len):
|
|
|
|
raiseAssert "setLen"
|
|
|
|
|
|
|
|
proc runTest(rewardsDir, identifier: string) =
|
|
|
|
let testDir = rewardsDir / identifier
|
|
|
|
|
|
|
|
proc `testImpl _ rewards _ identifier`() =
|
2022-01-05 08:42:56 +00:00
|
|
|
test "EF - Bellatrix - Rewards - " & identifier & preset():
|
2021-10-18 14:54:17 +00:00
|
|
|
var info: altair.EpochInfo
|
|
|
|
|
|
|
|
let
|
|
|
|
state = newClone(
|
2022-01-12 14:50:30 +00:00
|
|
|
parseTest(testDir/"pre.ssz_snappy", SSZ, bellatrix.BeaconState))
|
2021-10-18 14:54:17 +00:00
|
|
|
flagDeltas = [
|
|
|
|
parseTest(testDir/"source_deltas.ssz_snappy", SSZ, Deltas),
|
|
|
|
parseTest(testDir/"target_deltas.ssz_snappy", SSZ, Deltas),
|
|
|
|
parseTest(testDir/"head_deltas.ssz_snappy", SSZ, Deltas)]
|
|
|
|
inactivityPenaltyDeltas =
|
|
|
|
parseTest(testDir/"inactivity_penalty_deltas.ssz_snappy", SSZ, Deltas)
|
|
|
|
|
|
|
|
info.init(state[])
|
|
|
|
let
|
|
|
|
total_balance = info.balances.current_epoch
|
|
|
|
base_reward_per_increment = get_base_reward_per_increment(total_balance)
|
|
|
|
|
|
|
|
var
|
2023-08-02 23:03:40 +00:00
|
|
|
flagDeltas2: array[TimelyFlag, Deltas] = [
|
2021-10-18 14:54:17 +00:00
|
|
|
Deltas.init(state[].validators.len),
|
|
|
|
Deltas.init(state[].validators.len),
|
|
|
|
Deltas.init(state[].validators.len)]
|
|
|
|
inactivityPenaltyDeltas2 = Deltas.init(state[].validators.len)
|
|
|
|
|
2022-01-27 16:37:30 +00:00
|
|
|
let finality_delay = get_finality_delay(state[])
|
|
|
|
|
2023-08-02 23:03:40 +00:00
|
|
|
for flag_index in TimelyFlag:
|
2021-10-18 14:54:17 +00:00
|
|
|
for validator_index, delta in get_flag_index_deltas(
|
2022-01-27 16:37:30 +00:00
|
|
|
state[], flag_index, base_reward_per_increment, info, finality_delay):
|
2021-10-18 14:54:17 +00:00
|
|
|
if not is_eligible_validator(info.validators[validator_index]):
|
|
|
|
continue
|
|
|
|
flagDeltas2[flag_index].rewards[validator_index] = delta.rewards
|
|
|
|
flagDeltas2[flag_index].penalties[validator_index] = delta.penalties
|
|
|
|
|
|
|
|
for validator_index, delta in get_inactivity_penalty_deltas(
|
|
|
|
defaultRuntimeConfig, state[], info):
|
|
|
|
inactivityPenaltyDeltas2.penalties[validator_index] = delta
|
|
|
|
|
|
|
|
check:
|
|
|
|
flagDeltas == flagDeltas2
|
|
|
|
inactivityPenaltyDeltas == inactivityPenaltyDeltas2
|
|
|
|
|
|
|
|
`testImpl _ rewards _ identifier`()
|
|
|
|
|
2022-01-05 08:42:56 +00:00
|
|
|
suite "EF - Bellatrix - Rewards " & preset():
|
2021-10-18 14:54:17 +00:00
|
|
|
for rewardsDir in [RewardsDirBasic, RewardsDirLeak, RewardsDirRandom]:
|
|
|
|
for kind, path in walkDir(rewardsDir, relative = true, checkDir = true):
|
|
|
|
runTest(rewardsDir, path)
|