Changeset View
Changeset View
Standalone View
Standalone View
test/functional/abc-magnetic-anomaly-mining.py
Show All 19 Lines | def set_test_params(self): | ||||
# it errors out if it is not connected to other nodes | # it errors out if it is not connected to other nodes | ||||
self.num_nodes = 2 | self.num_nodes = 2 | ||||
self.setup_clean_chain = True | self.setup_clean_chain = True | ||||
self.block_heights = {} | self.block_heights = {} | ||||
self.tip = None | self.tip = None | ||||
self.blocks = {} | self.blocks = {} | ||||
self.mocktime = int(time.time()) - 600 * 100 | self.mocktime = int(time.time()) - 600 * 100 | ||||
extra_arg = ['-spendzeroconfchange=0', '-whitelist=noban@127.0.0.1'] | extra_arg = ["-spendzeroconfchange=0", "-whitelist=noban@127.0.0.1"] | ||||
self.extra_args = [extra_arg, extra_arg] | self.extra_args = [extra_arg, extra_arg] | ||||
def skip_test_if_missing_module(self): | def skip_test_if_missing_module(self): | ||||
self.skip_if_no_wallet() | self.skip_if_no_wallet() | ||||
def run_test(self): | def run_test(self): | ||||
mining_node = self.nodes[0] | mining_node = self.nodes[0] | ||||
Show All 15 Lines | def run_test(self): | ||||
transactions = {} | transactions = {} | ||||
# Spend all our coinbases | # Spend all our coinbases | ||||
while len(unspent): | while len(unspent): | ||||
inputs = [] | inputs = [] | ||||
# Grab a random number of inputs | # Grab a random number of inputs | ||||
for _ in range(random.randrange(1, 5)): | for _ in range(random.randrange(1, 5)): | ||||
txin = unspent.pop() | txin = unspent.pop() | ||||
inputs.append({ | inputs.append({"txid": txin["txid"], "vout": 0}) # This is a coinbase | ||||
'txid': txin['txid'], | |||||
'vout': 0 # This is a coinbase | |||||
}) | |||||
if len(unspent) == 0: | if len(unspent) == 0: | ||||
break | break | ||||
outputs = {} | outputs = {} | ||||
# Calculate a unique fee for this transaction | # Calculate a unique fee for this transaction | ||||
fee = decimal.Decimal(random.randint( | fee = decimal.Decimal(random.randint(1000, 2000)) / decimal.Decimal(1e2) | ||||
1000, 2000)) / decimal.Decimal(1e2) | |||||
# NOTE: There will be 1 sigCheck per output (which equals the number | # NOTE: There will be 1 sigCheck per output (which equals the number | ||||
# of inputs now). We need this randomization to ensure the | # of inputs now). We need this randomization to ensure the | ||||
# numbers are properly following the transactions in the block | # numbers are properly following the transactions in the block | ||||
# template metadata | # template metadata | ||||
addr = "" | addr = "" | ||||
for _ in range(len(inputs)): | for _ in range(len(inputs)): | ||||
addr = mining_node.getnewaddress() | addr = mining_node.getnewaddress() | ||||
output = { | output = { | ||||
# 50,000,000 XEC per coinbase | # 50,000,000 XEC per coinbase | ||||
addr: decimal.Decimal(50000000) | addr: decimal.Decimal(50000000) | ||||
} | } | ||||
outputs.update(output) | outputs.update(output) | ||||
# Take the fee off the last output to avoid rounding errors we | # Take the fee off the last output to avoid rounding errors we | ||||
# need the exact fee later for assertions | # need the exact fee later for assertions | ||||
outputs[addr] -= fee | outputs[addr] -= fee | ||||
rawtx = mining_node.createrawtransaction(inputs, outputs) | rawtx = mining_node.createrawtransaction(inputs, outputs) | ||||
signedtx = mining_node.signrawtransactionwithwallet(rawtx) | signedtx = mining_node.signrawtransactionwithwallet(rawtx) | ||||
txid = mining_node.sendrawtransaction(signedtx['hex']) | txid = mining_node.sendrawtransaction(signedtx["hex"]) | ||||
# number of outputs is the same as the number of sigCheck in this | # number of outputs is the same as the number of sigCheck in this | ||||
# case | # case | ||||
transactions.update( | transactions.update({txid: {"fee": fee, "sigchecks": len(outputs)}}) | ||||
{txid: {'fee': fee, 'sigchecks': len(outputs)}}) | |||||
tmpl = mining_node.getblocktemplate() | tmpl = mining_node.getblocktemplate() | ||||
assert 'proposal' in tmpl['capabilities'] | assert "proposal" in tmpl["capabilities"] | ||||
# Check the template transaction metadata and ordering | # Check the template transaction metadata and ordering | ||||
last_txid = 0 | last_txid = 0 | ||||
for txn in tmpl['transactions'][1:]: | for txn in tmpl["transactions"][1:]: | ||||
txid = txn['txid'] | txid = txn["txid"] | ||||
txnMetadata = transactions[txid] | txnMetadata = transactions[txid] | ||||
expectedFeeSats = int(txnMetadata['fee'] * 10**2) | expectedFeeSats = int(txnMetadata["fee"] * 10**2) | ||||
expectedSigChecks = txnMetadata['sigchecks'] | expectedSigChecks = txnMetadata["sigchecks"] | ||||
txid_decoded = int(txid, 16) | txid_decoded = int(txid, 16) | ||||
# Assert we got the expected metadata | # Assert we got the expected metadata | ||||
assert expectedFeeSats == txn['fee'] | assert expectedFeeSats == txn["fee"] | ||||
assert expectedSigChecks == txn['sigchecks'] | assert expectedSigChecks == txn["sigchecks"] | ||||
# Assert transaction ids are in order | # Assert transaction ids are in order | ||||
assert last_txid == 0 or last_txid < txid_decoded | assert last_txid == 0 or last_txid < txid_decoded | ||||
last_txid = txid_decoded | last_txid = txid_decoded | ||||
if __name__ == '__main__': | if __name__ == "__main__": | ||||
CTORMiningTest().main() | CTORMiningTest().main() |