Skip to content

Commit d910d42

Browse files
committed
Increase safety of code by avoiding mutation
1 parent dfe43ae commit d910d42

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

tests/beacon/test_helpers.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import pytest
44
import random
55

6+
import cytoolz
7+
68
from hypothesis import (
79
given,
810
strategies as st,
@@ -61,6 +63,10 @@
6163
)
6264

6365

66+
class UnreachableCodePathError(Exception):
67+
pass
68+
69+
6470
@pytest.fixture()
6571
def sample_block(sample_beacon_block_params):
6672
return SerenityBeaconBlock(**sample_beacon_block_params)
@@ -847,22 +853,27 @@ def _correct_slashable_vote_data_params(params, validators, messages, privkeys):
847853

848854

849855
def _corrupt_signature(params):
850-
params = copy.deepcopy(params)
851856
message = bytes.fromhex("deadbeefcafe")
852857
privkey = 42
853858
domain = SignatureDomain.DOMAIN_ATTESTATION
854-
params["aggregate_signature"] = bls.sign(message, privkey, domain)
855-
return params
859+
corrupt_signature = bls.sign(message, privkey, domain)
860+
861+
return cytoolz.assoc(params, "aggregate_signature", corrupt_signature)
856862

857863

858864
def _corrupt_vote_count(params):
859-
params = copy.deepcopy(params)
860865
key = "custody_bit_0_indices"
861866
for i in itertools.count():
862867
if i not in params[key]:
863-
params[key].append(i)
864-
break
865-
return params
868+
new_vote_count = params[key] + [i]
869+
return cytoolz.assoc(
870+
params,
871+
key,
872+
new_vote_count,
873+
)
874+
else:
875+
msg = "list of ``custody_bit_0_indices`` should not exhaust ``itertools.count``"
876+
raise UnreachableCodePathError(msg)
866877

867878

868879
def _create_slashable_vote_data_messages(params):

0 commit comments

Comments
 (0)