Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions lib/rpc/db/db.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
import { inHex } from '../../format/input';

export default class Db {
constructor (transport) {
this._transport = transport;
}

getHex (dbName, keyName) {
return this._transport.execute('db_getHex', dbName, keyName);
return this._transport
.execute('db_getHex', dbName, keyName);
}

getString (dbName, keyName) {
return this._transport.execute('db_getString', dbName, keyName);
return this._transport
.execute('db_getString', dbName, keyName);
}

putHex (dbName, keyName, hexData) {
return this._transport.execute('db_putHex', dbName, keyName, hexData);
return this._transport
.execute('db_putHex', dbName, keyName, inHex(hexData));
}

putString (dbName, keyName, stringData) {
return this._transport.execute('db_putString', dbName, keyName, stringData);
return this._transport
.execute('db_putString', dbName, keyName, stringData);
}
}
22 changes: 22 additions & 0 deletions lib/rpc/db/db.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { TEST_HOST, TEST_PORT, mockRpc } from '../../../test/mockRpc';

import Http from '../../transport/http';
import Db from './db';

const instance = new Db(new Http(TEST_HOST, TEST_PORT));

describe('lib/Db', () => {
let scope;

describe('putHex', () => {
beforeEach(() => {
scope = mockRpc([{ method: 'db_putHex', reply: { result: [] } }]);
});

it('formats the inputs correctly', () => {
return instance.putHex('db', 'key', '1234').then(() => {
expect(scope.body.db_putHex.params).to.deep.equal(['db', 'key', '0x1234']);
});
});
});
});
91 changes: 89 additions & 2 deletions lib/rpc/eth/eth.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('lib/Eth', () => {
scope = mockRpc([{ method: 'eth_call', reply: { result: [] } }]);
});

it('options & blockNumber', () => {
it('formats the input options & blockNumber', () => {
return instance.call({ data: '12345678' }, 'earliest').then(() => {
expect(scope.body.eth_call.params).to.deep.equal([{ data: '0x12345678' }, 'earliest']);
});
Expand Down Expand Up @@ -47,6 +47,19 @@ describe('lib/Eth', () => {
});
});

describe('gasPrice', () => {
beforeEach(() => {
scope = mockRpc([{ method: 'eth_gasPrice', reply: { result: '0x123' } }]);
});

it('returns the fomratted price', () => {
return instance.gasPrice().then((price) => {
expect(isBigNumber(price)).to.be.true;
expect(price.toString(16)).to.deep.equal('123');
});
});
});

describe('getBalance', () => {
beforeEach(() => {
scope = mockRpc([{ method: 'eth_getBalance', reply: { result: '0x123' } }]);
Expand Down Expand Up @@ -83,7 +96,7 @@ describe('lib/Eth', () => {
});
});

it('formats the input hash as a hash, default full = true', () => {
it('formats the input hash as a hash, full true', () => {
return instance.getBlockByHash('1234', true).then(() => {
expect(scope.body.eth_getBlockByHash.params).to.deep.equal(['0x1234', true]);
});
Expand All @@ -95,4 +108,78 @@ describe('lib/Eth', () => {
});
});
});

describe('getBlockByNumber', () => {
beforeEach(() => {
scope = mockRpc([{ method: 'eth_getBlockByNumber', reply: { result: { miner: address.toLowerCase() } } }]);
});

it('assumes blockNumber latest & full false', () => {
return instance.getBlockByNumber().then(() => {
expect(scope.body.eth_getBlockByNumber.params).to.deep.equal(['latest', false]);
});
});

it('uses input blockNumber & full false', () => {
return instance.getBlockByNumber('0x1234').then(() => {
expect(scope.body.eth_getBlockByNumber.params).to.deep.equal(['0x1234', false]);
});
});

it('formats the input blockNumber, full true', () => {
return instance.getBlockByNumber(0x1234, true).then(() => {
expect(scope.body.eth_getBlockByNumber.params).to.deep.equal(['0x1234', true]);
});
});

it('formats the output into block', () => {
return instance.getBlockByNumber(0x1234).then((block) => {
expect(block.miner).to.equal(address);
});
});
});

describe('getBlockTransactionCountByHash', () => {
beforeEach(() => {
scope = mockRpc([{ method: 'eth_getBlockTransactionCountByHash', reply: { result: '0x123' } }]);
});

it('formats input hash properly', () => {
return instance.getBlockTransactionCountByHash('abcdef').then(() => {
expect(scope.body.eth_getBlockTransactionCountByHash.params).to.deep.equal(['0xabcdef']);
});
});

it('formats the output number', () => {
return instance.getBlockTransactionCountByHash('0x1234').then((count) => {
expect(isBigNumber(count)).to.be.true;
expect(count.toString(16)).to.equal('123');
});
});
});

describe('getBlockTransactionCountByNumber', () => {
beforeEach(() => {
scope = mockRpc([{ method: 'eth_getBlockTransactionCountByNumber', reply: { result: '0x123' } }]);
});

it('specified blockNumber latest when none specified', () => {
return instance.getBlockTransactionCountByNumber().then(() => {
expect(scope.body.eth_getBlockTransactionCountByNumber.params).to.deep.equal(['latest']);
});
});

it('formats input blockNumber properly', () => {
return instance.getBlockTransactionCountByNumber(0xabcdef).then(() => {
expect(scope.body.eth_getBlockTransactionCountByNumber.params).to.deep.equal(['0xabcdef']);
});
});

it('formats the output number', () => {
return instance.getBlockTransactionCountByNumber('0x1234').then((count) => {
expect(isBigNumber(count)).to.be.true;
expect(count.toString(16)).to.equal('123');
});
});
});
});
22 changes: 22 additions & 0 deletions lib/rpc/web3/web3.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { TEST_HOST, TEST_PORT, mockRpc } from '../../../test/mockRpc';

import Http from '../../transport/http';
import Web3 from './web3';

const instance = new Web3(new Http(TEST_HOST, TEST_PORT));

describe('lib/Web3', () => {
let scope;

describe('sha3', () => {
beforeEach(() => {
scope = mockRpc([{ method: 'web3_sha3', reply: { result: [] } }]);
});

it('formats the inputs correctly', () => {
return instance.sha3('1234').then(() => {
expect(scope.body.web3_sha3.params).to.deep.equal(['0x1234']);
});
});
});
});