-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAccountWiseLedger.py
148 lines (114 loc) · 5.77 KB
/
AccountWiseLedger.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
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
from Blockchain import *
class AccountWiseLedger(object):
def __init__(self, ownerID, subNetwork, powDifficulty=4, transactionBalance=0, transactionTask=None, transactionTaskHandler=None, blockchain=None):
self.__ownerID = ownerID
self.__subNetwork = subNetwork
self.__powDifficulty = powDifficulty
self.__transactionBalance = transactionBalance
self.__transactionTask = json.loads(json.dumps(transactionTask)) if transactionTask else {}
self.__transactionTaskHandler = json.loads(json.dumps(transactionTaskHandler)) if transactionTask else {}
self.__blockchain = Blockchain.createByJsonBytes(json.dumps(blockchain)) if blockchain else Blockchain(self.__ownerID, self.__powDifficulty)
@classmethod
def createByJsonBytes(cls, inputStr):
input = json.loads(inputStr)
return cls(input["ownerID"], input["subNetwork"], input["powDifficulty"], input["transactionBalance"], input["transactionTask"], input["transactionTaskHandler"], input["blockchain"])
def __str__(self):
return str(self.outputDict)
def __repr__(self):
return str(self.outputDict)
def __len__(self):
return len(self.__blockchain)
def setTransaction(self, task, handlerDict):
if task["senderID"] == self.__ownerID or task["receiverID"] == self.__ownerID:
self.__transactionTask = task
self.__transactionTaskHandler = handlerDict
self.__transactionBalance += task["amount"] if task["receiverID"] == self.__ownerID else -task["amount"]
return True
else:
return False
def createNewBlock(self, task, preHash, taskAbortSignal=False):
return Blockchain.createNewBlock(self.__powDifficulty, Blockchain.hash256(task), task["senderID"], task["receiverID"], task["amount"], preHash, taskAbortSignal)
def receiveResult(self, block):
if block["msg"] == "Task Abort":
self.__transactionTask = {}
self.__transactionTaskHandler = {}
self.__transactionBalance += block["amount"] if block["senderID"] == self.__ownerID else -block["amount"]
return False
elif self.__transactionTask["senderID"] == block["senderID"] and self.__transactionTask["receiverID"] == block["receiverID"] and self.__transactionTask["amount"] == block["amount"] and self.__blockchain.append(block):
self.__transactionTask = {}
self.__transactionTaskHandler = {}
self.__transactionBalance += block["amount"] if block["senderID"] == self.__ownerID else -block["amount"]
return True
return False
@property
def viewOwnerID(self):
return self.__ownerID
@property
def viewSubNetwork(self):
return self.__subNetwork
@property
def viewPowDifficulty(self):
return self.__powDifficulty
@property
def viewActualBalance(self):
return self.__blockchain.viewBalance
@property
def viewPendingBalance(self):
return self.__transactionBalance
@property
def viewPlanningBalance(self):
return self.__transactionBalance + self.__blockchain.viewBalance
@property
def viewTransactionTask(self):
return self.__transactionTask
@property
def viewTransactionTaskHandler(self):
return self.__transactionTaskHandler
@property
def viewLastBlock(self):
return self.__blockchain.viewLastBlock
@property
def viewBlockchain(self):
return self.__blockchain.viewBlockchain
@property
def outputDict(self):
ans = {
"ownerID": self.__ownerID,
"subNetwork": self.__subNetwork,
"powDifficulty": self.__powDifficulty,
"transactionBalance": self.__transactionBalance,
"transactionTask": self.__transactionTask,
"transactionTaskHandler": self.__transactionTaskHandler,
"blockchain": self.__blockchain.outputDict}
return ans
@property
def outputJsonBytes(self):
return json.dumps(self.outputDict).encode()
def __unitTest():
testAccount = AccountWiseLedger("testOwner", "A")
testTask = {
"senderID": "testSender",
"receiverID": "testOwner",
"amount": 1500
}
print("New Transaction Creation: ", testAccount.setTransaction(testTask, {"HandlerA": True, "HandlerB": True}))
print("Actual, Pending, Planning Balance: ", testAccount.viewActualBalance, testAccount.viewPendingBalance, testAccount.viewPlanningBalance)
testBlock = testAccount.createNewBlock(testTask, Blockchain.hash256(testAccount.viewLastBlock))
print("Transaction Handler: ", testAccount.viewTransactionTaskHandler.keys())
print("Block Append:", testAccount.receiveResult(testBlock))
print(testAccount, testAccount.viewActualBalance, testAccount.viewPendingBalance)
testTask = {
"senderID": "testOwner",
"receiverID": "testReceiver",
"amount": 500
}
print("New Transaction Creation: ", testAccount.setTransaction(testTask, {"HandlerA": True, "HandlerB": True}))
print("Actual, Pending, Planning Balance: ", testAccount.viewActualBalance, testAccount.viewPendingBalance, testAccount.viewPlanningBalance)
testBlock = testAccount.createNewBlock(testTask, Blockchain.hash256(testAccount.viewLastBlock))
print("Transaction Handler: ", testAccount.viewTransactionTaskHandler.keys())
print("Block Append:", testAccount.receiveResult(testBlock))
print(testAccount, testAccount.viewActualBalance, testAccount.viewPendingBalance)
print(AccountWiseLedger.createByJsonBytes(testAccount.outputJsonBytes))
return
if __name__ == "__main__":
__unitTest()