181 lines
7.8 KiB
Python
Executable File
181 lines
7.8 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# Copyright (c) 2014-2016 The Bitcoin Core developers
|
|
# Distributed under the MIT software license, see the accompanying
|
|
# file COPYING or https://www.opensource.org/licenses/mit-license.php .
|
|
|
|
#
|
|
# Test re-org scenarios with a mempool that contains transactions
|
|
# that spend (directly or indirectly) coinbase transactions.
|
|
#
|
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
from test_framework.authproxy import JSONRPCException
|
|
from test_framework.util import assert_equal, \
|
|
start_nodes, connect_nodes_bi, assert_raises
|
|
|
|
from decimal import Decimal
|
|
|
|
# Create one-input, one-output, no-fee transaction:
|
|
class RawTransactionsTest(BitcoinTestFramework):
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.setup_clean_chain = True
|
|
self.num_nodes = 3
|
|
|
|
def setup_network(self, split=False):
|
|
self.nodes = start_nodes(self.num_nodes, self.options.tmpdir)
|
|
|
|
#connect to a local machine for debugging
|
|
#url = "http://bitcoinrpc:DP6DvqZtqXarpeNWyN3LZTFchCCyCUuHwNF7E8pX99x1@%s:%d" % ('127.0.0.1', 18232)
|
|
#proxy = AuthServiceProxy(url)
|
|
#proxy.url = url # store URL on proxy for info
|
|
#self.nodes.append(proxy)
|
|
|
|
connect_nodes_bi(self.nodes,0,1)
|
|
connect_nodes_bi(self.nodes,1,2)
|
|
connect_nodes_bi(self.nodes,0,2)
|
|
|
|
self.is_network_split=False
|
|
self.sync_all()
|
|
|
|
def run_test(self):
|
|
|
|
#prepare some coins for multiple *rawtransaction commands
|
|
self.nodes[2].generate(1)
|
|
self.sync_all()
|
|
self.nodes[0].generate(101)
|
|
self.sync_all()
|
|
self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(),1.5);
|
|
self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(),1.0);
|
|
self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(),5.0);
|
|
self.sync_all()
|
|
self.nodes[0].generate(5)
|
|
self.sync_all()
|
|
|
|
#########################################
|
|
# sendrawtransaction with missing input #
|
|
#########################################
|
|
inputs = [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1}] #won't exists
|
|
outputs = { self.nodes[0].getnewaddress() : 4.998 }
|
|
rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
|
|
rawtx = self.nodes[2].signrawtransaction(rawtx)
|
|
|
|
errorString = ""
|
|
try:
|
|
rawtx = self.nodes[2].sendrawtransaction(rawtx['hex'])
|
|
except JSONRPCException as e:
|
|
errorString = e.error['message']
|
|
|
|
assert_equal("Missing inputs" in errorString, True);
|
|
|
|
#####################################
|
|
# getrawtransaction with block hash #
|
|
#####################################
|
|
|
|
# make a tx by sending then generate 2 blocks; block1 has the tx in it
|
|
tx = self.nodes[2].sendtoaddress(self.nodes[1].getnewaddress(), 1)
|
|
block1, block2 = self.nodes[2].generate(2)
|
|
self.sync_all()
|
|
# We should be able to get the raw transaction by providing the correct block
|
|
gottx = self.nodes[0].getrawtransaction(tx, 1, block1)
|
|
assert_equal(gottx['txid'], tx)
|
|
assert_equal(gottx['in_active_chain'], True)
|
|
# We should not have the 'in_active_chain' flag when we don't provide a block
|
|
gottx = self.nodes[0].getrawtransaction(tx, 1)
|
|
assert_equal(gottx['txid'], tx)
|
|
assert 'in_active_chain' not in gottx
|
|
# We should not get the tx if we provide an unrelated block
|
|
assert_raises(JSONRPCException, self.nodes[0].getrawtransaction, tx, 1, block2)
|
|
# An invalid block hash should raise errors
|
|
assert_raises(JSONRPCException, self.nodes[0].getrawtransaction, tx, 1, True)
|
|
assert_raises(JSONRPCException, self.nodes[0].getrawtransaction, tx, 1, "foobar")
|
|
assert_raises(JSONRPCException, self.nodes[0].getrawtransaction, tx, 1, "abcd1234")
|
|
assert_raises(JSONRPCException, self.nodes[0].getrawtransaction, tx, 1, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
|
|
|
|
#########################
|
|
# RAW TX MULTISIG TESTS #
|
|
#########################
|
|
# 2of2 test
|
|
addr1 = self.nodes[2].getnewaddress()
|
|
addr2 = self.nodes[2].getnewaddress()
|
|
|
|
addr1Obj = self.nodes[2].validateaddress(addr1)
|
|
addr2Obj = self.nodes[2].validateaddress(addr2)
|
|
|
|
mSigObj = self.nodes[2].addmultisigaddress(2, [addr1Obj['pubkey'], addr2Obj['pubkey']])
|
|
mSigObjValid = self.nodes[2].validateaddress(mSigObj)
|
|
|
|
#use balance deltas instead of absolute values
|
|
bal = self.nodes[2].getbalance()
|
|
|
|
# send 1.2 BTC to msig adr
|
|
txId = self.nodes[0].sendtoaddress(mSigObj, 1.2);
|
|
self.sync_all()
|
|
self.nodes[0].generate(1)
|
|
self.sync_all()
|
|
assert_equal(self.nodes[2].getbalance(), bal+Decimal('1.20000000')) #node2 has both keys of the 2of2 ms addr., tx should affect the balance
|
|
|
|
|
|
|
|
|
|
# 2of3 test from different nodes
|
|
bal = self.nodes[2].getbalance()
|
|
addr1 = self.nodes[1].getnewaddress()
|
|
addr2 = self.nodes[2].getnewaddress()
|
|
addr3 = self.nodes[2].getnewaddress()
|
|
|
|
addr1Obj = self.nodes[1].validateaddress(addr1)
|
|
addr2Obj = self.nodes[2].validateaddress(addr2)
|
|
addr3Obj = self.nodes[2].validateaddress(addr3)
|
|
|
|
mSigObj = self.nodes[2].addmultisigaddress(2, [addr1Obj['pubkey'], addr2Obj['pubkey'], addr3Obj['pubkey']])
|
|
mSigObjValid = self.nodes[2].validateaddress(mSigObj)
|
|
assert_equal(mSigObjValid['isvalid'], True)
|
|
|
|
txId = self.nodes[0].sendtoaddress(mSigObj, 2.2);
|
|
decTx = self.nodes[0].gettransaction(txId)
|
|
rawTx = self.nodes[0].decoderawtransaction(decTx['hex'])
|
|
sPK = rawTx['vout'][0]['scriptPubKey']['hex']
|
|
[sPK] # hush pyflakes
|
|
self.sync_all()
|
|
self.nodes[0].generate(1)
|
|
self.sync_all()
|
|
|
|
# THIS IS A INCOMPLETE FEATURE
|
|
# NODE2 HAS TWO OF THREE KEY AND THE FUNDS SHOULD BE SPENDABLE AND COUNT AT BALANCE CALCULATION
|
|
assert_equal(self.nodes[2].getbalance(), bal) # for now, assume the funds of a 2of3 multisig tx are not marked as spendable
|
|
|
|
txDetails = self.nodes[0].gettransaction(txId, True)
|
|
rawTx = self.nodes[0].decoderawtransaction(txDetails['hex'])
|
|
vout = False
|
|
for outpoint in rawTx['vout']:
|
|
if outpoint['value'] == Decimal('2.20000000'):
|
|
vout = outpoint
|
|
break;
|
|
|
|
bal = self.nodes[0].getbalance()
|
|
inputs = [{ "txid" : txId, "vout" : vout['n'], "scriptPubKey" : vout['scriptPubKey']['hex'], "amount" : vout['value']}]
|
|
outputs = { self.nodes[0].getnewaddress() : 2.199 }
|
|
rawTx = self.nodes[2].createrawtransaction(inputs, outputs)
|
|
rawTxPartialSigned = self.nodes[1].signrawtransaction(rawTx, inputs)
|
|
assert_equal(rawTxPartialSigned['complete'], False) # node1 only has one key, can't comp. sign the tx
|
|
|
|
rawTxSigned = self.nodes[2].signrawtransaction(rawTx, inputs)
|
|
assert_equal(rawTxSigned['complete'], True) # node2 can sign the tx compl., own two of three keys
|
|
self.nodes[2].sendrawtransaction(rawTxSigned['hex'])
|
|
rawTx = self.nodes[0].decoderawtransaction(rawTxSigned['hex'])
|
|
self.sync_all()
|
|
self.nodes[0].generate(1)
|
|
self.sync_all()
|
|
assert_equal(self.nodes[0].getbalance(), bal+Decimal('10.00000000')+Decimal('2.19900000')) #block reward + tx
|
|
|
|
inputs = [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1, 'sequence' : 1000}]
|
|
outputs = { self.nodes[0].getnewaddress() : 1 }
|
|
rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
|
|
decrawtx= self.nodes[0].decoderawtransaction(rawtx)
|
|
assert_equal(decrawtx['vin'][0]['sequence'], 1000)
|
|
|
|
if __name__ == '__main__':
|
|
RawTransactionsTest().main()
|