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 21dfe34
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
15 changes: 13 additions & 2 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 @@ -161,22 +162,32 @@ export default class Node extends (NodeTransformed as unknown as NodeTransformed

async getNodeInfo(): Promise<NodeInfo> {
const {
nodeVersion: version,
nodeVersion,
networkId: nodeNetworkId,
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();
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,
version,
version: nodeVersion,
consensusProtocolVersion,
};
}
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 21dfe34

Please sign in to comment.