This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
354 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import sys | ||
import os, time | ||
data_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) | ||
sys.path.insert(0, os.path.join(data_dir, 'lib')) | ||
import subprocess | ||
import unittest | ||
import common | ||
import commontest | ||
from blockchaininterface import * | ||
import bitcoin as btc | ||
import binascii | ||
|
||
''' Just some random thoughts to motivate possible tests; | ||
almost none of this has really been done: | ||
Expectations | ||
1. Any bot should run indefinitely irrespective of the input | ||
messages it receives, except bots which perform a finite action | ||
2. A bot must never spend an unacceptably high transaction fee. | ||
3. A bot must explicitly reject interactions with another bot not | ||
respecting the JoinMarket protocol for its version. | ||
4. Bots must never send bitcoin data in the clear over the wire. | ||
''' | ||
|
||
'''helper functions put here to avoid polluting the main codebase.''' | ||
|
||
import platform | ||
OS = platform.system() | ||
PINL = '\r\n' if OS == 'Windows' else '\n' | ||
|
||
def local_command(command, bg=False, redirect=''): | ||
if redirect=='NULL': | ||
if OS=='Windows': | ||
command.append(' > NUL 2>&1') | ||
elif OS=='Linux': | ||
command.extend(['>', '/dev/null', '2>&1']) | ||
else: | ||
print "OS not recognised, quitting." | ||
elif redirect: | ||
command.extend(['>', redirect]) | ||
|
||
if bg: | ||
FNULL = open(os.devnull,'w') | ||
return subprocess.Popen(command, stdout=FNULL, stderr=subprocess.STDOUT, close_fds=True) | ||
else: | ||
#in case of foreground execution, we can use the output; if not | ||
#it doesn't matter | ||
return subprocess.check_output(command) | ||
|
||
|
||
|
||
class BlackListPassTests(unittest.TestCase): | ||
'''This test case intends to simulate | ||
a single join with a single counterparty. In that sense, | ||
it's not realistic, because nobody (should) do joins with only 1 maker, | ||
but this test has the virtue of being the simplest possible thing | ||
that JoinMarket can do. ''' | ||
def setUp(self): | ||
#create 2 new random wallets. | ||
#put 10 coins into the first receive address | ||
#to allow that bot to start. | ||
self.wallets = commontest.make_wallets(2, | ||
wallet_structures=[[1,0,0,0,0],[1,0,0,0,0]], mean_amt=10) | ||
|
||
|
||
def blacklist_run(self, n, m, fake): | ||
os.remove('logs/blacklist') | ||
#start yield generator with wallet1 | ||
yigen_proc = local_command(['python','yield-generator.py', | ||
str(self.wallets[0]['seed'])],bg=True) | ||
|
||
#A significant delay is needed to wait for the yield generator to sync its wallet | ||
time.sleep(10) | ||
|
||
#run a single sendpayment call with wallet2 | ||
amt = n*100000000 #in satoshis | ||
dest_address = btc.privkey_to_address(os.urandom(32), from_hex=False, magicbyte=common.get_p2pk_vbyte()) | ||
try: | ||
for i in range(m): | ||
sp_proc = local_command(['python','sendpayment.py','--yes','-N','1', self.wallets[1]['seed'],\ | ||
str(amt), dest_address]) | ||
except subprocess.CalledProcessError, e: | ||
if yigen_proc: | ||
yigen_proc.terminate() | ||
print e.returncode | ||
print e.message | ||
raise | ||
|
||
if yigen_proc: | ||
yigen_proc.terminate() | ||
if not fake: | ||
received = common.bc_interface.get_received_by_addr([dest_address], None)['data'][0]['balance'] | ||
if received != amt*m: | ||
common.debug('received was: '+str(received)+ ' but amount was: '+str(amt)) | ||
return False | ||
#check sanity in blacklist | ||
with open('logs/blacklist','rb') as f: | ||
blacklist_lines = f.readlines() | ||
if not fake: | ||
required_bl_lines = m | ||
bl_count = 1 | ||
else: | ||
required_bl_lines = 1 | ||
bl_count = m | ||
if len(blacklist_lines) != required_bl_lines: | ||
common.debug('wrong number of blacklist lines: '+str(len(blacklist_lines))) | ||
return False | ||
|
||
for bl in blacklist_lines: | ||
if len(bl.split(',')[0].strip()) != 66: | ||
common.debug('malformed utxo: '+str(len(bl.split(',')[0].strip()))) | ||
return False | ||
if int(bl.split(',')[1]) != bl_count: | ||
common.debug('wrong blacklist count:'+str(bl.split(',')[1])) | ||
return False | ||
return True | ||
|
||
def test_simple_send(self): | ||
self.failUnless(self.blacklist_run(2, 2, False)) | ||
|
||
|
||
|
||
|
||
def main(): | ||
os.chdir(data_dir) | ||
common.load_program_config() | ||
unittest.main() | ||
|
||
if __name__ == '__main__': | ||
#Big kludge, but there is currently no way to inject this code: | ||
print """this test is to be run in two modes, first | ||
with no changes, then second adding a 'return' in | ||
taker.CoinJoinTX.push() return (so it does nothing), | ||
and further changing the third parameter to blacklist_run to 'True' | ||
and the second parameter to '3' from '2' | ||
In both cases the test should pass for success. | ||
Also, WARNING! This test will delete your blacklist, better | ||
not run it in a "real" repo or back it up. | ||
""" | ||
raw_input("OK?") | ||
main() | ||
|
||
|
Oops, something went wrong.