-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransaction_rules.py
More file actions
343 lines (262 loc) · 11 KB
/
Copy pathtransaction_rules.py
File metadata and controls
343 lines (262 loc) · 11 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
from blockchain import Transaction
from config import *
# True, when it succeed the test
def test_inside_open_transactions(trans : Transaction):
''' Test if another transaction by the sender is already in open state '''
for open_trans in statics.OPEN_TRANSACTIONS:
if open_trans.sender == trans.sender:
return True
return False
def test_inside_current_block(trans : Transaction):
''' Test if another transaction by the sender is in the current block '''
for open_trans in statics.CURRENT_BLOCK.transactions:
if open_trans.sender == trans.sender:
return True
return False
def test_generel_formal(trans : Transaction):
# TODO: Implement...
return True
def test_trans_id(trans : Transaction):
if len(trans.id) != TRANSACTION_ID_LEN:
return False
for block in (statics.CHAIN + [statics.PENDING_BLOCK]):
if not block is None:
for compare_trans in block.transactions:
if trans.id == compare_trans.id:
return False
return True
# *** Specific tests
class test_account_creation():
def __init__(self, trans : Transaction) -> None:
self.trans = trans
def result(self):
if self.test_formal() is False:
return False
if self.test_existent() is False:
return False
return True
def test_formal(self):
if len(self.trans.data) != 1:
return False
if not "msg_public_key" in self.trans.data:
return False
if isinstance(self.trans.data["msg_public_key"], str) is False:
return False
return True
def test_existent(self):
# check wheter the sender is already listed
for block in reversed(statics.CHAIN + [statics.PENDING_BLOCK]):
if block is None:
# when pending_block is none
continue
for trans in block.transactions:
if trans.sender == self.trans.sender:
return False
return True
class test_account_setting():
def __init__(self, trans : Transaction) -> None:
self.trans = trans
def result(self):
if self.test_formal() is False:
return False
return True
def test_formal(self):
# name, profile_image, short_description, long_description, links, location
if len(self.trans.data) != 6:
return False
if not "name" in self.trans.data or not "profile_image" in self.trans.data or not "links" in self.trans.data:
return False
if not "short_description" in self.trans.data or not "long_description" or not "location" in self.trans.data:
return False
# Name
if isinstance(self.trans.data["name"], str) is False:
return False
if len(self.trans.data["name"]) > AccountSettings.MAX_NAME_LEN:
return False
# Location
if isinstance(self.trans.data["location"], str) is False:
return False
if len(self.trans.data["location"]) > AccountSettings.MAX_LOCATION_STR_LEN:
return False
# Profile Image URL
if isinstance(self.trans.data["profile_image"], str) is False:
return False
if len(self.trans.data["profile_image"]) > AccountSettings.MAX_PROFILE_IMAGE_URL_LEN:
return False
# Short description
if isinstance(self.trans.data["short_description"], str) is False:
return False
if len(self.trans.data["short_description"]) > AccountSettings.MAX_SHORT_DESCRIPTION_LEN:
return False
# Long description
if isinstance(self.trans.data["long_description"], str) is False:
return False
if len(self.trans.data["long_description"]) > AccountSettings.MAX_LONG_DESCRIPTION_LEN:
return False
# Links
if isinstance(self.trans.data["links"], list) is False:
return False
if len(self.trans.data["links"]) > AccountSettings.MAX_LINK_COUNT:
return False
for link in self.trans.data["links"]:
if len(link) != 2:
return False
if not "url" in link or not "name" in link:
return False
if isinstance(link["url"], str) is False or len(link["url"]) >= 2000:
return False
if isinstance(link["name"], str) is False or len(link["name"]) > AccountSettings.MAX_LINK_NAME_LEN:
return False
return True
class test_transfer():
def __init__(self, trans : Transaction) -> None:
self.trans = trans
def result(self):
if self.test_formal() is False:
return False
if self.test_receiver() is False:
return False
if self.test_amount() is False:
return False
return True
def test_formal(self):
if len(self.trans.data) != 3:
return False
if not "receiver" in self.trans.data or not "amount" in self.trans.data or not "message" in self.trans.data:
return False
if isinstance(self.trans.data["receiver"], str) is False:
return False
if isinstance(self.trans.data["amount"], int) is False:
return False
if self.trans.data["amount"] < 1:
return False
if isinstance(self.trans.data["message"], dict) is False:
return False
if len(self.trans.data["message"]) != 2:
return False
for x in ["sender", "receiver"]:
if not "enc_aes_key" in self.trans.data["message"][x] or not "nonce" in self.trans.data["message"][x] or not "tag" in self.trans.data["message"][x] or not "ciphertext" in self.trans.data["message"][x]:
return False
if isinstance(self.trans.data["message"][x]["enc_aes_key"], str) is False or len(self.trans.data["message"][x]["enc_aes_key"]) > 5000:
return False
if isinstance(self.trans.data["message"][x]["nonce"], str) is False or len(self.trans.data["message"][x]["nonce"]) > 500:
return False
if isinstance(self.trans.data["message"][x]["tag"], str) is False or len(self.trans.data["message"][x]["tag"]) > 500:
return False
if isinstance(self.trans.data["message"][x]["ciphertext"], str) is False or len(self.trans.data["message"][x]["ciphertext"]) > 250:
return False
return True
def test_receiver(self):
# test if receiver exists
for block in (statics.CHAIN + [statics.PENDING_BLOCK]):
if block is None:
# Maybe pending block
continue
for trans in block.transactions:
if str(trans.sender) == self.trans.data["receiver"]:
return True
return False
def test_amount(self):
if self.trans.data["amount"] <= 0:
# Test negative or zero values
return False
# Check acc balance
balance = 1000 if TEST_MODE else 0
for block in (statics.CHAIN + [statics.PENDING_BLOCK]):
if block is None:
# Maybe pending block
continue
for trans_listed in block.transactions:
if trans_listed.op_name == "transfer":
if str(self.trans.sender) == str(trans_listed.sender):
balance = balance - trans_listed.data["amount"]
elif str(trans_listed.data["receiver"]) == str(self.trans.sender):
balance = balance + trans_listed.data["amount"]
if self.trans.data["amount"] > balance:
# Test if amount is available
return False
return True
class test_set_friend():
def __init__(self, trans : Transaction) -> None:
self.trans = trans
def result(self):
if self.test_formal() is False:
return False
if self.test_target() is False:
return False
if self.test_friends() is False:
return False
return True
def test_formal(self):
if len(self.trans.data) != 2:
return False
if not "target" in self.trans.data or not "type" in self.trans.data:
return False
if self.trans.data["type"] != "add" and self.trans.data["type"] != "remove":
return False
if isinstance(self.trans.data["target"], str) is False:
return False
return True
def test_target(self):
# test if receiver exists
for block in (statics.CHAIN + [statics.PENDING_BLOCK]):
if block is None:
# Maybe pending block
continue
for trans in block.transactions:
if str(trans.sender) == self.trans.data["target"]:
return True
return False
def test_friends(self):
'''Check if the friend is already registered'''
friends = []
for block in (statics.CHAIN + [statics.PENDING_BLOCK]):
# Get all friends
if block is None:
# Maybe pending block
continue
for trans in block.transactions:
if trans.op_name == "set_friend" and str(trans.sender) == str(self.trans.sender):
if trans.data["type"] == "add":
friends.append(trans.data["target"])
elif trans.data["type"] == "remove":
friends.remove(trans.data["target"])
if self.trans.data["type"] == "add":
# Check if inside
for friend in friends:
if friend == self.trans.data["target"]:
# Already in
return False
return True
if self.trans.data["type"] == "remove":
for friend in friends:
if friend == self.trans.data["target"]:
# Inside -> can be removed
return True
return False
def check_if_valid(trans : Transaction) -> bool:
if trans.prove_signature() is False:
# Singature wrong
return False
if test_inside_open_transactions(trans) or test_inside_current_block(trans):
# Trans sender got a Trans already in open state
return False
if test_generel_formal(trans) is False:
return False
if test_trans_id(trans) is False:
return False
if trans.op_name == "account_creation":
if test_account_creation(trans).result() is False:
return False
elif trans.op_name == "account_setting":
if test_account_setting(trans).result() is False:
return False
elif trans.op_name == "transfer":
if test_transfer(trans).result() is False:
return False
elif trans.op_name == "set_friend":
if test_set_friend(trans).result() is False:
return False
else:
return False
return True