forked from mrsmkl/eth-patricia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockchain.sol
290 lines (255 loc) · 9.44 KB
/
blockchain.sol
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
pragma solidity ^0.4.19;
import './util.sol';
// Contract for mirroring needed parts of the blockchain
contract Blockchain is Util {
mapping (uint => bytes32) block_hash;
struct BlockData {
bytes32 stateRoot;
bytes32 transactionRoot;
mapping (uint => bytes32) transactions; // element 1 means not found
mapping (address => bytes32) accounts; // element 1 means not found
uint numTransactions;
}
mapping (bytes32 => BlockData) block_data;
struct TransactionData {
address to;
address sender;
bytes data;
}
mapping (bytes32 => TransactionData) transactions;
struct AccountData {
bytes32 storageRoot;
mapping (bytes32 => bytes32) stuff;
mapping (bytes32 => bool) stuff_checked;
}
mapping (bytes32 => AccountData) accounts;
function storeHashes(uint n) public {
for (uint i = 1; i <= n; i++) block_hash[block.number-i] = block.blockhash(block.number-i);
}
function getBytes32(bytes rlp) internal pure returns (bytes32) {
require(rlp.length == 33);
bytes32 res;
assembly {
res := mload(add(33,rlp))
}
return res;
}
function getAddress(bytes rlp) internal pure returns (address) {
if (rlp.length == 0) return 0;
require(rlp.length == 21);
return address(readSize(rlp, 1, 20));
}
function storeHeader(uint n, bytes header) public {
// sanity check
require(rlpArrayLength(header, 0) == 15);
require(keccak256(header) == block_hash[n]);
BlockData storage dta = block_data[block_hash[n]];
dta.stateRoot = getBytes32(rlpFindBytes(header, 3));
dta.transactionRoot = getBytes32(rlpFindBytes(header, 4));
}
function transactionSender(bytes32 hash, bytes tr) public pure returns (address) {
// w
uint v = readInteger(rlpFindBytes(tr, 6));
// r
uint r = readInteger(rlpFindBytes(tr, 7));
// s
uint s = readInteger(rlpFindBytes(tr, 8));
return ecrecover(hash, uint8(v), bytes32(r), bytes32(s));
}
function updateNumTransactions(uint blk, uint num) public {
BlockData storage dta = block_data[block_hash[blk]];
require(uint(dta.transactions[num]) == 1);
require(uint(dta.transactions[num-1]) > 1);
dta.numTransactions = num + 1;
}
function storeTransaction(bytes tr) public {
// read all the fields of transaction
require(rlpArrayLength(tr, 0) == 9);
bytes[] memory d = new bytes[](6);
d[0] = rlpFindBytes(tr, 0); // nonce
d[1] = rlpFindBytes(tr, 1); // price
d[2] = rlpFindBytes(tr, 2); // gas
d[3] = rlpFindBytes(tr, 3); // to
d[4] = rlpFindBytes(tr, 4); // value
d[5] = rlpFindBytes(tr, 5); // data
uint len = d[0].length + d[1].length + d[2].length + d[3].length + d[4].length + d[5].length;
bytes32 hash = keccak256(arrayPrefix(len+3), d[0], d[1], d[2], d[3], d[4], d[5], byte(0x1c), bytes2(0x8080));
TransactionData storage tr_data = transactions[keccak256(tr)];
tr_data.sender = transactionSender(hash, tr);
tr_data.to = getAddress(d[3]);
tr_data.data = d[5]; // probably should remove RLP prefix
}
function storeAccount(bytes rlp) public {
// read all the fields of account
require(rlpArrayLength(rlp, 0) == 4);
AccountData storage a_data = accounts[keccak256(rlp)];
// 0 nonce
// 1 balance
// 2 storage
// 3 code
a_data.storageRoot = getBytes32(rlpFindBytes(rlp, 2));
}
enum State {
UNFINISHED,
NOTFOUND,
FOUND
}
enum Kind {
TRANSACTION,
ACCOUNT,
STORAGE
}
struct Session {
uint8[] key;
bytes32 wantHash;
State state;
address owner;
Kind kind;
bytes32 root;
uint tr; // tr number
bytes32 ptr; // storage pointer
address addr; // account address
}
mapping (bytes32 => Session) sessions;
function initTransaction(uint blk, uint n) public {
Session storage s = sessions[keccak256(msg.sender, blk, n)];
s.owner = msg.sender;
s.key = bytesToNibbles(rlpInteger(n));
s.kind = Kind.TRANSACTION;
s.root = block_hash[blk];
s.wantHash = block_data[s.root].transactionRoot;
s.tr = n;
}
function initAccount(uint blk, address addr) public {
Session storage s = sessions[keccak256(msg.sender, blk, addr)];
s.owner = msg.sender;
s.key = bytesToNibbles(bytes32ToBytes(keccak256(addr)));
s.kind = Kind.ACCOUNT;
s.root = block_hash[blk];
s.wantHash = block_data[s.root].stateRoot;
s.addr = addr;
}
function initStorage(bytes32 acct, bytes32 ptr) public {
Session storage s = sessions[keccak256(msg.sender, acct, ptr)];
s.owner = msg.sender;
s.key = bytesToNibbles(bytes32ToBytes(keccak256(ptr)));
s.kind = Kind.STORAGE;
s.root = acct;
s.wantHash = accounts[acct].storageRoot;
s.ptr = ptr;
}
function sessionNotFound(bytes32 id) internal {
Session storage s = sessions[id];
s.state = State.NOTFOUND;
if (s.kind == Kind.TRANSACTION) {
block_data[s.root].transactions[s.tr] = bytes32(uint(1));
}
else if (s.kind == Kind.ACCOUNT) {
block_data[s.root].accounts[s.addr] = bytes32(uint(1));
}
else if (s.kind == Kind.STORAGE) {
accounts[s.root].stuff_checked[s.ptr] = true;
}
}
function sessionFound(bytes32 id, bytes found) internal {
Session storage s = sessions[id];
s.state = State.FOUND;
if (s.kind == Kind.TRANSACTION) {
block_data[s.root].transactions[s.tr] = getBytes32(found);
}
else if (s.kind == Kind.ACCOUNT) {
block_data[s.root].accounts[s.addr] = getBytes32(found);
}
else if (s.kind == Kind.STORAGE) {
accounts[s.root].stuff[s.ptr] = getBytes32(found);
accounts[s.root].stuff_checked[s.ptr] = true;
}
}
function stepProof(bytes32 id, bytes p) public {
Session storage s = sessions[id];
require(s.owner == msg.sender);
require(s.state == State.UNFINISHED);
require(s.wantHash == keccak256(p));
// The key cannot be found here
if (p.length == 0) {
sessionNotFound(id);
return;
}
// Branch
// p[0] == 192+17 == 209
if (rlpArrayLength(p,0) == 17) {
if (s.key.length == 0) {
sessionFound(id, rlpFindBytes(p, 16));
}
else {
bytes memory child = rlpFindBytes(p, uint(uint8(s.key[0])));
s.key = slice(s.key, 1, s.key.length-1);
if (child.length == 33) {
child = sliceBytes(child,1,32);
s.wantHash = bytesToBytes32(child);
}
else s.wantHash = keccak256(child);
}
}
// Leaf or extension
// p[0] == 194
else if (rlpArrayLength(p,0) == 2) {
bool kind;
uint8[] memory nibbles;
(kind, nibbles) = unhp(rlpFindBytes(p, 0));
// seems like the kind can be ignored
uint mlen = matchingNibbleLength(nibbles, s.key);
if (mlen != nibbles.length) {
sessionNotFound(id);
return;
}
bytes memory child2 = rlpFindBytes(p, 1);
s.key = slice(s.key, mlen, s.key.length-mlen);
if (s.key.length == 0) {
sessionFound(id, child2);
}
else {
if (child2.length == 33) {
child2 = sliceBytes(child2,1,32);
s.wantHash = bytesToBytes32(child2);
}
else s.wantHash = keccak256(child2);
}
}
// Bad node
else {
revert();
}
}
// Accessors
function blockTransactions(uint blk) public view returns (uint) {
uint num = block_data[block_hash[blk]].numTransactions;
require (num > 0);
return num-1;
}
function transactionSender(uint blk, uint num) public view returns (address) {
bytes32 tr_hash = block_data[block_hash[blk]].transactions[num];
require (uint(tr_hash) > 1);
TransactionData storage tr = transactions[tr_hash];
return tr.sender;
}
function transactionReceiver(uint blk, uint num) public view returns (address) {
bytes32 tr_hash = block_data[block_hash[blk]].transactions[num];
require (uint(tr_hash) > 1);
TransactionData storage tr = transactions[tr_hash];
return tr.to;
}
function transactionData(uint blk, uint num) public view returns (bytes) {
bytes32 tr_hash = block_data[block_hash[blk]].transactions[num];
require (uint(tr_hash) > 1);
TransactionData storage tr = transactions[tr_hash];
return tr.data;
}
function accountStorage(uint blk, address addr, bytes32 ptr) public view returns (bytes32) {
bytes32 a_hash = block_data[block_hash[blk]].accounts[addr];
require (uint(a_hash) > 1);
AccountData storage a = accounts[a_hash];
require(a.stuff_checked[ptr]);
return a.stuff[ptr];
}
}