From ec2c0c8ba5e056f810dec62bd24c1a2a75aa10ff Mon Sep 17 00:00:00 2001 From: mayar osama Date: Sun, 19 May 2024 18:25:53 +0300 Subject: [PATCH 1/7] Adding a batchSize and applying extrinsics per batch --- .../grid_client/src/clients/tf-grid/contracts.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/grid_client/src/clients/tf-grid/contracts.ts b/packages/grid_client/src/clients/tf-grid/contracts.ts index 73bd994ba4..f55e8e3e3c 100644 --- a/packages/grid_client/src/clients/tf-grid/contracts.ts +++ b/packages/grid_client/src/clients/tf-grid/contracts.ts @@ -317,11 +317,18 @@ class TFContracts extends Contracts { } async batchCancelContracts(ids: number[]): Promise { - const extrinsics: ExtrinsicResult[] = []; - for (const id of ids) { - extrinsics.push(await this.cancel({ id })); + const batchSize = 400; + + // Applying extrinsics per batch + for (let i = 0; i < ids.length; i += batchSize) { + const extrinsics: ExtrinsicResult[] = []; + const batch = ids.slice(i, i + batchSize); + for (const id of batch) { + extrinsics.push(await this.cancel({ id })); + } + await this.client.applyAllExtrinsics(extrinsics); } - await this.client.applyAllExtrinsics(extrinsics); + return ids; } From c7adbf0062e8a22e7f560027008de54558826416 Mon Sep 17 00:00:00 2001 From: mayar osama Date: Wed, 22 May 2024 17:51:40 +0300 Subject: [PATCH 2/7] Applying batch size in batch and batchAll method --- .../grid_client/src/clients/tf-grid/contracts.ts | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/packages/grid_client/src/clients/tf-grid/contracts.ts b/packages/grid_client/src/clients/tf-grid/contracts.ts index e9e062b82a..e9a45b2c17 100644 --- a/packages/grid_client/src/clients/tf-grid/contracts.ts +++ b/packages/grid_client/src/clients/tf-grid/contracts.ts @@ -318,17 +318,11 @@ class TFContracts extends Contracts { } async batchCancelContracts(ids: number[]): Promise { - const batchSize = 400; - - // Applying extrinsics per batch - for (let i = 0; i < ids.length; i += batchSize) { - const extrinsics: ExtrinsicResult[] = []; - const batch = ids.slice(i, i + batchSize); - for (const id of batch) { - extrinsics.push(await this.cancel({ id })); - } - await this.client.applyAllExtrinsics(extrinsics); + const extrinsics: ExtrinsicResult[] = []; + for (const id of ids) { + extrinsics.push(await this.cancel({ id })); } + await this.client.applyAllExtrinsics(extrinsics); return ids; } From e263610a14b3e0e93ff6113054b281bc046d957f Mon Sep 17 00:00:00 2001 From: mayar osama Date: Thu, 23 May 2024 10:09:35 +0300 Subject: [PATCH 3/7] Applying batch size in batch and batchAll method --- packages/tfchain_client/src/utility.ts | 28 ++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/packages/tfchain_client/src/utility.ts b/packages/tfchain_client/src/utility.ts index 6e17fec997..5ab9d6e6b6 100644 --- a/packages/tfchain_client/src/utility.ts +++ b/packages/tfchain_client/src/utility.ts @@ -12,10 +12,18 @@ class Utility { @checkConnection async batch(extrinsics: ExtrinsicResult[]): Promise { extrinsics = extrinsics.filter(Boolean); + const batchSize = 400; if (extrinsics.length > 0) { - const { resultSections, resultEvents } = this.extractResultSectionsAndEvents(extrinsics); - const batchExtrinsic = await this.client.api.tx.utility.batch(extrinsics); - return this.client.applyExtrinsic(batchExtrinsic, resultSections, resultEvents); + const result: T[][] = []; + for (let i = 0; i < extrinsics.length; i += batchSize) { + const batch = extrinsics.slice(i, i + batchSize); + const { resultSections, resultEvents } = this.extractResultSectionsAndEvents(batch); + const batchExtrinsic = await this.client.api.tx.utility.batch(batch); + const res = await this.client.applyExtrinsic(batchExtrinsic, resultSections, resultEvents); + result.push(res); + } + + return result.flat(); } return []; } @@ -23,10 +31,18 @@ class Utility { @checkConnection async batchAll(extrinsics: ExtrinsicResult[]): Promise { extrinsics = extrinsics.filter(Boolean); + const batchSize = 400; if (extrinsics.length > 0) { - const { resultSections, resultEvents } = this.extractResultSectionsAndEvents(extrinsics); - const batchAllExtrinsic = await this.client.api.tx.utility.batchAll(extrinsics); - return this.client.applyExtrinsic(batchAllExtrinsic, resultSections, resultEvents); + const result: T[][] = []; + for (let i = 0; i < extrinsics.length; i += batchSize) { + const batch = extrinsics.slice(i, i + batchSize); + const { resultSections, resultEvents } = this.extractResultSectionsAndEvents(batch); + const batchAllExtrinsic = await this.client.api.tx.utility.batchAll(batch); + const res = await this.client.applyExtrinsic(batchAllExtrinsic, resultSections, resultEvents); + result.push(res); + } + + return result.flat(); } return []; } From 4f1dd9575bfbf11cfa2539309ace50d1c53a81f7 Mon Sep 17 00:00:00 2001 From: mayar osama Date: Sun, 26 May 2024 17:31:27 +0300 Subject: [PATCH 4/7] Changing batchSize to a global constant and using concat instead of push --- .../grid_client/src/clients/tf-grid/contracts.ts | 1 - packages/tfchain_client/src/utility.ts | 14 +++++++------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/grid_client/src/clients/tf-grid/contracts.ts b/packages/grid_client/src/clients/tf-grid/contracts.ts index e9a45b2c17..e8374cd9ec 100644 --- a/packages/grid_client/src/clients/tf-grid/contracts.ts +++ b/packages/grid_client/src/clients/tf-grid/contracts.ts @@ -323,7 +323,6 @@ class TFContracts extends Contracts { extrinsics.push(await this.cancel({ id })); } await this.client.applyAllExtrinsics(extrinsics); - return ids; } diff --git a/packages/tfchain_client/src/utility.ts b/packages/tfchain_client/src/utility.ts index 5ab9d6e6b6..a9bff24b4f 100644 --- a/packages/tfchain_client/src/utility.ts +++ b/packages/tfchain_client/src/utility.ts @@ -2,6 +2,7 @@ import { Client } from "./client"; import { ExtrinsicResult } from "./types"; import { checkConnection } from "./utils"; +const batchSize = 400; class Utility { client: Client; @@ -12,18 +13,17 @@ class Utility { @checkConnection async batch(extrinsics: ExtrinsicResult[]): Promise { extrinsics = extrinsics.filter(Boolean); - const batchSize = 400; if (extrinsics.length > 0) { - const result: T[][] = []; + const result: T[] = []; for (let i = 0; i < extrinsics.length; i += batchSize) { const batch = extrinsics.slice(i, i + batchSize); const { resultSections, resultEvents } = this.extractResultSectionsAndEvents(batch); const batchExtrinsic = await this.client.api.tx.utility.batch(batch); const res = await this.client.applyExtrinsic(batchExtrinsic, resultSections, resultEvents); - result.push(res); + result.concat(res); } - return result.flat(); + return result; } return []; } @@ -33,16 +33,16 @@ class Utility { extrinsics = extrinsics.filter(Boolean); const batchSize = 400; if (extrinsics.length > 0) { - const result: T[][] = []; + const result: T[] = []; for (let i = 0; i < extrinsics.length; i += batchSize) { const batch = extrinsics.slice(i, i + batchSize); const { resultSections, resultEvents } = this.extractResultSectionsAndEvents(batch); const batchAllExtrinsic = await this.client.api.tx.utility.batchAll(batch); const res = await this.client.applyExtrinsic(batchAllExtrinsic, resultSections, resultEvents); - result.push(res); + result.concat(res); } - return result.flat(); + return result; } return []; } From 489dc2e4e5c1469712b482dc96ec144e6a756180 Mon Sep 17 00:00:00 2001 From: mayar osama Date: Mon, 27 May 2024 10:17:09 +0300 Subject: [PATCH 5/7] Removing unnecessary batchSize constant from batchAll method --- packages/tfchain_client/src/utility.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/tfchain_client/src/utility.ts b/packages/tfchain_client/src/utility.ts index a9bff24b4f..0a38836ed4 100644 --- a/packages/tfchain_client/src/utility.ts +++ b/packages/tfchain_client/src/utility.ts @@ -31,7 +31,6 @@ class Utility { @checkConnection async batchAll(extrinsics: ExtrinsicResult[]): Promise { extrinsics = extrinsics.filter(Boolean); - const batchSize = 400; if (extrinsics.length > 0) { const result: T[] = []; for (let i = 0; i < extrinsics.length; i += batchSize) { From 0b4d5505aaebdd122d704cf394372e414280db8b Mon Sep 17 00:00:00 2001 From: mayar osama Date: Mon, 27 May 2024 13:54:12 +0300 Subject: [PATCH 6/7] overwriting the result var --- packages/tfchain_client/src/utility.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/tfchain_client/src/utility.ts b/packages/tfchain_client/src/utility.ts index 0a38836ed4..1e0944e28c 100644 --- a/packages/tfchain_client/src/utility.ts +++ b/packages/tfchain_client/src/utility.ts @@ -14,13 +14,13 @@ class Utility { async batch(extrinsics: ExtrinsicResult[]): Promise { extrinsics = extrinsics.filter(Boolean); if (extrinsics.length > 0) { - const result: T[] = []; + let result: T[] = []; for (let i = 0; i < extrinsics.length; i += batchSize) { const batch = extrinsics.slice(i, i + batchSize); const { resultSections, resultEvents } = this.extractResultSectionsAndEvents(batch); const batchExtrinsic = await this.client.api.tx.utility.batch(batch); const res = await this.client.applyExtrinsic(batchExtrinsic, resultSections, resultEvents); - result.concat(res); + result = result.concat(res); } return result; @@ -32,13 +32,13 @@ class Utility { async batchAll(extrinsics: ExtrinsicResult[]): Promise { extrinsics = extrinsics.filter(Boolean); if (extrinsics.length > 0) { - const result: T[] = []; + let result: T[] = []; for (let i = 0; i < extrinsics.length; i += batchSize) { const batch = extrinsics.slice(i, i + batchSize); const { resultSections, resultEvents } = this.extractResultSectionsAndEvents(batch); const batchAllExtrinsic = await this.client.api.tx.utility.batchAll(batch); const res = await this.client.applyExtrinsic(batchAllExtrinsic, resultSections, resultEvents); - result.concat(res); + result = result.concat(res); } return result; From 814ffa556ea8cab8b7313dedfbe864b7602883f7 Mon Sep 17 00:00:00 2001 From: mayar osama Date: Mon, 27 May 2024 13:58:34 +0300 Subject: [PATCH 7/7] Renaming batchsize --- packages/tfchain_client/src/utility.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/tfchain_client/src/utility.ts b/packages/tfchain_client/src/utility.ts index 1e0944e28c..35db0fb6e5 100644 --- a/packages/tfchain_client/src/utility.ts +++ b/packages/tfchain_client/src/utility.ts @@ -2,7 +2,7 @@ import { Client } from "./client"; import { ExtrinsicResult } from "./types"; import { checkConnection } from "./utils"; -const batchSize = 400; +const BATCH_SIZE = 400; class Utility { client: Client; @@ -15,8 +15,8 @@ class Utility { extrinsics = extrinsics.filter(Boolean); if (extrinsics.length > 0) { let result: T[] = []; - for (let i = 0; i < extrinsics.length; i += batchSize) { - const batch = extrinsics.slice(i, i + batchSize); + for (let i = 0; i < extrinsics.length; i += BATCH_SIZE) { + const batch = extrinsics.slice(i, i + BATCH_SIZE); const { resultSections, resultEvents } = this.extractResultSectionsAndEvents(batch); const batchExtrinsic = await this.client.api.tx.utility.batch(batch); const res = await this.client.applyExtrinsic(batchExtrinsic, resultSections, resultEvents); @@ -33,8 +33,8 @@ class Utility { extrinsics = extrinsics.filter(Boolean); if (extrinsics.length > 0) { let result: T[] = []; - for (let i = 0; i < extrinsics.length; i += batchSize) { - const batch = extrinsics.slice(i, i + batchSize); + for (let i = 0; i < extrinsics.length; i += BATCH_SIZE) { + const batch = extrinsics.slice(i, i + BATCH_SIZE); const { resultSections, resultEvents } = this.extractResultSectionsAndEvents(batch); const batchAllExtrinsic = await this.client.api.tx.utility.batchAll(batch); const res = await this.client.applyExtrinsic(batchAllExtrinsic, resultSections, resultEvents);