本文整理汇总了Python中trie.Trie.update方法的典型用法代码示例。如果您正苦于以下问题:Python Trie.update方法的具体用法?Python Trie.update怎么用?Python Trie.update使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类trie.Trie
的用法示例。
在下文中一共展示了Trie.update方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Block
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import update [as 别名]
class Block(object):
def __init__(self, data=None):
if not data:
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.nonce,
self.extra] = header
self.transactions = [Transaction(x) for x in transaction_list]
self.state = Trie('statedb', state_root)
self.reward = 0
# 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
def get_base_fee(self):
return int(10 ** 21 / int(self.difficulty ** 0.5))
def pay_fee(self, address, fee, tominer=True):
# Subtract fee from sender
sender_state = rlp.decode(self.state.get(address))
if not sender_state or sender_state[1] < fee:
return False
sender_state[1] -= fee
self.state.update(address, sender_state)
# Pay fee to miner
if tominer:
miner_state = rlp.decode(self.state.get(self.coinbase)) or [0, 0, 0]
miner_state[1] += fee
self.state.update(self.coinbase, miner_state)
return True
def get_nonce(self, address):
state = rlp.decode(self.state.get(address))
if not state or state[0] == 0:
return False
return state[2]
def get_balance(self, address):
state = rlp.decode(self.state.get(address))
return state[1] if state else 0
def set_balance(self, address, balance):
state = rlp.decode(self.state.get(address)) or [0, 0, 0]
state[1] = balance
self.state.update(address, rlp.encode(state))
# Making updates to the object obtained from this method will do nothing. You need
# to call update_contract to finalize the changes.
def get_contract(self, address):
state = rlp.decode(self.state.get(address))
if not state or state[0] == 0:
return False
return Trie('statedb', state[2])
def update_contract(self, address, contract):
state = rlp.decode(self.state.get(address)) or [1, 0, '']
if state[0] == 0:
return False
state[2] = contract.root
self.state.update(address, state)
# 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.nonce,
self.extra]
return rlp.encode([header, txlist, self.uncles])
def hash(self):
return bin_sha256(self.serialize())
示例2: Block
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import update [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
# get_index(bin or hex, int) -> bin
def get_index(self,address,index):
if len(address) == 40: address = address.decode('hex')
acct = self.state.get(address) or ['','','','']
return acct[index]
# set_index(bin or hex, int, bin)
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):
#.........这里部分代码省略.........
示例3: set_storage_data
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import update [as 别名]
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)
示例4: Block
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import update [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(get_db_path())
self.transactions_root = ''
self.transactions = []
self.uncles = []
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 = decode_int(header[0])
self.prevhash = header[1]
self.uncles_root = header[2]
self.coinbase = header[3].encode('hex')
self.state = Trie(STATEDB_DIR, header[4])
self.transactions_root = header[5]
self.difficulty = decode_int(header[6])
self.timestamp = decode_int(header[7])
self.extradata = header[8]
self.nonce = decode_int(header[9])
self.transactions = [Transaction(x) for x in transaction_list]
# Verifications
if self.state.root != '' and self.state.db.get(self.state.root) == '':
raise Exception("State Merkle root not found in database!")
if sha3(rlp.encode(transaction_list)) != self.transactions_root:
raise Exception("Transaction list root hash does not match!")
if sha3(rlp.encode(self.uncles)) != self.uncles_root:
raise Exception("Uncle root hash does not match!")
# TODO: check POW
# _get_acct_item(bin or hex, int) -> bin
def _get_acct_item(self, address, index):
''' get account item
:param address: account address, can be binary or hex string
:param index: item index
'''
if len(address) == 40:
address = address.decode('hex')
acct = self.state.get(address) or ['', '', '', '']
return acct[index]
# _set_acct_item(bin or hex, int, bin)
def _set_acct_item(self, address, index, value):
''' set account item
:param address: account address, can be binary or hex string
:param index: item index
:param value: new value
'''
if len(address) == 40:
address = address.decode('hex')
acct = self.state.get(address) or ['', '', '', '']
acct[index] = value
self.state.update(address, acct)
# _delta_item(bin or hex, int, int) -> success/fail
def _delta_item(self, address, index, value):
''' add value to account item
:param address: account address, can be binary or hex string
:param index: item index
:param value: can be positive or negative
'''
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 get_nonce(self, address):
return decode_int(self._get_acct_item(address, NONCE_INDEX))
def increment_nonce(self, address):
return self._delta_item(address, NONCE_INDEX, 1)
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))
#.........这里部分代码省略.........
示例5: apply_op
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import update [as 别名]
#.........这里部分代码省略.........
if len(mem) < stackargs[0] + stackargs[1]:
mem.extend([0] * (stackargs[0] + stackargs[1] - len(mem)))
data = ''.join(map(chr,mem[stackargs[0]:stackargs[0] + stackargs[1]]))
stk.push(decode(sha3(data),256))
elif op == 'ADDRESS':
stk.push(msg.to)
elif op == 'BALANCE':
stk.push(block.state.get(msg.to)[BALANCE_INDEX])
elif op == 'ORIGIN':
stk.push(tx.sender)
elif op == 'CALLER':
stk.push(msg.sender)
elif op == 'CALLVALUE':
stk.push(msg.value)
elif op == 'CALLDATA':
if stackargs[-1] >= len(msg.data): stk.push(0)
else:
dat = ''.join(map(chr,msg.data[stackargs[-1]:stackargs[-1]+32]))
stk.push(decode(dat+'\x00'*(32-len(dat)),256))
elif op == 'CALLDATASIZE':
stk.push(len(msg.data))
elif op == 'GASPRICE':
stk.push(tx.gasprice)
elif op == 'PREVHASH':
stk.push(block.prevhash)
elif op == 'COINBASE':
stk.push(block.coinbase)
elif op == 'TIMESTAMP':
stk.push(block.timestamp)
elif op == 'NUMBER':
stk.push(block.number)
elif op == 'DIFFICULTY':
stk.push(block.difficulty)
elif op == 'GASLIMIT':
stk.push(block.gaslimit)
elif op == 'POP':
pass
elif op == 'DUP':
stk.push(stackargs[0])
stk.push(stackargs[0])
elif op == 'SWAP':
stk.push(stackargs[0])
stk.push(stackargs[1])
elif op == 'MLOAD':
if len(mem) < stackargs[0] + 32:
mem.extend([0] * (stackargs[0] + 32 - len(mem)))
data = ''.join(map(chr,mem[stackargs[0]:stackargs[0] + 32]))
stk.push(decode(data,256))
elif op == 'MSTORE':
if len(mem) < stackargs[0] + 32:
mem.extend([0] * (stackargs[0] + 32 - len(mem)))
v = stackargs[1]
for i in range(31,-1,-1):
mem[stackargs[0]+i] = v % 256
v /= 256
elif op == 'MSTORE8':
if len(mem) < stackargs[0] + 1:
mem.extend([0] * (stackargs[0] + 1 - len(mem)))
mem[stackargs[0]] = stackargs[1] % 256
elif op == 'SLOAD':
trie = Trie(block.state.get(msg.to)[STORAGE_INDEX],'statedb')
stk.push(trie.get(stackargs[0]))
elif op == 'SSTORE':
mystate = block.state.get(msg.to)
trie = Trie(mystate[STORAGE_INDEX],'statedb')
trie.update(stackargs[0],stackargs[1])
mystate[STORAGE_INDEX] = trie.root
block.state.update(msg.to,mystate)
elif op == 'JUMP':
newpc = stackargs[0]
elif op == 'JUMPI':
if stackargs[1]: newpc = stackargs[0]
elif op == 'PC':
stk.push(compustate.pc)
elif op == 'MSIZE':
stk.push(len(mem))
elif op == 'GAS':
stk.push(compustate.gas)
elif op[:4] == 'PUSH':
pushnum = int(op[4:])
newpc = compustate.pc + 1 + pushnum
dat = code[compustate.pc + 1: compustate.pc + 1 + pushnum]
stk.push(decode(dat+'\x00'*(32-len(dat)),256))
elif op == 'CREATE':
if len(mem) < stackargs[2] + stackargs[3]:
mem.extend([0] * (stackargs[2] + stackargs[3] - len(mem)))
value = stackhash[0]
gas = stackhash[1]
data = ''.join(map(chr,mem[stackhash[2]:stackhash[2]+stackhash[3]]))
create_contract(block,tx,Message(msg.to,'',value,gas,data))
elif op == 'CALL':
if len(mem) < stackargs[3] + stackargs[4]:
mem.extend([0] * (stackargs[3] + stackargs[4] - len(mem)))
if len(mem) < stackargs[5] + stackargs[6]:
mem.extend([0] * (stackargs[5] + stackargs[6] - len(mem)))
to = stackhash[0]
value = stackhash[1]
gas = stackhash[2]
data = ''.join(map(chr,mem[stackhash[3]:stackhash[3]+stackhash[4]]))
apply_msg(block,tx,Message(msg.to,to,value,gas,data))
示例6: Block
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import update [as 别名]
class Block(object):
def __init__(self,
prevhash='',
uncles_hash=block_structure_rev['uncles_hash'][2],
coinbase=block_structure_rev['coinbase'][2],
state_root='',
tx_list_root='',
difficulty=block_structure_rev['difficulty'][2],
number=0,
min_gas_price=block_structure_rev['min_gas_price'][2],
gas_limit=block_structure_rev['gas_limit'][2],
gas_used=0, timestamp=0, extra_data='', nonce='',
transaction_list=[],
uncles=[]):
self.prevhash = prevhash
self.uncles_hash = uncles_hash
self.coinbase = coinbase
self.state_root = state_root
self.tx_list_root = tx_list_root
self.difficulty = difficulty
self.number = number
self.min_gas_price = min_gas_price
self.gas_limit = gas_limit
self.gas_used = gas_used
self.timestamp = timestamp
self.extra_data = extra_data
self.nonce = nonce
self.transaction_list = transaction_list
self.uncles = uncles
self.transactions = Trie(get_db_path())
self.transaction_count = 0
# Fill in nodes for transaction trie
for tx in transaction_list:
self.add_transaction_to_list(tx)
self.state = Trie(get_db_path(), self.state_root)
# Basic consistency verifications
if self.state.root != '' and self.state.db.get(self.state.root) == '':
raise Exception("State Merkle root not found in database!")
if self.tx_list_root != self.transactions.root:
raise Exception("Transaction list root hash does not match!")
if sha3(rlp.encode(self.uncles)) != self.uncles_hash:
raise Exception("Uncle root hash does not match!")
if len(self.extra_data) > 1024:
raise Exception("Extra data cannot exceed 1024 bytes")
if self.coinbase == '':
raise Exception("Coinbase cannot be empty address")
# TODO: check POW
@classmethod
def deserialize(cls, rlpdata):
header_args, transaction_list, uncles = rlp.decode(rlpdata)
kargs = dict(transaction_list=transaction_list, uncles=uncles)
# Deserialize all properties
for i, (name, typ, default) in enumerate(block_structure):
kargs[name] = utils.decoders[typ](header_args[i])
return Block(**kargs)
@classmethod
def hex_deserialize(cls, hexrlpdata):
return cls.deserialize(hexrlpdata.decode('hex'))
# _get_acct_item(bin or hex, int) -> bin
def _get_acct_item(self, address, param):
''' get account item
:param address: account address, can be binary or hex string
:param param: parameter to get
'''
if len(address) == 40:
address = address.decode('hex')
acct = self.state.get(address) or ['', '', '', '']
decoder = utils.decoders[acct_structure_rev[param][1]]
return decoder(acct[acct_structure_rev[param][0]])
# _set_acct_item(bin or hex, int, bin)
def _set_acct_item(self, address, param, value):
''' set account item
:param address: account address, can be binary or hex string
:param param: parameter to set
:param value: new value
'''
if len(address) == 40:
address = address.decode('hex')
acct = self.state.get(address) or ['', '', '', '']
encoder = utils.encoders[acct_structure_rev[param][1]]
acct[acct_structure_rev[param][0]] = encoder(value)
self.state.update(address, acct)
# _delta_item(bin or hex, int, int) -> success/fail
def _delta_item(self, address, param, value):
''' add value to account item
:param address: account address, can be binary or hex string
:param param: parameter to increase/decrease
:param value: can be positive or negative
'''
#.........这里部分代码省略.........