Skip to content

Commit

Permalink
fix(node): throw clear error message if unsupported protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Sep 16, 2023
1 parent cd8dd7f commit c3b78c8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from './utils/autorest';
import { Node as NodeApi, NodeOptionalParams, ErrorModel } from './apis/node';
import { mapObject } from './utils/other';
import { UnsupportedVersionError } from './utils/errors';
import { Encoded } from './utils/encoder';
import { ConsensusProtocolVersion } from './tx/builder/constants';

Expand Down Expand Up @@ -166,13 +167,23 @@ export default class Node extends (NodeTransformed as unknown as NodeTransformed
protocols,
topBlockHeight,
} = await this.getStatus();

const consensusProtocolVersion = protocols
.filter(({ effectiveAtHeight }) => topBlockHeight >= effectiveAtHeight)
.reduce(
(acc, p) => (p.effectiveAtHeight > acc.effectiveAtHeight ? p : acc),
{ effectiveAtHeight: -1, version: 0 },
)
.version;
if (ConsensusProtocolVersion[consensusProtocolVersion] == null) {
const version = consensusProtocolVersion.toString();

Check failure on line 179 in src/Node.ts

View workflow job for this annotation

GitHub Actions / main

'version' is already declared in the upper scope on line 165 column 20
const versions = Object.values(ConsensusProtocolVersion)
.filter((el) => typeof el === 'number').map((el) => +el);
const geVersion = Math.min(...versions).toString();
const ltVersion = (Math.max(...versions) + 1).toString();
throw new UnsupportedVersionError('consensus protocol', version, geVersion, ltVersion);
}

return {
url: this.$host,
nodeNetworkId,
Expand Down
14 changes: 13 additions & 1 deletion test/integration/node.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { describe, it, before } from 'mocha';
import { expect } from 'chai';
import { createSandbox } from 'sinon';
import { PipelineRequest, PipelineResponse, SendRequest } from '@azure/core-rest-pipeline';
import { url, ignoreVersion } from '.';
import { AeSdkBase, Node, NodeNotFoundError } from '../../src';
import {
AeSdkBase, Node, NodeNotFoundError, ConsensusProtocolVersion,
} from '../../src';

describe('Node client', () => {
let node: Node;
Expand Down Expand Up @@ -48,6 +51,15 @@ describe('Node client', () => {
expect(counter).to.be.equal(requestCount);
}, Promise.resolve()));

it('throws exception if unsupported protocol', async () => {
const sandbox = createSandbox();
sandbox.stub(ConsensusProtocolVersion, 'Iris').value(undefined);
sandbox.stub(ConsensusProtocolVersion, '5' as 'Iris').value(undefined);
await expect(node.getNodeInfo()).to.be
.rejectedWith('Unsupported consensus protocol version 5. Supported: >= 6 < 7');
sandbox.restore();
});

describe('Node Pool', () => {
it('Throw error on using API without node', () => {
const nodes = new AeSdkBase({});
Expand Down

0 comments on commit c3b78c8

Please sign in to comment.