lint db
This commit is contained in:
parent
5604b1b064
commit
f8b7530702
26
xcat/db.py
26
xcat/db.py
|
@ -1,10 +1,7 @@
|
|||
import plyvel
|
||||
import json
|
||||
# import binascii
|
||||
# import sys
|
||||
# import ast
|
||||
from xcat.trades import *
|
||||
from xcat.utils import *
|
||||
import xcat.utils as utils
|
||||
from xcat.trades import Trade, Contract
|
||||
|
||||
db = plyvel.DB('/tmp/xcatDB', create_if_missing=True)
|
||||
preimageDB = plyvel.DB('/tmp/preimageDB', create_if_missing=True)
|
||||
|
@ -20,7 +17,7 @@ def create(trade, tradeid):
|
|||
trade = json.dumps(trade)
|
||||
else:
|
||||
trade = trade.toJSON()
|
||||
db.put(b(tradeid), b(trade))
|
||||
db.put(utils.b(tradeid), utils.b(trade))
|
||||
|
||||
|
||||
# Uses the funding txid as the key to save trade
|
||||
|
@ -29,11 +26,11 @@ def createByFundtx(trade):
|
|||
# # Save trade by initiating txid
|
||||
jt = json.loads(trade)
|
||||
txid = jt['sell']['fund_tx']
|
||||
db.put(b(txid), b(trade))
|
||||
db.put(utils.b(txid), utils.b(trade))
|
||||
|
||||
|
||||
def get(tradeid):
|
||||
rawtrade = db.get(b(tradeid))
|
||||
rawtrade = db.get(utils.b(tradeid))
|
||||
tradestr = str(rawtrade, 'utf-8')
|
||||
trade = instantiate(tradestr)
|
||||
return trade
|
||||
|
@ -42,7 +39,10 @@ def get(tradeid):
|
|||
def instantiate(trade):
|
||||
if type(trade) == str:
|
||||
tradestr = json.loads(trade)
|
||||
trade = Trade(buy=Contract(tradestr['buy']), sell=Contract(tradestr['sell']), commitment=tradestr['commitment'])
|
||||
trade = Trade(
|
||||
buy=Contract(tradestr['buy']),
|
||||
sell=Contract(tradestr['sell']),
|
||||
commitment=tradestr['commitment'])
|
||||
return trade
|
||||
|
||||
#############################################
|
||||
|
@ -52,11 +52,11 @@ def instantiate(trade):
|
|||
|
||||
# Stores secret locally in key/value store by tradeid
|
||||
def save_secret(tradeid, secret):
|
||||
res = preimageDB.put(b(tradeid), b(secret))
|
||||
preimageDB.put(utils.b(tradeid), utils.b(secret))
|
||||
|
||||
|
||||
def get_secret(tradeid):
|
||||
secret = preimageDB.get(b(tradeid))
|
||||
secret = preimageDB.get(utils.b(tradeid))
|
||||
secret = str(secret, 'utf-8')
|
||||
return secret
|
||||
|
||||
|
@ -69,7 +69,7 @@ def dump():
|
|||
results = []
|
||||
with db.iterator() as it:
|
||||
for k, v in it:
|
||||
j = json.loads(x2s(b2x(v)))
|
||||
j = json.loads(utils.x2s(utils.b2x(v)))
|
||||
results.append((str(k, 'utf-8'), j))
|
||||
return results
|
||||
|
||||
|
@ -78,7 +78,7 @@ def print_entries():
|
|||
it = db.iterator()
|
||||
with db.iterator() as it:
|
||||
for k, v in it:
|
||||
j = json.loads(x2s(b2x(v)))
|
||||
j = json.loads(utils.x2s(utils.b2x(v)))
|
||||
print("Key:", k)
|
||||
print('val: ', j)
|
||||
# print('sell: ', j['sell'])
|
||||
|
|
Loading…
Reference in New Issue