-
Notifications
You must be signed in to change notification settings - Fork 3
/
transactions.py
91 lines (66 loc) · 1.64 KB
/
transactions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import time
import json
import hashlib
from model import Model
from database import TransactionDB, LocalTransactionDB
class Transaction():
def __init__(self, sender, recipient, amount):
self.timestamp = int(time.time())
self.sender = sender
self.recipient = recipient
self.amount = amount
self.hash = self.gen_hash()
def gen_hash(self):
return hashlib.sha256((str(self.timestamp)).encode('utf-8')).hexdigest()
def addToLocalDB(self):
ldb = LocalTransactionDB()
transaction = {
'timestamp': self.timestamp,
'sender': self.sender,
'recipient': self.recipient,
'amount': self.amount,
'hash': self.hash,
'localIndex': ldb.getIndex()
}
ldb.insert(transaction)
return ldb.getIndex()
def writeToFile(trs):
with open('data/queue', 'r+') as f:
raw = f.readline()
if len(raw) > 0:
data = json.loads(raw)
else:
data = []
if isinstance(trs,list):
data = data + trs
else:
data.append(trs)
with open('data/queue', 'w+') as f:
f.write(json.dumps(data))
def addToTransferQueue_Hash(hash):
ldb = LocalTransactionDB()
trs = ldb.find(hash)
writeToFile(trs)
def addToTransferQueue_Index(localIndex):
ldb = LocalTransactionDB()
trs = ldb.find_with_index(localIndex)
writeToFile(trs)
def getTransferQueue():
f = open('data/queue', 'r+')
raw = f.readline()
f.close()
try:
data = json.loads(raw)
except:
data = []
return data
def getA():
f = open('data/queue', 'r+')
raw = f.readline()
f.close()
data = json.loads(raw)
return data
def emptyQueue():
f = open('data/queue', 'w')
f.write("")
f.close()