Commit 2541696799107df52fceda708dc8197d86ab843f
made blockhash async
wanderer committed on 10/31/2016, 1:25:27 PMParent: 125f59672710cb666380d7b6804357d3da16cff4
Files changed
environment.js | changed |
fakeBlockChain.js | changed |
interface.js | changed |
testEnvironment.js | changed |
environment.js | ||
---|---|---|
@@ -69,11 +69,12 @@ | ||
69 | 69 | // return Uint8Array.from(new Buffer([])) |
70 | 70 | // } |
71 | 71 | } |
72 | 72 | |
73 | - getBlockHash (height) { | |
73 | + async getBlockHash (height) { | |
74 | 74 | // return this.blockchain.getBlock(height).hash() |
75 | - return this.root.getBlockAt(height).then(block => block.hash()) | |
75 | + const block = await this.root.getBlockAt(height) | |
76 | + return block.hash() | |
76 | 77 | } |
77 | 78 | |
78 | 79 | set createHandler (value) { |
79 | 80 | this.createhandler = value |
fakeBlockChain.js | ||
---|---|---|
@@ -1,12 +1,13 @@ | ||
1 | 1 | const utils = require('ethereumjs-util') |
2 | +const U256 = require('./deps/u256.js') | |
2 | 3 | |
3 | 4 | module.exports = { |
4 | 5 | getBlock: (n) => { |
5 | 6 | const hash = utils.sha3(new Buffer(utils.bufferToInt(n).toString())) |
6 | 7 | const block = { |
7 | 8 | hash: () => { |
8 | - return hash | |
9 | + return new U256(hash) | |
9 | 10 | } |
10 | 11 | } |
11 | 12 | return block |
12 | 13 | } |
interface.js | ||
---|---|---|
@@ -263,20 +263,27 @@ | ||
263 | 263 | * Gets the hash of one of the 256 most recent complete blocks. |
264 | 264 | * @param {integer} number which block to load |
265 | 265 | * @param {integer} offset the offset to load the hash into |
266 | 266 | */ |
267 | - getBlockHash (number, offset) { | |
267 | + getBlockHash (number, offset, cbOffset) { | |
268 | 268 | this.takeGas(20) |
269 | 269 | |
270 | 270 | const diff = this.environment.block.number - number |
271 | - let hash | |
271 | + let opPromise | |
272 | 272 | |
273 | 273 | if (diff > 256 || diff <= 0) { |
274 | - hash = new U256(0) | |
274 | + opPromise = Promise.resolve(new U256(0)) | |
275 | 275 | } else { |
276 | - hash = new U256(this.environment.getBlockHash(number)) | |
276 | + opPromise = this.environment.getBlockHash(number) | |
277 | 277 | } |
278 | - this.setMemory(offset, U256_SIZE_BYTES, hash.toMemory()) | |
278 | + | |
279 | + // wait for all the prevouse async ops to finish before running the callback | |
280 | + this.kernel._runningOps = Promise.all([this.kernel._runningOps, opPromise]) | |
281 | + .then(values => { | |
282 | + const hash = values.pop() | |
283 | + this.setMemory(offset, U256_SIZE_BYTES, hash.toMemory()) | |
284 | + this.module.exports[cbOffset.toString()]() | |
285 | + }) | |
279 | 286 | } |
280 | 287 | |
281 | 288 | /** |
282 | 289 | * Gets the block’s beneficiary address and loads into memory. |
testEnvironment.js | ||
---|---|---|
@@ -1,82 +1,9 @@ | ||
1 | 1 | const Environment = require('./environment.js') |
2 | -const U256 = require('./u256.js') | |
3 | -const Address = require('./address.js') | |
4 | -const Block = require('./block.js') | |
5 | -const ethUtil = require('ethereumjs-util') | |
2 | +const fakeBlockchain = require('./fakeBlockChain') | |
6 | 3 | |
7 | 4 | module.exports = class TestEnvironment extends Environment { |
8 | - constructor (data) { | |
9 | - super() | |
10 | 5 | |
11 | - if (typeof data === 'string') { | |
12 | - data = JSON.parse(data) | |
13 | - } | |
14 | - | |
15 | - let self = this | |
16 | - | |
17 | - if (data.accounts) { | |
18 | - data.accounts.forEach((account) => { | |
19 | - let tmp = account[1] | |
20 | - self.addAccount(new Address(account[0]), { | |
21 | - balance: new U256(tmp.balance) | |
22 | - }) | |
23 | - }) | |
24 | - } | |
25 | - | |
26 | - if (data.address) { | |
27 | - self.address = new Address(data.address) | |
28 | - } | |
29 | - | |
30 | - if (data.origin) { | |
31 | - self.origin = new Address(data.origin) | |
32 | - } | |
33 | - | |
34 | - if (data.caller) { | |
35 | - self.caller = new Address(data.caller) | |
36 | - } | |
37 | - | |
38 | - if (data.callValue) { | |
39 | - self.callValue = new U256(data.callValue) | |
40 | - } | |
41 | - | |
42 | - if (data.callData) { | |
43 | - self.callData = Uint8Array.from(new Buffer(data.callData, 'hex')) | |
44 | - } | |
45 | - | |
46 | - if (data.gasPrice) { | |
47 | - self.gasPrice = data.gasPrice | |
48 | - } | |
49 | - | |
50 | - if (data.gasLeft) { | |
51 | - self.gasLeft = data.gasLeft | |
52 | - } | |
53 | - | |
54 | - if (data.block) { | |
55 | - let block = {} | |
56 | - | |
57 | - if (data.block.blockNumber) { | |
58 | - block.number = ethUtil.toBuffer(data.block.blockNumber) | |
59 | - } | |
60 | - | |
61 | - if (data.block.gasLimit) { | |
62 | - block.gasLimit = ethUtil.toBuffer(data.block.gasLimit) | |
63 | - } | |
64 | - | |
65 | - if (data.block.difficulty) { | |
66 | - block.difficulty = ethUtil.toBuffer(data.block.difficulty) | |
67 | - } | |
68 | - | |
69 | - if (data.block.timestamp) { | |
70 | - block.timestamp = ethUtil.toBuffer(data.block.timestam) | |
71 | - } | |
72 | - | |
73 | - if (data.block.coinbase) { | |
74 | - block.coinbase = ethUtil.toBuffer(data.block.coinbase) | |
75 | - } | |
76 | - | |
77 | - if (Object.keys(block).length > 0) { | |
78 | - self.block = new Block({ header: block, transactions: [], uncleHeaders: [] }) | |
79 | - } | |
80 | - } | |
6 | + async getBlockHash (height) { | |
7 | + return fakeBlockchain.getBlock(height).hash() | |
81 | 8 | } |
82 | 9 | } |
Built with git-ssb-web