From 9e087f2af890e27a802743e0c5b346e47ce9b360 Mon Sep 17 00:00:00 2001 From: Jay Graber Date: Tue, 18 Apr 2017 15:55:48 -0700 Subject: [PATCH] Move tests to folder, work on sweep-all.py --- pyZcash/examples/sweep_all.py | 35 +++++++++++------------------- pyZcash/examples/tests.py | 38 --------------------------------- pyZcash/rpc/ZDaemon.py | 40 ++++++++++------------------------- pyZcash/tests/__init.py__ | 0 pyZcash/tests/tests.py | 34 +++++++++++++++++++++++++++++ 5 files changed, 57 insertions(+), 90 deletions(-) delete mode 100644 pyZcash/examples/tests.py create mode 100644 pyZcash/tests/__init.py__ create mode 100644 pyZcash/tests/tests.py diff --git a/pyZcash/examples/sweep_all.py b/pyZcash/examples/sweep_all.py index b2a9bb6..9b6bbca 100644 --- a/pyZcash/examples/sweep_all.py +++ b/pyZcash/examples/sweep_all.py @@ -2,9 +2,8 @@ import os.path import sys import time -from pyZcash.rpc.ZDaemon import * -from pyZcash.settings import * - +from rpc.ZDaemon import * +from settings import * #Sweeps all unspent transparent txs, cleaning them through a temporary zaddr. def clean_and_collect_all(taddress=TEST_TADDR, fee=DEFAULT_FEE): @@ -17,45 +16,35 @@ def clean_and_collect_all(taddress=TEST_TADDR, fee=DEFAULT_FEE): exit() print "Generating temporary zaddress for tx..." - zaddress_full = zd.getNewRawZAddress() - zaddress = zaddress_full.get('zcaddress') - zsecret = zaddress_full.get('zcsecretkey') + # rewrite this + zaddr = zd.z_getnewaddress() - print "Generated: " + zaddress - print "Secret: " + zsecret + print "Generated zaddress: " + zaddr print "Gathering and transmitting unspent txs..." print "Please wait..." - notes = zd.pourAllUnspentTxs(zaddress) - encnote1 = notes.get('encryptednote1') + # rewrite this + shielded_txs = zd.sweepAllUnspentTxs(zaddress) - - print "Found a note to use: \n--------------------------------------------\n" + encnote1 + "\n--------------------------------------------" - - print "Waiting for note to show in blockchain before spendable..." + print "Sending unspent txs to shieleded address..." print "This may take a few minutes..." + + # rewrite this while zd.receiveTx(zsecret, encnote1).get('exists') is not True: print zd.receiveTx(zsecret,encnote1) time.sleep(5) - print "Found note in blockchain!" - total = zd.receiveTx(zsecret, encnote1).get('amount') - print "Examined note and found total: " + str(total) - - print "Spending note to target transparent address..." - tx_response = zd.sendNoteToAddress(encnote1, zsecret, taddress, total-fee, zaddress) - print "Sent! Check " + taddress + " shortly." if __name__ == "__main__": if len(sys.argv) <= 1: print "Usage: python sweep_all.py " - print "Ex: python sweep_all.py mfu8LbjAq15zmCDLCwUuay9cVc2FcGuY4d" + print "Ex: python sweep_all.py tm9yooiTA5bTheUD6VgQg2ya8j49XDhrtNW" exit() taddr = sys.argv[1] - if len(taddr) != len('mfu8LbjAq15zmCDLCwUuay9cVc2FcGuY4d'): + if len(taddr) != len('tm9yooiTA5bTheUD6VgQg2ya8j49XDhrtNW'): print "That doesn't look like a transparent address.. Maybe you are trying to use a zaddress?" exit() diff --git a/pyZcash/examples/tests.py b/pyZcash/examples/tests.py deleted file mode 100644 index d2f68d9..0000000 --- a/pyZcash/examples/tests.py +++ /dev/null @@ -1,38 +0,0 @@ -import os.path -import sys - -from pyZcash.rpc.ZDaemon import * -from pyZcash.settings import * - - -def test_daemon(): - zd = ZDaemon() - - print zd.getBlockHash(100) - print zd.getBlockByHash(zd.getBlockHash(100)) - print zd.getBlockByHeight(100) - print zd.getNetworkHeight() - print zd.getNetworkDifficulty() - print zd.getTotalBalance() - print zd.getConnectionCount() - print zd.getNewAddress() - print zd.getNewZAddress() - - print zd.getUnspentTxs() - tx_amount = zd.getTxInfo(TEST_TXID).get('details')[0].get('amount') - tx = zd.createNewRawTxFromTxid(TEST_TXID) - pourtx = zd.pourRawTx(tx, TEST_ZADDR, tx_amount) - hextx = zd.signRawTx(pourtx.get('rawtxn')) - - print zd.sendRawTx(hextx.get('hex')) - - print pourtx - print hextx - -# print zd.sendNoteToAddress(encnote, TEST_ZSECRET, TEST_TADDR, 0.33, TEST_ZADDR) - -# print zd.sendNoteToAddress(encnote, TEST_ZSECRET, faucet_addr, 0.33, TEST_ZADDR) - - -if __name__ == "__main__": - test_daemon() diff --git a/pyZcash/rpc/ZDaemon.py b/pyZcash/rpc/ZDaemon.py index 8b64c9b..19224ba 100644 --- a/pyZcash/rpc/ZDaemon.py +++ b/pyZcash/rpc/ZDaemon.py @@ -10,7 +10,6 @@ class ZDaemon(object): def __init__(self, network=NETWORK, user=RPCUSER, password=RPCPASSWORD, timeout=TIMEOUT): #TODO: check utf safety self.network = network - print "Network: ", self.network self.user = user.encode('utf8') self.password = password.encode('utf8') self.timeout = timeout @@ -40,19 +39,6 @@ class ZDaemon(object): return resp['result'] - def _get_response(self): - http_response = self.__conn.getresponse() - if http_response is None: - raise JSONRPCException({ - 'code': -342, 'message': 'missing HTTP response from server'}) - - responsedata = http_response.read().decode('utf8') - response = json.loads(responsedata, parse_float=decimal.Decimal) - if "error" in response and response["error"] is None: - log.debug("<-%s- %s"%(response["id"], json.dumps(response["result"], default=EncodeDecimal))) - else: - log.debug("<-- "+responsedata) - return response #Block Info def getBlockHash(self, blockheight): @@ -64,7 +50,7 @@ class ZDaemon(object): def getBlockByHeight(self, blockheight): return self.getBlockByHash(self.getBlockHash(blockheight)) - #Network Info + # Custom methods to get Network Info def getNetworkHeight(self): return self._call('getblockcount') @@ -80,30 +66,26 @@ class ZDaemon(object): def getConnectionCount(self): return self._call('getconnectioncount') - #Wallet Info (transparent) - def getTotalBalance(self, account=""): - if account: - return self._call('getbalance', account) - else: - return self._call('getbalance') - def getUnspentTxs(self, minconf=1): + # Wallet Info (transparent) + def getbalance(self): + return self._call('getbalance') + + def listunspent(self, minconf=1): return self._call('listunspent', minconf) #Raw Txs - def getTxInfo(self, txid): + def gettransaction(self, txid): return self._call('gettransaction', txid) # taddr methods - def getNewAddress(self, account=""): - if account: - return self._call('getnewaddress', account) - else: - return self._call('getnewaddress') + def getnewaddress(self): + return self._call('getnewaddress') - def sendTransparent(self, taddress, amount): + def sendtoaddress(self, taddress, amount): return self._call('sendtoaddress', taddress, amount) + # zaddr methods def z_getnewaddress(self): return self._call('z_getnewaddress') diff --git a/pyZcash/tests/__init.py__ b/pyZcash/tests/__init.py__ new file mode 100644 index 0000000..e69de29 diff --git a/pyZcash/tests/tests.py b/pyZcash/tests/tests.py new file mode 100644 index 0000000..fd8e31d --- /dev/null +++ b/pyZcash/tests/tests.py @@ -0,0 +1,34 @@ +import os.path +import sys + +from pyZcash.rpc.ZDaemon import * +from pyZcash.settings import * + +def test_daemon(): + zd = ZDaemon() + + # Network tests + print zd.getBlockHash(100) + print zd.getBlockByHash(zd.getBlockHash(100)) + print zd.getBlockByHeight(100) + print zd.getNetworkHeight() + print zd.getNetworkDifficulty() + print zd.getVersion() + print zd.getConnectionCount() + + # Taddr Wallet tests + print zd.getbalance() + print zd.listunspent() + print zd.getnewaddress() + + # Zaddr wallet tests + print zd.z_getnewaddress() + zaddrs = zd.z_listaddresses() + print zaddrs + zaddr_received = zd.z_listreceivedbyaddress(zaddr) + print zaddr_received + + # TODO: test z_sendmany, and use regtest + +if __name__ == "__main__": + test_daemon()