Python rlp.decode() Examples

The following are 30 code examples of rlp.decode(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module rlp , or try the search function .
Example #1
Source File: test_eth1_chaindb.py    From py-evm with MIT License 6 votes vote down vote up
def test_chaindb_get_score(chaindb):
    genesis = BlockHeader(difficulty=1, block_number=0, gas_limit=0)
    chaindb.persist_header(genesis)

    genesis_score_key = SchemaV1.make_block_hash_to_score_lookup_key(genesis.hash)
    genesis_score = rlp.decode(chaindb.db.get(genesis_score_key), sedes=rlp.sedes.big_endian_int)
    assert genesis_score == 1
    assert chaindb.get_score(genesis.hash) == 1

    block1 = BlockHeader(difficulty=10, block_number=1, gas_limit=0, parent_hash=genesis.hash)
    chaindb.persist_header(block1)

    block1_score_key = SchemaV1.make_block_hash_to_score_lookup_key(block1.hash)
    block1_score = rlp.decode(chaindb.db.get(block1_score_key), sedes=rlp.sedes.big_endian_int)
    assert block1_score == 11
    assert chaindb.get_score(block1.hash) == 11 
Example #2
Source File: refcount_db.py    From indy-plenum with Apache License 2.0 6 votes vote down vote up
def dec_refcount(self, k):
        # raise Exception("WHY AM I CHANGING A REFCOUNT?!:?")
        node_object = rlp.decode(self._keyValueStorage.get(b'r:' + k))
        refcount = utils.decode_int(node_object[0])
        if self.logging:
            sys.stderr.write('decreasing %s to: %d\n' % (
                utils.encode_hex(k), refcount - 1))
        if not refcount > 0:
            raise ValueError(
                "node object for key {} has {} number "
                "of references, expected > 0"
                .format(k, refcount)
            )
        self.journal.append([node_object[0], k])
        new_refcount = utils.encode_int(refcount - 1)
        self._keyValueStorage.put(
            b'r:' + k, rlp.encode([new_refcount, node_object[1]]))
        if new_refcount == ZERO_ENCODED:
            self.death_row.append(k) 
Example #3
Source File: refcount_db.py    From indy-plenum with Apache License 2.0 6 votes vote down vote up
def inc_refcount(self, k, v):
        # raise Exception("WHY AM I CHANGING A REFCOUNT?!:?")
        try:
            node_object = rlp.decode(self._keyValueStorage.get(b'r:' + k))
            refcount = utils.decode_int(node_object[0])
            self.journal.append([node_object[0], k])
            if refcount >= DEATH_ROW_OFFSET:
                refcount = 0
            new_refcount = utils.encode_int(refcount + 1)
            self._keyValueStorage.put(b'r:' + k, rlp.encode([new_refcount, v]))
            if self.logging:
                sys.stderr.write('increasing %s %r to: %d\n' % (
                    utils.encode_hex(k), v, refcount + 1))
        except BaseException:
            self._keyValueStorage.put(b'r:' + k, rlp.encode([ONE_ENCODED, v]))
            self.journal.append([ZERO_ENCODED, k])
            if self.logging:
                sys.stderr.write('increasing %s %r to: %d\n' % (
                    utils.encode_hex(k), v, 1)) 
Example #4
Source File: refcount_db.py    From indy-plenum with Apache License 2.0 6 votes vote down vote up
def revert_refcount_changes(self, epoch):
        timeout_epoch = epoch + self.ttl
        # Delete death row additions
        try:
            self._keyValueStorage.remove('deathrow:' + str(timeout_epoch))
        except BaseException:
            pass
        # Revert journal changes
        try:
            journal = rlp.decode(
                self._keyValueStorage.get('journal:' + str(epoch)))
            for new_refcount, hashkey in journal[::-1]:
                node_object = rlp.decode(
                    self._keyValueStorage.get(b'r:' + hashkey))
                k = b'r:' + hashkey
                v = rlp.encode([new_refcount, node_object[1]])
                self._keyValueStorage.put(k, v)
        except BaseException:
            pass 
Example #5
Source File: discovery.py    From pydevp2p with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def unpack(self, message):
        """
        macSize  = 256 / 8 = 32
        sigSize  = 520 / 8 = 65
        headSize = macSize + sigSize = 97
        hash, sig, sigdata := buf[:macSize], buf[macSize:headSize], buf[headSize:]
        shouldhash := crypto.Sha3(buf[macSize:])
        """
        mdc = message[:32]
        assert mdc == crypto.sha3(message[32:])
        signature = message[32:97]
        assert len(signature) == 65
        signed_data = crypto.sha3(message[97:])
        remote_pubkey = crypto.ecdsa_recover(signed_data, signature)
        assert len(remote_pubkey) == 512 / 8
        if not crypto.verify(remote_pubkey, signature, signed_data):
            raise InvalidSignature()
        cmd_id = self.decoders['cmd_id'](message[97])
        assert cmd_id in self.cmd_id_map.values()
        payload = rlp.decode(message[98:])
        assert isinstance(payload, list)
        expiration = self.decoders['expiration'](payload.pop())
        if time.time() > expiration:
            raise PacketExpired()
        return remote_pubkey, cmd_id, payload, mdc 
Example #6
Source File: protocol.py    From pydevp2p with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def decode_payload(cls, rlp_data):
            log.debug('decoding rlp', size=len(rlp_data))
            if isinstance(cls.structure, sedes.CountableList):
                decoder = cls.structure
            else:
                decoder = sedes.List([x[1] for x in cls.structure])
            try:
                data = rlp.decode(str(rlp_data), sedes=decoder)
            except (AssertionError, rlp.RLPException, TypeError) as e:
                print repr(rlp.decode(rlp_data))
                raise e
            if isinstance(cls.structure, sedes.CountableList):
                return data
            else:  # convert to dict
                return dict((cls.structure[i][0], v) for i, v in enumerate(data))

        # end command base ################################################### 
Example #7
Source File: state.py    From pyquarkchain with MIT License 6 votes vote down vote up
def to_dict(self):
        odict = self.storage_trie.to_dict()
        for k, v in self.storage_cache.items():
            odict[utils.encode_int(k)] = rlp.encode(utils.encode_int(v))
        return {
            "token_balances": self.token_balances.to_dict(),
            "nonce": str(self.nonce),
            "code": "0x" + encode_hex(self.code),
            "storage": {
                "0x"
                + encode_hex(key.lstrip(b"\x00") or b"\x00"): "0x"
                + encode_hex(rlp.decode(val))
                for key, val in odict.items()
            },
        }


# from ethereum.state import State 
Example #8
Source File: state.py    From pyquarkchain with MIT License 6 votes vote down vote up
def to_dict(self):
        if not self.token_trie:
            return self._balances

        # Iterating trie is costly. It's only for JSONRPC querying account information, which
        # can be improved later
        trie_dict = self.token_trie.to_dict()
        ret = {
            utils.big_endian_to_int(k): utils.big_endian_to_int(rlp.decode(v))
            for k, v in trie_dict.items()
        }
        # Get latest update
        for k, v in self._balances.items():
            if v == 0:
                ret.pop(k, None)
            else:
                ret[k] = v
        return ret 
Example #9
Source File: chain.py    From py-evm with MIT License 6 votes vote down vote up
def get_transaction_by_index(
            self,
            block_number: BlockNumber,
            transaction_index: int,
            transaction_class: Type[SignedTransactionAPI]) -> SignedTransactionAPI:
        try:
            block_header = self.get_canonical_block_header_by_number(block_number)
        except HeaderNotFound:
            raise TransactionNotFound(f"Block {block_number} is not in the canonical chain")
        transaction_db = HexaryTrie(self.db, root_hash=block_header.transaction_root)
        encoded_index = rlp.encode(transaction_index)
        encoded_transaction = transaction_db[encoded_index]
        if encoded_transaction != b'':
            return rlp.decode(encoded_transaction, sedes=transaction_class)
        else:
            raise TransactionNotFound(
                f"No transaction is at index {transaction_index} of block {block_number}"
            ) 
Example #10
Source File: multiplexer.py    From pydevp2p with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def decode(self, data=''):
        if data:
            self._decode_buffer.extend(data)
        if not self._cached_decode_header:
            if len(self._decode_buffer) < Frame.header_size + Frame.mac_size:
                return []
            else:
                self._cached_decode_header = self.decode_header(memoryview(self._decode_buffer))
                assert isinstance(self._cached_decode_header, str)

        body_size = struct.unpack('>I', '\x00' + self._cached_decode_header[:3])[0]
        required_len = Frame.header_size + Frame.mac_size + ceil16(body_size) + Frame.mac_size
        if len(self._decode_buffer) >= required_len:
            packet = self.decode_body(memoryview(self._decode_buffer), self._cached_decode_header)
            self._cached_decode_header = None
            self._decode_buffer = self._decode_buffer[required_len:]
            if packet:
                return [packet] + self.decode()
            else:
                return self.decode()
        return [] 
Example #11
Source File: utils.py    From airdrop with Apache License 2.0 6 votes vote down vote up
def _check_transaction(self, transaction, unsigned):
        """
        Checks a single (external) transaction against its expected unsigned (local) counterpart
        """
        decoded = rlp.decode(self.web3.toAscii(transaction['signedRaw']), Transaction)

        decoded_tx = dict(
            nonce=self.web3.toHex(decoded.nonce),
            gasPrice=self.web3.toHex(decoded.gasprice),
            gas=self.web3.toHex(decoded.startgas),
            to=self.web3.toHex(decoded.to),
            value=self.web3.toHex(decoded.value),
            data=self.web3.toHex(decoded.data)
        )

        unsigned['tx'].pop('from')

        if unsigned['tx'] != decoded_tx:
            logging.error("mismatch! signed tx: {}, local tx: {}".format(decoded_tx, unsigned['tx']))
            raise AirdropException("transaction mismatch for {}".format(unsigned['tx']['nonce'])) 
Example #12
Source File: protocol.py    From pyquarkchain with MIT License 6 votes vote down vote up
def decode_payload(self, rlp_data: bytes) -> PayloadType:
        if isinstance(self.structure, sedes.CountableList):
            decoder = self.structure
        else:
            decoder = sedes.List(
                [type_ for _, type_ in self.structure], strict=self.decode_strict
            )
        try:
            data = rlp.decode(rlp_data, sedes=decoder, recursive_cache=True)
        except rlp.DecodingError as err:
            raise MalformedMessage(
                "Malformed {} message: {}".format(type(self).__name__, repr(err))
            ) from err

        if isinstance(self.structure, sedes.CountableList):
            return data
        return {
            field_name: value
            for ((field_name, _), value) in zip(self.structure, data)
        } 
Example #13
Source File: discovery.py    From pyquarkchain with MIT License 6 votes vote down vote up
def _unpack_v5(
    message: bytes, V5_ID_STRING, HEAD_SIZE_V5
) -> Tuple[datatypes.PublicKey, int, Tuple[Any, ...], Hash32]:
    """Unpack a discovery v5 UDP message received from a remote node.

    Returns the public key used to sign the message, the cmd ID, payload and msg hash.
    """
    if not message.startswith(V5_ID_STRING):
        raise DefectiveMessage("Missing v5 version prefix")
    message_hash = keccak(message[len(V5_ID_STRING) :])
    signature = keys.Signature(message[len(V5_ID_STRING) : HEAD_SIZE_V5])
    body = message[HEAD_SIZE_V5:]
    remote_pubkey = signature.recover_public_key_from_msg(body)
    cmd_id = body[0]
    cmd = CMD_ID_MAP_V5[cmd_id]
    payload = tuple(rlp.decode(body[1:], strict=False))
    # Ignore excessive list elements as required by EIP-8.
    payload = payload[: cmd.elem_count]
    return remote_pubkey, cmd_id, payload, message_hash 
Example #14
Source File: discovery.py    From pyquarkchain with MIT License 6 votes vote down vote up
def _unpack_v4(
    message: bytes
) -> Tuple[datatypes.PublicKey, int, Tuple[Any, ...], Hash32]:
    """Unpack a discovery v4 UDP message received from a remote node.

    Returns the public key used to sign the message, the cmd ID, payload and hash.
    """
    message_hash = message[:MAC_SIZE]
    if message_hash != keccak(message[MAC_SIZE:]):
        raise WrongMAC("Wrong msg mac")
    signature = keys.Signature(message[MAC_SIZE:HEAD_SIZE])
    signed_data = message[HEAD_SIZE:]
    remote_pubkey = signature.recover_public_key_from_msg(signed_data)
    cmd_id = message[HEAD_SIZE]
    cmd = CMD_ID_MAP[cmd_id]
    payload = tuple(rlp.decode(message[HEAD_SIZE + 1 :], strict=False))
    # Ignore excessive list elements as required by EIP-8.
    payload = payload[: cmd.elem_count]
    return remote_pubkey, cmd_id, payload, message_hash 
Example #15
Source File: service.py    From trinity with MIT License 6 votes vote down vote up
def _get_account_from_peer(
            self,
            block_hash: Hash32,
            address: ETHAddress,
            peer: LESPeer) -> Account:
        state_key = keccak(address)
        proof = await self._get_proof(peer, block_hash, state_key=state_key, storage_key=None)
        header = await self._get_block_header_by_hash(block_hash, peer)
        try:
            rlp_account = HexaryTrie.get_from_proof(header.state_root, state_key, proof)
        except BadTrieProof as exc:
            raise BadLESResponse(
                f"Peer {peer} returned an invalid proof for account {encode_hex(address)} "
                f"at block {encode_hex(block_hash)}"
            ) from exc
        return rlp.decode(rlp_account, sedes=Account) 
Example #16
Source File: backfill.py    From trinity with MIT License 6 votes vote down vote up
def _get_children(self, encoded_node: bytes) -> Iterable[Hash32]:
        try:
            decoded_node = rlp.decode(encoded_node)
        except rlp.DecodingError:
            # Could not decode rlp, it's probably a bytecode, carry on...
            return set()

        if len(decoded_node) == 17:
            # branch node
            return set(node_hash for node_hash in decoded_node[:16] if len(node_hash) == 32)
        elif len(decoded_node) == 2 and len(decoded_node[1]) == 32:
            # leaf or extension node
            return {decoded_node[1]}
        else:
            # final value, ignore
            return set() 
Example #17
Source File: transactions.py    From py-evm with MIT License 6 votes vote down vote up
def create_transaction_signature(unsigned_txn: BaseTransaction,
                                 private_key: datatypes.PrivateKey,
                                 chain_id: int=None) -> VRS:

    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 VRS((v, r, s)) 
Example #18
Source File: helpers.py    From py-evm with MIT License 6 votes vote down vote up
def apply_fixture_block_to_chain(
        block_fixture: Dict[str, Any],
        chain: ChainAPI,
        perform_validation: bool=True) -> Tuple[BlockAPI, BlockAPI, BlockAPI]:
    """
    :return: (premined_block, mined_block, rlp_encoded_mined_block)
    """
    # The block to import may be in a different block-class-range than the
    # chain's current one, so we use the block number specified in the
    # fixture to look up the correct block class.
    if 'blockHeader' in block_fixture:
        block_number = block_fixture['blockHeader']['number']
        block_class = chain.get_vm_class_for_block_number(block_number).get_block_class()
    else:
        block_class = chain.get_vm().get_block_class()

    block = rlp.decode(block_fixture['rlp'], sedes=block_class)

    import_result = chain.import_block(block, perform_validation=perform_validation)
    mined_block = import_result.imported_block

    rlp_encoded_mined_block = rlp.encode(mined_block, sedes=block_class)

    return (block, mined_block, rlp_encoded_mined_block) 
Example #19
Source File: chain.py    From py-evm with MIT License 5 votes vote down vote up
def get_transaction_index(self, transaction_hash: Hash32) -> Tuple[BlockNumber, int]:
        key = SchemaV1.make_transaction_hash_to_block_lookup_key(transaction_hash)
        try:
            encoded_key = self.db[key]
        except KeyError:
            raise TransactionNotFound(
                f"Transaction {encode_hex(transaction_hash)} not found in canonical chain"
            )

        transaction_key = rlp.decode(encoded_key, sedes=TransactionKey)
        return (transaction_key.block_number, transaction_key.index) 
Example #20
Source File: chain.py    From py-evm with MIT License 5 votes vote down vote up
def get_receipts(self,
                     header: BlockHeaderAPI,
                     receipt_class: Type[ReceiptAPI]) -> Iterable[ReceiptAPI]:
        receipt_db = HexaryTrie(db=self.db, root_hash=header.receipt_root)
        for receipt_idx in itertools.count():
            receipt_key = rlp.encode(receipt_idx)
            receipt_data = receipt_db[receipt_key]
            if receipt_data != b'':
                yield rlp.decode(receipt_data, sedes=receipt_class)
            else:
                break 
Example #21
Source File: header.py    From py-evm with MIT License 5 votes vote down vote up
def _get_score(db: DatabaseAPI, block_hash: Hash32) -> int:
        try:
            encoded_score = db[SchemaV1.make_block_hash_to_score_lookup_key(block_hash)]
        except KeyError:
            raise HeaderNotFound(f"No header with hash {encode_hex(block_hash)} found")
        return rlp.decode(encoded_score, sedes=rlp.sedes.big_endian_int) 
Example #22
Source File: chain.py    From py-evm with MIT License 5 votes vote down vote up
def get_block_uncles(self, uncles_hash: Hash32) -> Tuple[BlockHeaderAPI, ...]:
        validate_word(uncles_hash, title="Uncles Hash")
        if uncles_hash == EMPTY_UNCLE_HASH:
            return ()
        try:
            encoded_uncles = self.db[uncles_hash]
        except KeyError as exc:
            raise HeaderNotFound(
                f"No uncles found for hash {uncles_hash!r}"
            ) from exc
        else:
            return tuple(rlp.decode(encoded_uncles, sedes=rlp.sedes.CountableList(BlockHeader))) 
Example #23
Source File: chain.py    From py-evm with MIT License 5 votes vote down vote up
def _get_block_transactions(
            self,
            transaction_root: Hash32,
            transaction_class: Type[SignedTransactionAPI]) -> Iterable[SignedTransactionAPI]:
        """
        Memoizable version of `get_block_transactions`
        """
        for encoded_transaction in self._get_block_transaction_data(self.db, transaction_root):
            yield rlp.decode(encoded_transaction, sedes=transaction_class) 
Example #24
Source File: encoding.py    From py-evm with MIT License 5 votes vote down vote up
def decode_address_tally_pair(pair: bytes) -> Tuple[Address, Tally]:
    address, tally_bytes, = rlp.decode(
        pair,
        sedes=ADDRESS_TALLY_SEDES,
    )

    tally = decode_tally(tally_bytes)

    return address, tally 
Example #25
Source File: header.py    From py-evm with MIT License 5 votes vote down vote up
def _get_canonical_block_hash(db: DatabaseAPI, block_number: BlockNumber) -> Hash32:
        validate_block_number(block_number)
        number_to_hash_key = SchemaV1.make_block_number_to_hash_lookup_key(block_number)

        try:
            encoded_key = db[number_to_hash_key]
        except KeyError:
            raise HeaderNotFound(
                f"No canonical header for block number #{block_number}"
            )
        else:
            return rlp.decode(encoded_key, sedes=rlp.sedes.binary) 
Example #26
Source File: nodes.py    From py-trie with MIT License 5 votes vote down vote up
def decode_node(encoded_node_or_hash):
    if encoded_node_or_hash == BLANK_NODE:
        return BLANK_NODE
    elif isinstance(encoded_node_or_hash, list):
        return encoded_node_or_hash
    else:
        return rlp.decode(encoded_node_or_hash) 
Example #27
Source File: chain.py    From py-evm with MIT License 5 votes vote down vote up
def _get_chain_gaps(cls, db: DatabaseAPI) -> ChainGaps:
        try:
            encoded_gaps = db[SchemaV1.make_chain_gaps_lookup_key()]
        except KeyError:
            return GENESIS_CHAIN_GAPS
        else:
            return rlp.decode(encoded_gaps, sedes=chain_gaps) 
Example #28
Source File: storage.py    From py-evm with MIT License 5 votes vote down vote up
def get(self, slot: int, from_journal: bool=True) -> int:
        self._accessed_slots.add(slot)
        key = int_to_big_endian(slot)
        lookup_db = self._journal_storage if from_journal else self._locked_changes
        try:
            encoded_value = lookup_db[key]
        except MissingStorageTrieNode:
            raise
        except KeyError:
            return 0

        if encoded_value == b'':
            return 0
        else:
            return rlp.decode(encoded_value, sedes=rlp.sedes.big_endian_int) 
Example #29
Source File: account.py    From py-evm with MIT License 5 votes vote down vote up
def _get_account(self, address: Address, from_journal: bool=True) -> Account:
        if from_journal and address in self._account_cache:
            return self._account_cache[address]

        rlp_account = self._get_encoded_account(address, from_journal)

        if rlp_account:
            account = rlp.decode(rlp_account, sedes=Account)
        else:
            account = Account()
        if from_journal:
            self._account_cache[address] = account
        return account 
Example #30
Source File: header.py    From py-evm with MIT License 5 votes vote down vote up
def _get_header_chain_gaps(cls, db: DatabaseAPI) -> ChainGaps:
        try:
            encoded_gaps = db[SchemaV1.make_header_chain_gaps_lookup_key()]
        except KeyError:
            return GENESIS_CHAIN_GAPS
        else:
            return rlp.decode(encoded_gaps, sedes=chain_gaps)