Skip to content

Commit d8c7885

Browse files
committed
Increase safety of code by avoiding mutation
1 parent ca0844c commit d8c7885

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,
@@ -60,6 +62,10 @@
6062
)
6163

6264

65+
class UnreachableCodePathError(Exception):
66+
pass
67+
68+
6369
@pytest.fixture()
6470
def sample_block(sample_beacon_block_params):
6571
return SerenityBeaconBlock(**sample_beacon_block_params)
@@ -822,22 +828,27 @@ def _correct_slashable_vote_data_params(params, validators, messages, privkeys):
822828

823829

824830
def _corrupt_signature(params):
825-
params = copy.deepcopy(params)
826831
message = bytes.fromhex("deadbeefcafe")
827832
privkey = 42
828833
domain = SignatureDomain.DOMAIN_ATTESTATION
829-
params["aggregate_signature"] = bls.sign(message, privkey, domain)
830-
return params
834+
corrupt_signature = bls.sign(message, privkey, domain)
835+
836+
return cytoolz.assoc(params, "aggregate_signature", corrupt_signature)
831837

832838

833839
def _corrupt_vote_count(params):
834-
params = copy.deepcopy(params)
835840
key = "custody_bit_0_indices"
836841
for i in itertools.count():
837842
if i not in params[key]:
838-
params[key].append(i)
839-
break
840-
return params
843+
new_vote_count = params[key] + [i]
844+
return cytoolz.assoc(
845+
params,
846+
key,
847+
new_vote_count,
848+
)
849+
else:
850+
msg = "list of ``custody_bit_0_indices`` should not exhaust ``itertools.count``"
851+
raise UnreachableCodePathError(msg)
841852

842853

843854
def _create_slashable_vote_data_messages(params):

0 commit comments

Comments
 (0)