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

Commit

Permalink
fix: Tendermint error reporting in applyStateTransition
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Shuplenkov authored Mar 10, 2020
1 parent 4c979a3 commit f8764e9
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ function applyStateTransitionHandlerFactory(rpcClient, handleAbciResponse) {

const tx = Buffer.from(stByteArray).toString('base64');

const { result, error: errorMessage } = await rpcClient.request('broadcast_tx_commit', { tx });
const { result, error: jsonRpcError } = await rpcClient.request('broadcast_tx_commit', { tx });

if (errorMessage) {
throw new Error(errorMessage);
if (jsonRpcError) {
const error = new Error();
Object.assign(error, jsonRpcError);

throw error;
}

const { check_tx: checkTx, deliver_tx: deliverTx } = result;
Expand Down
9 changes: 6 additions & 3 deletions lib/grpcServer/handlers/platform/getIdentityHandlerFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ function getIdentityHandlerFactory(rpcClient, handleAbciResponse) {

const data = Buffer.from(id).toString('hex');

const { result, error: errorMessage } = await rpcClient.request('abci_query', { path, data });
const { result, error: jsonRpcError } = await rpcClient.request('abci_query', { path, data });

if (errorMessage) {
throw new Error(errorMessage);
if (jsonRpcError) {
const error = new Error();
Object.assign(error, jsonRpcError);

throw error;
}

handleAbciResponse(result.response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ function applyStateTransitionHandlerFactory(rpcClient, handleAbciResponse, valid
validator.validate(args);
const { stateTransition: tx } = args;

const { result, error: errorMessage } = await rpcClient.request('broadcast_tx_commit', { tx });
const { result, error: jsonRpcError } = await rpcClient.request('broadcast_tx_commit', { tx });

if (errorMessage) {
throw new Error(errorMessage);
if (jsonRpcError) {
const error = new Error();
Object.assign(error, jsonRpcError);

throw error;
}

const { check_tx: checkTx, deliver_tx: deliverTx } = result;
Expand Down
9 changes: 6 additions & 3 deletions lib/rpcServer/commands/platform/getIdentityHandlerFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ function getIdentityHandlerFactory(rpcClient, handleAbciResponse, validator) {

const data = Buffer.from(id).toString('hex');

const { result, error: errorMessage } = await rpcClient.request('abci_query', { path, data });
const { result, error: jsonRpcError } = await rpcClient.request('abci_query', { path, data });

if (errorMessage) {
throw new Error(errorMessage);
if (jsonRpcError) {
const error = new Error();
Object.assign(error, jsonRpcError);

throw error;
}

handleAbciResponse(result.response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ describe('applyStateTransitionHandlerFactory', () => {
});

it('should throw an error if transaction broadcast returns error', async () => {
tendermintRpcMock.request.returns({ result: null, error: "Something didn't work" });
const error = { code: -1, message: "Something didn't work", data: 'Some data' };
tendermintRpcMock.request.returns({ result: null, error });
const applyStateTransitionHandler = applyStateTransitionHandlerFactory(
tendermintRpcMock, handleAbciMock, validator,
);
Expand All @@ -44,7 +45,9 @@ describe('applyStateTransitionHandlerFactory', () => {
await applyStateTransitionHandler({ stateTransition: st });
} catch (e) {
expect(e).to.be.an.instanceOf(Error);
expect(e.message).to.be.equal("Something didn't work");
expect(e.message).to.equal(error.message);
expect(e.data).to.equal(error.data);
expect(e.code).to.equal(error.code);
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,19 @@ describe('applyStateTransitionHandlerFactory', () => {
expect(e).to.equal(error);
}
});

it('should throw an error if transaction broadcast returns error', async () => {
const error = { code: -1, message: "Something didn't work", data: 'Some data' };

response.error = error;

try {
await applyStateTransitionHandler(call);
} catch (e) {
expect(e).to.be.an.instanceOf(Error);
expect(e.message).to.equal(error.message);
expect(e.data).to.equal(error.data);
expect(e.code).to.equal(error.code);
}
});
});

0 comments on commit f8764e9

Please sign in to comment.