Move tests to folder, work on sweep-all.py

This commit is contained in:
Jay Graber 2017-04-18 15:55:48 -07:00
parent 810d6fb42a
commit 9e087f2af8
5 changed files with 57 additions and 90 deletions

View File

@ -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 <transparent address>"
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()

View File

@ -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()

View File

@ -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')

View File

34
pyZcash/tests/tests.py Normal file
View File

@ -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()