git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 682f6674a0fc8dad41078c05d2dd54e7d71c0a42

Merge pull request #33 from ewasm/blocks

Support defining block header details in testEnvironment
Alex 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.jschanged
testEnvironment.jschanged
tests/interface/coinbase.jsonadded
tests/interface/coinbase.wastadded
environment.jsView
@@ -1,15 +1,14 @@
11 const U256 = require('./u256.js')
22 const Address = require('./address.js')
33 const Block = require('./block.js')
4-const blockChain = require('./fakeBlockChain.js')
4+const fakeBlockChain = require('./fakeBlockChain.js')
55
66 module.exports = class Environment {
77 constructor (data) {
8- const block = new Block()
9-
108 const defaults = {
11- block: block,
9+ block: new Block(),
10+ blockchain: fakeBlockChain,
1211 // gas tank
1312 gasPrice: 0,
1413 gasLeft: 1000000,
1514 gasRefund: 0,
@@ -51,9 +50,9 @@
5150 return this.state.get(address.toString())['code']
5251 }
5352
5453 getBlockHash (height) {
55- return blockChain.getBlock(height).hash()
54+ return this.blockchain.getBlock(height).hash()
5655 }
5756
5857 // kernal
5958 create (code, value) {
testEnvironment.jsView
@@ -1,7 +1,9 @@
11 const Environment = require('./environment.js')
22 const U256 = require('./u256.js')
33 const Address = require('./address.js')
4+const Block = require('./block.js')
5+const ethUtil = require('ethereumjs-util')
46
57 module.exports = class TestEnvironment extends Environment {
68 constructor (data) {
79 super()
@@ -32,12 +34,8 @@
3234 if (data.caller) {
3335 self.caller = new Address(data.caller)
3436 }
3537
36- if (data.coinbase) {
37- self.coinbase = new Address(data.coinbase)
38- }
39-
4038 if (data.callValue) {
4139 self.callValue = new U256(data.callValue)
4240 }
4341
@@ -51,6 +49,34 @@
5149
5250 if (data.gasLeft) {
5351 self.gasLeft = data.gasLeft
5452 }
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+ }
5581 }
5682 }
tests/interface/coinbase.jsonView
@@ -1,0 +1,5 @@
1+{
2+ "block": {
3+ "coinbase": "0x5d48c1018904a172886829bbbd9c6f4a2d06c47b"
4+ }
5+}
tests/interface/coinbase.wastView
@@ -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