Commit 682f6674a0fc8dad41078c05d2dd54e7d71c0a42
Merge pull request #33 from ewasm/blocks
Support defining block header details in testEnvironmentAlex Beregszaszi authored on 8/23/2016, 1:39:53 AM
GitHub committed on 8/23/2016, 1:39:53 AM
Parent: ffc7e461609ab3a4b2c3a041acb1124e6fc1c311
Parent: 59095b0502d442d94aafa6267d3d4e3cb01d7ba6
Files changed
environment.js | changed |
testEnvironment.js | changed |
tests/interface/coinbase.json | added |
tests/interface/coinbase.wast | added |
environment.js | ||
---|---|---|
@@ -1,15 +1,14 @@ | ||
1 | 1 | const U256 = require('./u256.js') |
2 | 2 | const Address = require('./address.js') |
3 | 3 | const Block = require('./block.js') |
4 | -const blockChain = require('./fakeBlockChain.js') | |
4 | +const fakeBlockChain = require('./fakeBlockChain.js') | |
5 | 5 | |
6 | 6 | module.exports = class Environment { |
7 | 7 | constructor (data) { |
8 | - const block = new Block() | |
9 | - | |
10 | 8 | const defaults = { |
11 | - block: block, | |
9 | + block: new Block(), | |
10 | + blockchain: fakeBlockChain, | |
12 | 11 | // gas tank |
13 | 12 | gasPrice: 0, |
14 | 13 | gasLeft: 1000000, |
15 | 14 | gasRefund: 0, |
@@ -51,9 +50,9 @@ | ||
51 | 50 | return this.state.get(address.toString())['code'] |
52 | 51 | } |
53 | 52 | |
54 | 53 | getBlockHash (height) { |
55 | - return blockChain.getBlock(height).hash() | |
54 | + return this.blockchain.getBlock(height).hash() | |
56 | 55 | } |
57 | 56 | |
58 | 57 | // kernal |
59 | 58 | create (code, value) { |
testEnvironment.js | ||
---|---|---|
@@ -1,7 +1,9 @@ | ||
1 | 1 | const Environment = require('./environment.js') |
2 | 2 | const U256 = require('./u256.js') |
3 | 3 | const Address = require('./address.js') |
4 | +const Block = require('./block.js') | |
5 | +const ethUtil = require('ethereumjs-util') | |
4 | 6 | |
5 | 7 | module.exports = class TestEnvironment extends Environment { |
6 | 8 | constructor (data) { |
7 | 9 | super() |
@@ -32,12 +34,8 @@ | ||
32 | 34 | if (data.caller) { |
33 | 35 | self.caller = new Address(data.caller) |
34 | 36 | } |
35 | 37 | |
36 | - if (data.coinbase) { | |
37 | - self.coinbase = new Address(data.coinbase) | |
38 | - } | |
39 | - | |
40 | 38 | if (data.callValue) { |
41 | 39 | self.callValue = new U256(data.callValue) |
42 | 40 | } |
43 | 41 | |
@@ -51,6 +49,34 @@ | ||
51 | 49 | |
52 | 50 | if (data.gasLeft) { |
53 | 51 | self.gasLeft = data.gasLeft |
54 | 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 | + } | |
55 | 81 | } |
56 | 82 | } |
tests/interface/coinbase.json | ||
---|---|---|
@@ -1,0 +1,5 @@ | ||
1 | +{ | |
2 | + "block": { | |
3 | + "coinbase": "0x5d48c1018904a172886829bbbd9c6f4a2d06c47b" | |
4 | + } | |
5 | +} |
tests/interface/coinbase.wast | ||
---|---|---|
@@ -1,0 +1,18 @@ | ||
1 | +;; starts with a coinbase of 5d48c1018904a172886829bbbd9c6f4a2d06c47b | |
2 | +(module | |
3 | + (memory 1) | |
4 | + | |
5 | + (import $coinbase "ethereum" "getBlockCoinbase" (param i32)) | |
6 | + (export "test" 0) | |
7 | + (export "a" memory) | |
8 | + (func | |
9 | + (block | |
10 | + ;; loads the coinbase into memory | |
11 | + (call_import $coinbase (i32.const 0)) | |
12 | + (if (i64.eq (i64.load (i32.const 0)) (i64.const 0x72a1048901c1485d)) ;; big endian | |
13 | + (return) | |
14 | + ) | |
15 | + (unreachable) | |
16 | + ) | |
17 | + ) | |
18 | +) |
Built with git-ssb-web