本文整理汇总了Python中utils.decode_int函数的典型用法代码示例。如果您正苦于以下问题:Python decode_int函数的具体用法?Python decode_int怎么用?Python decode_int使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了decode_int函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: to_dict
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
}
示例2: create_contract
def create_contract(block,tx,msg):
oldroot = block.state.root
senderstate = block.state.get(msg.sender) or ['','','','']
recvstate = ['','',sha3(msg.data),'']
recvaddr = sha3(rlp.encode([msg.sender,senderstate[NONCE_INDEX]]))[12:]
code = msg.data
statedb.put(sha3(msg.data),msg.data)
compustate = Compustate(gas=msg.gas)
# Not enough vaue to send, instaquit
if decode_int(senderstate[BALANCE_INDEX]) < msg.value:
recvstate[2] = []
block.state.update(recvaddr,recvstate)
return recvaddr, compustate.gas
# Transfer value and update nonce
senderstate[BALANCE_INDEX] = encode_int(decode_int(senderstate[BALANCE_INDEX])-msg.value)
senderstate[NONCE_INDEX] = encode_int(decode_int(senderstate[NONCE_INDEX])+1)
recvstate[BALANCE_INDEX] = encode_int(decode_int(senderstate[BALANCE_INDEX])+msg.value)
block.state.update(msg.sender.decode('hex'),senderstate)
block.state.update(recvaddr,recvstate)
# Temporary pre-POC5: don't do the code/init thing
return recvaddr, compustate.gas
# Main loop
while 1:
o = apply_op(block,tx,msg,msg.data,compustate.op)
if o is not None:
if o == OUT_OF_GAS:
block.state.root = oldroot
return 0, 0
else:
recvstate = block.state.get(recvaddr)
recvstate[CODE_INDEX] = sha3(map(chr,o))
statedb.put(sha3(map(chr,o)),map(chr,o))
block.state.update(recvaddr,recvstate)
return recvaddr, recvstate
示例3: delta_index
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
示例4: account_to_dict
def account_to_dict(self, address):
med_dict = {}
for i, val in enumerate(self.get_acct(address)):
med_dict[acct_structure[i][0]] = val
strie = trie.Trie(utils.get_db_path(), med_dict['storage']).to_dict()
med_dict['storage'] = {utils.decode_int(k): utils.decode_int(v)
for k, v in strie.iteritems()}
return med_dict
示例5: apply_tx
def apply_tx(block,tx):
fee = tx.gasprice * tx.startgas
addrstate = block.state.get(tx.sender.decode('hex'))
if not addrstate:
raise Exception("Sending from a not-yet-existent account!")
if decode_int(addrstate[NONCE_INDEX]) != tx.nonce:
print decode_int(addrstate[NONCE_INDEX]), tx.nonce
raise Exception("Invalid nonce!")
if decode_int(addrstate[BALANCE_INDEX]) < fee:
raise Exception("Not enough in account to pay fee!")
addrstate[NONCE_INDEX] = encode_int(decode_int(addrstate[NONCE_INDEX])+1)
addrstate[BALANCE_INDEX] = encode_int(decode_int(addrstate[BALANCE_INDEX])-fee)
block.state.update(tx.sender.decode('hex'),addrstate)
block.gas_consumed += fee
medroot = block.state.root
message_gas = tx.startgas - GTXDATA * len(tx.data)
message = Message(tx.sender,tx.to,tx.value,message_gas,tx.data)
if tx.to:
s,g,d = apply_msg(block,tx,message)
else:
s,g = create_contract(block,tx,message)
if not s:
block.state.root = medroot
minerstate = block.state.get(block.coinbase)
minerstate[BALANCE_INDEX] = encode_int(decode_int(minerstate[BALANCE_INDEX])+fee)
block.state.update(block.coinbase,minerstate)
else:
addrstate[BALANCE_INDEX] = encode_int(decode_int(addrstate[BALANCE_INDEX])+tx.gasprice * g)
block.state.update(tx.sender.decode('hex'),addrstate)
minerstate = block.state.get(block.coinbase.decode('hex')) or ['','','','']
minerstate[BALANCE_INDEX] = encode_int(decode_int(minerstate[BALANCE_INDEX])+(fee - g * tx.gasprice))
block.state.update(block.coinbase.decode('hex'),minerstate)
示例6: account_to_dict
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}
}
示例7: _account_to_dict
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.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
示例8: _delta_item
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
示例9: _delta_item
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
'''
if len(address) == 40:
address = address.decode('hex')
acct = self.state.get(address) or ['', '', '', '']
index = acct_structure_rev[param][0]
if utils.decode_int(acct[index]) + value < 0:
return False
acct[index] = utils.encode_int(utils.decode_int(acct[index]) + value)
self.state.update(address, acct)
return True
示例10: remote_blocks_received_handler
def remote_blocks_received_handler(sender, block_lst, peer, **kwargs):
logger.debug("received %d remote blocks", len(block_lst))
old_head = chain_manager.head
# assuming chain order w/ newest block first
for block_data in reversed(block_lst):
try:
block = blocks.Block.deserialize(rlp.encode(block_data))
except blocks.UnknownParentException:
# no way to ask peers for older parts of chain
bhash = utils.sha3(rlp.encode(block_data)).encode('hex')[:4]
phash = block_data[0][0].encode('hex')[:4]
number = utils.decode_int(block_data[0][6])
logger.debug('Block(#%d %s %s) with unknown parent, requesting ...',
number, bhash, phash.encode('hex')[:4])
chain_manager.synchronize_blockchain()
break
if block.hash in chain_manager:
logger.debug('Known %r', block)
else:
if block.has_parent():
# add block & set HEAD if it's longest chain
success = chain_manager.add_block(block)
if success:
logger.debug('Added %r', block)
else:
logger.debug('Orphant %r', block)
if chain_manager.head != old_head:
chain_manager.synchronize_blockchain()
示例11: get_transaction
def get_transaction(self, txhash):
"return (tx, block)"
blockhash, tx_num_enc = rlp.decode(self.db.get(txhash))
blk = blocks.get_block(blockhash)
num = utils.decode_int(tx_num_enc)
tx_data, msr, gas = blk.get_transaction(num)
return Transaction.create(tx_data), blk
示例12: to_dict
def to_dict(self, with_state=False, full_transactions=False, with_storage_roots=False):
"""
serializes the block
with_state: include state for all accounts
full_transactions: include serialized tx (hashes otherwise)
"""
b = {}
for name, typ, default in block_structure:
b[name] = utils.printers[typ](getattr(self, name))
txlist = []
for i in range(self.transaction_count):
tx_rlp = self.transactions.get(rlp.encode(utils.encode_int(i)))
tx, msr, gas = rlp.decode(tx_rlp)
if full_transactions:
txjson = transactions.Transaction.create(tx).to_dict()
else:
txjson = utils.sha3(rlp.descend(tx_rlp, 0)).encode('hex') # tx hash
txlist.append({
"tx": txjson,
"medstate": msr.encode('hex'),
"gas": str(utils.decode_int(gas))
})
b["transactions"] = txlist
if with_state:
state_dump = {}
for address, v in self.state.to_dict().iteritems():
state_dump[address.encode('hex')] = \
self.account_to_dict(address, with_storage_roots)
b['state'] = state_dump
return b
示例13: decode_datalist
def decode_datalist(arr):
if isinstance(arr, list):
arr = ''.join(map(chr, arr))
o = []
for i in range(0, len(arr), 32):
o.append(decode_int(arr[i:i + 32]))
return o
示例14: verify_independent_transaction_spv_proof
def verify_independent_transaction_spv_proof(proof):
_, prevheader, header, index, nodes = rlp.decode(proof)
index = utils.decode_int(index)
pb = blocks.Block.deserialize_header(prevheader)
b = blocks.Block.init_from_header(header)
b.set_proof_mode(blocks.VERIFYING, nodes)
if index != 0:
_, pre_med, pre_gas = b.get_transaction(index - 1)
else:
pre_med, pre_gas = pb['state_root'], ''
if utils.sha3(rlp.encode(prevheader)) != b.prevhash:
return False
b.state_root = pre_med
b.gas_used = utils.decode_int(pre_gas)
tx, post_med, post_gas = b.get_transaction(index)
tx = transactions.Transaction.create(tx)
o = verify_transaction_spv_proof(b, tx, nodes)
return o and b.state_root == post_med and b.gas_used == utils.decode_int(post_gas)
示例15: __init__
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!")