Auto merge of #2615 - bitcartel:2448_shield_coinbase, r=bitcartel
Add RPC command shield_coinbase #2448. Shield transparent coinbase funds by sending to a shielded z address. Closes #2448.
This commit is contained in:
commit
f34c60de37
|
@ -32,7 +32,7 @@ RPC calls by category:
|
|||
* Addresses : z_getnewaddress, z_listaddresses, z_validateaddress
|
||||
* Keys : z_exportkey, z_importkey, z_exportwallet, z_importwallet
|
||||
* Operation: z_getoperationresult, z_getoperationstatus, z_listoperationids
|
||||
* Payment : z_listreceivedbyaddress, z_sendmany
|
||||
* Payment : z_listreceivedbyaddress, z_sendmany, z_shieldcoinbase
|
||||
|
||||
RPC parameter conventions:
|
||||
|
||||
|
@ -72,6 +72,7 @@ Command | Parameters | Description
|
|||
--- | --- | ---
|
||||
z_listreceivedbyaddress<br> | zaddr [minconf=1] | Return a list of amounts received by a zaddr belonging to the node’s wallet.<br><br>Optionally set the minimum number of confirmations which a received amount must have in order to be included in the result. Use 0 to count unconfirmed transactions.<br><br>Output:<br>[{<br>“txid”: “4a0f…”,<br>“amount”: 0.54,<br>“memo”:”F0FF…”,}, {...}, {...}<br>]
|
||||
z_sendmany<br> | fromaddress amounts [minconf=1] [fee=0.0001] | _This is an Asynchronous RPC call_<br><br>Send funds from an address to multiple outputs. The address can be either a taddr or a zaddr.<br><br>Amounts is a list containing key/value pairs corresponding to the addresses and amount to pay. Each output address can be in taddr or zaddr format.<br><br>When sending to a zaddr, you also have the option of attaching a memo in hexadecimal format.<br><br>**NOTE:**When sending coinbase funds to a zaddr, the node's wallet does not allow any change. Put another way, spending a partial amount of a coinbase utxo is not allowed. This is not a consensus rule but a local wallet rule due to the current implementation of z_sendmany. In future, this rule may be removed.<br><br>Example of Outputs parameter:<br>[{“address”:”t123…”, “amount”:0.005},<br>,{“address”:”z010…”,”amount”:0.03, “memo”:”f508af…”}]<br><br>Optionally set the minimum number of confirmations which a private or transparent transaction must have in order to be used as an input. When sending from a zaddr, minconf must be greater than zero.<br><br>Optionally set a transaction fee, which by default is 0.0001 ZEC.<br><br>Any transparent change will be sent to a new transparent address. Any private change will be sent back to the zaddr being used as the source of funds.<br><br>Returns an operationid. You use the operationid value with z_getoperationstatus and z_getoperationresult to obtain the result of sending funds, which if successful, will be a txid.
|
||||
z_shieldcoinbase<br> | fromaddress toaddress [fee=0.0001] | _This is an Asynchronous RPC call_<br><br>Shield transparent coinbase funds by sending to a shielded z address. Utxos selected for shielding will be locked. If there is an error, they are unlocked. The RPC call `listlockunspent` can be used to return a list of locked utxos. The number of coinbase utxos selected for shielding is limited by both the -mempooltxinputlimit=xxx option and a consensus rule defining a maximum transaction size of 100000 bytes. <br><br>The from address is a taddr or "*" for all taddrs belonging to the wallet. The to address is a zaddr. The default fee is 0.0001.<br><br>Returns an object containing an operationid which can be used with z_getoperationstatus and z_getoperationresult, along with key-value pairs regarding how many utxos are being shielded in this trasaction and what remains to be shielded.
|
||||
|
||||
### Operations
|
||||
|
||||
|
|
|
@ -14,6 +14,7 @@ testScripts=(
|
|||
'prioritisetransaction.py'
|
||||
'wallet_treestate.py'
|
||||
'wallet_protectcoinbase.py'
|
||||
'wallet_shieldcoinbase.py'
|
||||
'wallet.py'
|
||||
'wallet_nullifiers.py'
|
||||
'wallet_1941.py'
|
||||
|
|
|
@ -0,0 +1,190 @@
|
|||
#!/usr/bin/env python2
|
||||
# Copyright (c) 2017 The Zcash developers
|
||||
# Distributed under the MIT software license, see the accompanying
|
||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
from test_framework.test_framework import BitcoinTestFramework
|
||||
from test_framework.authproxy import JSONRPCException
|
||||
from test_framework.util import assert_equal, initialize_chain_clean, \
|
||||
start_node, connect_nodes_bi, sync_blocks
|
||||
|
||||
import sys
|
||||
import time
|
||||
from decimal import Decimal
|
||||
|
||||
class WalletShieldCoinbaseTest (BitcoinTestFramework):
|
||||
|
||||
def setup_chain(self):
|
||||
print("Initializing test directory "+self.options.tmpdir)
|
||||
initialize_chain_clean(self.options.tmpdir, 4)
|
||||
|
||||
def setup_network(self, split=False):
|
||||
args = ['-regtestprotectcoinbase', '-debug=zrpcunsafe']
|
||||
self.nodes = []
|
||||
self.nodes.append(start_node(0, self.options.tmpdir, args))
|
||||
self.nodes.append(start_node(1, self.options.tmpdir, args))
|
||||
args2 = ['-regtestprotectcoinbase', '-debug=zrpcunsafe', "-mempooltxinputlimit=7"]
|
||||
self.nodes.append(start_node(2, self.options.tmpdir, args2))
|
||||
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()
|
||||
|
||||
# Returns txid if operation was a success or None
|
||||
def wait_and_assert_operationid_status(self, nodeid, myopid, in_status='success', in_errormsg=None):
|
||||
print('waiting for async operation {}'.format(myopid))
|
||||
opids = []
|
||||
opids.append(myopid)
|
||||
timeout = 300
|
||||
status = None
|
||||
errormsg = None
|
||||
txid = None
|
||||
for x in xrange(1, timeout):
|
||||
results = self.nodes[nodeid].z_getoperationresult(opids)
|
||||
if len(results)==0:
|
||||
time.sleep(1)
|
||||
else:
|
||||
status = results[0]["status"]
|
||||
if status == "failed":
|
||||
errormsg = results[0]['error']['message']
|
||||
elif status == "success":
|
||||
txid = results[0]['result']['txid']
|
||||
break
|
||||
print('...returned status: {}'.format(status))
|
||||
assert_equal(in_status, status)
|
||||
if errormsg is not None:
|
||||
assert(in_errormsg is not None)
|
||||
assert_equal(in_errormsg in errormsg, True)
|
||||
print('...returned error: {}'.format(errormsg))
|
||||
return txid
|
||||
|
||||
def run_test (self):
|
||||
print "Mining blocks..."
|
||||
|
||||
self.nodes[0].generate(1)
|
||||
do_not_shield_taddr = self.nodes[0].getnewaddress()
|
||||
|
||||
self.nodes[0].generate(4)
|
||||
walletinfo = self.nodes[0].getwalletinfo()
|
||||
assert_equal(walletinfo['immature_balance'], 50)
|
||||
assert_equal(walletinfo['balance'], 0)
|
||||
self.sync_all()
|
||||
self.nodes[2].generate(1)
|
||||
self.nodes[2].getnewaddress()
|
||||
self.nodes[2].generate(1)
|
||||
self.nodes[2].getnewaddress()
|
||||
self.nodes[2].generate(1)
|
||||
self.sync_all()
|
||||
self.nodes[1].generate(101)
|
||||
self.sync_all()
|
||||
assert_equal(self.nodes[0].getbalance(), 50)
|
||||
assert_equal(self.nodes[1].getbalance(), 10)
|
||||
assert_equal(self.nodes[2].getbalance(), 30)
|
||||
|
||||
# Prepare to send taddr->zaddr
|
||||
mytaddr = self.nodes[0].getnewaddress()
|
||||
myzaddr = self.nodes[0].z_getnewaddress()
|
||||
|
||||
# Shielding will fail when trying to spend from watch-only address
|
||||
self.nodes[2].importaddress(mytaddr)
|
||||
try:
|
||||
self.nodes[2].z_shieldcoinbase(mytaddr, myzaddr)
|
||||
except JSONRPCException,e:
|
||||
errorString = e.error['message']
|
||||
assert_equal("Could not find any coinbase funds to shield" in errorString, True)
|
||||
|
||||
# Shielding will fail because fee is negative
|
||||
try:
|
||||
self.nodes[0].z_shieldcoinbase("*", myzaddr, -1)
|
||||
except JSONRPCException,e:
|
||||
errorString = e.error['message']
|
||||
assert_equal("Amount out of range" in errorString, True)
|
||||
|
||||
# Shielding will fail because fee is larger than MAX_MONEY
|
||||
try:
|
||||
self.nodes[0].z_shieldcoinbase("*", myzaddr, Decimal('21000000.00000001'))
|
||||
except JSONRPCException,e:
|
||||
errorString = e.error['message']
|
||||
assert_equal("Amount out of range" in errorString, True)
|
||||
|
||||
# Shielding will fail because fee is larger than sum of utxos
|
||||
try:
|
||||
self.nodes[0].z_shieldcoinbase("*", myzaddr, 999)
|
||||
except JSONRPCException,e:
|
||||
errorString = e.error['message']
|
||||
assert_equal("Insufficient coinbase funds" in errorString, True)
|
||||
|
||||
# Shield coinbase utxos from node 0 of value 40, standard fee of 0.00010000
|
||||
result = self.nodes[0].z_shieldcoinbase(mytaddr, myzaddr)
|
||||
mytxid = self.wait_and_assert_operationid_status(0, result['opid'])
|
||||
self.sync_all()
|
||||
self.nodes[1].generate(1)
|
||||
self.sync_all()
|
||||
|
||||
# Confirm balances and that do_not_shield_taddr containing funds of 10 was left alone
|
||||
assert_equal(self.nodes[0].getbalance(), 10)
|
||||
assert_equal(self.nodes[0].z_getbalance(do_not_shield_taddr), Decimal('10.0'))
|
||||
assert_equal(self.nodes[0].z_getbalance(myzaddr), Decimal('39.99990000'))
|
||||
assert_equal(self.nodes[1].getbalance(), 20)
|
||||
assert_equal(self.nodes[2].getbalance(), 30)
|
||||
|
||||
# Shield coinbase utxos from any node 2 taddr, and set fee to 0
|
||||
result = self.nodes[2].z_shieldcoinbase("*", myzaddr, 0)
|
||||
mytxid = self.wait_and_assert_operationid_status(2, result['opid'])
|
||||
self.sync_all()
|
||||
self.nodes[1].generate(1)
|
||||
self.sync_all()
|
||||
|
||||
assert_equal(self.nodes[0].getbalance(), 10)
|
||||
assert_equal(self.nodes[0].z_getbalance(myzaddr), Decimal('69.99990000'))
|
||||
assert_equal(self.nodes[1].getbalance(), 30)
|
||||
assert_equal(self.nodes[2].getbalance(), 0)
|
||||
|
||||
# Generate 800 coinbase utxos on node 0, and 20 coinbase utxos on node 2
|
||||
self.nodes[0].generate(800)
|
||||
self.sync_all()
|
||||
self.nodes[2].generate(20)
|
||||
self.sync_all()
|
||||
self.nodes[1].generate(100)
|
||||
self.sync_all()
|
||||
mytaddr = self.nodes[0].getnewaddress()
|
||||
|
||||
# Shielding the 800 utxos will occur over two transactions, since max tx size is 100,000 bytes.
|
||||
# We don't verify shieldingValue as utxos are not selected in any specific order, so value can change on each test run.
|
||||
result = self.nodes[0].z_shieldcoinbase(mytaddr, myzaddr, 0)
|
||||
assert_equal(result["shieldingUTXOs"], Decimal('662'))
|
||||
assert_equal(result["remainingUTXOs"], Decimal('138'))
|
||||
remainingValue = result["remainingValue"]
|
||||
opid1 = result['opid']
|
||||
|
||||
# Verify that utxos are locked (not available for selection) by queuing up another shielding operation
|
||||
result = self.nodes[0].z_shieldcoinbase(mytaddr, myzaddr)
|
||||
assert_equal(result["shieldingValue"], Decimal(remainingValue))
|
||||
assert_equal(result["shieldingUTXOs"], Decimal('138'))
|
||||
assert_equal(result["remainingValue"], Decimal('0'))
|
||||
assert_equal(result["remainingUTXOs"], Decimal('0'))
|
||||
opid2 = result['opid']
|
||||
|
||||
# wait for both aysnc operations to complete
|
||||
self.wait_and_assert_operationid_status(0, opid1)
|
||||
self.wait_and_assert_operationid_status(0, opid2)
|
||||
|
||||
# sync_all() invokes sync_mempool() but node 2's mempool limit will cause tx1 and tx2 to be rejected.
|
||||
# So instead, we sync on blocks, and after a new block is generated, all nodes will have an empty mempool.
|
||||
sync_blocks(self.nodes)
|
||||
self.nodes[1].generate(1)
|
||||
self.sync_all()
|
||||
|
||||
# Verify maximum number of utxos which node 2 can shield is limited by option -mempooltxinputlimit
|
||||
mytaddr = self.nodes[2].getnewaddress()
|
||||
result = self.nodes[2].z_shieldcoinbase(mytaddr, myzaddr, 0)
|
||||
assert_equal(result["shieldingUTXOs"], Decimal('7'))
|
||||
assert_equal(result["remainingUTXOs"], Decimal('13'))
|
||||
mytxid = self.wait_and_assert_operationid_status(2, result['opid'])
|
||||
self.sync_all()
|
||||
self.nodes[1].generate(1)
|
||||
self.sync_all()
|
||||
|
||||
if __name__ == '__main__':
|
||||
WalletShieldCoinbaseTest().main()
|
|
@ -185,6 +185,7 @@ BITCOIN_CORE_H = \
|
|||
validationinterface.h \
|
||||
version.h \
|
||||
wallet/asyncrpcoperation_sendmany.h \
|
||||
wallet/asyncrpcoperation_shieldcoinbase.h \
|
||||
wallet/crypter.h \
|
||||
wallet/db.h \
|
||||
wallet/wallet.h \
|
||||
|
@ -271,6 +272,7 @@ libbitcoin_wallet_a_SOURCES = \
|
|||
zcbenchmarks.cpp \
|
||||
zcbenchmarks.h \
|
||||
wallet/asyncrpcoperation_sendmany.cpp \
|
||||
wallet/asyncrpcoperation_shieldcoinbase.cpp \
|
||||
wallet/crypter.cpp \
|
||||
wallet/db.cpp \
|
||||
wallet/rpcdump.cpp \
|
||||
|
|
|
@ -28,6 +28,7 @@
|
|||
#include "utilmoneystr.h"
|
||||
#include "validationinterface.h"
|
||||
#include "wallet/asyncrpcoperation_sendmany.h"
|
||||
#include "wallet/asyncrpcoperation_shieldcoinbase.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
|
|
|
@ -103,12 +103,13 @@ static const CRPCConvertParam vRPCConvertParams[] =
|
|||
{ "zcbenchmark", 1 },
|
||||
{ "zcbenchmark", 2 },
|
||||
{ "getblocksubsidy", 0},
|
||||
{ "z_listreceivedbyaddress", 1},
|
||||
{ "z_listreceivedbyaddress", 1},
|
||||
{ "z_getbalance", 1},
|
||||
{ "z_gettotalbalance", 0},
|
||||
{ "z_sendmany", 1},
|
||||
{ "z_sendmany", 2},
|
||||
{ "z_sendmany", 3},
|
||||
{ "z_shieldcoinbase", 2},
|
||||
{ "z_getoperationstatus", 0},
|
||||
{ "z_getoperationresult", 0},
|
||||
{ "z_importkey", 2 },
|
||||
|
|
|
@ -386,6 +386,7 @@ static const CRPCCommand vRPCCommands[] =
|
|||
{ "wallet", "z_getbalance", &z_getbalance, false },
|
||||
{ "wallet", "z_gettotalbalance", &z_gettotalbalance, false },
|
||||
{ "wallet", "z_sendmany", &z_sendmany, false },
|
||||
{ "wallet", "z_shieldcoinbase", &z_shieldcoinbase, false },
|
||||
{ "wallet", "z_getoperationstatus", &z_getoperationstatus, true },
|
||||
{ "wallet", "z_getoperationresult", &z_getoperationresult, true },
|
||||
{ "wallet", "z_listoperationids", &z_listoperationids, true },
|
||||
|
@ -426,7 +427,7 @@ bool StartRPC()
|
|||
|
||||
// Launch one async rpc worker. The ability to launch multiple workers is not recommended at present and thus the option is disabled.
|
||||
getAsyncRPCQueue()->addWorker();
|
||||
/*
|
||||
/*
|
||||
int n = GetArg("-rpcasyncthreads", 1);
|
||||
if (n<1) {
|
||||
LogPrintf("ERROR: Invalid value %d for -rpcasyncthreads. Must be at least 1.\n", n);
|
||||
|
|
|
@ -287,6 +287,7 @@ extern UniValue z_listreceivedbyaddress(const UniValue& params, bool fHelp); //
|
|||
extern UniValue z_getbalance(const UniValue& params, bool fHelp); // in rpcwallet.cpp
|
||||
extern UniValue z_gettotalbalance(const UniValue& params, bool fHelp); // in rpcwallet.cpp
|
||||
extern UniValue z_sendmany(const UniValue& params, bool fHelp); // in rpcwallet.cpp
|
||||
extern UniValue z_shieldcoinbase(const UniValue& params, bool fHelp); // in rpcwallet.cpp
|
||||
extern UniValue z_getoperationstatus(const UniValue& params, bool fHelp); // in rpcwallet.cpp
|
||||
extern UniValue z_getoperationresult(const UniValue& params, bool fHelp); // in rpcwallet.cpp
|
||||
extern UniValue z_listoperationids(const UniValue& params, bool fHelp); // in rpcwallet.cpp
|
||||
|
|
|
@ -17,6 +17,8 @@
|
|||
#include "asyncrpcqueue.h"
|
||||
#include "asyncrpcoperation.h"
|
||||
#include "wallet/asyncrpcoperation_sendmany.h"
|
||||
#include "wallet/asyncrpcoperation_shieldcoinbase.h"
|
||||
|
||||
#include "rpcprotocol.h"
|
||||
#include "init.h"
|
||||
|
||||
|
@ -289,20 +291,20 @@ BOOST_AUTO_TEST_CASE(rpc_wallet_getbalance)
|
|||
|
||||
LOCK(pwalletMain->cs_wallet);
|
||||
|
||||
|
||||
|
||||
BOOST_CHECK_THROW(CallRPC("z_getbalance too many args"), runtime_error);
|
||||
BOOST_CHECK_THROW(CallRPC("z_getbalance invalidaddress"), runtime_error);
|
||||
BOOST_CHECK_NO_THROW(CallRPC("z_getbalance tmC6YZnCUhm19dEXxh3Jb7srdBJxDawaCab"));
|
||||
BOOST_CHECK_THROW(CallRPC("z_getbalance tmC6YZnCUhm19dEXxh3Jb7srdBJxDawaCab -1"), runtime_error);
|
||||
BOOST_CHECK_NO_THROW(CallRPC("z_getbalance tmC6YZnCUhm19dEXxh3Jb7srdBJxDawaCab 0"));
|
||||
BOOST_CHECK_THROW(CallRPC("z_getbalance tnRZ8bPq2pff3xBWhTJhNkVUkm2uhzksDeW5PvEa7aFKGT9Qi3YgTALZfjaY4jU3HLVKBtHdSXxoPoLA3naMPcHBcY88FcF 1"), runtime_error);
|
||||
|
||||
|
||||
|
||||
|
||||
BOOST_CHECK_THROW(CallRPC("z_gettotalbalance too manyargs"), runtime_error);
|
||||
BOOST_CHECK_THROW(CallRPC("z_gettotalbalance -1"), runtime_error);
|
||||
BOOST_CHECK_NO_THROW(CallRPC("z_gettotalbalance 0"));
|
||||
|
||||
|
||||
|
||||
|
||||
BOOST_CHECK_THROW(CallRPC("z_listreceivedbyaddress too many args"), runtime_error);
|
||||
// negative minconf not allowed
|
||||
BOOST_CHECK_THROW(CallRPC("z_listreceivedbyaddress tmC6YZnCUhm19dEXxh3Jb7srdBJxDawaCab -1"), runtime_error);
|
||||
|
@ -374,7 +376,7 @@ BOOST_AUTO_TEST_CASE(rpc_wallet_z_exportwallet)
|
|||
CZCPaymentAddress paymentAddress = pwalletMain->GenerateNewZKey();
|
||||
pwalletMain->GetPaymentAddresses(addrs);
|
||||
BOOST_CHECK(addrs.size()==1);
|
||||
|
||||
|
||||
// Set up paths
|
||||
boost::filesystem::path tmppath = boost::filesystem::temp_directory_path();
|
||||
boost::filesystem::path tmpfilename = boost::filesystem::unique_path("%%%%%%%%");
|
||||
|
@ -402,10 +404,10 @@ BOOST_AUTO_TEST_CASE(rpc_wallet_z_exportwallet)
|
|||
|
||||
std::string s1 = paymentAddress.ToString();
|
||||
std::string s2 = CZCSpendingKey(key).ToString();
|
||||
|
||||
|
||||
// There's no way to really delete a private key so we will read in the
|
||||
// exported wallet file and search for the spending key and payment address.
|
||||
|
||||
|
||||
EnsureWalletIsUnlocked();
|
||||
|
||||
ifstream file;
|
||||
|
@ -434,7 +436,7 @@ BOOST_AUTO_TEST_CASE(rpc_wallet_z_exportwallet)
|
|||
BOOST_AUTO_TEST_CASE(rpc_wallet_z_importwallet)
|
||||
{
|
||||
LOCK2(cs_main, pwalletMain->cs_wallet);
|
||||
|
||||
|
||||
// error if no args
|
||||
BOOST_CHECK_THROW(CallRPC("z_importwallet"), runtime_error);
|
||||
|
||||
|
@ -446,7 +448,7 @@ BOOST_AUTO_TEST_CASE(rpc_wallet_z_importwallet)
|
|||
auto testPaymentAddress = testSpendingKey.address();
|
||||
std::string testAddr = CZCPaymentAddress(testPaymentAddress).ToString();
|
||||
std::string testKey = CZCSpendingKey(testSpendingKey).ToString();
|
||||
|
||||
|
||||
// create test data using the random key
|
||||
std::string format_str = "# Wallet dump created by Zcash v0.11.2.0.z8-9155cc6-dirty (2016-08-11 11:37:00 -0700)\n"
|
||||
"# * Created on 2016-08-12T21:55:36Z\n"
|
||||
|
@ -458,10 +460,10 @@ BOOST_AUTO_TEST_CASE(rpc_wallet_z_importwallet)
|
|||
"%s 2016-08-12T21:55:36Z # zaddr=%s\n"
|
||||
"\n"
|
||||
"\n# End of dump";
|
||||
|
||||
|
||||
boost::format formatobject(format_str);
|
||||
std::string testWalletDump = (formatobject % testKey % testAddr).str();
|
||||
|
||||
|
||||
// write test data to file
|
||||
boost::filesystem::path temp = boost::filesystem::temp_directory_path() /
|
||||
boost::filesystem::unique_path();
|
||||
|
@ -474,19 +476,19 @@ BOOST_AUTO_TEST_CASE(rpc_wallet_z_importwallet)
|
|||
std::set<libzcash::PaymentAddress> addrs;
|
||||
pwalletMain->GetPaymentAddresses(addrs);
|
||||
BOOST_CHECK(addrs.size()==0);
|
||||
|
||||
|
||||
// import test data from file into wallet
|
||||
BOOST_CHECK_NO_THROW(CallRPC(string("z_importwallet ") + path));
|
||||
|
||||
|
||||
// wallet should now have one zkey
|
||||
pwalletMain->GetPaymentAddresses(addrs);
|
||||
BOOST_CHECK(addrs.size()==1);
|
||||
|
||||
|
||||
// check that we have the spending key for the address
|
||||
CZCPaymentAddress address(testAddr);
|
||||
auto addr = address.Get();
|
||||
BOOST_CHECK(pwalletMain->HaveSpendingKey(addr));
|
||||
|
||||
|
||||
// Verify the spending key is the same as the test data
|
||||
libzcash::SpendingKey k;
|
||||
BOOST_CHECK(pwalletMain->GetSpendingKey(addr, k));
|
||||
|
@ -504,10 +506,10 @@ BOOST_AUTO_TEST_CASE(rpc_wallet_z_importexport)
|
|||
UniValue retValue;
|
||||
int n1 = 1000; // number of times to import/export
|
||||
int n2 = 1000; // number of addresses to create and list
|
||||
|
||||
|
||||
// error if no args
|
||||
BOOST_CHECK_THROW(CallRPC("z_importkey"), runtime_error);
|
||||
BOOST_CHECK_THROW(CallRPC("z_exportkey"), runtime_error);
|
||||
BOOST_CHECK_THROW(CallRPC("z_importkey"), runtime_error);
|
||||
BOOST_CHECK_THROW(CallRPC("z_exportkey"), runtime_error);
|
||||
|
||||
// error if too many args
|
||||
BOOST_CHECK_THROW(CallRPC("z_importkey way too many args"), runtime_error);
|
||||
|
@ -548,7 +550,7 @@ BOOST_AUTO_TEST_CASE(rpc_wallet_z_importexport)
|
|||
for (UniValue element : arr.getValues()) {
|
||||
myaddrs.insert(element.get_str());
|
||||
}
|
||||
|
||||
|
||||
// Make new addresses for the set
|
||||
for (int i=0; i<n2; i++) {
|
||||
myaddrs.insert((pwalletMain->GenerateNewZKey()).ToString());
|
||||
|
@ -558,19 +560,19 @@ BOOST_AUTO_TEST_CASE(rpc_wallet_z_importexport)
|
|||
int numAddrs = myaddrs.size();
|
||||
BOOST_CHECK(numAddrs == n1+n2);
|
||||
pwalletMain->GetPaymentAddresses(addrs);
|
||||
BOOST_CHECK(addrs.size()==numAddrs);
|
||||
|
||||
BOOST_CHECK(addrs.size()==numAddrs);
|
||||
|
||||
// Ask wallet to list addresses
|
||||
BOOST_CHECK_NO_THROW(retValue = CallRPC("z_listaddresses"));
|
||||
arr = retValue.get_array();
|
||||
BOOST_CHECK(arr.size() == numAddrs);
|
||||
|
||||
|
||||
// Create a set from them
|
||||
std::unordered_set<std::string> listaddrs;
|
||||
for (UniValue element : arr.getValues()) {
|
||||
listaddrs.insert(element.get_str());
|
||||
}
|
||||
|
||||
|
||||
// Verify the two sets of addresses are the same
|
||||
BOOST_CHECK(listaddrs.size() == numAddrs);
|
||||
BOOST_CHECK(myaddrs == listaddrs);
|
||||
|
@ -623,19 +625,19 @@ BOOST_AUTO_TEST_CASE(rpc_wallet_async_operations)
|
|||
BOOST_CHECK(ids.size()==0);
|
||||
|
||||
std::shared_ptr<AsyncRPCOperation> op1 = std::make_shared<AsyncRPCOperation>();
|
||||
q->addOperation(op1);
|
||||
q->addOperation(op1);
|
||||
BOOST_CHECK(q->getOperationCount() == 1);
|
||||
|
||||
|
||||
OperationStatus status = op1->getState();
|
||||
BOOST_CHECK(status == OperationStatus::READY);
|
||||
|
||||
|
||||
AsyncRPCOperationId id1 = op1->getId();
|
||||
int64_t creationTime1 = op1->getCreationTime();
|
||||
|
||||
|
||||
q->addWorker();
|
||||
BOOST_CHECK(q->getNumberOfWorkers() == 1);
|
||||
|
||||
// an AsyncRPCOperation doesn't do anything so will finish immediately
|
||||
|
||||
// an AsyncRPCOperation doesn't do anything so will finish immediately
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
BOOST_CHECK(q->getOperationCount() == 0);
|
||||
|
||||
|
@ -649,7 +651,7 @@ BOOST_AUTO_TEST_CASE(rpc_wallet_async_operations)
|
|||
BOOST_CHECK_EQUAL(op1->getResult().isNull(), false);
|
||||
BOOST_CHECK_EQUAL(op1->getStateAsString(), "success");
|
||||
BOOST_CHECK_NE(op1->getStateAsString(), "executing");
|
||||
|
||||
|
||||
// Create a second operation which just sleeps
|
||||
std::shared_ptr<AsyncRPCOperation> op2(new MockSleepOperation(2500));
|
||||
AsyncRPCOperationId id2 = op2->getId();
|
||||
|
@ -683,8 +685,8 @@ BOOST_AUTO_TEST_CASE(rpc_wallet_async_operations)
|
|||
BOOST_CHECK_EQUAL(op2->isSuccess(), true);
|
||||
BOOST_CHECK_EQUAL(op2->isCancelled(), false);
|
||||
BOOST_CHECK_EQUAL(op3->isCancelled(), true);
|
||||
|
||||
|
||||
|
||||
|
||||
v = q->getAllOperationIds();
|
||||
std::copy( v.begin(), v.end(), std::inserter( opids, opids.end() ) );
|
||||
BOOST_CHECK(opids.size() == 3);
|
||||
|
@ -702,7 +704,7 @@ class CountOperation : public AsyncRPCOperation {
|
|||
public:
|
||||
CountOperation() {}
|
||||
virtual ~CountOperation() {}
|
||||
virtual void main() {
|
||||
virtual void main() {
|
||||
set_state(OperationStatus::EXECUTING);
|
||||
gCounter++;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
||||
|
@ -714,7 +716,7 @@ public:
|
|||
BOOST_AUTO_TEST_CASE(rpc_wallet_async_operations_parallel_wait)
|
||||
{
|
||||
gCounter = 0;
|
||||
|
||||
|
||||
std::shared_ptr<AsyncRPCQueue> q = std::make_shared<AsyncRPCQueue>();
|
||||
q->addWorker();
|
||||
q->addWorker();
|
||||
|
@ -739,7 +741,7 @@ BOOST_AUTO_TEST_CASE(rpc_wallet_async_operations_parallel_wait)
|
|||
BOOST_AUTO_TEST_CASE(rpc_wallet_async_operations_parallel_cancel)
|
||||
{
|
||||
gCounter = 0;
|
||||
|
||||
|
||||
std::shared_ptr<AsyncRPCQueue> q = std::make_shared<AsyncRPCQueue>();
|
||||
q->addWorker();
|
||||
q->addWorker();
|
||||
|
@ -755,7 +757,7 @@ BOOST_AUTO_TEST_CASE(rpc_wallet_async_operations_parallel_cancel)
|
|||
q->closeAndWait();
|
||||
|
||||
int numSuccess = 0;
|
||||
int numCancelled = 0;
|
||||
int numCancelled = 0;
|
||||
for (auto & id : ids) {
|
||||
std::shared_ptr<AsyncRPCOperation> ptr = q->popOperationForId(id);
|
||||
if (ptr->isCancelled()) {
|
||||
|
@ -764,7 +766,7 @@ BOOST_AUTO_TEST_CASE(rpc_wallet_async_operations_parallel_cancel)
|
|||
numSuccess++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
BOOST_CHECK_EQUAL(numOperations, numSuccess+numCancelled);
|
||||
BOOST_CHECK_EQUAL(gCounter.load(), numSuccess);
|
||||
BOOST_CHECK(q->getOperationCount() == 0);
|
||||
|
@ -790,19 +792,19 @@ BOOST_AUTO_TEST_CASE(rpc_z_getoperations)
|
|||
BOOST_CHECK_NO_THROW(CallRPC("z_getoperationresult [\"opid-1234\"]"));
|
||||
BOOST_CHECK_THROW(CallRPC("z_getoperationresult [] toomanyargs"), runtime_error);
|
||||
BOOST_CHECK_THROW(CallRPC("z_getoperationresult not_an_array"), runtime_error);
|
||||
|
||||
|
||||
std::shared_ptr<AsyncRPCOperation> op1 = std::make_shared<AsyncRPCOperation>();
|
||||
q->addOperation(op1);
|
||||
std::shared_ptr<AsyncRPCOperation> op2 = std::make_shared<AsyncRPCOperation>();
|
||||
q->addOperation(op2);
|
||||
|
||||
|
||||
BOOST_CHECK(q->getOperationCount() == 2);
|
||||
BOOST_CHECK(q->getNumberOfWorkers() == 0);
|
||||
q->addWorker();
|
||||
BOOST_CHECK(q->getNumberOfWorkers() == 1);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
||||
BOOST_CHECK(q->getOperationCount() == 0);
|
||||
|
||||
|
||||
// Check if too many args
|
||||
BOOST_CHECK_THROW(CallRPC("z_listoperationids toomany args"), runtime_error);
|
||||
|
||||
|
@ -817,28 +819,28 @@ BOOST_AUTO_TEST_CASE(rpc_z_getoperations)
|
|||
// idempotent
|
||||
BOOST_CHECK_NO_THROW(retValue = CallRPC("z_getoperationstatus"));
|
||||
array = retValue.get_array();
|
||||
BOOST_CHECK(array.size() == 2);
|
||||
|
||||
BOOST_CHECK(array.size() == 2);
|
||||
|
||||
for (UniValue v : array.getValues()) {
|
||||
UniValue obj = v.get_obj();
|
||||
UniValue id = find_value(obj, "id");
|
||||
|
||||
|
||||
UniValue result;
|
||||
// removes result from internal storage
|
||||
BOOST_CHECK_NO_THROW(result = CallRPC("z_getoperationresult [\"" + id.get_str() + "\"]"));
|
||||
UniValue resultArray = result.get_array();
|
||||
BOOST_CHECK(resultArray.size() == 1);
|
||||
|
||||
|
||||
UniValue resultObj = resultArray[0].get_obj();
|
||||
UniValue resultId = find_value(resultObj, "id");
|
||||
BOOST_CHECK_EQUAL(id.get_str(), resultId.get_str());
|
||||
|
||||
// verify the operation has been removed
|
||||
|
||||
// verify the operation has been removed
|
||||
BOOST_CHECK_NO_THROW(result = CallRPC("z_getoperationresult [\"" + id.get_str() + "\"]"));
|
||||
resultArray = result.get_array();
|
||||
BOOST_CHECK(resultArray.size() == 0);
|
||||
}
|
||||
|
||||
|
||||
// operations removed
|
||||
BOOST_CHECK_NO_THROW(retValue = CallRPC("z_getoperationstatus"));
|
||||
array = retValue.get_array();
|
||||
|
@ -905,8 +907,8 @@ BOOST_AUTO_TEST_CASE(rpc_z_sendmany_parameters)
|
|||
std::string zaddr1 = pa.ToString();
|
||||
BOOST_CHECK_THROW(CallRPC(string("z_sendmany tmRr6yJonqGK23UVhrKuyvTpF8qxQQjKigJ ")
|
||||
+ "[{\"address\":\"" + zaddr1 + "\", \"amount\":123.456}]"), runtime_error);
|
||||
|
||||
// Test constructor of AsyncRPCOperation_sendmany
|
||||
|
||||
// Test constructor of AsyncRPCOperation_sendmany
|
||||
try {
|
||||
std::shared_ptr<AsyncRPCOperation> operation(new AsyncRPCOperation_sendmany("",{}, {}, -1));
|
||||
} catch (const UniValue& objError) {
|
||||
|
@ -959,13 +961,13 @@ BOOST_AUTO_TEST_CASE(rpc_z_sendmany_internals)
|
|||
LOCK(pwalletMain->cs_wallet);
|
||||
|
||||
UniValue retValue;
|
||||
|
||||
|
||||
// add keys manually
|
||||
BOOST_CHECK_NO_THROW(retValue = CallRPC("getnewaddress"));
|
||||
std::string taddr1 = retValue.get_str();
|
||||
CZCPaymentAddress pa = pwalletMain->GenerateNewZKey();
|
||||
std::string zaddr1 = pa.ToString();
|
||||
|
||||
|
||||
// there are no utxos to spend
|
||||
{
|
||||
std::vector<SendManyRecipient> recipients = { SendManyRecipient(zaddr1,100.0, "DEADBEEF") };
|
||||
|
@ -986,7 +988,7 @@ BOOST_AUTO_TEST_CASE(rpc_z_sendmany_internals)
|
|||
BOOST_CHECK(find_error(objError, "Minconf cannot be zero when sending from zaddr"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// there are no unspent notes to spend
|
||||
{
|
||||
|
@ -1004,7 +1006,7 @@ BOOST_AUTO_TEST_CASE(rpc_z_sendmany_internals)
|
|||
std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany(zaddr1, recipients, {}, 1) );
|
||||
std::shared_ptr<AsyncRPCOperation_sendmany> ptr = std::dynamic_pointer_cast<AsyncRPCOperation_sendmany> (operation);
|
||||
TEST_FRIEND_AsyncRPCOperation_sendmany proxy(ptr);
|
||||
|
||||
|
||||
std::string memo = "DEADBEEF";
|
||||
boost::array<unsigned char, ZC_MEMO_SIZE> array = proxy.get_memo_from_hex_string(memo);
|
||||
BOOST_CHECK_EQUAL(array[0], 0xDE);
|
||||
|
@ -1014,28 +1016,28 @@ BOOST_AUTO_TEST_CASE(rpc_z_sendmany_internals)
|
|||
for (int i=4; i<ZC_MEMO_SIZE; i++) {
|
||||
BOOST_CHECK_EQUAL(array[i], 0x00); // zero padding
|
||||
}
|
||||
|
||||
|
||||
// memo is longer than allowed
|
||||
std::vector<char> v (2 * (ZC_MEMO_SIZE+1));
|
||||
std::fill(v.begin(),v.end(), 'A');
|
||||
std::string bigmemo(v.begin(), v.end());
|
||||
|
||||
|
||||
try {
|
||||
proxy.get_memo_from_hex_string(bigmemo);
|
||||
} catch (const UniValue& objError) {
|
||||
BOOST_CHECK( find_error(objError, "too big"));
|
||||
}
|
||||
|
||||
|
||||
// invalid hexadecimal string
|
||||
std::fill(v.begin(),v.end(), '@'); // not a hex character
|
||||
std::string badmemo(v.begin(), v.end());
|
||||
|
||||
|
||||
try {
|
||||
proxy.get_memo_from_hex_string(badmemo);
|
||||
} catch (const UniValue& objError) {
|
||||
BOOST_CHECK( find_error(objError, "hexadecimal format"));
|
||||
}
|
||||
|
||||
|
||||
// odd length hexadecimal string
|
||||
std::fill(v.begin(),v.end(), 'A');
|
||||
v.resize(v.size() - 1);
|
||||
|
@ -1047,25 +1049,25 @@ BOOST_AUTO_TEST_CASE(rpc_z_sendmany_internals)
|
|||
BOOST_CHECK( find_error(objError, "hexadecimal format"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// add_taddr_change_output_to_tx() will append a vout to a raw transaction
|
||||
{
|
||||
std::vector<SendManyRecipient> recipients = { SendManyRecipient(zaddr1,100.0, "DEADBEEF") };
|
||||
std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany(zaddr1, recipients, {}, 1) );
|
||||
std::shared_ptr<AsyncRPCOperation_sendmany> ptr = std::dynamic_pointer_cast<AsyncRPCOperation_sendmany> (operation);
|
||||
TEST_FRIEND_AsyncRPCOperation_sendmany proxy(ptr);
|
||||
|
||||
|
||||
CTransaction tx = proxy.getTx();
|
||||
BOOST_CHECK(tx.vout.size() == 0);
|
||||
|
||||
|
||||
CAmount amount = 123.456;
|
||||
proxy.add_taddr_change_output_to_tx(amount);
|
||||
tx = proxy.getTx();
|
||||
BOOST_CHECK(tx.vout.size() == 1);
|
||||
CTxOut out = tx.vout[0];
|
||||
BOOST_CHECK_EQUAL(out.nValue, amount);
|
||||
|
||||
|
||||
amount = 1.111;
|
||||
proxy.add_taddr_change_output_to_tx(amount);
|
||||
tx = proxy.getTx();
|
||||
|
@ -1073,7 +1075,7 @@ BOOST_AUTO_TEST_CASE(rpc_z_sendmany_internals)
|
|||
out = tx.vout[1];
|
||||
BOOST_CHECK_EQUAL(out.nValue, amount);
|
||||
}
|
||||
|
||||
|
||||
// add_taddr_outputs_to_tx() will append many vouts to a raw transaction
|
||||
{
|
||||
std::vector<SendManyRecipient> recipients = {
|
||||
|
@ -1084,33 +1086,33 @@ BOOST_AUTO_TEST_CASE(rpc_z_sendmany_internals)
|
|||
std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany(zaddr1, recipients, {}, 1) );
|
||||
std::shared_ptr<AsyncRPCOperation_sendmany> ptr = std::dynamic_pointer_cast<AsyncRPCOperation_sendmany> (operation);
|
||||
TEST_FRIEND_AsyncRPCOperation_sendmany proxy(ptr);
|
||||
|
||||
|
||||
proxy.add_taddr_outputs_to_tx();
|
||||
|
||||
|
||||
CTransaction tx = proxy.getTx();
|
||||
BOOST_CHECK(tx.vout.size() == 3);
|
||||
BOOST_CHECK_EQUAL(tx.vout[0].nValue, CAmount(1.23));
|
||||
BOOST_CHECK_EQUAL(tx.vout[1].nValue, CAmount(4.56));
|
||||
BOOST_CHECK_EQUAL(tx.vout[2].nValue, CAmount(7.89));
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Raw joinsplit is a zaddr->zaddr
|
||||
{
|
||||
std::string raw = "020000000000000000000100000000000000001027000000000000183a0d4c46c369078705e39bcfebee59a978dbd210ce8de3efc9555a03fbabfd3cea16693d730c63850d7e48ccde79854c19adcb7e9dcd7b7d18805ee09083f6b16e1860729d2d4a90e2f2acd009cf78b5eb0f4a6ee4bdb64b1262d7ce9eb910c460b02022991e968d0c50ee44908e4ccccbc591d0053bcca154dd6d6fc400a29fa686af4682339832ccea362a62aeb9df0d5aa74f86a1e75ac0f48a8ccc41e0a940643c6c33e1d09223b0a46eaf47a1bb4407cfc12b1dcf83a29c0cef51e45c7876ca5b9e5bae86d92976eb3ef68f29cd29386a8be8451b50f82bf9da10c04651868655194da8f6ed3d241bb5b5ff93a3e2bbe44644544d88bcde5cc35978032ee92699c7a61fcbb395e7583f47e698c4d53ede54f956629400bf510fb5e22d03158cc10bdcaaf29e418ef18eb6480dd9c8b9e2a377809f9f32a556ef872febd0021d4ad013aa9f0b7255e98e408d302abefd33a71180b720271835b487ab309e160b06dfe51932120fb84a7ede16b20c53599a11071592109e10260f265ee60d48c62bfe24074020e9b586ce9e9356e68f2ad1a9538258234afe4b83a209f178f45202270eaeaeecaf2ce3100b2c5a714f75f35777a9ebff5ebf47059d2bbf6f3726190216468f2b152673b766225b093f3a2f827c86d6b48b42117fec1d0ac38dd7af700308dcfb02eba821612b16a2c164c47715b9b0c93900893b1aba2ea03765c94d87022db5be06ab338d1912e0936dfe87586d0a8ee49144a6cd2e306abdcb652faa3e0222739deb23154d778b50de75069a4a2cce1208cd1ced3cb4744c9888ce1c2fcd2e66dc31e62d3aa9e423d7275882525e9981f92e84ac85975b8660739407efbe1e34c2249420fde7e17db3096d5b22e83d051d01f0e6e7690dca7d168db338aadf0897fedac10de310db2b1bff762d322935dddbb60c2efb8b15d231fa17b84630371cb275c209f0c4c7d0c68b150ea5cd514122215e3f7fcfb351d69514788d67c2f3c8922581946e3a04bdf1f07f15696ca76eb95b10698bf1188fd882945c57657515889d042a6fc45d38cbc943540c4f0f6d1c45a1574c81f3e42d1eb8702328b729909adee8a5cfed7c79d54627d1fd389af941d878376f7927b9830ca659bf9ab18c5ca5192d52d02723008728d03701b8ab3e1c4a3109409ec0b13df334c7deec3523eeef4c97b5603e643de3a647b873f4c1b47fbfc6586ba66724f112e51fc93839648005043620aa3ce458e246d77977b19c53d98e3e812de006afc1a79744df236582943631d04cc02941ac4be500e4ed9fb9e3e7cc187b1c4050fad1d9d09d5fd70d5d01d615b439d8c0015d2eb10398bcdbf8c4b2bd559dbe4c288a186aed3f86f608da4d582e120c4a896e015e2241900d1daeccd05db968852677c71d752bec46de9962174b46f980e8cc603654daf8b98a3ee92dac066033954164a89568b70b1780c2ce2410b2f816dbeddb2cd463e0c8f21a52cf6427d9647a6fd4bafa8fb4cd4d47ac057b0160bee86c6b2fb8adce214c2bcdda277512200adf0eaa5d2114a2c077b009836a68ec254bfe56f51d147b9afe2ddd9cb917c0c2de19d81b7b8fd9f4574f51fa1207630dc13976f4d7587c962f761af267de71f3909a576e6bedaf6311633910d291ac292c467cc8331ef577aef7646a5d949322fa0367a49f20597a13def53136ee31610395e3e48d291fd8f58504374031fe9dcfba5e06086ebcf01a9106f6a4d6e16e19e4c5bb893f7da79419c94eca31a384be6fa1747284dee0fc3bbc8b1b860172c10b29c1594bb8c747d7fe05827358ff2160f49050001625ffe2e880bd7fc26cd0ffd89750745379a8e862816e08a5a2008043921ab6a4976064ac18f7ee37b6628bc0127d8d5ebd3548e41d8881a082d86f20b32e33094f15a0e6ea6074b08c6cd28142de94713451640a55985051f5577eb54572699d838cb34a79c8939e981c0c277d06a6e2ce69ccb74f8a691ff08f81d8b99e6a86223d29a2b7c8e7b041aba44ea678ae654277f7e91cbfa79158b989164a3d549d9f4feb0cc43169699c13e321fe3f4b94258c75d198ff9184269cd6986c55409e07528c93f64942c6c283ce3917b4bf4c3be2fe3173c8c38cccb35f1fbda0ca88b35a599c0678cb22aa8eabea8249dbd2e4f849fffe69803d299e435ebcd7df95854003d8eda17a74d98b4be0e62d45d7fe48c06a6f464a14f8e0570077cc631279092802a89823f031eef5e1028a6d6fdbd502869a731ee7d28b4d6c71b419462a30d31442d3ee444ffbcbd16d558c9000c97e949c2b1f9d6f6d8db7b9131ebd963620d3fc8595278d6f8fdf49084325373196d53e64142fa5a23eccd6ef908c4d80b8b3e6cc334b7f7012103a3682e4678e9b518163d262a39a2c1a69bf88514c52b7ccd7cc8dc80e71f7c2ec0701cff982573eb0c2c4daeb47fa0b586f4451c10d1da2e5d182b03dd067a5e971b3a6138ca6667aaf853d2ac03b80a1d5870905f2cfb6c78ec3c3719c02f973d638a0f973424a2b0f2b0023f136d60092fe15fba4bc180b9176bd0ff576e053f1af6939fe9ca256203ffaeb3e569f09774d2a6cbf91873e56651f4d6ff77e0b5374b0a1a201d7e523604e0247644544cc571d48c458a4f96f45580b";
|
||||
UniValue obj(UniValue::VOBJ);
|
||||
obj.push_back(Pair("rawtxn", raw));
|
||||
|
||||
|
||||
// we have the spending key for the dummy recipient zaddr1
|
||||
std::vector<SendManyRecipient> recipients = { SendManyRecipient(zaddr1, 0.0005, "ABCD") };
|
||||
|
||||
|
||||
std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany(zaddr1, {}, recipients, 1) );
|
||||
std::shared_ptr<AsyncRPCOperation_sendmany> ptr = std::dynamic_pointer_cast<AsyncRPCOperation_sendmany> (operation);
|
||||
TEST_FRIEND_AsyncRPCOperation_sendmany proxy(ptr);
|
||||
|
||||
|
||||
// Enable test mode so tx is not sent
|
||||
static_cast<AsyncRPCOperation_sendmany *>(operation.get())->testmode = true;
|
||||
|
||||
|
||||
// Pretend that the operation completed successfully
|
||||
proxy.set_state(OperationStatus::SUCCESS);
|
||||
|
||||
|
@ -1122,21 +1124,21 @@ BOOST_AUTO_TEST_CASE(rpc_z_sendmany_internals)
|
|||
std::string hex = find_value(resultObj, "hex").get_str();
|
||||
BOOST_CHECK_EQUAL(hex, raw);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// Test the perform_joinsplit methods.
|
||||
{
|
||||
// Dummy input so the operation object can be instantiated.
|
||||
std::vector<SendManyRecipient> recipients = { SendManyRecipient(zaddr1, 0.0005, "ABCD") };
|
||||
|
||||
|
||||
std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany(zaddr1, {}, recipients, 1) );
|
||||
std::shared_ptr<AsyncRPCOperation_sendmany> ptr = std::dynamic_pointer_cast<AsyncRPCOperation_sendmany> (operation);
|
||||
TEST_FRIEND_AsyncRPCOperation_sendmany proxy(ptr);
|
||||
TEST_FRIEND_AsyncRPCOperation_sendmany proxy(ptr);
|
||||
|
||||
// Enable test mode so tx is not sent and proofs are not generated
|
||||
static_cast<AsyncRPCOperation_sendmany *>(operation.get())->testmode = true;
|
||||
|
||||
AsyncJoinSplitInfo info;
|
||||
|
||||
AsyncJoinSplitInfo info;
|
||||
std::vector<boost::optional < ZCIncrementalWitness>> witnesses;
|
||||
uint256 anchor;
|
||||
try {
|
||||
|
@ -1158,7 +1160,7 @@ BOOST_AUTO_TEST_CASE(rpc_z_sendmany_internals)
|
|||
} catch (const std::runtime_error & e) {
|
||||
BOOST_CHECK( string(e.what()).find("number of notes")!= string::npos);
|
||||
}
|
||||
|
||||
|
||||
info.notes.clear();
|
||||
info.vjsin.push_back(JSInput());
|
||||
info.vjsin.push_back(JSInput());
|
||||
|
@ -1168,7 +1170,7 @@ BOOST_AUTO_TEST_CASE(rpc_z_sendmany_internals)
|
|||
} catch (const std::runtime_error & e) {
|
||||
BOOST_CHECK( string(e.what()).find("unsupported joinsplit input")!= string::npos);
|
||||
}
|
||||
|
||||
|
||||
info.vjsin.clear();
|
||||
try {
|
||||
proxy.perform_joinsplit(info);
|
||||
|
@ -1176,7 +1178,7 @@ BOOST_AUTO_TEST_CASE(rpc_z_sendmany_internals)
|
|||
BOOST_CHECK( string(e.what()).find("JoinSplit verifying key not loaded")!= string::npos);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -1214,29 +1216,160 @@ BOOST_AUTO_TEST_CASE(rpc_wallet_encrypted_wallet_zkeys)
|
|||
|
||||
boost::filesystem::current_path(GetArg("-datadir","/tmp/thisshouldnothappen"));
|
||||
BOOST_CHECK(pwalletMain->EncryptWallet(strWalletPass));
|
||||
|
||||
|
||||
// Verify we can still list the keys imported
|
||||
BOOST_CHECK_NO_THROW(retValue = CallRPC("z_listaddresses"));
|
||||
arr = retValue.get_array();
|
||||
BOOST_CHECK(arr.size() == n);
|
||||
|
||||
|
||||
// Try to add a new key, but we can't as the wallet is locked
|
||||
BOOST_CHECK_THROW(CallRPC("z_getnewaddress"), runtime_error);
|
||||
|
||||
|
||||
// We can't call RPC walletpassphrase as that invokes RPCRunLater which breaks tests.
|
||||
// So we manually unlock.
|
||||
BOOST_CHECK(pwalletMain->Unlock(strWalletPass));
|
||||
|
||||
|
||||
// Now add a key
|
||||
BOOST_CHECK_NO_THROW(CallRPC("z_getnewaddress"));
|
||||
|
||||
|
||||
// Verify the key has been added
|
||||
BOOST_CHECK_NO_THROW(retValue = CallRPC("z_listaddresses"));
|
||||
arr = retValue.get_array();
|
||||
BOOST_CHECK(arr.size() == n+1);
|
||||
BOOST_CHECK(arr.size() == n+1);
|
||||
|
||||
// We can't simulate over RPC the wallet closing and being reloaded
|
||||
// but there are tests for this in gtest.
|
||||
}
|
||||
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(rpc_z_shieldcoinbase_parameters)
|
||||
{
|
||||
SelectParams(CBaseChainParams::TESTNET);
|
||||
|
||||
LOCK(pwalletMain->cs_wallet);
|
||||
|
||||
BOOST_CHECK_THROW(CallRPC("z_shieldcoinbase"), runtime_error);
|
||||
BOOST_CHECK_THROW(CallRPC("z_shieldcoinbase toofewargs"), runtime_error);
|
||||
BOOST_CHECK_THROW(CallRPC("z_shieldcoinbase too many args here"), runtime_error);
|
||||
|
||||
// bad from address
|
||||
BOOST_CHECK_THROW(CallRPC("z_shieldcoinbase "
|
||||
"INVALIDtmRr6yJonqGK23UVhrKuyvTpF8qxQQjKigJ tnpoQJVnYBZZqkFadj2bJJLThNCxbADGB5gSGeYTAGGrT5tejsxY9Zc1BtY8nnHmZkB"), runtime_error);
|
||||
|
||||
// bad from address
|
||||
BOOST_CHECK_THROW(CallRPC("z_shieldcoinbase "
|
||||
"** tnpoQJVnYBZZqkFadj2bJJLThNCxbADGB5gSGeYTAGGrT5tejsxY9Zc1BtY8nnHmZkB"), runtime_error);
|
||||
|
||||
// bad to address
|
||||
BOOST_CHECK_THROW(CallRPC("z_shieldcoinbase "
|
||||
"tmRr6yJonqGK23UVhrKuyvTpF8qxQQjKigJ INVALIDtnpoQJVnYBZZqkFadj2bJJLThNCxbADGB5gSGeYTAGGrT5tejsxY9Zc1BtY8nnHmZkB"), runtime_error);
|
||||
|
||||
// invalid fee amount, cannot be negative
|
||||
BOOST_CHECK_THROW(CallRPC("z_shieldcoinbase "
|
||||
"tmRr6yJonqGK23UVhrKuyvTpF8qxQQjKigJ "
|
||||
"tnpoQJVnYBZZqkFadj2bJJLThNCxbADGB5gSGeYTAGGrT5tejsxY9Zc1BtY8nnHmZkB "
|
||||
"-0.0001"
|
||||
), runtime_error);
|
||||
|
||||
// invalid fee amount, bigger than MAX_MONEY
|
||||
BOOST_CHECK_THROW(CallRPC("z_shieldcoinbase "
|
||||
"tmRr6yJonqGK23UVhrKuyvTpF8qxQQjKigJ "
|
||||
"tnpoQJVnYBZZqkFadj2bJJLThNCxbADGB5gSGeYTAGGrT5tejsxY9Zc1BtY8nnHmZkB "
|
||||
"21000001"
|
||||
), runtime_error);
|
||||
|
||||
// Test constructor of AsyncRPCOperation_sendmany
|
||||
std::string testnetzaddr = "ztjiDe569DPNbyTE6TSdJTaSDhoXEHLGvYoUnBU1wfVNU52TEyT6berYtySkd21njAeEoh8fFJUT42kua9r8EnhBaEKqCpP";
|
||||
std::string mainnetzaddr = "zcMuhvq8sEkHALuSU2i4NbNQxshSAYrpCExec45ZjtivYPbuiFPwk6WHy4SvsbeZ4siy1WheuRGjtaJmoD1J8bFqNXhsG6U";
|
||||
|
||||
try {
|
||||
std::shared_ptr<AsyncRPCOperation> operation(new AsyncRPCOperation_shieldcoinbase({}, testnetzaddr, -1 ));
|
||||
} catch (const UniValue& objError) {
|
||||
BOOST_CHECK( find_error(objError, "Fee is out of range"));
|
||||
}
|
||||
|
||||
try {
|
||||
std::shared_ptr<AsyncRPCOperation> operation(new AsyncRPCOperation_shieldcoinbase({}, testnetzaddr, 1));
|
||||
} catch (const UniValue& objError) {
|
||||
BOOST_CHECK( find_error(objError, "Empty inputs"));
|
||||
}
|
||||
|
||||
// Testnet payment addresses begin with 'zt'. This test detects an incorrect prefix.
|
||||
try {
|
||||
std::vector<ShieldCoinbaseUTXO> inputs = { ShieldCoinbaseUTXO{uint256(),0,0} };
|
||||
std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_shieldcoinbase(inputs, mainnetzaddr, 1) );
|
||||
} catch (const UniValue& objError) {
|
||||
BOOST_CHECK( find_error(objError, "payment address is for wrong network type"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(rpc_z_shieldcoinbase_internals)
|
||||
{
|
||||
SelectParams(CBaseChainParams::TESTNET);
|
||||
|
||||
LOCK(pwalletMain->cs_wallet);
|
||||
|
||||
UniValue retValue;
|
||||
|
||||
// Test that option -mempooltxinputlimit is respected.
|
||||
mapArgs["-mempooltxinputlimit"] = "1";
|
||||
|
||||
// Add keys manually
|
||||
CZCPaymentAddress pa = pwalletMain->GenerateNewZKey();
|
||||
std::string zaddr = pa.ToString();
|
||||
|
||||
// Supply 2 inputs when mempool limit is 1
|
||||
{
|
||||
std::vector<ShieldCoinbaseUTXO> inputs = { ShieldCoinbaseUTXO{uint256(),0,0}, ShieldCoinbaseUTXO{uint256(),0,0} };
|
||||
std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_shieldcoinbase(inputs, zaddr) );
|
||||
operation->main();
|
||||
BOOST_CHECK(operation->isFailed());
|
||||
std::string msg = operation->getErrorMessage();
|
||||
BOOST_CHECK( msg.find("Number of inputs 2 is greater than mempooltxinputlimit of 1") != string::npos);
|
||||
}
|
||||
|
||||
// Insufficient funds
|
||||
{
|
||||
std::vector<ShieldCoinbaseUTXO> inputs = { ShieldCoinbaseUTXO{uint256(),0,0} };
|
||||
std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_shieldcoinbase(inputs, zaddr) );
|
||||
operation->main();
|
||||
BOOST_CHECK(operation->isFailed());
|
||||
std::string msg = operation->getErrorMessage();
|
||||
BOOST_CHECK( msg.find("Insufficient coinbase funds") != string::npos);
|
||||
}
|
||||
|
||||
// Test the perform_joinsplit methods.
|
||||
{
|
||||
// Dummy input so the operation object can be instantiated.
|
||||
std::vector<ShieldCoinbaseUTXO> inputs = { ShieldCoinbaseUTXO{uint256(),0,100000} };
|
||||
std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_shieldcoinbase(inputs, zaddr) );
|
||||
std::shared_ptr<AsyncRPCOperation_shieldcoinbase> ptr = std::dynamic_pointer_cast<AsyncRPCOperation_shieldcoinbase> (operation);
|
||||
TEST_FRIEND_AsyncRPCOperation_shieldcoinbase proxy(ptr);
|
||||
static_cast<AsyncRPCOperation_shieldcoinbase *>(operation.get())->testmode = true;
|
||||
|
||||
ShieldCoinbaseJSInfo info;
|
||||
info.vjsin.push_back(JSInput());
|
||||
info.vjsin.push_back(JSInput());
|
||||
info.vjsin.push_back(JSInput());
|
||||
try {
|
||||
proxy.perform_joinsplit(info);
|
||||
} catch (const std::runtime_error & e) {
|
||||
BOOST_CHECK( string(e.what()).find("unsupported joinsplit input")!= string::npos);
|
||||
}
|
||||
|
||||
info.vjsin.clear();
|
||||
try {
|
||||
proxy.perform_joinsplit(info);
|
||||
} catch (const std::runtime_error & e) {
|
||||
BOOST_CHECK( string(e.what()).find("JoinSplit verifying key not loaded")!= string::npos);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
|
|
@ -0,0 +1,441 @@
|
|||
// Copyright (c) 2017 The Zcash developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include "asyncrpcqueue.h"
|
||||
#include "amount.h"
|
||||
#include "core_io.h"
|
||||
#include "init.h"
|
||||
#include "main.h"
|
||||
#include "net.h"
|
||||
#include "netbase.h"
|
||||
#include "rpcserver.h"
|
||||
#include "timedata.h"
|
||||
#include "util.h"
|
||||
#include "utilmoneystr.h"
|
||||
#include "wallet.h"
|
||||
#include "walletdb.h"
|
||||
#include "script/interpreter.h"
|
||||
#include "utiltime.h"
|
||||
#include "rpcprotocol.h"
|
||||
#include "zcash/IncrementalMerkleTree.hpp"
|
||||
#include "sodium.h"
|
||||
#include "miner.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
#include <string>
|
||||
|
||||
#include "asyncrpcoperation_shieldcoinbase.h"
|
||||
|
||||
using namespace libzcash;
|
||||
|
||||
static int find_output(UniValue obj, int n) {
|
||||
UniValue outputMapValue = find_value(obj, "outputmap");
|
||||
if (!outputMapValue.isArray()) {
|
||||
throw JSONRPCError(RPC_WALLET_ERROR, "Missing outputmap for JoinSplit operation");
|
||||
}
|
||||
|
||||
UniValue outputMap = outputMapValue.get_array();
|
||||
assert(outputMap.size() == ZC_NUM_JS_OUTPUTS);
|
||||
for (size_t i = 0; i < outputMap.size(); i++) {
|
||||
if (outputMap[i].get_int() == n) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
throw std::logic_error("n is not present in outputmap");
|
||||
}
|
||||
|
||||
AsyncRPCOperation_shieldcoinbase::AsyncRPCOperation_shieldcoinbase(
|
||||
std::vector<ShieldCoinbaseUTXO> inputs,
|
||||
std::string toAddress,
|
||||
CAmount fee,
|
||||
UniValue contextInfo) :
|
||||
inputs_(inputs), fee_(fee), contextinfo_(contextInfo)
|
||||
{
|
||||
if (fee < 0 || fee > MAX_MONEY) {
|
||||
throw JSONRPCError(RPC_INVALID_PARAMETER, "Fee is out of range");
|
||||
}
|
||||
|
||||
if (inputs.size() == 0) {
|
||||
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Empty inputs");
|
||||
}
|
||||
|
||||
// Check the destination address is valid for this network i.e. not testnet being used on mainnet
|
||||
CZCPaymentAddress address(toAddress);
|
||||
try {
|
||||
tozaddr_ = address.Get();
|
||||
} catch (const std::runtime_error& e) {
|
||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, string("runtime error: ") + e.what());
|
||||
}
|
||||
|
||||
// Log the context info
|
||||
if (LogAcceptCategory("zrpcunsafe")) {
|
||||
LogPrint("zrpcunsafe", "%s: z_shieldcoinbase initialized (context=%s)\n", getId(), contextInfo.write());
|
||||
} else {
|
||||
LogPrint("zrpc", "%s: z_shieldcoinbase initialized\n", getId());
|
||||
}
|
||||
|
||||
// Lock UTXOs
|
||||
lock_utxos();
|
||||
}
|
||||
|
||||
AsyncRPCOperation_shieldcoinbase::~AsyncRPCOperation_shieldcoinbase() {
|
||||
}
|
||||
|
||||
void AsyncRPCOperation_shieldcoinbase::main() {
|
||||
if (isCancelled()) {
|
||||
unlock_utxos(); // clean up
|
||||
return;
|
||||
}
|
||||
|
||||
set_state(OperationStatus::EXECUTING);
|
||||
start_execution_clock();
|
||||
|
||||
bool success = false;
|
||||
|
||||
#ifdef ENABLE_MINING
|
||||
#ifdef ENABLE_WALLET
|
||||
GenerateBitcoins(false, NULL, 0);
|
||||
#else
|
||||
GenerateBitcoins(false, 0);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
try {
|
||||
success = main_impl();
|
||||
} catch (const UniValue& objError) {
|
||||
int code = find_value(objError, "code").get_int();
|
||||
std::string message = find_value(objError, "message").get_str();
|
||||
set_error_code(code);
|
||||
set_error_message(message);
|
||||
} catch (const runtime_error& e) {
|
||||
set_error_code(-1);
|
||||
set_error_message("runtime error: " + string(e.what()));
|
||||
} catch (const logic_error& e) {
|
||||
set_error_code(-1);
|
||||
set_error_message("logic error: " + string(e.what()));
|
||||
} catch (const exception& e) {
|
||||
set_error_code(-1);
|
||||
set_error_message("general exception: " + string(e.what()));
|
||||
} catch (...) {
|
||||
set_error_code(-2);
|
||||
set_error_message("unknown error");
|
||||
}
|
||||
|
||||
#ifdef ENABLE_MINING
|
||||
#ifdef ENABLE_WALLET
|
||||
GenerateBitcoins(GetBoolArg("-gen",false), pwalletMain, GetArg("-genproclimit", 1));
|
||||
#else
|
||||
GenerateBitcoins(GetBoolArg("-gen",false), GetArg("-genproclimit", 1));
|
||||
#endif
|
||||
#endif
|
||||
|
||||
stop_execution_clock();
|
||||
|
||||
if (success) {
|
||||
set_state(OperationStatus::SUCCESS);
|
||||
} else {
|
||||
set_state(OperationStatus::FAILED);
|
||||
}
|
||||
|
||||
std::string s = strprintf("%s: z_shieldcoinbase finished (status=%s", getId(), getStateAsString());
|
||||
if (success) {
|
||||
s += strprintf(", txid=%s)\n", tx_.GetHash().ToString());
|
||||
} else {
|
||||
s += strprintf(", error=%s)\n", getErrorMessage());
|
||||
}
|
||||
LogPrintf("%s",s);
|
||||
|
||||
unlock_utxos(); // clean up
|
||||
}
|
||||
|
||||
|
||||
bool AsyncRPCOperation_shieldcoinbase::main_impl() {
|
||||
|
||||
CAmount minersFee = fee_;
|
||||
|
||||
size_t numInputs = inputs_.size();
|
||||
|
||||
// Check mempooltxinputlimit to avoid creating a transaction which the local mempool rejects
|
||||
size_t limit = (size_t)GetArg("-mempooltxinputlimit", 0);
|
||||
if (limit>0 && numInputs > limit) {
|
||||
throw JSONRPCError(RPC_WALLET_ERROR,
|
||||
strprintf("Number of inputs %d is greater than mempooltxinputlimit of %d",
|
||||
numInputs, limit));
|
||||
}
|
||||
|
||||
CAmount targetAmount = 0;
|
||||
for (ShieldCoinbaseUTXO & utxo : inputs_) {
|
||||
targetAmount += utxo.amount;
|
||||
}
|
||||
|
||||
if (targetAmount <= minersFee) {
|
||||
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS,
|
||||
strprintf("Insufficient coinbase funds, have %s and miners fee is %s",
|
||||
FormatMoney(targetAmount), FormatMoney(minersFee)));
|
||||
}
|
||||
|
||||
CAmount sendAmount = targetAmount - minersFee;
|
||||
LogPrint("zrpc", "%s: spending %s to shield %s with fee %s\n",
|
||||
getId(), FormatMoney(targetAmount), FormatMoney(sendAmount), FormatMoney(minersFee));
|
||||
|
||||
// update the transaction with these inputs
|
||||
CMutableTransaction rawTx(tx_);
|
||||
for (ShieldCoinbaseUTXO & t : inputs_) {
|
||||
CTxIn in(COutPoint(t.txid, t.vout));
|
||||
rawTx.vin.push_back(in);
|
||||
}
|
||||
tx_ = CTransaction(rawTx);
|
||||
|
||||
// Prepare raw transaction to handle JoinSplits
|
||||
CMutableTransaction mtx(tx_);
|
||||
mtx.nVersion = 2;
|
||||
crypto_sign_keypair(joinSplitPubKey_.begin(), joinSplitPrivKey_);
|
||||
mtx.joinSplitPubKey = joinSplitPubKey_;
|
||||
tx_ = CTransaction(mtx);
|
||||
|
||||
// Create joinsplit
|
||||
UniValue obj(UniValue::VOBJ);
|
||||
ShieldCoinbaseJSInfo info;
|
||||
info.vpub_old = sendAmount;
|
||||
info.vpub_new = 0;
|
||||
JSOutput jso = JSOutput(tozaddr_, sendAmount);
|
||||
info.vjsout.push_back(jso);
|
||||
obj = perform_joinsplit(info);
|
||||
|
||||
sign_send_raw_transaction(obj);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sign and send a raw transaction.
|
||||
* Raw transaction as hex string should be in object field "rawtxn"
|
||||
*/
|
||||
void AsyncRPCOperation_shieldcoinbase::sign_send_raw_transaction(UniValue obj)
|
||||
{
|
||||
// Sign the raw transaction
|
||||
UniValue rawtxnValue = find_value(obj, "rawtxn");
|
||||
if (rawtxnValue.isNull()) {
|
||||
throw JSONRPCError(RPC_WALLET_ERROR, "Missing hex data for raw transaction");
|
||||
}
|
||||
std::string rawtxn = rawtxnValue.get_str();
|
||||
|
||||
UniValue params = UniValue(UniValue::VARR);
|
||||
params.push_back(rawtxn);
|
||||
UniValue signResultValue = signrawtransaction(params, false);
|
||||
UniValue signResultObject = signResultValue.get_obj();
|
||||
UniValue completeValue = find_value(signResultObject, "complete");
|
||||
bool complete = completeValue.get_bool();
|
||||
if (!complete) {
|
||||
// TODO: #1366 Maybe get "errors" and print array vErrors into a string
|
||||
throw JSONRPCError(RPC_WALLET_ENCRYPTION_FAILED, "Failed to sign transaction");
|
||||
}
|
||||
|
||||
UniValue hexValue = find_value(signResultObject, "hex");
|
||||
if (hexValue.isNull()) {
|
||||
throw JSONRPCError(RPC_WALLET_ERROR, "Missing hex data for signed transaction");
|
||||
}
|
||||
std::string signedtxn = hexValue.get_str();
|
||||
|
||||
// Send the signed transaction
|
||||
if (!testmode) {
|
||||
params.clear();
|
||||
params.setArray();
|
||||
params.push_back(signedtxn);
|
||||
UniValue sendResultValue = sendrawtransaction(params, false);
|
||||
if (sendResultValue.isNull()) {
|
||||
throw JSONRPCError(RPC_WALLET_ERROR, "Send raw transaction did not return an error or a txid.");
|
||||
}
|
||||
|
||||
std::string txid = sendResultValue.get_str();
|
||||
|
||||
UniValue o(UniValue::VOBJ);
|
||||
o.push_back(Pair("txid", txid));
|
||||
set_result(o);
|
||||
} else {
|
||||
// Test mode does not send the transaction to the network.
|
||||
|
||||
CDataStream stream(ParseHex(signedtxn), SER_NETWORK, PROTOCOL_VERSION);
|
||||
CTransaction tx;
|
||||
stream >> tx;
|
||||
|
||||
UniValue o(UniValue::VOBJ);
|
||||
o.push_back(Pair("test", 1));
|
||||
o.push_back(Pair("txid", tx.GetHash().ToString()));
|
||||
o.push_back(Pair("hex", signedtxn));
|
||||
set_result(o);
|
||||
}
|
||||
|
||||
// Keep the signed transaction so we can hash to the same txid
|
||||
CDataStream stream(ParseHex(signedtxn), SER_NETWORK, PROTOCOL_VERSION);
|
||||
CTransaction tx;
|
||||
stream >> tx;
|
||||
tx_ = tx;
|
||||
}
|
||||
|
||||
|
||||
UniValue AsyncRPCOperation_shieldcoinbase::perform_joinsplit(ShieldCoinbaseJSInfo & info) {
|
||||
uint256 anchor = pcoinsTip->GetBestAnchor();
|
||||
if (anchor.IsNull()) {
|
||||
throw std::runtime_error("anchor is null");
|
||||
}
|
||||
|
||||
// Make sure there are two inputs and two outputs
|
||||
while (info.vjsin.size() < ZC_NUM_JS_INPUTS) {
|
||||
info.vjsin.push_back(JSInput());
|
||||
}
|
||||
|
||||
while (info.vjsout.size() < ZC_NUM_JS_OUTPUTS) {
|
||||
info.vjsout.push_back(JSOutput());
|
||||
}
|
||||
|
||||
if (info.vjsout.size() != ZC_NUM_JS_INPUTS || info.vjsin.size() != ZC_NUM_JS_OUTPUTS) {
|
||||
throw runtime_error("unsupported joinsplit input/output counts");
|
||||
}
|
||||
|
||||
CMutableTransaction mtx(tx_);
|
||||
|
||||
LogPrint("zrpcunsafe", "%s: creating joinsplit at index %d (vpub_old=%s, vpub_new=%s, in[0]=%s, in[1]=%s, out[0]=%s, out[1]=%s)\n",
|
||||
getId(),
|
||||
tx_.vjoinsplit.size(),
|
||||
FormatMoney(info.vpub_old), FormatMoney(info.vpub_new),
|
||||
FormatMoney(info.vjsin[0].note.value), FormatMoney(info.vjsin[1].note.value),
|
||||
FormatMoney(info.vjsout[0].value), FormatMoney(info.vjsout[1].value)
|
||||
);
|
||||
|
||||
// Generate the proof, this can take over a minute.
|
||||
boost::array<libzcash::JSInput, ZC_NUM_JS_INPUTS> inputs
|
||||
{info.vjsin[0], info.vjsin[1]};
|
||||
boost::array<libzcash::JSOutput, ZC_NUM_JS_OUTPUTS> outputs
|
||||
{info.vjsout[0], info.vjsout[1]};
|
||||
boost::array<size_t, ZC_NUM_JS_INPUTS> inputMap;
|
||||
boost::array<size_t, ZC_NUM_JS_OUTPUTS> outputMap;
|
||||
JSDescription jsdesc = JSDescription::Randomized(
|
||||
*pzcashParams,
|
||||
joinSplitPubKey_,
|
||||
anchor,
|
||||
inputs,
|
||||
outputs,
|
||||
inputMap,
|
||||
outputMap,
|
||||
info.vpub_old,
|
||||
info.vpub_new,
|
||||
!this->testmode);
|
||||
|
||||
{
|
||||
auto verifier = libzcash::ProofVerifier::Strict();
|
||||
if (!(jsdesc.Verify(*pzcashParams, verifier, joinSplitPubKey_))) {
|
||||
throw std::runtime_error("error verifying joinsplit");
|
||||
}
|
||||
}
|
||||
|
||||
mtx.vjoinsplit.push_back(jsdesc);
|
||||
|
||||
// Empty output script.
|
||||
CScript scriptCode;
|
||||
CTransaction signTx(mtx);
|
||||
uint256 dataToBeSigned = SignatureHash(scriptCode, signTx, NOT_AN_INPUT, SIGHASH_ALL);
|
||||
|
||||
// Add the signature
|
||||
if (!(crypto_sign_detached(&mtx.joinSplitSig[0], NULL,
|
||||
dataToBeSigned.begin(), 32,
|
||||
joinSplitPrivKey_
|
||||
) == 0))
|
||||
{
|
||||
throw std::runtime_error("crypto_sign_detached failed");
|
||||
}
|
||||
|
||||
// Sanity check
|
||||
if (!(crypto_sign_verify_detached(&mtx.joinSplitSig[0],
|
||||
dataToBeSigned.begin(), 32,
|
||||
mtx.joinSplitPubKey.begin()
|
||||
) == 0))
|
||||
{
|
||||
throw std::runtime_error("crypto_sign_verify_detached failed");
|
||||
}
|
||||
|
||||
CTransaction rawTx(mtx);
|
||||
tx_ = rawTx;
|
||||
|
||||
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
|
||||
ss << rawTx;
|
||||
|
||||
std::string encryptedNote1;
|
||||
std::string encryptedNote2;
|
||||
{
|
||||
CDataStream ss2(SER_NETWORK, PROTOCOL_VERSION);
|
||||
ss2 << ((unsigned char) 0x00);
|
||||
ss2 << jsdesc.ephemeralKey;
|
||||
ss2 << jsdesc.ciphertexts[0];
|
||||
ss2 << jsdesc.h_sig(*pzcashParams, joinSplitPubKey_);
|
||||
|
||||
encryptedNote1 = HexStr(ss2.begin(), ss2.end());
|
||||
}
|
||||
{
|
||||
CDataStream ss2(SER_NETWORK, PROTOCOL_VERSION);
|
||||
ss2 << ((unsigned char) 0x01);
|
||||
ss2 << jsdesc.ephemeralKey;
|
||||
ss2 << jsdesc.ciphertexts[1];
|
||||
ss2 << jsdesc.h_sig(*pzcashParams, joinSplitPubKey_);
|
||||
|
||||
encryptedNote2 = HexStr(ss2.begin(), ss2.end());
|
||||
}
|
||||
|
||||
UniValue arrInputMap(UniValue::VARR);
|
||||
UniValue arrOutputMap(UniValue::VARR);
|
||||
for (size_t i = 0; i < ZC_NUM_JS_INPUTS; i++) {
|
||||
arrInputMap.push_back(inputMap[i]);
|
||||
}
|
||||
for (size_t i = 0; i < ZC_NUM_JS_OUTPUTS; i++) {
|
||||
arrOutputMap.push_back(outputMap[i]);
|
||||
}
|
||||
|
||||
UniValue obj(UniValue::VOBJ);
|
||||
obj.push_back(Pair("encryptednote1", encryptedNote1));
|
||||
obj.push_back(Pair("encryptednote2", encryptedNote2));
|
||||
obj.push_back(Pair("rawtxn", HexStr(ss.begin(), ss.end())));
|
||||
obj.push_back(Pair("inputmap", arrInputMap));
|
||||
obj.push_back(Pair("outputmap", arrOutputMap));
|
||||
return obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override getStatus() to append the operation's context object to the default status object.
|
||||
*/
|
||||
UniValue AsyncRPCOperation_shieldcoinbase::getStatus() const {
|
||||
UniValue v = AsyncRPCOperation::getStatus();
|
||||
if (contextinfo_.isNull()) {
|
||||
return v;
|
||||
}
|
||||
|
||||
UniValue obj = v.get_obj();
|
||||
obj.push_back(Pair("method", "z_shieldcoinbase"));
|
||||
obj.push_back(Pair("params", contextinfo_ ));
|
||||
return obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lock input utxos
|
||||
*/
|
||||
void AsyncRPCOperation_shieldcoinbase::lock_utxos() {
|
||||
LOCK2(cs_main, pwalletMain->cs_wallet);
|
||||
for (auto utxo : inputs_) {
|
||||
COutPoint outpt(utxo.txid, utxo.vout);
|
||||
pwalletMain->LockCoin(outpt);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unlock input utxos
|
||||
*/
|
||||
void AsyncRPCOperation_shieldcoinbase::unlock_utxos() {
|
||||
LOCK2(cs_main, pwalletMain->cs_wallet);
|
||||
for (auto utxo : inputs_) {
|
||||
COutPoint outpt(utxo.txid, utxo.vout);
|
||||
pwalletMain->UnlockCoin(outpt);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,122 @@
|
|||
// Copyright (c) 2017 The Zcash developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#ifndef ASYNCRPCOPERATION_SHIELDCOINBASE_H
|
||||
#define ASYNCRPCOPERATION_SHIELDCOINBASE_H
|
||||
|
||||
#include "asyncrpcoperation.h"
|
||||
#include "amount.h"
|
||||
#include "base58.h"
|
||||
#include "primitives/transaction.h"
|
||||
#include "zcash/JoinSplit.hpp"
|
||||
#include "zcash/Address.hpp"
|
||||
#include "wallet.h"
|
||||
|
||||
#include <unordered_map>
|
||||
#include <tuple>
|
||||
|
||||
#include <univalue.h>
|
||||
|
||||
// Default transaction fee if caller does not specify one.
|
||||
#define SHIELD_COINBASE_DEFAULT_MINERS_FEE 10000
|
||||
|
||||
using namespace libzcash;
|
||||
|
||||
struct ShieldCoinbaseUTXO {
|
||||
uint256 txid;
|
||||
int vout;
|
||||
CAmount amount;
|
||||
};
|
||||
|
||||
// Package of info which is passed to perform_joinsplit methods.
|
||||
struct ShieldCoinbaseJSInfo
|
||||
{
|
||||
std::vector<JSInput> vjsin;
|
||||
std::vector<JSOutput> vjsout;
|
||||
CAmount vpub_old = 0;
|
||||
CAmount vpub_new = 0;
|
||||
};
|
||||
|
||||
class AsyncRPCOperation_shieldcoinbase : public AsyncRPCOperation {
|
||||
public:
|
||||
AsyncRPCOperation_shieldcoinbase(std::vector<ShieldCoinbaseUTXO> inputs, std::string toAddress, CAmount fee = SHIELD_COINBASE_DEFAULT_MINERS_FEE, UniValue contextInfo = NullUniValue);
|
||||
virtual ~AsyncRPCOperation_shieldcoinbase();
|
||||
|
||||
// We don't want to be copied or moved around
|
||||
AsyncRPCOperation_shieldcoinbase(AsyncRPCOperation_shieldcoinbase const&) = delete; // Copy construct
|
||||
AsyncRPCOperation_shieldcoinbase(AsyncRPCOperation_shieldcoinbase&&) = delete; // Move construct
|
||||
AsyncRPCOperation_shieldcoinbase& operator=(AsyncRPCOperation_shieldcoinbase const&) = delete; // Copy assign
|
||||
AsyncRPCOperation_shieldcoinbase& operator=(AsyncRPCOperation_shieldcoinbase &&) = delete; // Move assign
|
||||
|
||||
virtual void main();
|
||||
|
||||
virtual UniValue getStatus() const;
|
||||
|
||||
bool testmode = false; // Set to true to disable sending txs and generating proofs
|
||||
|
||||
private:
|
||||
friend class TEST_FRIEND_AsyncRPCOperation_shieldcoinbase; // class for unit testing
|
||||
|
||||
UniValue contextinfo_; // optional data to include in return value from getStatus()
|
||||
|
||||
CAmount fee_;
|
||||
PaymentAddress tozaddr_;
|
||||
|
||||
uint256 joinSplitPubKey_;
|
||||
unsigned char joinSplitPrivKey_[crypto_sign_SECRETKEYBYTES];
|
||||
|
||||
std::vector<ShieldCoinbaseUTXO> inputs_;
|
||||
|
||||
CTransaction tx_;
|
||||
|
||||
bool main_impl();
|
||||
|
||||
// JoinSplit without any input notes to spend
|
||||
UniValue perform_joinsplit(ShieldCoinbaseJSInfo &);
|
||||
|
||||
void sign_send_raw_transaction(UniValue obj); // throws exception if there was an error
|
||||
|
||||
void lock_utxos();
|
||||
|
||||
void unlock_utxos();
|
||||
};
|
||||
|
||||
|
||||
// To test private methods, a friend class can act as a proxy
|
||||
class TEST_FRIEND_AsyncRPCOperation_shieldcoinbase {
|
||||
public:
|
||||
std::shared_ptr<AsyncRPCOperation_shieldcoinbase> delegate;
|
||||
|
||||
TEST_FRIEND_AsyncRPCOperation_shieldcoinbase(std::shared_ptr<AsyncRPCOperation_shieldcoinbase> ptr) : delegate(ptr) {}
|
||||
|
||||
CTransaction getTx() {
|
||||
return delegate->tx_;
|
||||
}
|
||||
|
||||
void setTx(CTransaction tx) {
|
||||
delegate->tx_ = tx;
|
||||
}
|
||||
|
||||
// Delegated methods
|
||||
|
||||
bool main_impl() {
|
||||
return delegate->main_impl();
|
||||
}
|
||||
|
||||
UniValue perform_joinsplit(ShieldCoinbaseJSInfo &info) {
|
||||
return delegate->perform_joinsplit(info);
|
||||
}
|
||||
|
||||
void sign_send_raw_transaction(UniValue obj) {
|
||||
delegate->sign_send_raw_transaction(obj);
|
||||
}
|
||||
|
||||
void set_state(OperationStatus state) {
|
||||
delegate->state_.store(state);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif /* ASYNCRPCOPERATION_SHIELDCOINBASE_H */
|
||||
|
|
@ -24,6 +24,7 @@
|
|||
#include "asyncrpcoperation.h"
|
||||
#include "asyncrpcqueue.h"
|
||||
#include "wallet/asyncrpcoperation_sendmany.h"
|
||||
#include "wallet/asyncrpcoperation_shieldcoinbase.h"
|
||||
|
||||
#include "sodium.h"
|
||||
|
||||
|
@ -3506,6 +3507,182 @@ UniValue z_sendmany(const UniValue& params, bool fHelp)
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
When estimating the number of coinbase utxos we can shield in a single transaction:
|
||||
1. Joinsplit description is 1802 bytes.
|
||||
2. Transaction overhead ~ 100 bytes
|
||||
3. Spending a typical P2PKH is >=148 bytes, as defined in CTXIN_SPEND_DUST_SIZE.
|
||||
4. Spending a multi-sig P2SH address can vary greatly:
|
||||
https://github.com/bitcoin/bitcoin/blob/c3ad56f4e0b587d8d763af03d743fdfc2d180c9b/src/main.cpp#L517
|
||||
In real-world coinbase utxos, we consider a 3-of-3 multisig, where the size is roughly:
|
||||
(3*(33+1))+3 = 105 byte redeem script
|
||||
105 + 1 + 3*(73+1) = 328 bytes of scriptSig, rounded up to 400 based on testnet experiments.
|
||||
*/
|
||||
#define CTXIN_SPEND_P2SH_SIZE 400
|
||||
|
||||
UniValue z_shieldcoinbase(const UniValue& params, bool fHelp)
|
||||
{
|
||||
if (!EnsureWalletIsAvailable(fHelp))
|
||||
return NullUniValue;
|
||||
|
||||
if (fHelp || params.size() < 2 || params.size() > 3)
|
||||
throw runtime_error(
|
||||
"z_shieldcoinbase \"fromaddress\" \"tozaddress\" ( fee )\n"
|
||||
"\nShield transparent coinbase funds by sending to a shielded zaddr. This is an asynchronous operation and utxos"
|
||||
"\nselected for shielding will be locked. If there is an error, they are unlocked. The RPC call `listlockunspent`"
|
||||
"\ncan be used to return a list of locked utxos. The number of coinbase utxos selected for shielding is limited by"
|
||||
"\nboth the -mempooltxinputlimit=xxx option and a consensus rule defining a maximum transaction size of "
|
||||
+ strprintf("%d bytes.", MAX_TX_SIZE)
|
||||
+ HelpRequiringPassphrase() + "\n"
|
||||
"\nArguments:\n"
|
||||
"1. \"fromaddress\" (string, required) The address is a taddr or \"*\" for all taddrs belonging to the wallet.\n"
|
||||
"2. \"toaddress\" (string, required) The address is a zaddr.\n"
|
||||
"3. fee (numeric, optional, default="
|
||||
+ strprintf("%s", FormatMoney(SHIELD_COINBASE_DEFAULT_MINERS_FEE)) + ") The fee amount to attach to this transaction.\n"
|
||||
"\nResult:\n"
|
||||
"{\n"
|
||||
" \"operationid\": xxx (string) An operationid to pass to z_getoperationstatus to get the result of the operation.\n"
|
||||
" \"shieldedUTXOs\": xxx (numeric) Number of coinbase utxos being shielded.\n"
|
||||
" \"shieldedValue\": xxx (numeric) Value of coinbase utxos being shielded.\n"
|
||||
" \"remainingUTXOs\": xxx (numeric) Number of coinbase utxos still available for shielding.\n"
|
||||
" \"remainingValue\": xxx (numeric) Value of coinbase utxos still available for shielding.\n"
|
||||
"}\n"
|
||||
);
|
||||
|
||||
LOCK2(cs_main, pwalletMain->cs_wallet);
|
||||
|
||||
// Validate the from address
|
||||
auto fromaddress = params[0].get_str();
|
||||
bool isFromWildcard = fromaddress == "*";
|
||||
CBitcoinAddress taddr;
|
||||
if (!isFromWildcard) {
|
||||
taddr = CBitcoinAddress(fromaddress);
|
||||
if (!taddr.IsValid()) {
|
||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid from address, should be a taddr or \"*\".");
|
||||
}
|
||||
}
|
||||
|
||||
// Validate the destination address
|
||||
auto destaddress = params[1].get_str();
|
||||
try {
|
||||
CZCPaymentAddress pa(destaddress);
|
||||
libzcash::PaymentAddress zaddr = pa.Get();
|
||||
} catch (const std::runtime_error&) {
|
||||
throw JSONRPCError(RPC_INVALID_PARAMETER, string("Invalid parameter, unknown address format: ") + destaddress );
|
||||
}
|
||||
|
||||
// Convert fee from currency format to zatoshis
|
||||
CAmount nFee = SHIELD_COINBASE_DEFAULT_MINERS_FEE;
|
||||
if (params.size() > 2) {
|
||||
if (params[2].get_real() == 0.0) {
|
||||
nFee = 0;
|
||||
} else {
|
||||
nFee = AmountFromValue( params[2] );
|
||||
}
|
||||
}
|
||||
|
||||
// Prepare to get coinbase utxos
|
||||
std::vector<ShieldCoinbaseUTXO> inputs;
|
||||
CAmount shieldedValue = 0;
|
||||
CAmount remainingValue = 0;
|
||||
size_t estimatedTxSize = 2000; // 1802 joinsplit description + tx overhead + wiggle room
|
||||
size_t utxoCounter = 0;
|
||||
bool maxedOutFlag = false;
|
||||
size_t mempoolLimit = (size_t)GetArg("-mempooltxinputlimit", 0);
|
||||
|
||||
// Set of addresses to filter utxos by
|
||||
set<CBitcoinAddress> setAddress = {};
|
||||
if (!isFromWildcard) {
|
||||
setAddress.insert(taddr);
|
||||
}
|
||||
|
||||
// Get available utxos
|
||||
vector<COutput> vecOutputs;
|
||||
pwalletMain->AvailableCoins(vecOutputs, true, NULL, false, true);
|
||||
|
||||
// Find unspent coinbase utxos and update estimated size
|
||||
BOOST_FOREACH(const COutput& out, vecOutputs) {
|
||||
if (!out.fSpendable) {
|
||||
continue;
|
||||
}
|
||||
|
||||
CTxDestination address;
|
||||
if (!ExtractDestination(out.tx->vout[out.i].scriptPubKey, address)) {
|
||||
continue;
|
||||
}
|
||||
// If taddr is not wildcard "*", filter utxos
|
||||
if (setAddress.size()>0 && !setAddress.count(address)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!out.tx->IsCoinBase()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
utxoCounter++;
|
||||
CAmount nValue = out.tx->vout[out.i].nValue;
|
||||
|
||||
if (!maxedOutFlag) {
|
||||
CBitcoinAddress ba(address);
|
||||
size_t increase = (ba.IsScript()) ? CTXIN_SPEND_P2SH_SIZE : CTXIN_SPEND_DUST_SIZE;
|
||||
if (estimatedTxSize + increase >= MAX_TX_SIZE ||
|
||||
(mempoolLimit > 0 && utxoCounter > mempoolLimit))
|
||||
{
|
||||
maxedOutFlag = true;
|
||||
} else {
|
||||
estimatedTxSize += increase;
|
||||
ShieldCoinbaseUTXO utxo = {out.tx->GetHash(), out.i, nValue};
|
||||
inputs.push_back(utxo);
|
||||
shieldedValue += nValue;
|
||||
}
|
||||
}
|
||||
|
||||
if (maxedOutFlag) {
|
||||
remainingValue += nValue;
|
||||
}
|
||||
}
|
||||
|
||||
size_t numUtxos = inputs.size();
|
||||
|
||||
if (numUtxos == 0) {
|
||||
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Could not find any coinbase funds to shield.");
|
||||
}
|
||||
|
||||
if (shieldedValue < nFee) {
|
||||
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS,
|
||||
strprintf("Insufficient coinbase funds, have %s, which is less than miners fee %s",
|
||||
FormatMoney(shieldedValue), FormatMoney(nFee)));
|
||||
}
|
||||
|
||||
// Check that the user specified fee is sane (if too high, it can result in error -25 absurd fee)
|
||||
CAmount netAmount = shieldedValue - nFee;
|
||||
if (nFee > netAmount) {
|
||||
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Fee %s is greater than the net amount to be shielded %s", FormatMoney(nFee), FormatMoney(netAmount)));
|
||||
}
|
||||
|
||||
// Keep record of parameters in context object
|
||||
UniValue contextInfo(UniValue::VOBJ);
|
||||
contextInfo.push_back(Pair("fromaddress", params[0]));
|
||||
contextInfo.push_back(Pair("toaddress", params[1]));
|
||||
contextInfo.push_back(Pair("fee", ValueFromAmount(nFee)));
|
||||
|
||||
// Create operation and add to global queue
|
||||
std::shared_ptr<AsyncRPCQueue> q = getAsyncRPCQueue();
|
||||
std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_shieldcoinbase(inputs, destaddress, nFee, contextInfo) );
|
||||
q->addOperation(operation);
|
||||
AsyncRPCOperationId operationId = operation->getId();
|
||||
|
||||
// Return continuation information
|
||||
UniValue o(UniValue::VOBJ);
|
||||
o.push_back(Pair("remainingUTXOs", utxoCounter - numUtxos));
|
||||
o.push_back(Pair("remainingValue", ValueFromAmount(remainingValue)));
|
||||
o.push_back(Pair("shieldingUTXOs", numUtxos));
|
||||
o.push_back(Pair("shieldingValue", ValueFromAmount(shieldedValue)));
|
||||
o.push_back(Pair("opid", operationId));
|
||||
return o;
|
||||
}
|
||||
|
||||
|
||||
UniValue z_listoperationids(const UniValue& params, bool fHelp)
|
||||
{
|
||||
if (!EnsureWalletIsAvailable(fHelp))
|
||||
|
|
Loading…
Reference in New Issue