Skip to content

Commit

Permalink
Merge pull request #44 from dashevo/feat/get-block-stats
Browse files Browse the repository at this point in the history
feat: add handling of mixed type argument (integer or string)
  • Loading branch information
pshenmic authored Jan 13, 2022
2 parents a60889f + d51b7a4 commit 236d995
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
9 changes: 8 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ RpcClient.callspec = {
getBlockHash: 'int',
getBlockHeader: 'str bool',
getBlockHeaders: 'str int bool',
getBlockStats: 'str obj',
getBlockStats: 'int_str obj',
getBlockTemplate: '',
getConnectionCount: '',
getChainTips: 'int int',
Expand Down Expand Up @@ -332,6 +332,13 @@ function generateRPCMethods(constructor, apiCalls, rpc) {
int(arg) {
return parseFloat(arg);
},
int_str(arg) {
if (typeof arg === 'number') {
return parseFloat(arg)
}

return arg.toString()
},
float(arg) {
return parseFloat(arg);
},
Expand Down
39 changes: 19 additions & 20 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,41 +161,40 @@ describe('RpcClient', function() {

});

it('should process object arguments', function(done) {

var client = new RpcClient({
it('should process int_str arguments', async () => {
const client = new RpcClient({
user: 'user',
pass: 'pass',
host: 'localhost',
port: 8332,
rejectUnauthorized: true,
disableAgent: false
disableAgent: false,
});

var requestStub = sinon.stub(client.protocol, 'request').callsFake(function(options, callback){
var res = new FakeResponse();
var req = new FakeRequest();
setImmediate(function(){
const requestStub = sinon.stub(client.protocol, 'request').callsFake(function(options, callback){
const res = new FakeResponse();
const req = new FakeRequest();
setImmediate(() => {
res.emit('data', req.data);
res.emit('end');
});
callback(res);
return req;
});

var obj = {'n28S35tqEMbt6vNad7A5K3mZ7vdn8dZ86X': 1};
async.eachSeries([obj, JSON.stringify(obj)], function(i, next) {
client.sendMany('account', i, function(error, parsedBuf) {
should.not.exist(error);
should.exist(parsedBuf);
parsedBuf.params[1].should.have.property('n28S35tqEMbt6vNad7A5K3mZ7vdn8dZ86X', 1);
next();
});
}, function(err) {
requestStub.restore();
done();
});
const blockByHeight = await (new Promise((res) => client.getBlockStats(1, ['height'], (err, data) => res(data))));

should.exist(blockByHeight);
blockByHeight.params[0].should.equal(1);
blockByHeight.params[1].should.deep.equal(['height']);

const blockByHash = await (new Promise((res) => client.getBlockStats('fake_hash', ['height'], (err, data) => res(data))));

should.exist(blockByHash);
blockByHash.params[0].should.equal('fake_hash');
blockByHash.params[1].should.deep.equal(['height']);

requestStub.restore();
});

it('should batch calls for a method and receive a response', function(done) {
Expand Down

0 comments on commit 236d995

Please sign in to comment.