Example #1
0
def ecadd(computation):
    computation.consume_gas(constants.GAS_ECADD, reason='ECADD Precompile')

    try:
        result = _ecadd(computation.msg.data)
    except ValidationError:
        raise VMError("Invalid ECADD parameters")

    result_x, result_y = result
    result_bytes = b''.join((
        pad32(int_to_big_endian(result_x.n)),
        pad32(int_to_big_endian(result_y.n)),
    ))
    computation.output = result_bytes
    return computation
Example #2
0
def test_chunk_iteration():
    chunk_number = COLLATION_SIZE // CHUNK_SIZE
    test_chunks = [
        zpad_left(int_to_big_endian(i), CHUNK_SIZE)
        for i in range(chunk_number)
    ]

    chunks = test_chunks
    body = b"".join(chunks)
    for recovered, original in zip_longest(iterate_chunks(body),
                                           chunks,
                                           fillvalue=None):
        assert recovered is not None and original is not None
        assert recovered == original

    chunks = test_chunks[:-2]
    body = b"".join(chunks)
    with pytest.raises(ValidationError):
        next(iterate_chunks(body))

    chunks = test_chunks
    body = b"".join(chunks)[:-2]
    with pytest.raises(ValidationError):
        next(iterate_chunks(body))

    chunks = test_chunks
    body = b"".join(chunks) + b"\x00"
    with pytest.raises(ValidationError):
        next(iterate_chunks(body))

    chunks = test_chunks + [b"\x00" * CHUNK_SIZE]
    body = b"".join(chunks)
    with pytest.raises(ValidationError):
        next(iterate_chunks(body))
Example #3
0
    def get_storage(self, address, slot):
        validate_canonical_address(address, title="Storage Address")
        validate_uint256(slot, title="Storage Slot")

        account = self._get_account(address)
        storage = HashTrie(HexaryTrie(self._journaldb, account.storage_root))

        slot_as_key = pad32(int_to_big_endian(slot))

        if slot_as_key in storage:
            encoded_value = storage[slot_as_key]
            return rlp.decode(encoded_value, sedes=rlp.sedes.big_endian_int)
        else:
            return 0
Example #4
0
def modexp(computation):
    """
    https://github.com/ethereum/EIPs/pull/198
    """
    gas_fee = _compute_modexp_gas_fee(computation.msg.data)
    computation.consume_gas(gas_fee, reason='MODEXP Precompile')

    result = _modexp(computation.msg.data)

    _, _, modulus_length = _extract_lengths(computation.msg.data)
    result_bytes = zpad_left(int_to_big_endian(result), to_size=modulus_length)

    computation.output = result_bytes
    return computation
Example #5
0
 def get_message_for_signing(self):
     if is_eip_155_signed_transaction(self):
         txn_parts = rlp.decode(rlp.encode(self))
         txn_parts_for_signing = txn_parts[:-3] + [
             int_to_big_endian(self.chain_id), b'', b''
         ]
         return rlp.encode(txn_parts_for_signing)
     else:
         return rlp.encode(
             SpuriousDragonUnsignedTransaction(
                 nonce=self.nonce,
                 gas_price=self.gas_price,
                 gas=self.gas,
                 to=self.to,
                 value=self.value,
                 data=self.data,
             ))
Example #6
0
    def set_storage(self, address, slot, value):
        validate_uint256(value, title="Storage Value")
        validate_uint256(slot, title="Storage Slot")
        validate_canonical_address(address, title="Storage Address")

        account = self._get_account(address)
        storage = HashTrie(HexaryTrie(self._journaldb, account.storage_root))

        slot_as_key = pad32(int_to_big_endian(slot))

        if value:
            encoded_value = rlp.encode(value)
            storage[slot_as_key] = encoded_value
        else:
            del storage[slot_as_key]

        self._set_account(address,
                          account.copy(storage_root=storage.root_hash))
Example #7
0
def create_transaction_signature(unsigned_txn, private_key, chain_id=None):
    transaction_parts = rlp.decode(rlp.encode(unsigned_txn))

    if chain_id:
        transaction_parts_for_signature = (
            transaction_parts + [int_to_big_endian(chain_id), b'', b''])
    else:
        transaction_parts_for_signature = transaction_parts

    message = rlp.encode(transaction_parts_for_signature)
    signature = private_key.sign_msg(message)

    canonical_v, r, s = signature.vrs

    if chain_id:
        v = canonical_v + chain_id * 2 + EIP155_CHAIN_ID_OFFSET
    else:
        v = canonical_v + V_OFFSET

    return v, r, s
Example #8
0
 def _pop(self, num_items, type_hint):
     for _ in range(num_items):
         if type_hint == constants.UINT256:
             value = self.values.pop()
             if isinstance(value, int):
                 yield value
             else:
                 yield big_endian_to_int(value)
         elif type_hint == constants.BYTES:
             value = self.values.pop()
             if isinstance(value, bytes):
                 yield value
             else:
                 yield int_to_big_endian(value)
         elif type_hint == constants.ANY:
             yield self.values.pop()
         else:
             raise TypeError(
                 "Unknown type_hint: {0}.  Must be one of {1}".format(
                     type_hint,
                     ", ".join((constants.UINT256, constants.BYTES)),
                 ))