|
|
|
@ -1,12 +1,13 @@ |
|
|
|
|
|
|
|
|
|
const Blocks = function (executionContext, _options) { |
|
|
|
|
class Blocks { |
|
|
|
|
constructor (executionContext, _options) { |
|
|
|
|
this.executionContext = executionContext |
|
|
|
|
const options = _options || {} |
|
|
|
|
this.coinbase = options.coinbase || '0x0000000000000000000000000000000000000000' |
|
|
|
|
this.blockNumber = 0 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Blocks.prototype.methods = function () { |
|
|
|
|
methods () { |
|
|
|
|
return { |
|
|
|
|
eth_getBlockByNumber: this.eth_getBlockByNumber.bind(this), |
|
|
|
|
eth_gasPrice: this.eth_gasPrice.bind(this), |
|
|
|
@ -21,7 +22,7 @@ Blocks.prototype.methods = function () { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Blocks.prototype.eth_getBlockByNumber = function (payload, cb) { |
|
|
|
|
eth_getBlockByNumber (payload, cb) { |
|
|
|
|
let blockIndex = payload.params[0] |
|
|
|
|
if (blockIndex === 'latest') { |
|
|
|
|
blockIndex = this.executionContext.latestBlockNumber |
|
|
|
@ -34,22 +35,22 @@ Blocks.prototype.eth_getBlockByNumber = function (payload, cb) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let b = { |
|
|
|
|
'number': toHex(block.header.number), |
|
|
|
|
'hash': toHex(block.hash()), |
|
|
|
|
'parentHash': toHex(block.header.parentHash), |
|
|
|
|
'nonce': toHex(block.header.nonce), |
|
|
|
|
'number': this.toHex(block.header.number), |
|
|
|
|
'hash': this.toHex(block.hash()), |
|
|
|
|
'parentHash': this.toHex(block.header.parentHash), |
|
|
|
|
'nonce': this.toHex(block.header.nonce), |
|
|
|
|
'sha3Uncles': '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347', |
|
|
|
|
'logsBloom': '0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331', |
|
|
|
|
'transactionsRoot': '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', |
|
|
|
|
'stateRoot': toHex(block.header.stateRoot), |
|
|
|
|
'stateRoot': this.toHex(block.header.stateRoot), |
|
|
|
|
'miner': this.coinbase, |
|
|
|
|
'difficulty': toHex(block.header.difficulty), |
|
|
|
|
'totalDifficulty': toHex(block.header.totalDifficulty), |
|
|
|
|
'extraData': toHex(block.header.extraData), |
|
|
|
|
'difficulty': this.toHex(block.header.difficulty), |
|
|
|
|
'totalDifficulty': this.toHex(block.header.totalDifficulty), |
|
|
|
|
'extraData': this.toHex(block.header.extraData), |
|
|
|
|
'size': '0x027f07', // 163591
|
|
|
|
|
'gasLimit': toHex(block.header.gasLimit), |
|
|
|
|
'gasUsed': toHex(block.header.gasUsed), |
|
|
|
|
'timestamp': toHex(block.header.timestamp), |
|
|
|
|
'gasLimit': this.toHex(block.header.gasLimit), |
|
|
|
|
'gasUsed': this.toHex(block.header.gasUsed), |
|
|
|
|
'timestamp': this.toHex(block.header.timestamp), |
|
|
|
|
'transactions': block.transactions.map((t) => '0x' + t.hash().toString('hex')), |
|
|
|
|
'uncles': [] |
|
|
|
|
} |
|
|
|
@ -57,32 +58,32 @@ Blocks.prototype.eth_getBlockByNumber = function (payload, cb) { |
|
|
|
|
cb(null, b) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function toHex (value) { |
|
|
|
|
toHex (value) { |
|
|
|
|
if (!value) return '0x0' |
|
|
|
|
let v = value.toString('hex') |
|
|
|
|
return ((v === '0x' || v === '') ? '0x0' : ('0x' + v)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Blocks.prototype.eth_getBlockByHash = function (payload, cb) { |
|
|
|
|
eth_getBlockByHash (payload, cb) { |
|
|
|
|
var block = this.executionContext.blocks[payload.params[0]] |
|
|
|
|
|
|
|
|
|
let b = { |
|
|
|
|
'number': toHex(block.header.number), |
|
|
|
|
'hash': toHex(block.hash()), |
|
|
|
|
'parentHash': toHex(block.header.parentHash), |
|
|
|
|
'nonce': toHex(block.header.nonce), |
|
|
|
|
'number': this.toHex(block.header.number), |
|
|
|
|
'hash': this.toHex(block.hash()), |
|
|
|
|
'parentHash': this.toHex(block.header.parentHash), |
|
|
|
|
'nonce': this.toHex(block.header.nonce), |
|
|
|
|
'sha3Uncles': '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347', |
|
|
|
|
'logsBloom': '0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331', |
|
|
|
|
'transactionsRoot': '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', |
|
|
|
|
'stateRoot': toHex(block.header.stateRoot), |
|
|
|
|
'stateRoot': this.toHex(block.header.stateRoot), |
|
|
|
|
'miner': this.coinbase, |
|
|
|
|
'difficulty': toHex(block.header.difficulty), |
|
|
|
|
'totalDifficulty': toHex(block.header.totalDifficulty), |
|
|
|
|
'extraData': toHex(block.header.extraData), |
|
|
|
|
'difficulty': this.toHex(block.header.difficulty), |
|
|
|
|
'totalDifficulty': this.toHex(block.header.totalDifficulty), |
|
|
|
|
'extraData': this.toHex(block.header.extraData), |
|
|
|
|
'size': '0x027f07', // 163591
|
|
|
|
|
'gasLimit': toHex(block.header.gasLimit), |
|
|
|
|
'gasUsed': toHex(block.header.gasUsed), |
|
|
|
|
'timestamp': toHex(block.header.timestamp), |
|
|
|
|
'gasLimit': this.toHex(block.header.gasLimit), |
|
|
|
|
'gasUsed': this.toHex(block.header.gasUsed), |
|
|
|
|
'timestamp': this.toHex(block.header.timestamp), |
|
|
|
|
'transactions': block.transactions.map((t) => '0x' + t.hash().toString('hex')), |
|
|
|
|
'uncles': [] |
|
|
|
|
} |
|
|
|
@ -90,39 +91,39 @@ Blocks.prototype.eth_getBlockByHash = function (payload, cb) { |
|
|
|
|
cb(null, b) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Blocks.prototype.eth_gasPrice = function (payload, cb) { |
|
|
|
|
eth_gasPrice (payload, cb) { |
|
|
|
|
cb(null, 1) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Blocks.prototype.eth_coinbase = function (payload, cb) { |
|
|
|
|
eth_coinbase (payload, cb) { |
|
|
|
|
cb(null, this.coinbase) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Blocks.prototype.eth_blockNumber = function (payload, cb) { |
|
|
|
|
eth_blockNumber (payload, cb) { |
|
|
|
|
cb(null, this.blockNumber) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Blocks.prototype.eth_getBlockTransactionCountByHash = function (payload, cb) { |
|
|
|
|
eth_getBlockTransactionCountByHash (payload, cb) { |
|
|
|
|
var block = this.executionContext.blocks[payload.params[0]] |
|
|
|
|
|
|
|
|
|
cb(null, block.transactions.length) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Blocks.prototype.eth_getBlockTransactionCountByNumber = function (payload, cb) { |
|
|
|
|
eth_getBlockTransactionCountByNumber (payload, cb) { |
|
|
|
|
var block = this.executionContext.blocks[payload.params[0]] |
|
|
|
|
|
|
|
|
|
cb(null, block.transactions.length) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Blocks.prototype.eth_getUncleCountByBlockHash = function (payload, cb) { |
|
|
|
|
eth_getUncleCountByBlockHash (payload, cb) { |
|
|
|
|
cb(null, 0) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Blocks.prototype.eth_getUncleCountByBlockNumber = function (payload, cb) { |
|
|
|
|
eth_getUncleCountByBlockNumber (payload, cb) { |
|
|
|
|
cb(null, 0) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Blocks.prototype.eth_getStorageAt = function (payload, cb) { |
|
|
|
|
eth_getStorageAt (payload, cb) { |
|
|
|
|
const [address, position, blockNumber] = payload.params |
|
|
|
|
|
|
|
|
|
this.executionContext.web3().debug.storageRangeAt(blockNumber, 'latest', address.toLowerCase(), position, 1, (err, result) => { |
|
|
|
@ -134,5 +135,6 @@ Blocks.prototype.eth_getStorageAt = function (payload, cb) { |
|
|
|
|
cb(err, value) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
module.exports = Blocks |
|
|
|
|