-
Notifications
You must be signed in to change notification settings - Fork 2
/
blockchain.py
230 lines (184 loc) · 8.8 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
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
from hashlib import sha256
import copy
import json
class Hashable:
@staticmethod
def hash_fn(string): return sha256(string).hexdigest()[:8]
def hash(self): return Hashable.hash_fn(self.__str__())
# An account is the main piece of state in Ethereum
# A regular externally owned account holds a balance (just like a Bitcoin account),
# but a Contract account also holds code and a data storage
class Account(Hashable):
EXTERNALLY_OWNED = 'externally_owned'
CONTRACT = 'contract'
def __init__(self, address, nonce, balance, code=None, storage={}, creation_tx_hash=None):
self.address = address
self.nonce = nonce
self.balance = balance
self.code = code
self.storage = storage
self.creation_tx_hash = creation_tx_hash
def type(self):
return self.EXTERNALLY_OWNED if self.code is None else self.CONTRACT
def __str__(self):
ret = 'Contract ' if self.type() == self.CONTRACT else ''
return ret + 'Account: {0}\nNonce: {1}, Balance: {2}\nCode: {3}\nStorage: {4}\n'.format(self.address, self.nonce, self.balance, self.code, self.storage)
def call_contract(self, args):
exec(self.code, {'storage': self.storage, 'args': args})
# The state of the entire world is just a set of Accounts
class WorldState(Hashable):
def __init__(self, accounts):
self.accounts = accounts
def __str__(self):
return '\n'.join([account.__str__() for account in self.accounts.values()])
def signature(self):
return self.__str__()
def account_created_by_tx_hash(self, creation_tx_hash):
return next((a for a in self.accounts.values() if a.creation_tx_hash == creation_tx_hash), None)
# A block is just a bunch of transactions and a pointer to the previous block
class Block(Hashable):
def __init__(self, transactions, prev_block_hash, end_state_signature):
self.prev_block_hash = prev_block_hash
self.transactions = transactions
self.end_state_signature = end_state_signature
def __str__(self):
stringified_txs = '\t'.join([tx.__str__() for tx in self.transactions.values()])
return '{0}\t{1}\t{2}'.format(self.prev_block_hash, self.end_state_signature, stringified_txs)
# A transaction is the main way to "do something" in Ethereum. Transactions can be used to:
# - Transfer ether
# - Create accounts (including contract accounts)
# - Call contract accounts (call the contract code and pass it some data)
#
# Each transaction changes the world state S via apply_transaction(S, TX) => S'
class Transaction(Hashable):
def __init__(self, sender_addr, receiver_addr, nonce, amount, data):
self.sender_addr = sender_addr
self.receiver_addr = receiver_addr
self.amount = amount
self.data = data
self.nonce = nonce
def __str__(self):
return '{0},{1},{2},{3}'.format(self.sender_addr, self.receiver_addr, self.amount, self.data)
# The above is mainly data models. Most of the interesting code begins here
ROOT_ACCOUNT_ADDR = 'deadbeef'
class BlockChain(Hashable):
PRE_GENESIS_BLOCK_HASH = '00000000'
def genesis_block(self):
return Block({}, self.PRE_GENESIS_BLOCK_HASH, self.genesis_world_state().signature())
def genesis_world_state(self):
return WorldState({ROOT_ACCOUNT_ADDR: Account(ROOT_ACCOUNT_ADDR, 0, 1000)})
## Toplevel BlockChain API
def __init__(self):
self.blocks = {self.genesis_block().hash(): self.genesis_block()}
self.tx_queue = {}
def enqueue_transaction(self, tx):
self.tx_queue[tx.hash()] = tx
def mine_new_block(self):
# We first create a fake block. We just use it to calculate the end state signature.
block_without_end_state_sig = Block(self.tx_queue, self.last_block().hash(), 'REPLACE_ME')
sig = self.end_state_signature(block_without_end_state_sig)
# Now that we have the end state sig, we commit the TXs, add the block to the chain, and empty the TX queue
block = Block(self.tx_queue, self.last_block().hash(), sig)
self.add_block(block)
self.empty_tx_queue()
return block
def empty_tx_queue(self):
self.tx_queue = {}
def add_block(self, block):
if not self.is_block_valid(block):
print('New block is invalid')
return
self.blocks[block.hash()] = block
def find_block_by(self, fun): return next((b for b in self.blocks.values() if fun(b)), None)
def last_block(self):
current_block = self.genesis_block()
while True:
next_block = self.find_block_by((lambda b: b.prev_block_hash == current_block.hash()))
if next_block is None:
break
current_block = next_block
return current_block
def end_state(self): return self.end_state_for_block(self.last_block())
## Block methods
def is_block_valid(self, block):
if block.hash() == self.genesis_block().hash():
return True
# Check that the previous block is valid
if (block.prev_block_hash not in self.blocks or
not self.is_block_valid(self.blocks[block.prev_block_hash])):
print('Previous block w/ hash {0} not valid'.format(block.prev_block_hash))
return False
# TODO: check timestamp
# TODO: Check difficulty, block number, tx root
# TODO: Check proof of work
return self.end_state_signature(block) == block.end_state_signature
def end_state_for_block(self, block):
if block.hash() == self.genesis_block().hash():
return self.genesis_world_state()
state = self.end_state_for_block(self.blocks[block.prev_block_hash])
for tx in block.transactions.values():
state = self.apply_transaction(state, tx)
return state
def end_state_signature(self, block): return self.end_state_for_block(block).signature()
## Transaction Methods
def apply_transaction(self, state, tx):
# So apply_transaction doesn't mutate anything
state = copy.deepcopy(state)
sender_account = state.accounts[tx.sender_addr]
if tx.nonce != sender_account.nonce:
raise Exception('Transaction nonce must match that of sender account')
sender_account.nonce += 1
# TODO: check well formed tx
# TODO: do GAS calculations
if tx.amount > sender_account.balance:
raise Exception('{0} only has {1} balance. Not enough to cover {2} amount'.format(tx.sender_addr,
sender_account.balance, tx.amount))
# A None receiver_addr signals that this is a Contract Creation!
if tx.receiver_addr is None:
if tx.data is None:
raise Exception('Contract creation must provide code via the data argument')
new_contract = Account(Hashable.hash_fn(tx.data), 0, 0, tx.data, {}, tx.hash())
state.accounts[new_contract.address] = new_contract
return state
# If we're trying to send ether to an account that doesn't exist yet, create it
if tx.receiver_addr not in state.accounts:
state.accounts[tx.receiver_addr] = Account(tx.receiver_addr, 0, 0, None, {}, tx.hash())
# Move the ether
sender_account.balance -= tx.amount
receiver_account = state.accounts[tx.receiver_addr]
receiver_account.balance += tx.amount
# Call the contract code if the receiver account is a Contract
if receiver_account.type() == Account.CONTRACT:
# load data dict from string and call contract
receiver_account.call_contract(None if tx.data is None else json.loads(tx.data))
return state
# Main
blockchain = BlockChain()
print('Initial world state is now:')
print(blockchain.end_state())
# REPL
while True:
try:
input_line = raw_input('Submit a transaction formatted as: from_addr to_addr nonce amount \'data\'\n')
from_addr, to_addr, nonce, amount, data = input_line.split(' ', 4)
print(data)
# Typefy inputs
def nonify(string): return None if string == 'None' else string
to_addr, nonce, amount, data = nonify(to_addr), int(nonce), int(amount), nonify(data.strip("'"))
# Enqueue transaction and mine new block
blockchain.enqueue_transaction(Transaction(from_addr, to_addr, nonce, amount, data))
block = blockchain.mine_new_block()
[tx] = block.transactions.values()
end_state = blockchain.end_state()
except KeyboardInterrupt:
break
except Exception as e:
print('Invalid transaction. Try again')
print(e)
blockchain.empty_tx_queue()
continue
print('##################')
print('\nTransaction submitted')
print('Mined new block {0}\n It contains one transaction {1}'.format(block.hash(), tx.hash()))
print('\nThe world state is now:')
print(end_state)