Skip to content
This repository has been archived by the owner on Dec 10, 2020. It is now read-only.

Commit

Permalink
Added eth_getBlockTransactionCountByHash RPC method
Browse files Browse the repository at this point in the history
  • Loading branch information
holgerd77 committed Jun 8, 2020
1 parent f15259a commit d7d3df9
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 28 deletions.
23 changes: 23 additions & 0 deletions lib/rpc/modules/eth.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class Eth {
this.getBlockByHash = middleware(this.getBlockByHash.bind(this), 2,
[[validators.hex, validators.blockHash], [validators.bool]])

this.getBlockTransactionCountByHash = middleware(this.getBlockTransactionCountByHash.bind(this), 1,
[[validators.hex, validators.blockHash]])

this.protocolVersion = middleware(this.protocolVersion.bind(this), 0, [])
}

Expand Down Expand Up @@ -76,6 +79,26 @@ class Eth {
}
}

/**
* Returns the transaction count for a block given by the block hash
* @param {Array<string>} [params] An array of one parameter: A block hash
* @param {Function} [cb] A function with an error object as the first argument and an actual value
* as the second argument
* @return {Promise}
*/
async getBlockTransactionCountByHash (params, cb) {
let [blockHash] = params

try {
let block = await this._chain.getBlock(toBuffer(blockHash))

const json = block.toJSON(true)
cb(null, `0x${json.transactions.length.toString(16)}`)
} catch (err) {
cb(err)
}
}

/**
* Returns the current ethereum protocol version
* @param {Array<*>} [params] An empty array
Expand Down
15 changes: 1 addition & 14 deletions test/rpc/eth/getBlockByHash.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,7 @@ const test = require('tape')
const request = require('supertest')
const { INVALID_PARAMS } = require('../../../lib/rpc/error-code')
const { startRPC, closeRPC, createManager, createNode } = require('../helpers')

function checkError (expectedCode, expectedMessage) {
return function (res) {
if (!res.body.error) {
throw new Error('should return an error object')
}
if (res.body.error.code !== expectedCode) {
throw new Error(`should have an error code ${expectedCode}`)
}
if (expectedMessage && res.body.error.message !== expectedMessage) {
throw new Error(`should have an error message "${expectedMessage}"`)
}
}
}
const { checkError } = require('../util')

test('call eth_getBlockByHash with valid arguments', t => {
const manager = createManager(createNode())
Expand Down
15 changes: 1 addition & 14 deletions test/rpc/eth/getBlockByNumber.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { checkError } from '../util'
const test = require('tape')

const request = require('supertest')
Expand All @@ -18,20 +19,6 @@ function createBlockchain () {
}
}

function checkError (expectedCode, expectedMessage) {
return function (res) {
if (!res.body.error) {
throw new Error('should return an error object')
}
if (res.body.error.code !== expectedCode) {
throw new Error(`should have an error code ${expectedCode}`)
}
if (expectedMessage && res.body.error.message !== expectedMessage) {
throw new Error(`should have an error message "${expectedMessage}"`)
}
}
}

test('call eth_getBlockByNumber with valid arguments', t => {
const manager = createManager(createNode({ blockchain: createBlockchain() }))
const server = startRPC(manager.getMethods())
Expand Down
142 changes: 142 additions & 0 deletions test/rpc/eth/getBlockTransactionCountByHash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
const test = require('tape')

const request = require('supertest')
const { INVALID_PARAMS } = require('../../../lib/rpc/error-code')
const { startRPC, closeRPC, createManager, createNode } = require('../helpers')
const { checkError } = require('../util')

test('call eth_getBlockTransactionCountByHash with valid arguments', t => {
const manager = createManager(createNode())
const server = startRPC(manager.getMethods())

const req = {
jsonrpc: '2.0',
method: 'eth_getBlockTransactionCountByHash',
params: [
'0x910abca1728c53e8d6df870dd7af5352e974357dc58205dea1676be17ba6becf'
],
id: 1
}

request(server)
.post('/')
.set('Content-Type', 'application/json')
.send(req)
.expect(200)
.expect(res => {
if (res.body.result !== `0x1`) {
throw new Error('transaction count is not 1')
}
})
.end((err, res) => {
closeRPC(server)
t.end(err)
})
})

test('call eth_getBlockTransactionCountByHash with invalid block hash without 0x', t => {
const manager = createManager(createNode())
const server = startRPC(manager.getMethods())

const req = {
jsonrpc: '2.0',
method: 'eth_getBlockTransactionCountByHash',
params: ['WRONG BLOCK NUMBER'],
id: 1
}
const checkInvalidParams = checkError(
INVALID_PARAMS,
'invalid argument 0: hex string without 0x prefix'
)

request(server)
.post('/')
.set('Content-Type', 'application/json')
.send(req)
.expect(200)
.expect(checkInvalidParams)
.end(err => {
closeRPC(server)
t.end(err)
})
})

test('call eth_getBlockTransactionCountByHash with invalid hex string as block hash', t => {
const manager = createManager(createNode())
const server = startRPC(manager.getMethods())

const req = {
jsonrpc: '2.0',
method: 'eth_getBlockTransactionCountByHash',
params: ['0xWRONG BLOCK NUMBER', true],
id: 1
}
const checkInvalidParams = checkError(
INVALID_PARAMS,
'invalid argument 0: invalid block hash'
)

request(server)
.post('/')
.set('Content-Type', 'application/json')
.send(req)
.expect(200)
.expect(checkInvalidParams)
.end(err => {
closeRPC(server)
t.end(err)
})
})

test('call eth_getBlockTransactionCountByHash without first parameter', t => {
const manager = createManager(createNode())
const server = startRPC(manager.getMethods())

const req = {
jsonrpc: '2.0',
method: 'eth_getBlockTransactionCountByHash',
params: [],
id: 1
}

const checkInvalidParams = checkError(
INVALID_PARAMS,
'missing value for required argument 0'
)

request(server)
.post('/')
.set('Content-Type', 'application/json')
.send(req)
.expect(200)
.expect(checkInvalidParams)
.end(err => {
closeRPC(server)
t.end(err)
})
})

test('call eth_getBlockTransactionCountByHash with invalid second parameter', t => {
const manager = createManager(createNode())
const server = startRPC(manager.getMethods())

const req = {
jsonrpc: '2.0',
method: 'eth_getBlockTransactionCountByHash',
params: ['INVALID PARAMETER'],
id: 1
}

const checkInvalidParams = checkError(INVALID_PARAMS)

request(server)
.post('/')
.set('Content-Type', 'application/json')
.send(req)
.expect(200)
.expect(checkInvalidParams)
.end(err => {
closeRPC(server)
t.end(err)
})
})
15 changes: 15 additions & 0 deletions test/rpc/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
checkError (expectedCode, expectedMessage) {
return function (res) {
if (!res.body.error) {
throw new Error('should return an error object')
}
if (res.body.error.code !== expectedCode) {
throw new Error(`should have an error code ${expectedCode}`)
}
if (expectedMessage && res.body.error.message !== expectedMessage) {
throw new Error(`should have an error message "${expectedMessage}"`)
}
}
}
}

0 comments on commit d7d3df9

Please sign in to comment.