Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(node): throw clear error message if unsupported protocol #1898

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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