-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmartBlockchain.py
65 lines (55 loc) · 1.81 KB
/
SmartBlockchain.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
import json
import time
import hashlib
class SmartBlockchain:
def __init__(self, current_transactions=[], chain=[]):
self.current_transactions = current_transactions
self.chain = chain
self.genesis_block()
def hash(self, value):
return hashlib.sha256(value)
def hash_to_json(self):
return json.loads(self.last_block().decode("utf-8"))
def dict_to_hash(self, value):
return hashlib.sha256(
json.dumps(value, sort_keys=True).encode("utf-8")
).hexdigest()
def genesis_block(self):
block = {
"index": 0,
"timestamp": time.time(),
"transactions": self.current_transactions,
"previous_hash": 0,
}
self.chain.append(block)
def new_block(self):
previous_block = self.dict_to_hash(self.last_block())
block = {
"index": len(self.chain),
"timestamp": time.time(),
"transactions": self.current_transactions,
"previous_hash": previous_block,
}
self.chain.append(block)
self.current_transactions = []
def new_transaction(self, sender, amount, receiver):
fees = 0.02
self.sender = sender
self.amount = amount
self.receiver = receiver
transaction = {
"sender": self.sender,
"amount": self.amount,
"bpsc": "bpsc_wallet_address",
"amount_bpsc": self.amount * fees,
"receiver": self.receiver,
"amount_receiver": amount * (1 - 0.99995),
}
self.current_transactions.append(transaction)
return len(self.chain)
# @property
def last_block(self):
return self.chain[-1]
# @property
def last_block_json(self):
return json.dumps(self.last_block())