Skip to content
This repository was archived by the owner on May 16, 2019. It is now read-only.

Commit d13324b

Browse files
committed
Limit checks of unfunded orders
To prevent putting too much stress on the libbitcoin server we will only check unfunded orders made within the past 24 hours for payment. If an order is older than 24 you can still check for payment by calling the get_order api (which will query the libbitcoin server).
1 parent 5f9ae2a commit d13324b

File tree

4 files changed

+57
-41
lines changed

4 files changed

+57
-41
lines changed

api/restapi.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from keys.keychain import KeyChain
2626
from dht.utils import digest
2727
from market.profile import Profile
28-
from market.contracts import Contract
28+
from market.contracts import Contract, check_order_for_payment
2929
from net.upnp import PortMapper
3030

3131
DEFAULT_RECORDS_COUNT = 20
@@ -1105,19 +1105,26 @@ def get_order(self, request):
11051105

11061106
if os.path.exists(DATA_FOLDER + "purchases/unfunded/" + request.args["order_id"][0] + ".json"):
11071107
file_path = DATA_FOLDER + "purchases/unfunded/" + request.args["order_id"][0] + ".json"
1108+
status = self.db.purchases.get_status(request.args["order_id"][0])
11081109
elif os.path.exists(DATA_FOLDER + "purchases/in progress/" + request.args["order_id"][0] + ".json"):
11091110
file_path = DATA_FOLDER + "purchases/in progress/" + request.args["order_id"][0] + ".json"
1111+
status = self.db.purchases.get_status(request.args["order_id"][0])
11101112
elif os.path.exists(DATA_FOLDER + "purchases/trade receipts/" + request.args["order_id"][0] + ".json"):
11111113
file_path = DATA_FOLDER + "purchases/trade receipts/" + request.args["order_id"][0] + ".json"
1114+
status = self.db.purchases.get_status(request.args["order_id"][0])
11121115
elif os.path.exists(DATA_FOLDER + "store/contracts/unfunded/" + request.args["order_id"][0] + ".json"):
11131116
file_path = DATA_FOLDER + "store/contracts/unfunded/" + request.args["order_id"][0] + ".json"
1117+
status = self.db.sales.get_status(request.args["order_id"][0])
11141118
elif os.path.exists(DATA_FOLDER + "store/contracts/in progress/" + request.args["order_id"][0] + ".json"):
11151119
file_path = DATA_FOLDER + "store/contracts/in progress/" + request.args["order_id"][0] + ".json"
1120+
status = self.db.sales.get_status(request.args["order_id"][0])
11161121
elif os.path.exists(DATA_FOLDER +
11171122
"store/contracts/trade receipts/" + request.args["order_id"][0] + ".json"):
11181123
file_path = DATA_FOLDER + "store/contracts/trade receipts/" + request.args["order_id"][0] + ".json"
1124+
status = self.db.sales.get_status(request.args["order_id"][0])
11191125
elif os.path.exists(DATA_FOLDER + "cases/" + request.args["order_id"][0] + ".json"):
11201126
file_path = DATA_FOLDER + "cases/" + request.args["order_id"][0] + ".json"
1127+
status = 4
11211128
else:
11221129
request.write(json.dumps({}, indent=4))
11231130
request.finish()
@@ -1126,6 +1133,12 @@ def get_order(self, request):
11261133
with open(file_path, 'r') as filename:
11271134
order = json.load(filename, object_pairs_hook=OrderedDict)
11281135

1136+
self.protocol.blockchain.refresh_connection()
1137+
if status == 0:
1138+
check_order_for_payment(request.args["order_id"][0], self.db, self.protocol.blockchain,
1139+
self.mserver.protocol.get_notification_listener(),
1140+
self.protocol.testnet)
1141+
11291142
def return_order():
11301143
request.setHeader('content-type', "application/json")
11311144
request.write(str(bleach.clean(json.dumps(order, indent=4), tags=ALLOWED_TAGS)))

db/datastore.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,7 @@ def get_all(self):
952952
def get_unfunded(self):
953953
conn = Database.connect_database(self.PATH)
954954
cursor = conn.cursor()
955-
cursor.execute('''SELECT id FROM sales WHERE status=0''')
955+
cursor.execute('''SELECT id, timestamp FROM sales WHERE status=0''')
956956
ret = cursor.fetchall()
957957
conn.close()
958958
return ret

market/contracts.py

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,45 +1257,50 @@ def check_unfunded_for_payment(db, libbitcoin_client, notification_listener, tes
12571257
Run through the unfunded contracts in our database and query the
12581258
libbitcoin server to see if they received a payment.
12591259
"""
1260-
def check(order_id):
1261-
try:
1262-
if os.path.exists(DATA_FOLDER + "purchases/unfunded/" + order_id[0] + ".json"):
1263-
file_path = DATA_FOLDER + "purchases/unfunded/" + order_id[0] + ".json"
1264-
is_purchase = True
1265-
elif os.path.exists(DATA_FOLDER + "store/contracts/unfunded/" + order_id[0] + ".json"):
1266-
file_path = DATA_FOLDER + "store/contracts/unfunded/" + order_id[0] + ".json"
1267-
is_purchase = False
1268-
with open(file_path, 'r') as filename:
1269-
order = json.load(filename, object_pairs_hook=OrderedDict)
1270-
c = Contract(db, contract=order, testnet=testnet)
1271-
c.blockchain = libbitcoin_client
1272-
c.notification_listener = notification_listener
1273-
c.is_purchase = is_purchase
1274-
addr = c.contract["buyer_order"]["order"]["payment"]["address"]
1275-
def history_fetched(ec, history):
1276-
if not ec:
1277-
# pylint: disable=W0612
1278-
# pylint: disable=W0640
1279-
for objid, txhash, index, height, value in history:
1280-
def cb_txpool(ec, result):
1281-
if ec:
1282-
libbitcoin_client.fetch_transaction(txhash, cb_chain)
1283-
else:
1284-
c.on_tx_received(None, None, None, None, result)
1285-
1286-
def cb_chain(ec, result):
1287-
if not ec:
1288-
c.on_tx_received(None, None, None, None, result)
1289-
1290-
libbitcoin_client.fetch_txpool_transaction(txhash, cb_txpool)
1291-
1292-
libbitcoin_client.fetch_history2(addr, history_fetched)
1293-
except Exception:
1294-
pass
1260+
current_time = time.time()
12951261
libbitcoin_client.refresh_connection()
12961262
purchases = db.purchases.get_unfunded()
12971263
for purchase in purchases:
1298-
check(purchase)
1264+
if current_time - purchase[1] <= 600:
1265+
check_order_for_payment(purchase[0], db, libbitcoin_client, notification_listener, testnet)
12991266
sales = db.sales.get_unfunded()
13001267
for sale in sales:
1301-
check(sale)
1268+
if current_time - sale[1] <= 600:
1269+
check_order_for_payment(sale[0], db, libbitcoin_client, notification_listener, testnet)
1270+
1271+
1272+
def check_order_for_payment(order_id, db, libbitcoin_client, notification_listener, testnet=False):
1273+
try:
1274+
if os.path.exists(DATA_FOLDER + "purchases/unfunded/" + order_id[0] + ".json"):
1275+
file_path = DATA_FOLDER + "purchases/unfunded/" + order_id[0] + ".json"
1276+
is_purchase = True
1277+
elif os.path.exists(DATA_FOLDER + "store/contracts/unfunded/" + order_id[0] + ".json"):
1278+
file_path = DATA_FOLDER + "store/contracts/unfunded/" + order_id[0] + ".json"
1279+
is_purchase = False
1280+
with open(file_path, 'r') as filename:
1281+
order = json.load(filename, object_pairs_hook=OrderedDict)
1282+
c = Contract(db, contract=order, testnet=testnet)
1283+
c.blockchain = libbitcoin_client
1284+
c.notification_listener = notification_listener
1285+
c.is_purchase = is_purchase
1286+
addr = c.contract["buyer_order"]["order"]["payment"]["address"]
1287+
def history_fetched(ec, history):
1288+
if not ec:
1289+
# pylint: disable=W0612
1290+
# pylint: disable=W0640
1291+
for objid, txhash, index, height, value in history:
1292+
def cb_txpool(ec, result):
1293+
if ec:
1294+
libbitcoin_client.fetch_transaction(txhash, cb_chain)
1295+
else:
1296+
c.on_tx_received(None, None, None, None, result)
1297+
1298+
def cb_chain(ec, result):
1299+
if not ec:
1300+
c.on_tx_received(None, None, None, None, result)
1301+
1302+
libbitcoin_client.fetch_txpool_transaction(txhash, cb_txpool)
1303+
1304+
libbitcoin_client.fetch_history2(addr, history_fetched)
1305+
except Exception:
1306+
pass

market/moderation.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,6 @@ def close_dispute(resolution_json, db, message_listener, notification_listener,
157157
moderator_pubkey = unhexlify(moderator["pubkeys"]["guid"])
158158
moderator_avatar = unhexlify(moderator["avatar"])
159159

160-
print json.dumps(resolution_json, indent=4)
161-
162160
verify_key = nacl.signing.VerifyKey(moderator_pubkey)
163161
verify_key.verify(json.dumps(resolution_json["dispute_resolution"]["resolution"], indent=4),
164162
base64.b64decode(resolution_json["dispute_resolution"]["signature"]))

0 commit comments

Comments
 (0)