diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 7c2e92bc0a..0da07df467 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -8,8 +8,8 @@ jobs: runs-on: ubuntu-22.04 steps: - run: | - wget -q https://packages.erlang-solutions.com/erlang/debian/pool/esl-erlang_25.0.4-1~ubuntu~jammy_amd64.deb - sudo apt install --allow-downgrades ./esl-erlang_25.0.4-1~ubuntu~jammy_amd64.deb + wget -q https://packages.erlang-solutions.com/erlang/debian/pool/esl-erlang_25.2.3-1~ubuntu~jammy_amd64.deb + sudo apt install ./esl-erlang_25.2.3-1~ubuntu~jammy_amd64.deb - uses: actions/checkout@v4 with: fetch-depth: 100 diff --git a/src/chain.ts b/src/chain.ts index 87c1e38f69..1c9bca7694 100644 --- a/src/chain.ts +++ b/src/chain.ts @@ -21,15 +21,25 @@ import { * @param type - Type * @param options - Options */ -export function _getPollInterval( - type: 'block' | 'microblock', // TODO: rename to 'key-block' | 'micro-block' - { _expectedMineRate = 180000, _microBlockCycle = 3000 }: - { _expectedMineRate?: number; _microBlockCycle?: number }, -): number { - const base = { - block: _expectedMineRate, - microblock: _microBlockCycle, - }[type]; +export async function _getPollInterval( + type: 'key-block' | 'micro-block', + { _expectedMineRate, _microBlockCycle, onNode }: + { _expectedMineRate?: number; _microBlockCycle?: number; onNode: Node }, +): Promise { + const getVal = async ( + t: string, + val: number | undefined, + devModeDef: number, + def: number, + ): Promise => { + if (t !== type) return null; + if (val != null) return val; + return await onNode?.getNetworkId() === 'ae_dev' ? devModeDef : def; + }; + + const base = await getVal('key-block', _expectedMineRate, 0, 180000) + ?? await getVal('micro-block', _microBlockCycle, 0, 3000) + ?? (() => { throw new InternalError(`Unknown type: ${type}`); })(); return Math.floor(base / 3); } @@ -53,7 +63,7 @@ export async function getHeight( const onNode = unwrapProxy(options.onNode); if (cached) { const cache = heightCache.get(onNode); - if (cache != null && cache.time > Date.now() - _getPollInterval('block', options)) { + if (cache != null && cache.time > Date.now() - await _getPollInterval('key-block', options)) { return cache.height; } } @@ -77,21 +87,21 @@ export async function getHeight( export async function poll( th: Encoded.TxHash, { - blocks = 5, interval, onNode, ...options + blocks = 5, interval, ...options }: { blocks?: number; interval?: number; onNode: Node } & Parameters[1], ): Promise> { - interval ??= _getPollInterval('microblock', options); + interval ??= await _getPollInterval('micro-block', options); let max; do { - const tx = await onNode.getTransactionByHash(th); + const tx = await options.onNode.getTransactionByHash(th); if (tx.blockHeight !== -1) return tx; if (max == null) { max = tx.tx.ttl !== 0 ? -1 - : await getHeight({ ...options, onNode, cached: true }) + blocks; + : await getHeight({ ...options, cached: true }) + blocks; } await pause(interval); - } while (max === -1 ? true : await getHeight({ ...options, onNode, cached: true }) < max); + } while (max === -1 ? true : await getHeight({ ...options, cached: true }) < max); throw new TxTimedOutError(blocks, th); } @@ -106,14 +116,14 @@ export async function poll( */ export async function awaitHeight( height: number, - { interval, onNode, ...options }: + { interval, ...options }: { interval?: number; onNode: Node } & Parameters[1], ): Promise { - interval ??= Math.min(_getPollInterval('block', options), 5000); + interval ??= Math.min(await _getPollInterval('key-block', options), 5000); let currentHeight; do { if (currentHeight != null) await pause(interval); - currentHeight = await getHeight({ onNode }); + currentHeight = await getHeight(options); } while (currentHeight < height); return currentHeight; } diff --git a/src/oracle.ts b/src/oracle.ts index 3c64635651..d8fb034a58 100644 --- a/src/oracle.ts +++ b/src/oracle.ts @@ -34,13 +34,12 @@ type OracleQueries = Awaited>['orac export function pollForQueries( oracleId: Encoded.OracleAddress, onQuery: (query: OracleQueries[number]) => void, - { interval, onNode, ...options }: { interval?: number; onNode: Node } + { interval, ...options }: { interval?: number; onNode: Node } & Parameters[1], ): () => void { - interval ??= _getPollInterval('microblock', options); const knownQueryIds = new Set(); const checkNewQueries = async (): Promise => { - const queries = ((await onNode.getOracleQueriesByPubkey(oracleId)).oracleQueries ?? []) + const queries = ((await options.onNode.getOracleQueriesByPubkey(oracleId)).oracleQueries ?? []) .filter(({ id }) => !knownQueryIds.has(id)); queries.forEach((query) => { knownQueryIds.add(query.id); @@ -52,6 +51,7 @@ export function pollForQueries( // eslint-disable-next-line @typescript-eslint/no-floating-promises (async () => { + interval ??= await _getPollInterval('micro-block', options); while (!stopped) { // eslint-disable-line no-unmodified-loop-condition // TODO: allow to handle this error somehow await checkNewQueries().catch(console.error); @@ -74,19 +74,19 @@ export function pollForQueries( export async function pollForQueryResponse( oracleId: Encoded.OracleAddress, queryId: Encoded.OracleQueryId, - { interval, onNode, ...options }: + { interval, ...options }: { interval?: number; onNode: Node } & Parameters[1], ): Promise { - interval ??= _getPollInterval('microblock', options); + interval ??= await _getPollInterval('micro-block', options); let height; let ttl; let response; do { - ({ response, ttl } = await onNode.getOracleQueryByPubkeyAndQueryId(oracleId, queryId)); + ({ response, ttl } = await options.onNode.getOracleQueryByPubkeyAndQueryId(oracleId, queryId)); const responseBuffer = decode(response as Encoded.OracleResponse); if (responseBuffer.length > 0) return responseBuffer.toString(); await pause(interval); - height = await getHeight({ ...options, onNode, cached: true }); + height = await getHeight({ ...options, cached: true }); } while (ttl >= height); throw new RequestTimedOutError(height); } diff --git a/src/tx/builder/field-types/ttl.ts b/src/tx/builder/field-types/ttl.ts index a3196ba603..de2d1690de 100644 --- a/src/tx/builder/field-types/ttl.ts +++ b/src/tx/builder/field-types/ttl.ts @@ -23,7 +23,7 @@ export default { onNode?: Node; absoluteTtl?: boolean; _isInternalBuild?: boolean; - } & Parameters[1], + } & Omit[1], 'onNode'>, ) { if (absoluteTtl !== true && value !== 0 && (value != null || _isInternalBuild === true)) { if (onNode == null) throw new ArgumentError('onNode', 'provided', onNode); diff --git a/test/integration/AeSdk.ts b/test/integration/AeSdk.ts new file mode 100644 index 0000000000..42745e093d --- /dev/null +++ b/test/integration/AeSdk.ts @@ -0,0 +1,24 @@ +import { describe, it } from 'mocha'; +import { expect } from 'chai'; +import { getSdk, networkId } from '.'; + +describe('AeSdk', () => { + describe('_getPollInterval', () => { + it('returns value based on options', async () => { + const aeSdk = await getSdk(0); + aeSdk._options._expectedMineRate = 1000; + aeSdk._options._microBlockCycle = 300; + expect(await aeSdk._getPollInterval('key-block')).to.be.equal(333); + expect(await aeSdk._getPollInterval('micro-block')).to.be.equal(100); + }); + + it('returns correct value', async () => { + const aeSdk = await getSdk(0); + delete aeSdk._options._expectedMineRate; + delete aeSdk._options._microBlockCycle; + const [kb, mb] = networkId === 'ae_dev' ? [0, 0] : [60000, 1000]; + expect(await aeSdk._getPollInterval('key-block')).to.be.equal(kb); + expect(await aeSdk._getPollInterval('micro-block')).to.be.equal(mb); + }); + }); +}); diff --git a/test/unit/ae-sdk.ts b/test/unit/ae-sdk.ts index 5d37c7cada..66863a6a7c 100644 --- a/test/unit/ae-sdk.ts +++ b/test/unit/ae-sdk.ts @@ -4,8 +4,8 @@ import '../index'; import { AeSdk } from '../../src'; describe('AeSdk', () => { - it('executes methods without node, compiler, accounts', () => { - const aeSdk = new AeSdk(); - expect(aeSdk._getPollInterval('block')).to.be.a('number'); + it('executes methods without node, compiler, accounts', async () => { + const aeSdk = new AeSdk({ _expectedMineRate: 1000 }); + expect(await aeSdk._getPollInterval('key-block')).to.be.equal(333); }); });