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

Commit 22933a2

Browse files
committed
Fix migration number
2 parents e41eba5 + ac37ea0 commit 22933a2

File tree

10 files changed

+105
-25
lines changed

10 files changed

+105
-25
lines changed

api/restapi.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@ def get_settings(self, request):
925925
"blocked_guids": json.loads(settings[8]),
926926
"libbitcoin_server": get_value(
927927
"LIBBITCOIN_SERVERS_TESTNET", "testnet_server_custom")if self.protocol.testnet else get_value(
928-
"LIBBITCOIN_SERVERS", "server_custom"),
928+
"LIBBITCOIN_SERVERS", "mainnet_server_custom"),
929929
"seed": KeyChain(self.db).signing_key.encode(encoder=nacl.encoding.HexEncoder),
930930
"terms_conditions": "" if settings[9] is None else settings[9],
931931
"refund_policy": "" if settings[10] is None else settings[10],
@@ -1123,7 +1123,8 @@ def get_sales(self, request):
11231123
"thumbnail_hash": sale[6],
11241124
"buyer": sale[7],
11251125
"contract_type": sale[8],
1126-
"unread": sale[9]
1126+
"unread": sale[9],
1127+
"status_changed": False if sale[10] == 0 else True
11271128
}
11281129
sales_list.append(sale_json)
11291130
request.setHeader('content-type', "application/json")
@@ -1147,7 +1148,8 @@ def get_purchases(self, request):
11471148
"thumbnail_hash": purchase[6],
11481149
"vendor": purchase[7],
11491150
"contract_type": purchase[8],
1150-
"unread": purchase[9]
1151+
"unread": purchase[9],
1152+
"status_changed": False if purchase[10] == 0 else True
11511153
}
11521154
purchases_list.append(purchase_json)
11531155
request.setHeader('content-type', "application/json")
@@ -1185,23 +1187,30 @@ def get_order(self, request):
11851187
if os.path.exists(os.path.join(DATA_FOLDER, "purchases", "unfunded", filename)):
11861188
file_path = os.path.join(DATA_FOLDER, "purchases", "unfunded", filename)
11871189
status = self.db.purchases.get_status(request.args["order_id"][0])
1190+
self.db.purchases.status_changed(request.args["order_id"][0], 0)
11881191
elif os.path.exists(os.path.join(DATA_FOLDER, "purchases", "in progress", filename)):
11891192
file_path = os.path.join(DATA_FOLDER, "purchases", "in progress", filename)
11901193
status = self.db.purchases.get_status(request.args["order_id"][0])
1194+
self.db.purchases.status_changed(request.args["order_id"][0], 0)
11911195
elif os.path.exists(os.path.join(DATA_FOLDER, "purchases", "trade receipts", filename)):
11921196
file_path = os.path.join(DATA_FOLDER, "purchases", "trade receipts", filename)
11931197
status = self.db.purchases.get_status(request.args["order_id"][0])
1198+
self.db.purchases.status_changed(request.args["order_id"][0], 0)
11941199
elif os.path.exists(os.path.join(DATA_FOLDER, "store", "contracts", "unfunded", filename)):
11951200
file_path = os.path.join(DATA_FOLDER, "store", "contracts", "unfunded", filename)
11961201
status = self.db.sales.get_status(request.args["order_id"][0])
1202+
self.db.sales.status_changed(request.args["order_id"][0], 0)
11971203
elif os.path.exists(os.path.join(DATA_FOLDER, "store", "contracts", "in progress", filename)):
11981204
file_path = os.path.join(DATA_FOLDER, "store", "contracts", "in progress", filename)
11991205
status = self.db.sales.get_status(request.args["order_id"][0])
1206+
self.db.sales.status_changed(request.args["order_id"][0], 0)
12001207
elif os.path.exists(os.path.join(DATA_FOLDER, "store", "contracts", "trade receipts", filename)):
12011208
file_path = os.path.join(DATA_FOLDER, "store", "contracts", "trade receipts", filename)
12021209
status = self.db.sales.get_status(request.args["order_id"][0])
1210+
self.db.sales.status_changed(request.args["order_id"][0], 0)
12031211
elif os.path.exists(os.path.join(DATA_FOLDER, "cases", filename)):
12041212
file_path = os.path.join(DATA_FOLDER, "cases", filename)
1213+
self.db.cases.status_changed(request.args["order_id"][0], 0)
12051214
status = 4
12061215
else:
12071216
request.write(json.dumps({}, indent=4))
@@ -1329,7 +1338,8 @@ def get_cases(self, request):
13291338
"vendor": case[7],
13301339
"validation": json.loads(case[8]),
13311340
"status": "closed" if case[10] == 1 else "open",
1332-
"unread": case[11]
1341+
"unread": case[11],
1342+
"status_changed": False if case[12] == 0 else True
13331343
}
13341344
cases_list.append(purchase_json)
13351345
request.setHeader('content-type', "application/json")

db/datastore.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from protos import objects
1212
from protos.objects import Listings, Followers, Following
1313
from os.path import join
14-
from db.migrations import migration1, migration2, migration3, migration4, migration5, migration6
14+
from db.migrations import migration1, migration2, migration3, migration4, migration5, migration6, migration7
1515

1616

1717
class Database(object):
@@ -154,15 +154,15 @@ def _create_database(database_path):
154154

155155
cursor.execute('''CREATE TABLE purchases(id TEXT PRIMARY KEY, title TEXT, description TEXT,
156156
timestamp INTEGER, btc FLOAT, address TEXT, status INTEGER, outpoint BLOB, thumbnail BLOB, vendor TEXT,
157-
proofSig BLOB, contractType TEXT, unread INTEGER)''')
157+
proofSig BLOB, contractType TEXT, unread INTEGER, statusChanged INTEGER)''')
158158

159159
cursor.execute('''CREATE TABLE sales(id TEXT PRIMARY KEY, title TEXT, description TEXT,
160160
timestamp INTEGER, btc REAL, address TEXT, status INTEGER, thumbnail BLOB, outpoint BLOB, buyer TEXT,
161-
paymentTX TEXT, contractType TEXT, unread INTEGER)''')
161+
paymentTX TEXT, contractType TEXT, unread INTEGER, statusChanged INTEGER)''')
162162

163163
cursor.execute('''CREATE TABLE cases(id TEXT PRIMARY KEY, title TEXT, timestamp INTEGER, orderDate TEXT,
164164
btc REAL, thumbnail BLOB, buyer TEXT, vendor TEXT, validation TEXT, claim TEXT, status INTEGER,
165-
unread INTEGER)''')
165+
unread INTEGER, statusChanged INTEGER)''')
166166

167167
cursor.execute('''CREATE TABLE ratings(listing TEXT, ratingID TEXT, rating TEXT)''')
168168
cursor.execute('''CREATE INDEX index_listing ON ratings(listing);''')
@@ -224,6 +224,9 @@ def _run_migrations(self):
224224
migration6.migrate(self.PATH)
225225
elif version == 5:
226226
migration6.migrate(self.PATH)
227+
migration7.migrate(self.PATH)
228+
elif version == 6:
229+
migration7.migrate(self.PATH)
227230

228231

229232
class HashMap(object):
@@ -947,7 +950,7 @@ def get_all(self):
947950
conn = Database.connect_database(self.PATH)
948951
cursor = conn.cursor()
949952
cursor.execute('''SELECT id, title, description, timestamp, btc, status,
950-
thumbnail, vendor, contractType, unread FROM purchases ''')
953+
thumbnail, vendor, contractType, unread, statusChanged FROM purchases ''')
951954
ret = cursor.fetchall()
952955
conn.close()
953956
return ret
@@ -968,6 +971,14 @@ def update_status(self, order_id, status):
968971
conn.commit()
969972
conn.close()
970973

974+
def status_changed(self, order_id, status):
975+
conn = Database.connect_database(self.PATH)
976+
with conn:
977+
cursor = conn.cursor()
978+
cursor.execute('''UPDATE purchases SET statusChanged=? WHERE id=?;''', (status, order_id))
979+
conn.commit()
980+
conn.close()
981+
971982
def get_status(self, order_id):
972983
conn = Database.connect_database(self.PATH)
973984
cursor = conn.cursor()
@@ -1068,7 +1079,7 @@ def get_all(self):
10681079
conn = Database.connect_database(self.PATH)
10691080
cursor = conn.cursor()
10701081
cursor.execute('''SELECT id, title, description, timestamp, btc, status,
1071-
thumbnail, buyer, contractType, unread FROM sales ''')
1082+
thumbnail, buyer, contractType, unread, statusChanged FROM sales ''')
10721083
ret = cursor.fetchall()
10731084
conn.close()
10741085
return ret
@@ -1089,6 +1100,14 @@ def update_status(self, order_id, status):
10891100
conn.commit()
10901101
conn.close()
10911102

1103+
def status_changed(self, order_id, status):
1104+
conn = Database.connect_database(self.PATH)
1105+
with conn:
1106+
cursor = conn.cursor()
1107+
cursor.execute('''UPDATE sales SET statusChanged=? WHERE id=?;''', (status, order_id))
1108+
conn.commit()
1109+
conn.close()
1110+
10921111
def get_status(self, order_id):
10931112
conn = Database.connect_database(self.PATH)
10941113
cursor = conn.cursor()
@@ -1174,7 +1193,7 @@ def get_all(self):
11741193
conn = Database.connect_database(self.PATH)
11751194
cursor = conn.cursor()
11761195
cursor.execute('''SELECT id, title, timestamp, orderDate, btc, thumbnail,
1177-
buyer, vendor, validation, claim, status, unread FROM cases ''')
1196+
buyer, vendor, validation, claim, status, unread, statusChanged FROM cases ''')
11781197
ret = cursor.fetchall()
11791198
conn.close()
11801199
return ret
@@ -1209,6 +1228,14 @@ def update_status(self, order_id, status):
12091228
conn.commit()
12101229
conn.close()
12111230

1231+
def status_changed(self, order_id, status):
1232+
conn = Database.connect_database(self.PATH)
1233+
with conn:
1234+
cursor = conn.cursor()
1235+
cursor.execute('''UPDATE cases SET statusChanged=? WHERE id=?;''', (status, order_id))
1236+
conn.commit()
1237+
conn.close()
1238+
12121239

12131240
class Ratings(object):
12141241
"""

db/migrations/migration5.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import sqlite3
22

3-
43
def migrate(database_path):
54
print "migrating to db version 5"
65
conn = sqlite3.connect(database_path)

db/migrations/migration6.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,13 @@ def migrate(database_path):
77
conn.text_factory = str
88
cursor = conn.cursor()
99

10-
# create new table
11-
cursor.execute('''CREATE TABLE IF NOT EXISTS audit_shopping (
12-
audit_shopping_id integer PRIMARY KEY NOT NULL,
13-
shopper_guid text NOT NULL,
14-
contract_hash text,
15-
"timestamp" integer NOT NULL,
16-
action_id integer NOT NULL
17-
);''')
18-
cursor.execute('''CREATE INDEX IF NOT EXISTS shopper_guid_index ON audit_shopping (audit_shopping_id ASC);''')
19-
cursor.execute('''CREATE INDEX IF NOT EXISTS action_id_index ON audit_shopping (audit_shopping_id ASC);''')
10+
cursor.execute('''ALTER TABLE sales ADD COLUMN "statusChanged" INTEGER''')
11+
cursor.execute('''ALTER TABLE purchases ADD COLUMN "statusChanged" INTEGER''')
12+
cursor.execute('''ALTER TABLE cases ADD COLUMN "statusChanged" INTEGER''')
13+
14+
cursor.execute('''UPDATE purchases SET statusChanged = 0;''')
15+
cursor.execute('''UPDATE purchases SET statusChanged = 0;''')
16+
cursor.execute('''UPDATE purchases SET statusChanged = 0;''')
2017

2118
# update version
2219
cursor.execute('''PRAGMA user_version = 6''')

db/migrations/migration7.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import sqlite3
2+
3+
4+
def migrate(database_path):
5+
print "migrating to db version 7"
6+
conn = sqlite3.connect(database_path)
7+
conn.text_factory = str
8+
cursor = conn.cursor()
9+
10+
# create new table
11+
cursor.execute('''CREATE TABLE IF NOT EXISTS audit_shopping (
12+
audit_shopping_id integer PRIMARY KEY NOT NULL,
13+
shopper_guid text NOT NULL,
14+
contract_hash text,
15+
"timestamp" integer NOT NULL,
16+
action_id integer NOT NULL
17+
);''')
18+
cursor.execute('''CREATE INDEX IF NOT EXISTS shopper_guid_index ON audit_shopping (audit_shopping_id ASC);''')
19+
cursor.execute('''CREATE INDEX IF NOT EXISTS action_id_index ON audit_shopping (audit_shopping_id ASC);''')
20+
21+
# update version
22+
cursor.execute('''PRAGMA user_version = 7''')
23+
conn.commit()
24+
conn.close()

market/contracts.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ def accept_order_confirmation(self, notification_listener, confirmation_json=Non
516516

517517
# update the order status in the db
518518
self.db.purchases.update_status(contract_hash, 2)
519+
self.db.purchases.status_changed(contract_hash, 1)
519520
file_path = os.path.join(DATA_FOLDER, "purchases", "in progress", contract_hash + ".json")
520521

521522
# update the contract in the file system
@@ -770,6 +771,7 @@ def accept_receipt(self, notification_listener, blockchain, receipt_json=None):
770771
json.dumps(self.contract["buyer_receipt"]["receipt"]["rating"], indent=4))
771772

772773
if status == 2:
774+
self.db.sales.status_changed(order_id, 1)
773775
self.db.sales.update_status(order_id, 3)
774776
file_path = os.path.join(DATA_FOLDER, "store", "contracts", "trade receipts", order_id + ".json")
775777
with open(file_path, 'w') as outfile:
@@ -934,6 +936,7 @@ def payment_received(self):
934936
% order_id)
935937

936938
self.db.sales.update_status(order_id, 1)
939+
self.db.sales.status_changed(order_id, 1)
937940
self.db.sales.update_outpoint(order_id, json.dumps(self.outpoints))
938941
self.log.info("Received new order %s" % order_id)
939942

@@ -1105,6 +1108,7 @@ def process_refund(self, refund_json, blockchain, notification_listener):
11051108
self.log.info("broadcasting refund tx %s to network" % tx.get_hash())
11061109

11071110
self.db.purchases.update_status(order_id, 7)
1111+
self.db.purchases.status_changed(order_id, 1)
11081112
file_path = os.path.join(DATA_FOLDER, "purchases", "trade receipts", order_id + ".json")
11091113
with open(file_path, 'w') as outfile:
11101114
outfile.write(json.dumps(self.contract, indent=4))

market/moderation.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,11 @@ def process_dispute(contract, db, message_listener, notification_listener, testn
7878
p.avatar_hash = unhexlify(str(contract["dispute"]["info"]["avatar_hash"]))
7979

8080
if db.purchases.get_purchase(order_id) is not None:
81+
db.purchases.status_changed(order_id, 1)
8182
db.purchases.update_status(order_id, 4)
8283

8384
elif db.sales.get_sale(order_id) is not None:
85+
db.sales.status_changed(order_id, 1)
8486
db.sales.update_status(order_id, 4)
8587

8688
elif "moderators" in contract["vendor_offer"]["listing"]:
@@ -175,9 +177,11 @@ def close_dispute(resolution_json, db, message_listener, notification_listener,
175177
contract["dispute_resolution"] = resolution_json["dispute_resolution"]
176178

177179
if db.purchases.get_purchase(order_id) is not None:
180+
db.purchases.status_changed(order_id, 1)
178181
db.purchases.update_status(order_id, 5)
179182

180183
elif db.sales.get_sale(order_id) is not None:
184+
db.sales.status_changed(order_id, 1)
181185
db.sales.update_status(order_id, 5)
182186

183187
with open(file_path, 'wb') as outfile:

market/network.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
import os.path
1414
import pickle
1515
import time
16+
import struct
1617
from binascii import unhexlify
18+
from bitcoin.core import b2lx
1719
from collections import OrderedDict
1820
from config import DATA_FOLDER, TRANSACTION_FEE
1921
from dht.node import Node
@@ -953,6 +955,7 @@ def release_funds(self, order_id):
953955
This function should be called to release funds from a disputed contract after
954956
the moderator has resolved the dispute and provided his signature.
955957
"""
958+
956959
if os.path.exists(os.path.join(DATA_FOLDER, "purchases", "in progress", order_id + ".json")):
957960
file_path = os.path.join(DATA_FOLDER, "purchases", "in progress", order_id + ".json")
958961
outpoints = json.loads(self.db.purchases.get_outpoint(order_id))
@@ -983,6 +986,16 @@ def release_funds(self, order_id):
983986
["resolution"]["vendor_payout"]) * 100000000)),
984987
'address': vendor_address})
985988

989+
# version 0.2.1 and above ensure same sort order as moderator.
990+
o = []
991+
for s in contract["dispute_resolution"]["resolution"]["tx_signatures"]:
992+
if "outpoint" in s:
993+
for outpoint in outpoints:
994+
ser = outpoint["txid"] + b2lx(struct.pack(b"<I", outpoint["vout"]))
995+
if ser == s["outpoint"]:
996+
o.append(outpoint)
997+
if len(o) != 0:
998+
outpoints = o
986999
tx = BitcoinTransaction.make_unsigned(outpoints, outputs, testnet=self.protocol.multiplexer.testnet)
9871000
chaincode = contract["buyer_order"]["order"]["payment"]["chaincode"]
9881001
redeem_script = str(contract["buyer_order"]["order"]["payment"]["redeem_script"])

market/transactions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ def create_signature(self, privkey, reedem_script):
9797
sighash = SignatureHash(CScript(x(reedem_script)), self.tx, i, SIGHASH_ALL)
9898
signatures.append({
9999
"index": i,
100-
"signature": (seckey.sign(sighash) + struct.pack('<B', SIGHASH_ALL)).encode("hex")
100+
"signature": (seckey.sign(sighash) + struct.pack('<B', SIGHASH_ALL)).encode("hex"),
101+
"outpoint": b2lx(self.tx.vin[i].prevout.hash) + b2lx(struct.pack(b"<I", self.tx.vin[i].prevout.n))
101102
})
102103
return signatures
103104

openbazaard.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from twisted.python import log, logfile
3838
from txws import WebSocketFactory
3939

40+
4041
def run(*args):
4142
TESTNET = args[0]
4243
LOGLEVEL = args[1]
@@ -213,7 +214,7 @@ class Parser(object):
213214
def __init__(self, daemon):
214215
self.daemon = daemon
215216
parser = argparse.ArgumentParser(
216-
description='OpenBazaar-Server v0.2.0',
217+
description='OpenBazaar-Server v0.2.1',
217218
usage='''
218219
python openbazaard.py <command> [<args>]
219220
python openbazaard.py <command> --help
@@ -302,6 +303,6 @@ def print_splash_screen():
302303
print "\_______ / __/ \___ >___| /" + OKBLUE + "______ /(____ /_____ \(____ (____ /__|" + ENDC
303304
print " \/|__| \/ \/ " + OKBLUE + " \/ \/ \/ \/ \/" + ENDC
304305
print
305-
print "OpenBazaar Server v0.1 starting..."
306+
print "OpenBazaar Server v0.2.1 starting..."
306307

307308
Parser(OpenBazaard('/tmp/openbazaard.pid'))

0 commit comments

Comments
 (0)