예제 #1
0
 def intrinsic_gas_used(self):
     num_zero_bytes = str_to_bytes(self.data).count(ascii_chr(0))
     num_non_zero_bytes = len(self.data) - num_zero_bytes
     return (opcodes.GTXCOST
             #         + (0 if self.to else opcodes.CREATE[3])
             + opcodes.GTXDATAZERO * num_zero_bytes +
             opcodes.GTXDATANONZERO * num_non_zero_bytes)
예제 #2
0
 def sync_child_chain(self):
     self.current_block = self.client.get_current_block_num()
     while self.synced_block < self.current_block:
         block_number = self.synced_block
         block = self.client.get_block(self.synced_block)
         self.db.put(bytes(block_number), utils.str_to_bytes(block))
         print("Synced %s" % self.synced_block)
         self.synced_block += 1
     print("Syncing complete!")
예제 #3
0
    def _encode_node(self, node, put_in_db=True):
        if node == BLANK_NODE:
            return BLANK_NODE
        # assert isinstance(node, list)
        rlpnode = rlp_encode(node)
        if len(rlpnode) < 32:
            return node

        hashkey = utils.sha3(rlpnode)
        if put_in_db:
            self.db.put(hashkey, str_to_bytes(rlpnode))
        return hashkey
def test_transaction(filename, testname, testdata):

    try:
        rlpdata = decode_hex(testdata["rlp"][2:])
        o = {}
        tx = rlp.decode(rlpdata, transactions.Transaction)
        blknum = int(testdata["blocknumber"])
        # if blknum >= config.default_config["HOMESTEAD_FORK_BLKNUM"]:
        #    tx.check_low_s_homestead()
        assert config_fork_specific_validation(konfig, blknum, tx)
        assert tx.startgas >= tx.intrinsic_gas_used
        if tx.sender == null_address:
            assert tx.value == 0 and tx.gasprice == 0 and tx.nonce == 0
        o["sender"] = tx.sender
        o["transaction"] = {
            "data": '0x' * (len(tx.data) > 0) + encode_hex(tx.data),
            "gasLimit": str(tx.startgas),
            "gasPrice": str(tx.gasprice),
            "nonce": str(tx.nonce),
            "r":
            '0x' + encode_hex(utils.zpad(utils.int_to_big_endian(tx.r), 32)),
            "s":
            '0x' + encode_hex(utils.zpad(utils.int_to_big_endian(tx.s), 32)),
            "v": str(tx.v),
            "value": str(tx.value),
            "to": encode_hex(tx.to),
        }
    except Exception as e:
        tx = None
        sys.stderr.write(str(e))
    if 'transaction' not in testdata:  # expected to fail
        # print(tx.to_dict(), testdata)
        assert tx is None
    else:
        assert set(o['transaction'].keys()) == set(
            testdata.get("transaction", dict()).keys())
        o.get("transaction", None) == testdata.get("transaction", None)
        assert str_to_bytes(encode_hex(o.get("sender", ''))) == str_to_bytes(
            testdata.get("sender", ''))
예제 #5
0
    def _to_dict(self, node):
        """convert (key, value) stored in this and the descendant nodes
        to dict items.

        :param node: node in form of list, or BLANK_NODE

        .. note::

            Here key is in full form, rather than key of the individual node
        """
        if node == BLANK_NODE:
            return {}

        node_type = self._get_node_type(node)

        if is_key_value_type(node_type):
            nibbles = without_terminator(unpack_to_nibbles(node[0]))
            key = b'+'.join([to_string(x) for x in nibbles])
            if node_type == NODE_TYPE_EXTENSION:
                sub_dict = self._to_dict(self._decode_to_node(node[1]))
            else:
                sub_dict = {to_string(NIBBLE_TERMINATOR): node[1]}

            # prepend key of this node to the keys of children
            res = {}
            for sub_key, sub_value in sub_dict.items():
                full_key = (key + b'+' + sub_key).strip(b'+')
                res[full_key] = sub_value
            return res

        elif node_type == NODE_TYPE_BRANCH:
            res = {}
            for i in range(16):
                sub_dict = self._to_dict(self._decode_to_node(node[i]))

                for sub_key, sub_value in sub_dict.items():
                    full_key = (
                        str_to_bytes(
                            str(i)) +
                        b'+' +
                        sub_key).strip(b'+')
                    res[full_key] = sub_value

            if node[16]:
                res[to_string(NIBBLE_TERMINATOR)] = node[-1]
            return res
예제 #6
0
    def _iter_branch(self, node):
        """yield (key, value) stored in this and the descendant nodes
        :param node: node in form of list, or BLANK_NODE

        .. note::
            Here key is in full form, rather than key of the individual node
        """
        if node == BLANK_NODE:
            raise StopIteration

        node_type = self._get_node_type(node)

        if is_key_value_type(node_type):
            nibbles = without_terminator(unpack_to_nibbles(node[0]))
            key = b'+'.join([to_string(x) for x in nibbles])
            if node_type == NODE_TYPE_EXTENSION:
                sub_tree = self._iter_branch(self._decode_to_node(node[1]))
            else:
                sub_tree = [(to_string(NIBBLE_TERMINATOR), node[1])]

            # prepend key of this node to the keys of children
            for sub_key, sub_value in sub_tree:
                full_key = (key + b'+' + sub_key).strip(b'+')
                yield (full_key, sub_value)

        elif node_type == NODE_TYPE_BRANCH:
            for i in range(16):
                sub_tree = self._iter_branch(self._decode_to_node(node[i]))
                for sub_key, sub_value in sub_tree:
                    full_key = (
                        str_to_bytes(
                            str(i)) +
                        b'+' +
                        sub_key).strip(b'+')
                    yield (full_key, sub_value)
            if node[16]:
                yield (to_string(NIBBLE_TERMINATOR), node[-1])
예제 #7
0
def bytesify(li):
    return [str_to_bytes(x) if isinstance(x, str) else x for x in li]
예제 #8
0
파일: utils.py 프로젝트: kevjue/plasma-dex
def hashPersonalMessage(message_hex):
    message = bytearray.fromhex(u.remove_0x_head(message_hex))
    prefix = u.str_to_bytes(
        chr(25) + 'Ethereum Signed Message:\n' + str(len(message)))
    return u.sha3_256(prefix + decode_hex(u.remove_0x_head(message_hex)))
예제 #9
0
 def update(self, k, v):
     h = utils.sha3(k)
     self.db.put(h, utils.str_to_bytes(k))
     self.trie.update(h, v)
예제 #10
0
 def _update_root_hash(self):
     val = rlp_encode(self.root_node)
     key = utils.sha3(val)
     self.db.put(key, str_to_bytes(val))
     self._root_hash = key
예제 #11
0
 def __hash__(self):
     return utils.big_endian_to_int(str_to_bytes(self.__repr__()))