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

Commit

Permalink
feat: replace broadcast_tx_commit with broadcast_tx_async (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Shuplenkov authored Jan 20, 2021
1 parent 85ddf2d commit d704cf4
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 69 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ before_install:
- curl -L https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-`uname -s`-`uname -m` > docker-compose
- chmod +x docker-compose
- sudo mv docker-compose /usr/local/bin
- echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
- if [[ -n $SDK_BRANCH ]]; then export SDK_INSTALL=github:dashevo/DashJS#$SDK_BRANCH; fi

install:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@ const {
},
} = require('@dashevo/dapi-grpc');

const AbciResponseError = require('../../../errors/AbciResponseError');

/**
* @param {jaysonClient} rpcClient
* @param {handleAbciResponseError} handleAbciResponseError
*
* @returns {broadcastStateTransitionHandler}
*/
function broadcastStateTransitionHandlerFactory(rpcClient, handleAbciResponseError) {
function broadcastStateTransitionHandlerFactory(rpcClient) {
/**
* @typedef broadcastStateTransitionHandler
*
Expand All @@ -39,10 +36,10 @@ function broadcastStateTransitionHandlerFactory(rpcClient, handleAbciResponseErr

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

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

if (jsonRpcError) {
if (jsonRpcError.data === 'error on broadcastTxCommit: tx already exists in cache') {
if (jsonRpcError.data === 'tx already exists in cache') {
throw new FailedPreconditionGrpcError(jsonRpcError.data, jsonRpcError);
}

Expand All @@ -52,24 +49,6 @@ function broadcastStateTransitionHandlerFactory(rpcClient, handleAbciResponseErr
throw error;
}

const { check_tx: checkTx, deliver_tx: deliverTx } = result;

if (checkTx.code !== undefined && checkTx.code !== 0) {
const { error: abciError } = JSON.parse(checkTx.log);

handleAbciResponseError(
new AbciResponseError(checkTx.code, abciError),
);
}

if (deliverTx.code !== undefined && deliverTx.code !== 0) {
const { error: abciError } = JSON.parse(deliverTx.log);

handleAbciResponseError(
new AbciResponseError(deliverTx.code, abciError),
);
}

return new BroadcastStateTransitionResponse();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ describe('broadcastStateTransitionHandlerFactory', () => {
let stateTransitionFixture;
let log;
let code;
let handleAbciResponseErrorMock;

beforeEach(async function beforeEach() {
const dpp = new DashPlatformProtocol();
Expand Down Expand Up @@ -70,11 +69,8 @@ describe('broadcastStateTransitionHandlerFactory', () => {
request: this.sinon.stub().resolves(response),
};

handleAbciResponseErrorMock = this.sinon.stub();

broadcastStateTransitionHandler = broadcastStateTransitionHandlerFactory(
rpcClientMock,
handleAbciResponseErrorMock,
);
});

Expand All @@ -93,7 +89,6 @@ describe('broadcastStateTransitionHandlerFactory', () => {
expect(e).to.be.an.instanceOf(InvalidArgumentGrpcError);
expect(e.getMessage()).to.equal('State Transition is not specified');
expect(rpcClientMock.request).to.not.be.called();
expect(handleAbciResponseErrorMock).to.not.be.called();
}
});

Expand All @@ -103,45 +98,7 @@ describe('broadcastStateTransitionHandlerFactory', () => {
const tx = stateTransitionFixture.toBuffer().toString('base64');

expect(result).to.be.an.instanceOf(BroadcastStateTransitionResponse);
expect(rpcClientMock.request).to.be.calledOnceWith('broadcast_tx_commit', { tx });
expect(handleAbciResponseErrorMock).to.not.be.called();
});

it('should throw error if checkTx.code !== 0', async () => {
const error = new InvalidArgumentGrpcError('Some error');
code = 2;

response.result.check_tx.code = code;

handleAbciResponseErrorMock.throws(error);

rpcClientMock.request.resolves(response);

try {
await broadcastStateTransitionHandler(call);

expect.fail('InternalGrpcError was not thrown');
} catch (e) {
expect(e).to.be.equal(error);
}
});
it('should throw error if deliverTx.code !== 0', async () => {
const error = new InvalidArgumentGrpcError('Some error');
code = 2;

response.result.deliver_tx.code = code;

handleAbciResponseErrorMock.throws(error);

rpcClientMock.request.resolves(response);

try {
await broadcastStateTransitionHandler(call);

expect.fail('InternalGrpcError was not thrown');
} catch (e) {
expect(e).to.be.equal(error);
}
expect(rpcClientMock.request).to.be.calledOnceWith('broadcast_tx_async', { tx });
});

it('should throw an error if transaction broadcast returns error', async () => {
Expand All @@ -162,7 +119,7 @@ describe('broadcastStateTransitionHandlerFactory', () => {
const error = {
code: -32603,
message: 'Internal error',
data: 'error on broadcastTxCommit: tx already exists in cache',
data: 'tx already exists in cache',
};

response.error = error;
Expand Down

0 comments on commit d704cf4

Please sign in to comment.