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

feat!: respond with already exists grpc error #394

Merged
merged 9 commits into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat: respond with already exists grpc error
  • Loading branch information
shumkov committed Aug 9, 2021
commit bb42062c396d48c386c9474feb6c250bbeabf65a
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
const {
server: {
error: {
AlreadyExistsGrpcError,
InvalidArgumentGrpcError,
},
},
Expand Down Expand Up @@ -61,7 +62,7 @@ function broadcastTransactionHandlerFactory(coreRPCClient) {

if (e.code === -27) {
// RPC_VERIFY_ALREADY_IN_CHAIN
throw new InvalidArgumentGrpcError(`Cannot broadcast transaction: ${e.message}`);
throw new AlreadyExistsGrpcError(`Transaction already in chain: ${e.message}`);
}

throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const {
server: {
error: {
InvalidArgumentGrpcError,
FailedPreconditionGrpcError,
AlreadyExistsGrpcError,
},
},
} = require('@dashevo/grpc-common');
Expand Down Expand Up @@ -43,7 +43,7 @@ function broadcastStateTransitionHandlerFactory(rpcClient, handleAbciResponseErr

if (jsonRpcError) {
if (jsonRpcError.data === 'tx already exists in cache') {
throw new FailedPreconditionGrpcError(jsonRpcError.data, jsonRpcError);
throw new AlreadyExistsGrpcError('State transition already in chain', jsonRpcError);
}

const error = new Error();
Expand Down
105 changes: 99 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"@dashevo/dashcore-lib": "~0.19.25",
"@dashevo/dashd-rpc": "^2.0.2",
"@dashevo/dpp": "~0.20.0",
"@dashevo/grpc-common": "~0.3.3",
"@dashevo/grpc-common": "github:dashevo/js-grpc-common#add-already-exists",
"ajv": "^8.6.0",
"bs58": "^4.0.1",
"cbor": "^4.1.5",
Expand All @@ -43,7 +43,7 @@
"lodash": "^4.17.19",
"request": "^2.87.0",
"request-promise-native": "^1.0.5",
"ws": "^7.4.2",
"ws": "^7.5.3",
"zeromq": "5.2.0"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const {
server: {
error: {
InvalidArgumentGrpcError,
AlreadyExistsGrpcError,
},
},
} = require('@dashevo/grpc-common');
Expand Down Expand Up @@ -98,4 +99,21 @@ describe('broadcastTransactionHandlerFactory', () => {
expect(coreRPCClientMock.sendRawTransaction).to.be.not.called();
}
});

it('should throw AlreadyExistsGrpcError error if transaction already in chain', async () => {
const error = new Error();
error.code = -27;
error.message = 'dup-tx-something';

coreRPCClientMock.sendRawTransaction.throws(error);

try {
await broadcastTransactionHandler(call);

expect.fail('should thrown AlreadyExistsGrpcError error');
} catch (e) {
expect(e).to.be.instanceOf(AlreadyExistsGrpcError);
expect(e.getMessage()).to.equal(`Transaction already in chain: ${error.message}`);
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const {
server: {
error: {
InvalidArgumentGrpcError,
FailedPreconditionGrpcError,
AlreadyExistsGrpcError,
},
},
} = require('@dashevo/grpc-common');
Expand Down Expand Up @@ -117,19 +117,17 @@ describe('broadcastStateTransitionHandlerFactory', () => {
});

it('should throw FailedPreconditionGrpcError if transaction was broadcasted twice', async () => {
const error = {
response.error = {
code: -32603,
message: 'Internal error',
data: 'tx already exists in cache',
};

response.error = error;

try {
await broadcastStateTransitionHandler(call);
} catch (e) {
expect(e).to.be.an.instanceOf(FailedPreconditionGrpcError);
expect(e.getMessage()).to.equal(`Failed precondition: ${error.data}`);
expect(e).to.be.an.instanceOf(AlreadyExistsGrpcError);
expect(e.getMessage()).to.equal('State transition already in chain');
}
});
});