-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockchain.py
62 lines (49 loc) · 1.84 KB
/
blockchain.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
from collections import defaultdict
from block import Block
NUM_ACCOUNTS = 2
# the Blockchain class holds the chain itself and a ledger of address balances.
class Blockchain():
#constructor
def __init__(self):
self.chain = []
self.state = defaultdict(int)
self.current_block = 0
# gets the balance of an address
def get_balance(self, addr):
return self.state[addr]
# adds a new block to the Blockchain
def add_block(self, b):
if not self.validate_block(b):
return False
self.chain.append(b)
self.update_state(b)
self.current_block = b.num
return True
# gets a Block from the Blockchain given the block number 'num'
def get_block(self, num):
for b in self.chain:
if b.num == num:
return b
# validates that a Block meets the criteria to be part of the chain
def validate_block(self, b):
if not b:
return False
if not isinstance(b, Block):
return False
if b.prev != self.current_block:
return False
if b.num <= self.current_block:
return False
return True
# updates the Blockchain's ledger of address balances given a list of transactions from an incoming Block
def update_state(self, b):
for tx in b.transactions:
if self.state[tx.sender] == 0:
print("[transaction #%s]: failed due to low balance"%(tx.id))
continue
self.state[tx.sender] -= 1
self.state[tx.receiver] += 1
print("[transaction #%s]: success [%s -> %s]"%(tx.id, tx.sender, tx.receiver))
# (helper function) gives an address some balance
def faucet(self, addr):
self.state[addr] += 100