本文整理汇总了Python中common.utils.Basic.read_int方法的典型用法代码示例。如果您正苦于以下问题:Python Basic.read_int方法的具体用法?Python Basic.read_int怎么用?Python Basic.read_int使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类common.utils.Basic
的用法示例。
在下文中一共展示了Basic.read_int方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: unpack_and_mint
# 需要导入模块: from common.utils import Basic [as 别名]
# 或者: from common.utils.Basic import read_int [as 别名]
def unpack_and_mint(self, msg):
"""unpacks the request retreiving the number of coins packed and the total value desired.
Verification: values must be positive!
"""
self.number, msg = Basic.read_short(msg)
value, msg = Basic.read_int(msg)
log_msg('REQUEST:: %s %s'%(self.number, self.hexId), 0)
if not BankUtil.is_positive_integer(self.number):
raise ValueError('number of coins must be greater than 0!')
if value != ACOIN_VALUE or not BankUtil.is_positive_integer(value):
raise ValueError('coins must have a positive, integer value')
self.bill = 0
self.signatures = ""
for i in range(0, self.number):
#TODO: move to a worker pool or something
sig = Globals.ACOIN_KEY.decrypt(msg[:Globals.ACOIN_KEY_BYTES], False)
self.signatures += struct.pack('!%ss'%(Globals.ACOIN_KEY_BYTES), sig)
msg = msg[Globals.ACOIN_KEY_BYTES:]
self.bill += value
#TODO: move this constraint to postgres to get rid of any potential race conditions
sql = "SELECT balance FROM Accounts WHERE Username = %s"
inj = (self.user,)
d = db.read(sql, inj)
return d
示例2: handle_payment
# 需要导入模块: from common.utils import Basic [as 别名]
# 或者: from common.utils.Basic import read_int [as 别名]
def handle_payment(self, msg):
"""Unpack, process, and respond to a payment message.
@param msg: the payment message from the origin.
@type msg: str"""
#if there are any failures, log them, and close the circuit:
try:
#read the PAR protocol version:
version, msg = Basic.read_byte(msg)
assert version == 1, "currently only accept PAR version 1"
readTokens, msg = Basic.read_int(msg)
writeTokens, msg = Basic.read_int(msg)
#read their request ID too
theirId, msg = Basic.read_long(msg)
#read the number of coins:
numCoins, msg = Basic.read_byte(msg)
#read each coin:
creditsEarned = 0
requests = []
for i in range(0, numCoins):
#what type of coin is this?
coinType, msg = Basic.read_byte(msg)
#we only accept acoins for now:
assert coinType == PaymentStream.COIN_TYPES['A'], "bad coin type"
#get the matching request:
requestId, msg = Basic.read_long(msg)
requests.append(requestId)
assert len(msg) % numCoins == 0, "bad payment message length"
coinLen = len(msg) / numCoins
for requestId in requests:
#if this is not true, there wont even be another part to the response
assert Basic.read_byte(msg)[0] == ord('0'), "bad leading byte in payment message"
blob, msg = msg[:coinLen], msg[coinLen:]
request = self.requests[requestId]
del self.requests[requestId]
code, sig = Basic.read_byte(blob)
#validate the ACoin
coin = BankMessages.parse_acoin_response(self.bank, sig, request)
if not coin:
raise Exception("Invalid ACoin sent for payment!")
#success!
creditsEarned += coin.get_expected_value()
coin.originCircuit = self
self.bank.on_earned_coin(coin)
receiptMessageDeferred = self.send_receipt_message(theirId, numCoins)
if not receiptMessageDeferred:
return
#check that they paid enough:
requestedTokens = readTokens + writeTokens
paidTokens = creditsEarned * Globals.CELLS_PER_PAYMENT
if paidTokens < requestedTokens:
raise Exception("Relays asked for %s, but only paid for %s" % (requestedTokens, paidTokens))
#inform Tor that we got a payment message:
addTokensDeferred = self.add_tokens(readTokens, writeTokens)
if not addTokensDeferred:
return
def response(result):
if result:
read, write = result
log_msg("%s paid us %s for exit stream, now %d / %d" % (Basic.clean(self.baseCircuit.prevHexId[:4]), creditsEarned, read, write), 3, "par")
addTokensDeferred.addCallback(response)
except Exception, error:
log_ex(error, "Got bad PAR message")
self.close()