From 0073555752229ae72d3332afe439aa03cbacf499 Mon Sep 17 00:00:00 2001 From: Dustin Brody Date: Wed, 6 Nov 2019 15:02:06 +0000 Subject: [PATCH] transition deposit operation fixtures to 0.9.0 (#515) * transition deposit operations fixture to 0.9.0 * mark slash_validator(...) as 0.9.0 * switch remaining non-ref objects to ref objects to maybe avoid crashes in CI * remove unused helpers/debug_state imports --- beacon_chain/spec/beaconstate.nim | 2 +- beacon_chain/spec/datatypes.nim | 106 ++++++++++++++++++ .../test_fixture_operations_attestations.nim | 3 +- .../test_fixture_operations_block_header.nim | 3 +- .../test_fixture_operations_deposits.nim | 22 ++-- tests/official/test_fixture_sanity_blocks.nim | 3 +- 6 files changed, 123 insertions(+), 16 deletions(-) diff --git a/beacon_chain/spec/beaconstate.nim b/beacon_chain/spec/beaconstate.nim index e215d19ad..eef33ceb4 100644 --- a/beacon_chain/spec/beaconstate.nim +++ b/beacon_chain/spec/beaconstate.nim @@ -146,7 +146,7 @@ func initiate_validator_exit*(state: var BeaconState, validator.withdrawable_epoch = validator.exit_epoch + MIN_VALIDATOR_WITHDRAWABILITY_DELAY -# https://github.com/ethereum/eth2.0-specs/blob/v0.8.4/specs/core/0_beacon-chain.md#slash_validator +# https://github.com/ethereum/eth2.0-specs/blob/v0.9.0/specs/core/0_beacon-chain.md#slash_validator func slash_validator*(state: var BeaconState, slashed_index: ValidatorIndex, stateCache: var StateCache) = # Slash the validator with index ``index``. diff --git a/beacon_chain/spec/datatypes.nim b/beacon_chain/spec/datatypes.nim index ff0261b30..77d4aee15 100644 --- a/beacon_chain/spec/datatypes.nim +++ b/beacon_chain/spec/datatypes.nim @@ -237,6 +237,64 @@ type voluntary_exits*: seq[VoluntaryExit] transfers*: seq[Transfer] + # https://github.com/ethereum/eth2.0-specs/blob/v0.9.0/specs/core/0_beacon-chain.md#beaconstate + BeaconStateNew* = object + # Versioning + genesis_time*: uint64 + slot*: Slot + fork*: Fork + + # History + latest_block_header*: BeaconBlockHeader ##\ + ## `latest_block_header.state_root == ZERO_HASH` temporarily + + block_roots*: array[SLOTS_PER_HISTORICAL_ROOT, Eth2Digest] ##\ + ## Needed to process attestations, older to newer + + state_roots*: array[SLOTS_PER_HISTORICAL_ROOT, Eth2Digest] + + historical_roots*: seq[Eth2Digest] ##\ + ## model List with HISTORICAL_ROOTS_LIMIT limit as seq + ## TODO bound explicitly somewhere + + # Eth1 + eth1_data*: Eth1Data + + eth1_data_votes*: seq[Eth1Data] ##\ + ## As with `hitorical_roots`, this is a `List`. TODO bound explicitly. + + eth1_deposit_index*: uint64 + + # Registry + validators*: seq[Validator] + balances*: seq[uint64] ##\ + ## Validator balances in Gwei! + ## Also more `List`s which need to be bounded explicitly at + ## VALIDATOR_REGISTRY_LIMIT + + # Randomness + randao_mixes*: array[EPOCHS_PER_HISTORICAL_VECTOR, Eth2Digest] + + # Slashings + slashings*: array[EPOCHS_PER_SLASHINGS_VECTOR, uint64] ##\ + ## Per-epoch sums of slashed effective balances + + # Attestations + previous_epoch_attestations*: seq[PendingAttestation] + current_epoch_attestations*: seq[PendingAttestation] + + # Finality + justification_bits*: uint8 ##\ + ## Bit set for every recent justified epoch + ## Model a Bitvector[4] as a one-byte uint, which should remain consistent + ## with ssz/hashing. + + previous_justified_checkpoint*: Checkpoint ##\ + ## Previous epoch snapshot + + current_justified_checkpoint*: Checkpoint + finalized_checkpoint*: Checkpoint + # https://github.com/ethereum/eth2.0-specs/blob/v0.8.4/specs/core/0_beacon-chain.md#beaconstate BeaconState* = object # Versioning @@ -527,6 +585,54 @@ proc `$`*(x: ValidatorIndex): auto = $(x.int64) ethTimeUnit Slot ethTimeUnit Epoch +func GetOldBeaconState*(newState: BeaconStateNew): BeaconState = + BeaconState( + genesis_time: newState.genesis_time, + slot: newState.slot, + fork: newState.fork, + latest_block_header: newState.latest_block_header, + block_roots: newState.block_roots, + state_roots: newState.state_roots, + historical_roots: newState.historical_roots, + eth1_data: newState.eth1_data, + eth1_data_votes: newState.eth1_data_votes, + eth1_deposit_index: newState.eth1_deposit_index, + validators: newState.validators, + balances: newState.balances, + randao_mixes: newState.randao_mixes, + slashings: newState.slashings, + previous_epoch_attestations: newState.previous_epoch_attestations, + current_epoch_attestations: newState.current_epoch_attestations, + justification_bits: newState.justification_bits, + previous_justified_checkpoint: newState.previous_justified_checkpoint, + current_justified_checkpoint: newState.current_justified_checkpoint, + finalized_checkpoint: newState.finalized_checkpoint + ) + +func GetNewBeaconState*(oldState: BeaconState): BeaconStateNew = + BeaconStateNew( + genesis_time: oldState.genesis_time, + slot: oldState.slot, + fork: oldState.fork, + latest_block_header: oldState.latest_block_header, + block_roots: oldState.block_roots, + state_roots: oldState.state_roots, + historical_roots: oldState.historical_roots, + eth1_data: oldState.eth1_data, + eth1_data_votes: oldState.eth1_data_votes, + eth1_deposit_index: oldState.eth1_deposit_index, + validators: oldState.validators, + balances: oldState.balances, + randao_mixes: oldState.randao_mixes, + slashings: oldState.slashings, + previous_epoch_attestations: oldState.previous_epoch_attestations, + current_epoch_attestations: oldState.current_epoch_attestations, + justification_bits: oldState.justification_bits, + previous_justified_checkpoint: oldState.previous_justified_checkpoint, + current_justified_checkpoint: oldState.current_justified_checkpoint, + finalized_checkpoint: oldState.finalized_checkpoint + ) + Json.useCustomSerialization(BeaconState.justification_bits): read: let s = reader.readValue(string) diff --git a/tests/official/test_fixture_operations_attestations.nim b/tests/official/test_fixture_operations_attestations.nim index 2ee62a99a..d18d62a1e 100644 --- a/tests/official/test_fixture_operations_attestations.nim +++ b/tests/official/test_fixture_operations_attestations.nim @@ -14,8 +14,7 @@ import # Test utilities ../testutil, ./fixtures_utils, - ../helpers/debug_state, - ../mocking/mock_blocks + ../helpers/debug_state const OperationsAttestationsDir = SszTestsDir/const_preset/"phase0"/"operations"/"attestation"/"pyspec_tests" diff --git a/tests/official/test_fixture_operations_block_header.nim b/tests/official/test_fixture_operations_block_header.nim index 6b5a6f21e..4249136df 100644 --- a/tests/official/test_fixture_operations_block_header.nim +++ b/tests/official/test_fixture_operations_block_header.nim @@ -14,8 +14,7 @@ import # Test utilities ../testutil, ./fixtures_utils, - ../helpers/debug_state, - ../mocking/mock_blocks + ../helpers/debug_state const OpBlockHeaderDir = SszTestsDir/const_preset/"phase0"/"operations"/"block_header"/"pyspec_tests" diff --git a/tests/official/test_fixture_operations_deposits.nim b/tests/official/test_fixture_operations_deposits.nim index 073e412c4..9fae4af9b 100644 --- a/tests/official/test_fixture_operations_deposits.nim +++ b/tests/official/test_fixture_operations_deposits.nim @@ -14,10 +14,9 @@ import # Test utilities ../testutil, ./fixtures_utils, - ../helpers/debug_state, - ../mocking/mock_blocks + ../helpers/debug_state -const OperationsDepositsDir = SszTestsDir/const_preset/"phase0"/"operations"/"deposit"/"pyspec_tests" +const OperationsDepositsDir = FixturesDir/"tests-v0.9.0"/const_preset/"phase0"/"operations"/"deposit"/"pyspec_tests" template runTest(testName: string, identifier: untyped) = # We wrap the tests in a proc to avoid running out of globals @@ -39,24 +38,29 @@ template runTest(testName: string, identifier: untyped) = prefix = "[Invalid] " test prefix & testName & " (" & astToStr(identifier) & ")": - var stateRef, postRef: ref BeaconState + var stateRef, postRef: ref BeaconStateNew + var sr_pre, sr_post: ref BeaconState var depositRef: ref Deposit new depositRef new stateRef + new sr_pre + new sr_post depositRef[] = parseTest(testDir/"deposit.ssz", SSZ, Deposit) - stateRef[] = parseTest(testDir/"pre.ssz", SSZ, BeaconState) + stateRef[] = parseTest(testDir/"pre.ssz", SSZ, BeaconStateNew) + sr_pre[] = GetOldBeaconState(stateRef[]) if existsFile(testDir/"post.ssz"): new postRef - postRef[] = parseTest(testDir/"post.ssz", SSZ, BeaconState) + postRef[] = parseTest(testDir/"post.ssz", SSZ, BeaconStateNew) + sr_post[] = GetOldBeaconState(postRef[]) if postRef.isNil: expect(AssertionError): - let done = process_deposit(stateRef[], depositRef[], flags) + let done = process_deposit(sr_pre[], depositRef[], flags) else: - let done = process_deposit(stateRef[], depositRef[], flags) - reportDiff(stateRef, postRef) + let done = process_deposit(sr_pre[], depositRef[], flags) + reportDiff(sr_pre, sr_post) `testImpl _ operations_deposits _ identifier`() diff --git a/tests/official/test_fixture_sanity_blocks.nim b/tests/official/test_fixture_sanity_blocks.nim index ec2b1f8f5..425004b34 100644 --- a/tests/official/test_fixture_sanity_blocks.nim +++ b/tests/official/test_fixture_sanity_blocks.nim @@ -14,8 +14,7 @@ import # Test utilities ../testutil, ./fixtures_utils, - ../helpers/debug_state, - ../mocking/mock_blocks + ../helpers/debug_state const SanityBlocksDir = SszTestsDir/const_preset/"phase0"/"sanity"/"blocks"/"pyspec_tests"