Skip to content

Commit

Permalink
fix copies, no deepcopy anymore, cheaper with datasharing
Browse files Browse the repository at this point in the history
  • Loading branch information
protolambda committed Jan 2, 2020
1 parent 0d753e4 commit f39aa73
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 18 deletions.
4 changes: 1 addition & 3 deletions test_libs/pyspec/eth2spec/test/helpers/attester_slashings.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from copy import deepcopy

from eth2spec.test.helpers.attestations import get_valid_attestation, sign_attestation


def get_valid_attester_slashing(spec, state, signed_1=False, signed_2=False):
attestation_1 = get_valid_attestation(spec, state, signed=signed_1)

attestation_2 = deepcopy(attestation_1)
attestation_2 = attestation_1.copy()
attestation_2.data.target.root = b'\x01' * 32

if signed_2:
Expand Down
6 changes: 2 additions & 4 deletions test_libs/pyspec/eth2spec/test/helpers/block.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from copy import deepcopy

from eth2spec.test.helpers.keys import privkeys
from eth2spec.utils.bls import bls_sign, only_with_bls
from eth2spec.utils.ssz.ssz_impl import hash_tree_root
Expand All @@ -15,7 +13,7 @@ def get_proposer_index_maybe(spec, state, slot, proposer_index=None):
print("warning: block slot far away, and no proposer index manually given."
" Signing block is slow due to transition for proposer index calculation.")
# use stub state to get proposer index of future slot
stub_state = deepcopy(state)
stub_state = state.copy()
spec.process_slots(stub_state, slot)
proposer_index = spec.get_beacon_proposer_index(stub_state)
return proposer_index
Expand Down Expand Up @@ -81,7 +79,7 @@ def build_empty_block(spec, state, slot=None):
empty_block = spec.BeaconBlock()
empty_block.slot = slot
empty_block.body.eth1_data.deposit_count = state.eth1_deposit_index
previous_block_header = deepcopy(state.latest_block_header)
previous_block_header = state.latest_block_header.copy()
if previous_block_header.state_root == spec.Root():
previous_block_header.state_root = hash_tree_root(state)
empty_block.parent_root = hash_tree_root(previous_block_header)
Expand Down
2 changes: 1 addition & 1 deletion test_libs/pyspec/eth2spec/test/helpers/genesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def create_genesis_state(spec, validator_balances, activation_threshold):

# We "hack" in the initial validators,
# as it is much faster than creating and processing genesis deposits for every single test case.
state.balances = copy.deepcopy(validator_balances)
state.balances = validator_balances
state.validators = [build_mock_validator(spec, i, state.balances[i]) for i in range(len(validator_balances))]

# Process genesis activations
Expand Down
6 changes: 2 additions & 4 deletions test_libs/pyspec/eth2spec/test/helpers/phase1/shard_block.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from copy import deepcopy

from eth2spec.test.helpers.keys import privkeys
from eth2spec.utils.bls import (
bls_sign,
Expand Down Expand Up @@ -41,12 +39,12 @@ def build_empty_shard_block(spec,
if slot is None:
slot = shard_state.slot

previous_beacon_header = deepcopy(beacon_state.latest_block_header)
previous_beacon_header = beacon_state.latest_block_header.copy()
if previous_beacon_header.state_root == spec.Bytes32():
previous_beacon_header.state_root = beacon_state.hash_tree_root()
beacon_block_root = hash_tree_root(previous_beacon_header)

previous_block_header = deepcopy(shard_state.latest_block_header)
previous_block_header = shard_state.latest_block_header.copy()
if previous_block_header.state_root == spec.Bytes32():
previous_block_header.state_root = shard_state.hash_tree_root()
parent_root = hash_tree_root(previous_block_header)
Expand Down
4 changes: 1 addition & 3 deletions test_libs/pyspec/eth2spec/test/helpers/proposer_slashings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from copy import deepcopy

from eth2spec.test.helpers.block_header import sign_block_header
from eth2spec.test.helpers.keys import pubkey_to_privkey

Expand All @@ -16,7 +14,7 @@ def get_valid_proposer_slashing(spec, state, signed_1=False, signed_2=False):
state_root=b'\x44' * 32,
body_root=b'\x55' * 32,
)
header_2 = deepcopy(header_1)
header_2 = header_1.copy()
header_2.parent_root = b'\x99' * 32

if signed_1:
Expand Down
4 changes: 1 addition & 3 deletions test_libs/pyspec/eth2spec/test/helpers/state.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from copy import deepcopy

from eth2spec.test.context import expect_assertion_error
from eth2spec.test.helpers.attestations import get_valid_attestation
from eth2spec.test.helpers.block import sign_block, build_empty_block_for_next_slot, transition_unsigned_block
Expand Down Expand Up @@ -61,7 +59,7 @@ def next_epoch_with_attestations(spec,
fill_prev_epoch):
assert state.slot % spec.SLOTS_PER_EPOCH == 0

post_state = deepcopy(state)
post_state = state.copy()
signed_blocks = []
for _ in range(spec.SLOTS_PER_EPOCH):
block = build_empty_block_for_next_slot(spec, post_state)
Expand Down

0 comments on commit f39aa73

Please sign in to comment.