This repository has been archived by the owner on Dec 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added eth_getBlockTransactionCountByHash RPC method
- Loading branch information
Showing
5 changed files
with
182 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_getBlockByHash 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_getBlockByHash 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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"`) | ||
} | ||
} | ||
} | ||
} |