From a07219c57045a22d13304148dbae8bb9121a0181 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Tue, 19 Mar 2019 11:39:19 +0800 Subject: [PATCH] Fix linter --- scripts/phase0/build_spec.py | 1 - scripts/phase0/function_puller.py | 2 +- tests/phase0/helpers.py | 5 +++-- tests/phase0/test_sanity.py | 34 ++++++++----------------------- utils/phase0/bls_stub.py | 2 +- utils/phase0/hash_function.py | 5 +++-- utils/phase0/merkle_minimal.py | 12 ++++++----- utils/phase0/minimal_ssz.py | 24 ++++++++++++++++++---- utils/phase0/state_transition.py | 2 +- 9 files changed, 45 insertions(+), 42 deletions(-) diff --git a/scripts/phase0/build_spec.py b/scripts/phase0/build_spec.py index eb4f580bd..ae5a5a4f2 100644 --- a/scripts/phase0/build_spec.py +++ b/scripts/phase0/build_spec.py @@ -34,7 +34,6 @@ Any = None Store = None """) - code_lines += function_puller.get_lines(sourcefile) code_lines.append(""" diff --git a/scripts/phase0/function_puller.py b/scripts/phase0/function_puller.py index 8d1c1a0cc..7d5796fc7 100644 --- a/scripts/phase0/function_puller.py +++ b/scripts/phase0/function_puller.py @@ -9,7 +9,7 @@ def get_lines(file_name): for linenum, line in enumerate(open(sys.argv[1]).readlines()): line = line.rstrip() if pulling_from is None and len(line) > 0 and line[0] == '#' and line[-1] == '`': - current_name = line[line[:-1].rfind('`')+1: -1] + current_name = line[line[:-1].rfind('`') + 1: -1] if line[:9] == '```python': assert pulling_from is None pulling_from = linenum + 1 diff --git a/tests/phase0/helpers.py b/tests/phase0/helpers.py index fa0ba61b5..f7c39ffec 100644 --- a/tests/phase0/helpers.py +++ b/tests/phase0/helpers.py @@ -26,7 +26,7 @@ from build.phase0.utils.merkle_minimal import ( ) -privkeys_list = [i+1 for i in range(1000)] +privkeys_list = [i + 1 for i in range(1000)] pubkeys_list = [bls.privtopub(privkey) for privkey in privkeys_list] pubkey_to_privkey = {pubkey: privkey for privkey, pubkey in zip(privkeys_list, pubkeys_list)} @@ -77,6 +77,7 @@ def create_genesis_state(num_validators, deposit_data_leaves): ), ) + def build_empty_block_for_next_slot(state): empty_block = get_empty_block() empty_block.slot = state.slot + 1 @@ -91,7 +92,7 @@ def build_deposit_data(state, pubkey, privkey, amount): deposit_input = DepositInput( pubkey=pubkey, withdrawal_credentials=privkey.to_bytes(32, byteorder='big'), - proof_of_possession=b'00'*96, + proof_of_possession=b'\x00' * 96, ) proof_of_possession = bls.sign( message_hash=signed_root(deposit_input), diff --git a/tests/phase0/test_sanity.py b/tests/phase0/test_sanity.py index bfbb2de94..8799c1ffb 100644 --- a/tests/phase0/test_sanity.py +++ b/tests/phase0/test_sanity.py @@ -1,6 +1,3 @@ -import os -import sys -import time from copy import deepcopy import pytest @@ -12,32 +9,21 @@ from build.phase0.utils.minimal_ssz import signed_root from build.phase0.spec import ( # SSZ Attestation, - AttestationData, AttestationDataAndCustodyBit, BeaconBlockHeader, Deposit, - DepositData, - DepositInput, - Eth1Data, Transfer, ProposerSlashing, - Validator, VoluntaryExit, # functions - int_to_bytes32, - int_to_bytes48, get_active_validator_indices, get_attestation_participants, get_block_root, get_crosslink_committees_at_slot, get_current_epoch, get_domain, - get_empty_block, - get_epoch_start_slot, - get_genesis_beacon_state, get_state_root, advance_slot, - slot_to_epoch, cache_state, verify_merkle_branch, hash, @@ -60,6 +46,7 @@ from tests.phase0.helpers import ( # mark entire file as 'sanity' pytestmark = pytest.mark.sanity + def test_slot_transition(state): test_state = deepcopy(state) cache_state(test_state) @@ -126,18 +113,17 @@ def test_proposer_slashing(state, pubkeys, privkeys): test_state = deepcopy(state) current_epoch = get_current_epoch(test_state) validator_index = get_active_validator_indices(test_state.validator_registry, current_epoch)[-1] - pubkey = pubkeys[validator_index] privkey = privkeys[validator_index] slot = spec.GENESIS_SLOT header_1 = BeaconBlockHeader( slot=slot, - previous_block_root=b'\x00'*32, - state_root=b'\x00'*32, - block_body_root=b'\x00'*32, - signature=b'\x00'*96 + previous_block_root=b'\x00' * 32, + state_root=b'\x00' * 32, + block_body_root=b'\x00' * 32, + signature=b'\x00' * 96 ) header_2 = deepcopy(header_1) - header_2.previous_block_root = b'\x02'*32 + header_2.previous_block_root = b'\x02' * 32 header_2.slot = slot + 1 domain = get_domain( @@ -273,7 +259,7 @@ def test_attestation(state, pubkeys, privkeys): aggregation_bitfield=aggregation_bitfield, data=attestation_data, custody_bitfield=custody_bitfield, - aggregate_signature=b'\x00'*96, + aggregate_signature=b'\x00' * 96, ) participants = get_attestation_participants( test_state, @@ -283,7 +269,6 @@ def test_attestation(state, pubkeys, privkeys): assert len(participants) == 1 validator_index = participants[0] - pubkey = pubkeys[validator_index] privkey = privkeys[validator_index] message_hash = AttestationDataAndCustodyBit( @@ -329,7 +314,6 @@ def test_attestation(state, pubkeys, privkeys): def test_voluntary_exit(state, pubkeys, privkeys): pre_state = deepcopy(state) validator_index = get_active_validator_indices(pre_state.validator_registry, get_current_epoch(pre_state))[-1] - pubkey = pubkeys[validator_index] # move state forward PERSISTENT_COMMITTEE_PERIOD epochs to allow for exit pre_state.slot += spec.PERSISTENT_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH @@ -341,7 +325,7 @@ def test_voluntary_exit(state, pubkeys, privkeys): voluntary_exit = VoluntaryExit( epoch=get_current_epoch(pre_state), validator_index=validator_index, - signature=b'\x00'*96, + signature=b'\x00' * 96, ) voluntary_exit.signature = bls.sign( message_hash=signed_root(voluntary_exit), @@ -392,7 +376,7 @@ def test_transfer(state, pubkeys, privkeys): fee=0, slot=pre_state.slot + 1, pubkey=transfer_pubkey, - signature=b'\x00'*96, + signature=b'\x00' * 96, ) transfer.signature = bls.sign( message_hash=signed_root(transfer), diff --git a/utils/phase0/bls_stub.py b/utils/phase0/bls_stub.py index 7e3a6a308..108c4ef71 100644 --- a/utils/phase0/bls_stub.py +++ b/utils/phase0/bls_stub.py @@ -9,4 +9,4 @@ def bls_verify_multiple(pubkeys, message_hashes, signature, domain): def bls_aggregate_pubkeys(pubkeys): - return b'\x42'*96 + return b'\x42' * 96 diff --git a/utils/phase0/hash_function.py b/utils/phase0/hash_function.py index da5b4d979..21e6555bf 100644 --- a/utils/phase0/hash_function.py +++ b/utils/phase0/hash_function.py @@ -1,6 +1,7 @@ -from hashlib import sha256 +# from hashlib import sha256 from eth_utils import keccak # def hash(x): return sha256(x).digest() -def hash(x): return keccak(x) +def hash(x): + return keccak(x) diff --git a/utils/phase0/merkle_minimal.py b/utils/phase0/merkle_minimal.py index a811350ce..7c5483de3 100644 --- a/utils/phase0/merkle_minimal.py +++ b/utils/phase0/merkle_minimal.py @@ -2,8 +2,9 @@ from .hash_function import hash zerohashes = [b'\x00' * 32] -for i in range(1, 32): - zerohashes.append(hash(zerohashes[i-1] + zerohashes[i-1])) +for layer in range(1, 32): + zerohashes.append(hash(zerohashes[layer - 1] + zerohashes[layer - 1])) + # Compute a Merkle root of a right-zerobyte-padded 2**32 sized tree def calc_merkle_tree_from_leaves(values): @@ -12,17 +13,18 @@ def calc_merkle_tree_from_leaves(values): for h in range(32): if len(values) % 2 == 1: values.append(zerohashes[h]) - # print(values) - values = [hash(values[i] + values[i+1]) for i in range(0, len(values), 2)] + values = [hash(values[i] + values[i + 1]) for i in range(0, len(values), 2)] tree.append(values[::]) return tree + def get_merkle_root(values): return calc_merkle_tree_from_leaves(values)[-1][0] + def get_merkle_proof(tree, item_index): proof = [] for i in range(32): - subindex = (item_index//2**i)^1 + subindex = (item_index // 2**i) ^ 1 proof.append(tree[i][subindex] if subindex < len(tree[i]) else zerohashes[i]) return proof diff --git a/utils/phase0/minimal_ssz.py b/utils/phase0/minimal_ssz.py index 845de18c3..08bd68357 100644 --- a/utils/phase0/minimal_ssz.py +++ b/utils/phase0/minimal_ssz.py @@ -5,6 +5,7 @@ BYTES_PER_CHUNK = 32 BYTES_PER_LENGTH_PREFIX = 4 ZERO_CHUNK = b'\x00' * BYTES_PER_CHUNK + def SSZType(fields): class SSZObject(): def __init__(self, **kwargs): @@ -37,6 +38,7 @@ def SSZType(fields): SSZObject.fields = fields return SSZObject + class Vector(list): def __init__(self, x): list.__init__(self, x) @@ -47,9 +49,11 @@ class Vector(list): remove = clear = extend = pop = insert = append + def is_basic(typ): return isinstance(typ, str) and (typ[:4] in ('uint', 'bool') or typ == 'byte') + def is_constant_sized(typ): if is_basic(typ): return True @@ -67,6 +71,7 @@ def is_constant_sized(typ): else: raise Exception("Type not recognized") + def coerce_to_bytes(x): if isinstance(x, str): o = x.encode('utf-8') @@ -77,6 +82,7 @@ def coerce_to_bytes(x): else: raise Exception("Expecting bytes") + def serialize_value(value, typ=None): if typ is None: typ = infer_type(value) @@ -110,28 +116,34 @@ def serialize_value(value, typ=None): print(value, typ) raise Exception("Type not recognized") + def chunkify(bytez): bytez += b'\x00' * (-len(bytez) % BYTES_PER_CHUNK) - return [bytez[i:i+32] for i in range(0, len(bytez), 32)] + return [bytez[i:i + 32] for i in range(0, len(bytez), 32)] + def pack(values, subtype): return chunkify(b''.join([serialize_value(value, subtype) for value in values])) + def is_power_of_two(x): - return x > 0 and x & (x-1) == 0 + return x > 0 and x & (x - 1) == 0 + def merkleize(chunks): tree = chunks[::] while not is_power_of_two(len(tree)): tree.append(ZERO_CHUNK) tree = [ZERO_CHUNK] * len(tree) + tree - for i in range(len(tree)//2-1, 0, -1): - tree[i] = hash(tree[i*2] + tree[i*2+1]) + for i in range(len(tree) // 2 - 1, 0, -1): + tree[i] = hash(tree[i * 2] + tree[i * 2 + 1]) return tree[1] + def mix_in_length(root, length): return hash(root + length.to_bytes(32, 'little')) + def infer_type(value): if hasattr(value.__class__, 'fields'): return value.__class__ @@ -146,6 +158,7 @@ def infer_type(value): else: raise Exception("Failed to infer type") + def hash_tree_root(value, typ=None): if typ is None: typ = infer_type(value) @@ -170,6 +183,7 @@ def hash_tree_root(value, typ=None): else: raise Exception("Type not recognized") + def truncate(container): field_keys = list(container.fields.keys()) truncated_fields = { @@ -183,8 +197,10 @@ def truncate(container): } return truncated_class(**kwargs) + def signed_root(container): return hash_tree_root(truncate(container)) + def serialize(ssz_object): return getattr(ssz_object, 'serialize')() diff --git a/utils/phase0/state_transition.py b/utils/phase0/state_transition.py index 170f647ab..92d67c45a 100644 --- a/utils/phase0/state_transition.py +++ b/utils/phase0/state_transition.py @@ -1,7 +1,7 @@ from . import spec -from typing import ( +from typing import ( # noqa: F401 Any, Callable, List,