当前位置: 首页>>代码示例>>Python>>正文


Python Trie.to_dict方法代码示例

本文整理汇总了Python中trie.Trie.to_dict方法的典型用法代码示例。如果您正苦于以下问题:Python Trie.to_dict方法的具体用法?Python Trie.to_dict怎么用?Python Trie.to_dict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在trie.Trie的用法示例。


在下文中一共展示了Trie.to_dict方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: to_dict

# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import to_dict [as 别名]
    def to_dict(self):
        state = self.state.to_dict(True)
        nstate = {}
        for s in state:
            t = Trie(STATEDB_DIR, state[s][STORAGE_INDEX])
            o = [0] * ACCT_RLP_LENGTH
            o[NONCE_INDEX] = decode_int(state[s][NONCE_INDEX])
            o[BALANCE_INDEX] = decode_int(state[s][BALANCE_INDEX])
            o[CODE_INDEX] = state[s][CODE_INDEX]
            td = t.to_dict(True)
            o[STORAGE_INDEX] = {decode_int(k): decode_int(td[k]) for k in td}
            nstate[s.encode('hex')] = o

        return {
            "number": self.number,
            "prevhash": self.prevhash,
            "uncles_root": self.uncles_root,
            "coinbase": self.coinbase,
            "state": nstate,
            "transactions_root": self.transactions_root,
            "difficulty": self.difficulty,
            "timestamp": self.timestamp,
            "extradata": self.extradata,
            "nonce": self.nonce
        }
开发者ID:jo,项目名称:pyethereum,代码行数:27,代码来源:blocks.py

示例2: to_dict

# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import to_dict [as 别名]
 def to_dict(self):
     state = self.state.to_dict(True)
     nstate = {}
     for s in state:
         t = Trie('statedb',state[s][3])
         nstate[s.encode('hex')] = [ decode_int(state[s][0]),
                                     decode_int(state[s][1]),
                                     state[s][2],
                                     t.to_dict(True) ]
         
     return {
         "number": self.number,
         "prevhash": self.prevhash,
         "uncles_root": self.uncles_root,
         "coinbase": self.coinbase,
         "state": nstate,
         "transactions_root": self.transactions_root,
         "difficulty": self.difficulty,
         "timestamp": self.timestamp,
         "extradata": self.extradata,
         "nonce": self.nonce
     }
开发者ID:lessc0de,项目名称:pyethereum,代码行数:24,代码来源:blocks.py

示例3: Block

# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import to_dict [as 别名]
class Block(object):
    def __init__(self, data=None):

        self.reward = 10**18
        self.gas_consumed = 0
        self.gaslimit = 1000000 # for now

        if not data:
            self.number = 0
            self.prevhash = ''
            self.uncles_root = ''
            self.coinbase = '0'*40
            self.state = Trie('statedb')
            self.transactions_root = ''
            self.transactions = []
            self.difficulty = 2**23
            self.timestamp = 0
            self.extradata = ''
            self.nonce = 0
            return

        if re.match('^[0-9a-fA-F]*$', data):
            data = data.decode('hex')

        header,  transaction_list, self.uncles = rlp.decode(data)
        [self.number,
         self.prevhash,
         self.uncles_root,
         self.coinbase,
         state_root,
         self.transactions_root,
         self.difficulty,
         self.timestamp,
         self.extradata,
         self.nonce] = header
        self.transactions = [Transaction(x) for x in transaction_list]
        self.state = Trie('statedb', state_root)


        # Verifications
        if self.state.root != '' and self.state.db.get(self.state.root) == '':
            raise Exception("State Merkle root not found in database!")
        if bin_sha256(rlp.encode(transaction_list)) != self.transactions_root:
            raise Exception("Transaction list root hash does not match!")
        if bin_sha256(rlp.encode(self.uncles)) != self.uncles_root:
            raise Exception("Uncle root hash does not match!")
        # TODO: check POW

    # Serialization method; should act as perfect inverse function of the constructor
    # assuming no verification failures
    def serialize(self):
        txlist = [x.serialize() for x in self.transactions]
        header = [self.number,
                  self.prevhash,
                  bin_sha256(rlp.encode(self.uncles)),
                  self.coinbase,
                  self.state.root,
                  bin_sha256(rlp.encode(txlist)),
                  self.difficulty,
                  self.timestamp,
                  self.extradata,
                  self.nonce]
        return rlp.encode([header, txlist, self.uncles])

    def to_dict(self):
        state = self.state.to_dict(True)
        nstate = {}
        for s in state:
            t = Trie('statedb',state[s][3])
            nstate[s.encode('hex')] = [ decode_int(state[s][0]),
                                        decode_int(state[s][1]),
                                        state[s][2],
                                        t.to_dict(True) ]
            
        return {
            "number": self.number,
            "prevhash": self.prevhash,
            "uncles_root": self.uncles_root,
            "coinbase": self.coinbase,
            "state": nstate,
            "transactions_root": self.transactions_root,
            "difficulty": self.difficulty,
            "timestamp": self.timestamp,
            "extradata": self.extradata,
            "nonce": self.nonce
        }

    @classmethod
    def genesis(cls,initial_alloc):
        block = cls()
        for addr in initial_alloc:
            addr2 = addr.decode('hex') if len(addr) == 40 else addr
            block.state.update(addr2,['',encode_int(initial_alloc[addr]),'',''])
        return block

    def hash(self):
        return bin_sha256(self.serialize())
开发者ID:lessc0de,项目名称:pyethereum,代码行数:99,代码来源:blocks.py

示例4: Block

# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import to_dict [as 别名]

#.........这里部分代码省略.........
    def set_index(self,address,index,value):
        if len(address) == 40: address = address.decode('hex')
        acct = self.state.get(address) or ['','','','']
        acct[index] = value
        self.state.update(address,acct)

    # delta_index(bin or hex, int, int) -> success/fail
    def delta_index(self,address,index,value):
        if len(address) == 40: address = address.decode('hex')
        acct = self.state.get(address) or ['','','','']
        if decode_int(acct[index]) + value < 0:
            return False
        acct[index] = encode_int(decode_int(acct[index])+value)
        self.state.update(address,acct)
        return True

    def coerce_to_enc(self,n):
        return encode_int(n) if isinstance(n,(int,long)) else n
    def get_nonce(self,address):
        return decode_int(self.get_index(address,NONCE_INDEX))
    def increment_nonce(self,address):
        return self.delta_index(address,NONCE_INDEX,1)
    def get_balance(self,address):
        return decode_int(self.get_index(address,BALANCE_INDEX))
    def set_balance(self,address,value):
        self.set_index(address,BALANCE_INDEX,encode_int(value))
    def delta_balance(self,address,value):
        return self.delta_index(address,BALANCE_INDEX,value)
    def get_code(self,address):
        codehash = self.get_index(address,CODE_INDEX)
        return DB('statedb').get(codehash) if codehash else ''
    def set_code(self,address,value):
        DB('statedb').put(sha3(value),value)
        self.set_index(address,CODE_INDEX,sha3(value))
    def get_storage(self,address):
        return Trie('statedb',self.get_index(address,STORAGE_INDEX))
    def get_storage_data(self,address,index):
        return decode_int(self.get_storage(address).get(self.coerce_to_enc(index)))
    def set_storage_data(self,address,index,val):
        t = Trie('statedb',self.get_index(address,STORAGE_INDEX))
        t.update(self.coerce_to_enc(index),encode_int(val))
        self.set_index(address,STORAGE_INDEX,t.root)
    
    # Revert computation
    def snapshot(self):
        return self.state.root

    def revert(self,root):
        self.state.root = root

    # Serialization method; should act as perfect inverse function of the
    # constructor assuming no verification failures
    def serialize(self):
        txlist = [x.serialize() for x in self.transactions]
        header = [self.number,
                  self.prevhash,
                  bin_sha256(rlp.encode(self.uncles)),
                  self.coinbase,
                  self.state.root,
                  bin_sha256(rlp.encode(txlist)),
                  self.difficulty,
                  self.timestamp,
                  self.extradata,
                  self.nonce]
        return rlp.encode([header, txlist, self.uncles])

    def to_dict(self):
        state = self.state.to_dict(True)
        nstate = {}
        for s in state:
            t = Trie('statedb',state[s][STORAGE_INDEX])
            o = [0] * ACCT_RLP_LENGTH
            o[NONCE_INDEX] = decode_int(state[s][NONCE_INDEX])
            o[BALANCE_INDEX] = decode_int(state[s][BALANCE_INDEX])
            o[CODE_INDEX] = state[s][CODE_INDEX]
            o[STORAGE_INDEX] = t.to_dict(True)
            nstate[s.encode('hex')] = o
            
        return {
            "number": self.number,
            "prevhash": self.prevhash,
            "uncles_root": self.uncles_root,
            "coinbase": self.coinbase,
            "state": nstate,
            "transactions_root": self.transactions_root,
            "difficulty": self.difficulty,
            "timestamp": self.timestamp,
            "extradata": self.extradata,
            "nonce": self.nonce
        }

    @classmethod
    def genesis(cls,initial_alloc):
        block = cls()
        for addr in initial_alloc:
            block.set_balance(addr,initial_alloc[addr])
        return block

    def hash(self):
        return bin_sha256(self.serialize())
开发者ID:gojira,项目名称:pyethereum,代码行数:104,代码来源:blocks.py

示例5: Block

# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import to_dict [as 别名]

#.........这里部分代码省略.........
    def get_balance(self, address):
        return decode_int(self._get_acct_item(address, BALANCE_INDEX))

    def set_balance(self, address, value):
        self._set_acct_item(address, BALANCE_INDEX, encode_int(value))

    def delta_balance(self, address, value):
        return self._delta_item(address, BALANCE_INDEX, value)

    def get_code(self, address):
        codehash = self._get_acct_item(address, CODE_INDEX)
        return self.state.db.get(codehash) if codehash else ''

    def set_code(self, address, value):
        self.state.db.put(sha3(value), value)
        self.state.db.commit()
        self._set_acct_item(address, CODE_INDEX, sha3(value))

    def get_storage(self, address):
        return Trie(STATEDB_DIR, self._get_acct_item(address, STORAGE_INDEX))

    def get_storage_data(self, address, index):
        t = self.get_storage(address)
        return decode_int(t.get(utils.coerce_to_bytes(index)))

    def set_storage_data(self, address, index, val):
        t = self.get_storage(address)
        if val:
            t.update(utils.coerce_to_bytes(index), encode_int(val))
        else:
            t.delete(utils.coerce_to_bytes(index))
        self._set_acct_item(address, STORAGE_INDEX, t.root)

    def account_to_dict(self, address):
        if len(address) == 40:
            address = address.decode('hex')
        acct = self.state.get(address) or ['', '', '', '']
        chash = acct[CODE_INDEX]
        stdict = Trie(STATEDB_DIR, acct[STORAGE_INDEX]).to_dict(True)
        return {
            'nonce': decode_int(acct[NONCE_INDEX]),
            'balance': decode_int(acct[BALANCE_INDEX]),
            'code': self.state.db.get(chash).encode('hex') if chash else '',
            'storage': {decode_int(k): decode_int(stdict[k]) for k in stdict}
        }

    # Revert computation
    def snapshot(self):
        return {'state': self.state.root, 'gas': self.gas_consumed}

    def revert(self, mysnapshot):
        self.state.root = mysnapshot['state']
        self.gas_consumed = mysnapshot['gas']

    # Serialization method; should act as perfect inverse function of the
    # constructor assuming no verification failures
    def serialize(self):
        txlist = [x.serialize() for x in self.transactions]
        header = [encode_int(self.number),
                  self.prevhash,
                  sha3(rlp.encode(self.uncles)),
                  self.coinbase.decode('hex'),
                  self.state.root,
                  sha3(rlp.encode(txlist)),
                  encode_int(self.difficulty),
                  encode_int(self.timestamp),
开发者ID:jo,项目名称:pyethereum,代码行数:70,代码来源:blocks.py

示例6: Block

# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import to_dict [as 别名]

#.........这里部分代码省略.........

    def set_balance(self, address, value):
        self._set_acct_item(address, 'balance', value)

    def delta_balance(self, address, value):
        return self._delta_item(address, 'balance', value)

    def get_code(self, address):
        codehash = self._get_acct_item(address, 'code')
        return self.state.db.get(codehash) if codehash else ''

    def set_code(self, address, value):
        self.state.db.put(sha3(value), value)
        self.state.db.commit()
        self._set_acct_item(address, 'code', sha3(value))

    def get_storage(self, address):
        storage_root = self._get_acct_item(address, 'storage')
        return Trie(utils.get_db_path(), storage_root)

    def get_storage_data(self, address, index):
        t = self.get_storage(address)
        val = t.get(utils.coerce_to_bytes(index))
        return utils.decode_int(val) if val else 0

    def set_storage_data(self, address, index, val):
        t = self.get_storage(address)
        if val:
            t.update(utils.coerce_to_bytes(index), encode_int(val))
        else:
            t.delete(utils.coerce_to_bytes(index))
        self._set_acct_item(address, 'storage', t.root)

    def _account_to_dict(self, acct):
        med_dict = {}
        for i, (name, typ, default) in enumerate(acct_structure):
            med_dict[name] = utils.decoders[typ](acct[i])
        chash = med_dict['code']
        strie = Trie(utils.get_db_path(), med_dict['storage']).to_dict()
        med_dict['code'] = \
            self.state.db.get(chash).encode('hex') if chash else ''
        med_dict['storage'] = {
            utils.decode_int(k): utils.decode_int(strie[k]) for k in strie
        }
        return med_dict

    def account_to_dict(self, address):
        if len(address) == 40:
            address = address.decode('hex')
        acct = self.state.get(address) or ['', '', '', '']
        return self._account_to_dict(acct)

    # Revert computation
    def snapshot(self):
        return {
            'state': self.state.root,
            'gas': self.gas_used,
            'txs': self.transactions,
            'txcount': self.transaction_count,
        }

    def revert(self, mysnapshot):
        self.state.root = mysnapshot['state']
        self.gas_used = mysnapshot['gas']
        self.transactions = mysnapshot['txs']
        self.transaction_count = mysnapshot['txcount']
开发者ID:zancas,项目名称:pyethereum,代码行数:70,代码来源:blocks.py


注:本文中的trie.Trie.to_dict方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。