Skip to content

Commit

Permalink
test: update broadcastStateTransitionHandler tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shumkov committed Oct 23, 2024
1 parent d5b6698 commit 4ca4cb3
Showing 1 changed file with 77 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {
AlreadyExistsGrpcError,
UnavailableGrpcError,
ResourceExhaustedGrpcError,
InternalGrpcError,
},
},
} = require('@dashevo/grpc-common');
Expand Down Expand Up @@ -36,6 +37,7 @@ describe('broadcastStateTransitionHandlerFactory', () => {
let log;
let code;
let createGrpcErrorFromDriveResponseMock;
let requestTenderRpcMock;

before(async () => {
await loadWasmDpp();
Expand Down Expand Up @@ -82,11 +84,14 @@ describe('broadcastStateTransitionHandlerFactory', () => {
request: this.sinon.stub().resolves(response),
};

requestTenderRpcMock = this.sinon.stub();

createGrpcErrorFromDriveResponseMock = this.sinon.stub();

broadcastStateTransitionHandler = broadcastStateTransitionHandlerFactory(
rpcClientMock,
createGrpcErrorFromDriveResponseMock,
requestTenderRpcMock,
);
});

Expand Down Expand Up @@ -182,13 +187,38 @@ describe('broadcastStateTransitionHandlerFactory', () => {
}
});

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

requestTenderRpcMock.withArgs('unconfirmed_txs').resolves({
txs: [stateTransitionFixture.toBuffer().toString('base64')],
});

try {
await broadcastStateTransitionHandler(call);

expect.fail('should throw AlreadyExistsGrpcError');
} catch (e) {
expect(e).to.be.an.instanceOf(AlreadyExistsGrpcError);
expect(e.getMessage()).to.equal('state transition already in mempool');
}
});

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

requestTenderRpcMock.withArgs('tx').resolves({
tx_result: { },
});

try {
await broadcastStateTransitionHandler(call);

Expand All @@ -199,6 +229,52 @@ describe('broadcastStateTransitionHandlerFactory', () => {
}
});

it('should throw consensus result for invalid transition in cache', async () => {
response.error = {
code: -32603,
message: 'Internal error',
data: 'tx already exists in cache',
};

requestTenderRpcMock.withArgs('check_tx').resolves({
code: 1,
info: 'some info',
});

const error = new Error('some error');

createGrpcErrorFromDriveResponseMock.resolves(error);

try {
await broadcastStateTransitionHandler(call);

expect.fail('should throw consensus error');
} catch (e) {
expect(e).to.equal(error);
}
});

it('should throw internal error for transition in cache that passing check tx', async () => {
response.error = {
code: -32603,
message: 'Internal error',
data: 'tx already exists in cache',
};

requestTenderRpcMock.withArgs('check_tx').resolves({
code: 0,
});

try {
await broadcastStateTransitionHandler(call);

expect.fail('should throw InternalError');
} catch (e) {
expect(e).to.be.an.instanceOf(InternalGrpcError);
expect(e.getMessage()).to.equal('Internal error');
}
});

it('should throw a gRPC error based on drive\'s response', async () => {
const message = 'not found';
const metadata = {
Expand Down

0 comments on commit 4ca4cb3

Please sign in to comment.