|
import uuid |
|
import time |
|
import json |
|
import hashlib |
|
|
|
aliases['run'] = $GOPATH[0] + '/src/github.com/fiatjaf/etleneum/runcall' |
|
def make_hash(): |
|
preimage = str(uuid.uuid4()) |
|
hash = hashlib.sha256(preimage.encode('ascii')).hexdigest() |
|
return preimage, hash |
|
|
|
jsonstate = 'null' |
|
state = None |
|
|
|
def call(method, payload={}, satoshis=0, funds=0): |
|
global state, jsonstate |
|
r = !(run --contract contract.lua --method @(method) --payload @(json.dumps(payload)) --state @(jsonstate) --satoshis @(satoshis) --funds @(funds)) |
|
if not r: |
|
print('') |
|
raise Exception(r.output) |
|
jsonstate = $(echo @(r.output) | jq .State) |
|
print('state after: ' + jsonstate) |
|
state = json.loads(jsonstate) |
|
return json.loads($(echo @(r.output) | jq .ReturnedValue)) |
|
|
|
withdraw_toomuch = 'lnbc10u1pw8hwmzpp5awlp7xqyyknc3yfmnhg59sk4hfexmfpemrx2m4hrg7k3pdmfmwysdq2w9mk2am3v5xqxae4j0lcqp2rzjqtqkejjy2c44jrwj08y5ygqtmn8af7vscwnflttzpsgw7tuz9r407zyusgqq44sqqqqqqqqqqqqqqqgqpckcnfy2j42leleand2hseqv4zgsc06upmmn6sj509nrahvcdmnkyjvn39785cks97mcnqvatmag4l2hpxdvke0jhgq798m85jmkdrntspg4rm2z' |
|
withdraw_10 = 'lnbc100n1pw8hw3upp552llrqm5274emxpkjyguxtuyxgxwcs8n75sc87fax6mv0asnptksdq2w9mk2am3v5xqxae4j0lcqp2rzjqtqkejjy2c44jrwj08y5ygqtmn8af7vscwnflttzpsgw7tuz9r407zyusgqq44sqqqqqqqqqqqqqqqgqpc5dhs4tmlgmc9z7aw4d236y3c24qd48zdkyk2hse75h4ef28lmaw53dx560d8ddy8503xp0zr8xmwc4fehcf0m55mgf6hlkczca5h7mgpru0jma' |
|
|
|
p1, h1 = make_hash() |
|
p2, h2 = make_hash() |
|
p3, h3 = make_hash() |
|
|
|
print('init') |
|
call('__init__') |
|
|
|
print('open with given preimage') |
|
exp1 = int(time.time()) + 86400 * 5 |
|
call('open', {'hash': h1, 'withdraw_hash': h2}, 10) |
|
assert state[h1] == {'withdraw_hash': h2, 'amount': 10, 'expiration': exp1} |
|
|
|
print('open another with small expiration time') |
|
exp2 = int(time.time()) + 1 |
|
call('open', {'hash': h2, 'withdraw_hash': h3, 'timelock': 1}, 10) |
|
assert state[h2] == {'withdraw_hash': h3, 'amount': 10, 'expiration': exp2} |
|
|
|
print('overwrite fail') |
|
exc = False |
|
try: |
|
call('open', {'hash': h2, 'withdraw_hash': h3}, 6547) |
|
except: |
|
exc = True |
|
assert exc |
|
exc = False |
|
try: |
|
call('open', {'hash': h2, 'withdraw_hash': h3}, 6547) |
|
except: |
|
exc = True |
|
assert exc |
|
assert state[h1] == {'withdraw_hash': h2, 'amount': 10, 'expiration': exp1} |
|
assert state[h2] == {'withdraw_hash': h3, 'amount': 10, 'expiration': exp2} |
|
|
|
time.sleep(1) |
|
|
|
print('claim fail due to wrong hash') |
|
res = call('claim', {'hash': h1, 'preimage': p2}, 0, funds=99999) # not expired |
|
assert res['ok'] == False |
|
res = call('claim', {'hash': h2, 'preimage': p2}, 0, funds=99999) # expired |
|
assert res['ok'] == False |
|
assert state[h1] == {'withdraw_hash': h2, 'amount': 10, 'expiration': exp1} |
|
|
|
print('claim fail due to invalid invoice') |
|
exc = False |
|
try: |
|
call('claim', {'hash': h1, 'preimage': p1, '_invoice': withdraw_toomuch}, 0, funds=99999) |
|
except: |
|
exc = True |
|
assert exc |
|
assert state[h1] == {'withdraw_hash': h2, 'amount': 10, 'expiration': exp1} |
|
|
|
print('claim success') |
|
res = call('claim', {'hash': h1, 'preimage': p1, '_invoice': withdraw_10}, 0, funds=99999) |
|
assert res['ok'] == True |
|
assert res['hash'] == h1 |
|
assert res['amount'] == 10 |
|
assert len(state) == 1 |
|
call('claim', {'hash': h2, 'preimage': p3, '_invoice': withdraw_10}, 0, funds=99999) |
|
assert state == {} |
|
|
|
print('tests passed') |