#!/usr/bin/env python3 # Copyright (c) 2016-2025 The Hush developers # Distributed under the GPLv3 software license, see the accompanying # file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html from test_framework.test_framework import BitcoinTestFramework from test_framework.authproxy import JSONRPCException from test_framework.util import assert_equal, assert_greater_than, assert_greater_than_or_equal, \ initialize_chain_clean, initialize_chain, start_nodes, start_node, connect_nodes_bi, \ stop_nodes, sync_blocks, sync_mempools, wait_bitcoinds, rpc_port, assert_raises, assert_true, \ wait_and_assert_operationid_status import time from decimal import Decimal def assert_success(result): assert_equal(result['result'], 'success') def assert_error(result): assert_equal(result['result'], 'error') class ShieldCoinbaseDonationTest (BitcoinTestFramework): def setup_chain(self): print("Initializing test directory "+self.options.tmpdir) self.num_nodes = 1 self.options.nocleanup = 1 # do not delete datadir after test run #self.options.nocleanup = 0 initialize_chain_clean(self.options.tmpdir, self.num_nodes) def setup_network(self, split = False): print("Setting up network...") self.supply = 555 self.nodes = start_nodes(self.num_nodes, self.options.tmpdir, extra_args=[[ # always give -ac_name as first extra_arg and port as third '-ac_name=ZZZ', #'-ac_algo=randomx', #'-testnode=1', # why does this make the test node hang before run_test() ? '-conf='+self.options.tmpdir+'/node0/regtest/ZZZ.conf', '-port=63367', '-rpcport=63368', '-ac_supply=' + str(self.supply), '-ac_reward=100000000', '-ac_private=1', '-allowlist=127.0.0.1', '-regtest', '--daemon', #'-debug', #'-zrpc', #'-zdebug', ##'-zrpcunsafe' ]] ) self.is_network_split = False self.rpc = self.nodes[0] self.sync_all() print("Done setting up network") def run_test (self): self.run_test_default() self.run_test_custom() def run_test_default(self): rpc0 = self.nodes[0] # mine initial ac_supply rpc0.generate(1) self.sync_all() zaddr1 = rpc0.z_getnewaddress() #rpc0.z_exportkey(zaddr1) # first we test the default situation where no donation is given and # it defaults to 0 response = rpc0.z_shieldcoinbase('*', zaddr1, 0, 1) opid = response['opid'] shieldingValue = response['shieldingValue'] assert_true( shieldingValue >= self.supply ) print("opid=" + opid) time.sleep(2) # give some time for the ztx to complete # 555 supply plus magic utxo = 555.02070592 totalSupply = 55502070592 # in puposhis expectedAmount = totalSupply json = rpc0.z_getoperationstatus() txid = json[0]['result']['txid'] wait_and_assert_operationid_status(rpc0, opid) rawtx0 = rpc0.z_viewtransaction(txid) assert_equal( rawtx0['outputs'][0]['valueZat'] , expectedAmount, '5% donation sends correct sendAmount') def run_test_custom(self): rpc0 = self.nodes[0] zaddr1 = rpc0.z_getnewaddress() # generate some new coinbase funds rpc0.generate(1) self.sync_all() testing_zaddr = "zregtestsapling1y30nwg0clsu6gcyrnvht8hdyfk3vwtszlh6kc4z5hv9hmpxzg2g0nx7c60xeecggm9x9gma96t4" testing_privkey = "secret-extended-key-regtest1q0hgrms7qqqqpqrsz6myrtnh3ccp8uzp0kgxj6029wr6vq5hqvyccdlz7a745pgm5eeaamxqp9rxll2xctfrlw2l8xhxsc7zsut2tyz0374rrlk8srjswx7rhm6hcf2d7fuwajazvjesafduzxyka4w02tqjxdehzvghyrsd2zll90k3g2ckdvc5kqd6r7r7nglrtj0ej5a40d6lh8zxrvdlxrpuc59y5m8n9tekdxh4wpqn3smv5nxu4vvu58f8dgwn92qfqrvxqlscchtyh" # import zaddr that receives donation , no rescan rpc0.z_importkey(testing_privkey, "no") rpc0.z_listaddresses() # now we test giving a donation parameter donation = 5 # shield funds to a new zaddr in this wallet response = rpc0.z_shieldcoinbase('*', zaddr1, 0, 1, donation) opid = response['opid'] print("opid=" + opid) #wait_and_assert_operationid_status(rpc0, opid) shieldingValue = response['shieldingValue'] assert_greater_than_or_equal( shieldingValue , 1.0 ) time.sleep(2) # give some time for the ztx to complete rpc0.getinfo() rpc0.generate(1) self.sync_all() # get the txid json = rpc0.z_getoperationstatus() txid = json[0]['result']['txid'] print("txid=" + txid) rpc0.z_listunspent() expectedAmount = 5000000 # lookup txid on node1 which should have received donation rawtx1 = rpc0.z_viewtransaction(txid) # there should be two outputs to different addresses, order is nondeterministic if rawtx1['outputs'][0]['address'] == testing_zaddr: zout = 0 else: zout = 1 assert_equal( rawtx1['outputs'][zout]['address'] , testing_zaddr, 'correct zaddr gets donation') assert_equal( rawtx1['outputs'][zout]['valueZat'] , expectedAmount, '5% donation sends correct donationAmount') if __name__ == '__main__': ShieldCoinbaseDonationTest().main()