2016-05-06 11:23:48 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Copyright (c) 2014-2016 The Bitcoin Core developers
|
2014-12-29 20:17:21 +01:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2019-01-07 10:55:35 +01:00
|
|
|
"""Test the rawtranscation RPCs.
|
2014-12-29 20:17:21 +01:00
|
|
|
|
2019-01-07 10:55:35 +01:00
|
|
|
Test the following RPCs:
|
|
|
|
- createrawtransaction
|
|
|
|
- signrawtransaction
|
|
|
|
- sendrawtransaction
|
|
|
|
- decoderawtransaction
|
|
|
|
- getrawtransaction
|
2016-11-23 07:13:35 +01:00
|
|
|
"""
|
2014-12-29 20:17:21 +01:00
|
|
|
|
2015-05-02 13:58:10 +02:00
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
|
|
from test_framework.util import *
|
2014-12-29 20:17:21 +01:00
|
|
|
|
|
|
|
# Create one-input, one-output, no-fee transaction:
|
|
|
|
class RawTransactionsTest(BitcoinTestFramework):
|
2015-04-01 15:08:00 +02:00
|
|
|
|
2016-05-20 15:16:51 +02:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.setup_clean_chain = True
|
|
|
|
self.num_nodes = 3
|
2014-12-29 20:17:21 +01:00
|
|
|
|
|
|
|
def setup_network(self, split=False):
|
2017-05-02 20:02:55 +02:00
|
|
|
super().setup_network()
|
2014-12-29 20:17:21 +01:00
|
|
|
connect_nodes_bi(self.nodes,0,2)
|
2015-04-01 15:08:00 +02:00
|
|
|
|
2014-12-29 20:17:21 +01:00
|
|
|
def run_test(self):
|
2015-04-01 15:08:00 +02:00
|
|
|
|
2014-12-29 20:17:21 +01:00
|
|
|
#prepare some coins for multiple *rawtransaction commands
|
|
|
|
self.nodes[2].generate(1)
|
2015-07-10 20:11:44 +02:00
|
|
|
self.sync_all()
|
2014-12-29 20:17:21 +01:00
|
|
|
self.nodes[0].generate(101)
|
|
|
|
self.sync_all()
|
2015-12-02 18:12:23 +01:00
|
|
|
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)
|
2014-12-29 20:17:21 +01:00
|
|
|
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)
|
2015-04-01 15:08:00 +02:00
|
|
|
|
2017-03-09 10:02:13 +01:00
|
|
|
# This will raise an exception since there are missing inputs
|
|
|
|
assert_raises_jsonrpc(-25, "Missing inputs", self.nodes[2].sendrawtransaction, rawtx['hex'])
|
2015-04-01 15:08:00 +02:00
|
|
|
|
|
|
|
#########################
|
|
|
|
# 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
|
2015-12-02 18:12:23 +01:00
|
|
|
txId = self.nodes[0].sendtoaddress(mSigObj, 1.2)
|
2015-04-01 15:08:00 +02:00
|
|
|
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)
|
|
|
|
|
2015-12-02 18:12:23 +01:00
|
|
|
txId = self.nodes[0].sendtoaddress(mSigObj, 2.2)
|
2015-04-01 15:08:00 +02:00
|
|
|
decTx = self.nodes[0].gettransaction(txId)
|
|
|
|
rawTx = self.nodes[0].decoderawtransaction(decTx['hex'])
|
|
|
|
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
|
2015-12-02 18:12:23 +01:00
|
|
|
break
|
2015-04-01 15:08:00 +02:00
|
|
|
|
|
|
|
bal = self.nodes[0].getbalance()
|
|
|
|
inputs = [{ "txid" : txId, "vout" : vout['n'], "scriptPubKey" : vout['scriptPubKey']['hex']}]
|
|
|
|
outputs = { self.nodes[0].getnewaddress() : 2.19 }
|
|
|
|
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
|
2014-12-29 20:17:21 +01:00
|
|
|
|
2015-04-01 15:08:00 +02:00
|
|
|
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()
|
2016-03-06 16:14:39 +01:00
|
|
|
assert_equal(self.nodes[0].getbalance(), bal+Decimal('500.00000000')+Decimal('2.19000000')) #block reward + tx
|
2014-12-29 20:17:21 +01:00
|
|
|
|
2016-11-23 07:13:35 +01:00
|
|
|
# getrawtransaction tests
|
|
|
|
# 1. valid parameters - only supply txid
|
2018-01-15 08:26:15 +01:00
|
|
|
txHash = rawTx["txid"]
|
2016-11-23 07:13:35 +01:00
|
|
|
assert_equal(self.nodes[0].getrawtransaction(txHash), rawTxSigned['hex'])
|
|
|
|
|
|
|
|
# 2. valid parameters - supply txid and 0 for non-verbose
|
|
|
|
assert_equal(self.nodes[0].getrawtransaction(txHash, 0), rawTxSigned['hex'])
|
|
|
|
|
|
|
|
# 3. valid parameters - supply txid and False for non-verbose
|
|
|
|
assert_equal(self.nodes[0].getrawtransaction(txHash, False), rawTxSigned['hex'])
|
|
|
|
|
|
|
|
# 4. valid parameters - supply txid and 1 for verbose.
|
|
|
|
# We only check the "hex" field of the output so we don't need to update this test every time the output format changes.
|
|
|
|
assert_equal(self.nodes[0].getrawtransaction(txHash, 1)["hex"], rawTxSigned['hex'])
|
|
|
|
|
|
|
|
# 5. valid parameters - supply txid and True for non-verbose
|
|
|
|
assert_equal(self.nodes[0].getrawtransaction(txHash, True)["hex"], rawTxSigned['hex'])
|
|
|
|
|
|
|
|
# 6. invalid parameters - supply txid and string "Flase"
|
2017-03-09 10:02:13 +01:00
|
|
|
assert_raises_jsonrpc(-3,"Invalid type", self.nodes[0].getrawtransaction, txHash, "Flase")
|
2016-11-23 07:13:35 +01:00
|
|
|
|
|
|
|
# 7. invalid parameters - supply txid and empty array
|
2017-03-09 10:02:13 +01:00
|
|
|
assert_raises_jsonrpc(-3,"Invalid type", self.nodes[0].getrawtransaction, txHash, [])
|
2016-11-23 07:13:35 +01:00
|
|
|
|
|
|
|
# 8. invalid parameters - supply txid and empty dict
|
2017-03-09 10:02:13 +01:00
|
|
|
assert_raises_jsonrpc(-3,"Invalid type", self.nodes[0].getrawtransaction, txHash, {})
|
2016-11-23 07:13:35 +01:00
|
|
|
|
2016-06-07 18:25:10 +02:00
|
|
|
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)
|
2016-06-16 11:05:32 +02:00
|
|
|
|
2017-03-09 10:02:13 +01:00
|
|
|
# 9. invalid parameters - sequence number out of range
|
2016-06-16 11:05:32 +02:00
|
|
|
inputs = [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1, 'sequence' : -1}]
|
|
|
|
outputs = { self.nodes[0].getnewaddress() : 1 }
|
2017-03-09 10:02:13 +01:00
|
|
|
assert_raises_jsonrpc(-8, 'Invalid parameter, sequence number is out of range', self.nodes[0].createrawtransaction, inputs, outputs)
|
2016-06-16 11:05:32 +02:00
|
|
|
|
2017-03-09 10:02:13 +01:00
|
|
|
# 10. invalid parameters - sequence number out of range
|
2016-06-16 11:05:32 +02:00
|
|
|
inputs = [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1, 'sequence' : 4294967296}]
|
|
|
|
outputs = { self.nodes[0].getnewaddress() : 1 }
|
2017-03-09 10:02:13 +01:00
|
|
|
assert_raises_jsonrpc(-8, 'Invalid parameter, sequence number is out of range', self.nodes[0].createrawtransaction, inputs, outputs)
|
2016-06-16 11:05:32 +02:00
|
|
|
|
|
|
|
inputs = [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1, 'sequence' : 4294967294}]
|
|
|
|
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'], 4294967294)
|
2016-06-07 18:25:10 +02:00
|
|
|
|
2014-12-29 20:17:21 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
RawTransactionsTest().main()
|