git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 2541696799107df52fceda708dc8197d86ab843f

made blockhash async

wanderer committed on 10/31/2016, 1:25:27 PM
Parent: 125f59672710cb666380d7b6804357d3da16cff4

Files changed

environment.jschanged
fakeBlockChain.jschanged
interface.jschanged
testEnvironment.jschanged
environment.jsView
@@ -69,11 +69,12 @@
6969 // return Uint8Array.from(new Buffer([]))
7070 // }
7171 }
7272
73- getBlockHash (height) {
73+ async getBlockHash (height) {
7474 // 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()
7677 }
7778
7879 set createHandler (value) {
7980 this.createhandler = value
fakeBlockChain.jsView
@@ -1,12 +1,13 @@
11 const utils = require('ethereumjs-util')
2+const U256 = require('./deps/u256.js')
23
34 module.exports = {
45 getBlock: (n) => {
56 const hash = utils.sha3(new Buffer(utils.bufferToInt(n).toString()))
67 const block = {
78 hash: () => {
8- return hash
9+ return new U256(hash)
910 }
1011 }
1112 return block
1213 }
interface.jsView
@@ -263,20 +263,27 @@
263263 * Gets the hash of one of the 256 most recent complete blocks.
264264 * @param {integer} number which block to load
265265 * @param {integer} offset the offset to load the hash into
266266 */
267- getBlockHash (number, offset) {
267+ getBlockHash (number, offset, cbOffset) {
268268 this.takeGas(20)
269269
270270 const diff = this.environment.block.number - number
271- let hash
271+ let opPromise
272272
273273 if (diff > 256 || diff <= 0) {
274- hash = new U256(0)
274+ opPromise = Promise.resolve(new U256(0))
275275 } else {
276- hash = new U256(this.environment.getBlockHash(number))
276+ opPromise = this.environment.getBlockHash(number)
277277 }
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+ })
279286 }
280287
281288 /**
282289 * Gets the block’s beneficiary address and loads into memory.
testEnvironment.jsView
@@ -1,82 +1,9 @@
11 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')
63
74 module.exports = class TestEnvironment extends Environment {
8- constructor (data) {
9- super()
105
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()
818 }
829 }

Built with git-ssb-web