Skip to content

Commit 6157941

Browse files
committed
refactor(node): split into NodeBase and NodeDirect
1 parent 0ff2091 commit 6157941

22 files changed

+128
-120
lines changed

Diff for: src/AeSdkAepp.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
UnAuthorizedAccountError,
1616
RpcConnectionError,
1717
} from './utils/errors';
18-
import Node from './Node';
18+
import Node from './node/Direct';
1919
import BrowserConnection from './aepp-wallet-communication/connection/Browser';
2020

2121
/**

Diff for: src/AeSdkBase.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Node from './Node';
1+
import Node from './node/Direct';
22
import AccountBase from './account/Base';
33
import {
44
CompilerError, DuplicateNodeError, NodeNotFoundError, NotImplementedError, TypeError,

Diff for: src/AeSdkMethods.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import createDelegationSignature from './contract/delegation-signature';
77
import * as contractGaMethods from './contract/ga';
88
import { buildTxAsync } from './tx/builder';
99
import { mapObject, UnionToIntersection } from './utils/other';
10-
import Node from './Node';
10+
import Node from './node/Direct';
1111
import { TxParamsAsync } from './tx/builder/schema.generated';
1212
import AccountBase from './account/Base';
1313
import { Encoded } from './utils/encoder';

Diff for: src/account/Base.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Encoded } from '../utils/encoder';
2-
import Node from '../Node';
2+
import Node from '../node/Direct';
33
import CompilerBase from '../contract/compiler/Base';
44
import { AensName, ConsensusProtocolVersion, Int } from '../tx/builder/constants';
55
import { AciValue, Domain } from '../utils/typed-data';

Diff for: src/account/LedgerFactory.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import AccountLedger, { CLA, GET_ADDRESS, GET_APP_CONFIGURATION } from './Ledger
33
import { UnsupportedVersionError } from '../utils/errors';
44
import { Encoded } from '../utils/encoder';
55
import semverSatisfies from '../utils/semver-satisfies';
6-
import Node from '../Node';
6+
import Node from '../node/Direct';
77

88
/**
99
* A factory class that generates instances of AccountLedger based on provided transport.

Diff for: src/aens.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { Encoded, Encoding } from './utils/encoder';
1414
import { UnsupportedProtocolError } from './utils/errors';
1515
import { sendTransaction, SendTransactionOptions, getName } from './chain';
1616
import { buildTxAsync, BuildTxOptions } from './tx/builder';
17-
import { TransformNodeType } from './Node';
17+
import { TransformNodeType } from './node/Base';
1818
import { NameEntry, NamePointer } from './apis/node';
1919
import AccountBase from './account/Base';
2020
import { AddressEncodings } from './tx/builder/field-types/address';

Diff for: src/aepp-wallet-communication/rpc/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Encoded } from '../../utils/encoder';
22
import { Domain, AciValue } from '../../utils/typed-data';
33
import { METHODS, SUBSCRIPTION_TYPES, WALLET_TYPE } from '../schema';
4-
import { TransformNodeType } from '../../Node';
4+
import { TransformNodeType } from '../../node/Base';
55
import { SignedTx } from '../../apis/node';
66
import { AensName } from '../../tx/builder/constants';
77

Diff for: src/chain.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import {
88
AensPointerContextError, DryRunError, InvalidAensNameError, TransactionError,
99
TxTimedOutError, TxNotInChainError, InternalError,
1010
} from './utils/errors';
11-
import Node, { TransformNodeType } from './Node';
11+
import { TransformNodeType } from './node/Base';
12+
import Node from './node/Direct';
1213
import {
1314
Account as AccountNode, ByteCode, ContractObject, DryRunResult, DryRunResults,
1415
Generation, KeyBlock, MicroBlockHeader, NameEntry, SignedTx,

Diff for: src/contract/Contract.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ import {
3737
ContractCallObject as NodeContractCallObject, Event as NodeEvent,
3838
} from '../apis/node';
3939
import CompilerBase, { Aci } from './compiler/Base';
40-
import Node, { TransformNodeType } from '../Node';
40+
import { TransformNodeType } from '../node/Base';
41+
import Node from '../node/Direct';
4142
import {
4243
getAccount, getContract, getContractByteCode, resolveName, txDryRun, sendTransaction,
4344
SendTransactionOptions,

Diff for: src/contract/delegation-signature.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ArgumentError } from '../utils/errors';
33
import { AensName } from '../tx/builder/constants';
44
import AccountBase from '../account/Base';
55
import { isNameValid } from '../tx/builder/helpers';
6-
import Node from '../Node';
6+
import Node from '../node/Direct';
77

88
function ensureOracleQuery(oq: string): asserts oq is Encoded.OracleQueryId {
99
if (!oq.startsWith('oq_')) throw new ArgumentError('oq', 'oracle query', oq);

Diff for: src/contract/ga.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { ArgumentError, IllegalArgumentError } from '../utils/errors';
1414
import { concatBuffers } from '../utils/other';
1515
import AccountBase from '../account/Base';
1616
import Contract from './Contract';
17-
import Node from '../Node';
17+
import Node from '../node/Direct';
1818
import { sendTransaction, SendTransactionOptions, getAccount } from '../chain';
1919
import CompilerBase from './compiler/Base';
2020

Diff for: src/index-browser.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export { default as AeSdkBase } from './AeSdkBase';
6161
export { default as AeSdk } from './AeSdk';
6262
export { default as AeSdkAepp } from './AeSdkAepp';
6363
export { default as AeSdkWallet } from './AeSdkWallet';
64-
export { default as Node } from './Node';
64+
export { default as Node } from './node/Direct';
6565
export { default as verifyTransaction } from './tx/validator';
6666
export { default as AccountBase } from './account/Base';
6767
export { default as MemoryAccount } from './account/Memory';

Diff for: src/Node.ts renamed to src/node/Base.ts

+4-100
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
// eslint-disable-next-line max-classes-per-file
22
import BigNumber from 'bignumber.js';
33
import { OperationArguments, OperationSpec } from '@azure/core-client';
4-
import {
5-
genRequestQueuesPolicy, genCombineGetRequestsPolicy, genErrorFormatterPolicy,
6-
genVersionCheckPolicy, genRetryOnFailurePolicy,
7-
} from './utils/autorest';
8-
import { Node as NodeApi, NodeOptionalParams, ErrorModel } from './apis/node';
9-
import { mapObject } from './utils/other';
10-
import { UnsupportedVersionError } from './utils/errors';
11-
import { Encoded } from './utils/encoder';
12-
import { ConsensusProtocolVersion } from './tx/builder/constants';
4+
import { Node as NodeApi } from '../apis/node';
5+
import { mapObject } from '../utils/other';
6+
import { Encoded } from '../utils/encoder';
137

148
const bigIntPropertyNames = [
159
'balance', 'queryFee', 'fee', 'amount', 'nameFee', 'channelAmount',
@@ -104,95 +98,5 @@ type NodeTransformedApi = new (...args: ConstructorParameters<typeof NodeApi>) =
10498
? NodeApi[Name] : TransformNodeType<NodeApi[Name]>
10599
};
106100

107-
interface NodeInfo {
108-
url: string;
109-
nodeNetworkId: string;
110-
version: string;
111-
consensusProtocolVersion: ConsensusProtocolVersion;
112-
}
113-
114-
export default class Node extends (NodeTransformed as unknown as NodeTransformedApi) {
115-
#networkIdPromise?: Promise<string | Error>;
116-
117-
/**
118-
* @param url - Url for node API
119-
* @param options - Options
120-
* @param options.ignoreVersion - Don't ensure that the node is supported
121-
* @param options.retryCount - Amount of extra requests to do in case of failure
122-
* @param options.retryOverallDelay - Time in ms to wait between all retries
123-
*/
124-
constructor(
125-
url: string,
126-
{
127-
ignoreVersion = false, retryCount = 3, retryOverallDelay = 800, ...options
128-
}: NodeOptionalParams & {
129-
ignoreVersion?: boolean;
130-
retryCount?: number;
131-
retryOverallDelay?: number;
132-
} = {},
133-
) {
134-
// eslint-disable-next-line constructor-super
135-
super(url, {
136-
allowInsecureConnection: true,
137-
additionalPolicies: [
138-
genRequestQueuesPolicy(),
139-
genCombineGetRequestsPolicy(),
140-
genRetryOnFailurePolicy(retryCount, retryOverallDelay),
141-
genErrorFormatterPolicy((body: ErrorModel) => ` ${body.reason}`),
142-
],
143-
...options,
144-
});
145-
if (!ignoreVersion) {
146-
const statusPromise = this.getStatus();
147-
const versionPromise = statusPromise.then(({ nodeVersion }) => nodeVersion, (error) => error);
148-
this.#networkIdPromise = statusPromise.then(({ networkId }) => networkId, (error) => error);
149-
this.pipeline.addPolicy(
150-
genVersionCheckPolicy('node', '/v3/status', versionPromise, '6.2.0', '7.0.0'),
151-
);
152-
}
153-
this.intAsString = true;
154-
}
155-
156-
/**
157-
* Returns network ID provided by node.
158-
* This method won't do extra requests on subsequent calls.
159-
*/
160-
async getNetworkId(): Promise<string> {
161-
this.#networkIdPromise ??= this.getStatus().then(({ networkId }) => networkId);
162-
const networkId = await this.#networkIdPromise;
163-
if (networkId instanceof Error) throw networkId;
164-
return networkId;
165-
}
166-
167-
async getNodeInfo(): Promise<NodeInfo> {
168-
const {
169-
nodeVersion,
170-
networkId: nodeNetworkId,
171-
protocols,
172-
topBlockHeight,
173-
} = await this.getStatus();
174-
175-
const consensusProtocolVersion = protocols
176-
.filter(({ effectiveAtHeight }) => topBlockHeight >= effectiveAtHeight)
177-
.reduce(
178-
(acc, p) => (p.effectiveAtHeight > acc.effectiveAtHeight ? p : acc),
179-
{ effectiveAtHeight: -1, version: 0 },
180-
)
181-
.version;
182-
if (ConsensusProtocolVersion[consensusProtocolVersion] == null) {
183-
const version = consensusProtocolVersion.toString();
184-
const versions = Object.values(ConsensusProtocolVersion)
185-
.filter((el) => typeof el === 'number').map((el) => +el);
186-
const geVersion = Math.min(...versions).toString();
187-
const ltVersion = (Math.max(...versions) + 1).toString();
188-
throw new UnsupportedVersionError('consensus protocol', version, geVersion, ltVersion);
189-
}
190-
191-
return {
192-
url: this.$host,
193-
nodeNetworkId,
194-
version: nodeVersion,
195-
consensusProtocolVersion,
196-
};
197-
}
101+
export default class NodeBase extends (NodeTransformed as unknown as NodeTransformedApi) {
198102
}

Diff for: src/node/Direct.ts

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import {
2+
genRequestQueuesPolicy, genCombineGetRequestsPolicy, genErrorFormatterPolicy,
3+
genVersionCheckPolicy, genRetryOnFailurePolicy,
4+
} from '../utils/autorest';
5+
import NodeBase from './Base';
6+
import { UnsupportedVersionError } from '../utils/errors';
7+
import { ConsensusProtocolVersion } from '../tx/builder/constants';
8+
import { NodeOptionalParams, ErrorModel } from '../apis/node';
9+
10+
interface NodeInfo {
11+
url: string;
12+
nodeNetworkId: string;
13+
version: string;
14+
consensusProtocolVersion: ConsensusProtocolVersion;
15+
}
16+
17+
export default class NodeDefault extends NodeBase {
18+
#networkIdPromise?: Promise<string | Error>;
19+
20+
/**
21+
* @param url - Url for node API
22+
* @param options - Options
23+
* @param options.ignoreVersion - Don't ensure that the node is supported
24+
* @param options.retryCount - Amount of extra requests to do in case of failure
25+
* @param options.retryOverallDelay - Time in ms to wait between all retries
26+
*/
27+
constructor(
28+
url: string,
29+
{
30+
ignoreVersion = false, retryCount = 3, retryOverallDelay = 800, ...options
31+
}: NodeOptionalParams & {
32+
ignoreVersion?: boolean;
33+
retryCount?: number;
34+
retryOverallDelay?: number;
35+
} = {},
36+
) {
37+
// eslint-disable-next-line constructor-super
38+
super(url, {
39+
allowInsecureConnection: true,
40+
additionalPolicies: [
41+
genRequestQueuesPolicy(),
42+
genCombineGetRequestsPolicy(),
43+
genRetryOnFailurePolicy(retryCount, retryOverallDelay),
44+
genErrorFormatterPolicy((body: ErrorModel) => ` ${body.reason}`),
45+
],
46+
...options,
47+
});
48+
if (!ignoreVersion) {
49+
const statusPromise = this.getStatus();
50+
const versionPromise = statusPromise.then(({ nodeVersion }) => nodeVersion, (error) => error);
51+
this.#networkIdPromise = statusPromise.then(({ networkId }) => networkId, (error) => error);
52+
this.pipeline.addPolicy(
53+
genVersionCheckPolicy('node', '/v3/status', versionPromise, '6.2.0', '7.0.0'),
54+
);
55+
}
56+
this.intAsString = true;
57+
}
58+
59+
/**
60+
* Returns network ID provided by node.
61+
* This method won't do extra requests on subsequent calls.
62+
*/
63+
async getNetworkId(): Promise<string> {
64+
this.#networkIdPromise ??= this.getStatus().then(({ networkId }) => networkId);
65+
const networkId = await this.#networkIdPromise;
66+
if (networkId instanceof Error) throw networkId;
67+
return networkId;
68+
}
69+
70+
async getNodeInfo(): Promise<NodeInfo> {
71+
const {
72+
nodeVersion,
73+
networkId: nodeNetworkId,
74+
protocols,
75+
topBlockHeight,
76+
} = await this.getStatus();
77+
78+
const consensusProtocolVersion = protocols
79+
.filter(({ effectiveAtHeight }) => topBlockHeight >= effectiveAtHeight)
80+
.reduce(
81+
(acc, p) => (p.effectiveAtHeight > acc.effectiveAtHeight ? p : acc),
82+
{ effectiveAtHeight: -1, version: 0 },
83+
)
84+
.version;
85+
if (ConsensusProtocolVersion[consensusProtocolVersion] == null) {
86+
const version = consensusProtocolVersion.toString();
87+
const versions = Object.values(ConsensusProtocolVersion)
88+
.filter((el) => typeof el === 'number').map((el) => +el);
89+
const geVersion = Math.min(...versions).toString();
90+
const ltVersion = (Math.max(...versions) + 1).toString();
91+
throw new UnsupportedVersionError('consensus protocol', version, geVersion, ltVersion);
92+
}
93+
94+
return {
95+
url: this.$host,
96+
nodeNetworkId,
97+
version: nodeVersion,
98+
consensusProtocolVersion,
99+
};
100+
}
101+
}

Diff for: src/oracle.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
import {
1818
_getPollInterval, getHeight, sendTransaction, SendTransactionOptions,
1919
} from './chain';
20-
import Node from './Node';
20+
import Node from './node/Direct';
2121
import AccountBase from './account/Base';
2222

2323
type OracleQueries = Awaited<ReturnType<Node['getOracleQueriesByPubkey']>>['oracleQueries'];

Diff for: src/tx/builder/field-types/abi-version.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Tag, ConsensusProtocolVersion, AbiVersion } from '../constants';
22
import { getProtocolDetails } from './ct-version';
3-
import Node from '../../../Node';
3+
import Node from '../../../node/Direct';
44

55
export default {
66
_getProtocolDetails(c: ConsensusProtocolVersion, tag: Tag): AbiVersion {

Diff for: src/tx/builder/field-types/ct-version.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ConsensusProtocolVersion, VmVersion, AbiVersion } from '../constants';
2-
import Node from '../../../Node';
2+
import Node from '../../../node/Direct';
33

44
/*
55
* First abi/vm by default

Diff for: src/tx/builder/field-types/nonce.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { isAccountNotFoundError } from '../../../utils/other';
22
import shortUInt from './short-u-int';
3-
import Node from '../../../Node';
3+
import Node from '../../../node/Direct';
44
import { ArgumentError } from '../../../utils/errors';
55
import { NextNonceStrategy } from '../../../apis/node';
66

Diff for: src/tx/builder/field-types/query-fee.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import coinAmount from './coin-amount';
22
import { Int } from '../constants';
3-
import Node from '../../../Node';
3+
import Node from '../../../node/Direct';
44
import { Encoded } from '../../../utils/encoder';
55
import { ArgumentError } from '../../../utils/errors';
66

Diff for: src/tx/builder/field-types/ttl.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import shortUInt from './short-u-int';
2-
import Node from '../../../Node';
2+
import Node from '../../../node/Direct';
33
import { ArgumentError } from '../../../utils/errors';
44

55
/**

Diff for: src/tx/execution-cost.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Tag } from './builder/constants';
44
import { verify } from '../utils/crypto';
55
import { getBufferToSign } from '../account/Memory';
66
import { IllegalArgumentError, InternalError, TransactionError } from '../utils/errors';
7-
import Node from '../Node';
7+
import Node from '../node/Direct';
88
import getTransactionSignerAddress from './transaction-signer';
99

1010
/**

Diff for: src/tx/validator.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import { Tag, ConsensusProtocolVersion } from './builder/constants';
66
import { buildTx, unpackTx } from './builder';
77
import { concatBuffers, isAccountNotFoundError } from '../utils/other';
88
import { Encoded, decode } from '../utils/encoder';
9-
import Node, { TransformNodeType } from '../Node';
9+
import { TransformNodeType } from '../node/Base';
10+
import Node from '../node/Direct';
1011
import { Account } from '../apis/node';
1112
import { genAggressiveCacheGetResponsesPolicy } from '../utils/autorest';
1213
import { UnexpectedTsError } from '../utils/errors';

0 commit comments

Comments
 (0)