From 189b2d28c3a8bf7a17b0881623d24cfd2cf70512 Mon Sep 17 00:00:00 2001 From: Francis O'Brien <42175565+F-OBrien@users.noreply.github.com> Date: Mon, 15 Dec 2025 02:44:24 +0000 Subject: [PATCH] fix: correctly handle v1 weights for historical blocks (#12065) --- packages/page-explorer/src/BlockInfo/Summary.tsx | 15 +++++++++++++-- packages/react-hooks/src/types.ts | 2 +- packages/react-hooks/src/useTxBatch.ts | 12 +++++------- packages/react-hooks/src/useWeight.ts | 2 +- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/packages/page-explorer/src/BlockInfo/Summary.tsx b/packages/page-explorer/src/BlockInfo/Summary.tsx index e2cd294633..085308a5f4 100644 --- a/packages/page-explorer/src/BlockInfo/Summary.tsx +++ b/packages/page-explorer/src/BlockInfo/Summary.tsx @@ -30,9 +30,20 @@ function accumulateWeights ( const totalRefTime = new BN(0); const totalProofSize = new BN(0); + if (!weight) { + return { totalProofSize, totalRefTime }; + } + (['normal', 'operational', 'mandatory'] as const).forEach((cls) => { - totalRefTime.iadd(weight?.[cls].refTime.toBn() ?? new BN(0)); - totalProofSize.iadd(weight?.[cls].proofSize.toBn() ?? new BN(0)); + const classWeight = weight[cls]; + + if (classWeight) { + // convertWeight handles both V1 and V2 weights + const { v2Weight } = convertWeight(classWeight as V2Weight); + + totalRefTime.iadd(isBn(v2Weight.refTime) ? v2Weight.refTime : v2Weight.refTime.toBn()); + totalProofSize.iadd(isBn(v2Weight.proofSize) ? v2Weight.proofSize : v2Weight.proofSize.toBn()); + } }); return { totalProofSize, totalRefTime }; diff --git a/packages/react-hooks/src/types.ts b/packages/react-hooks/src/types.ts index 20e45af73c..288bae201b 100644 --- a/packages/react-hooks/src/types.ts +++ b/packages/react-hooks/src/types.ts @@ -216,7 +216,7 @@ export interface Preimage extends PreimageBytes, PreimageStatus { export interface V2WeightConstruct { refTime: BN | ICompact; - proofSize?: BN | ICompact; + proofSize: BN | ICompact; } export interface WeightResult { diff --git a/packages/react-hooks/src/useTxBatch.ts b/packages/react-hooks/src/useTxBatch.ts index 1beca2b3af..885174cff7 100644 --- a/packages/react-hooks/src/useTxBatch.ts +++ b/packages/react-hooks/src/useTxBatch.ts @@ -32,13 +32,11 @@ interface Known { // converts a weight construct to only contain BN values function bnWeight (a: WeightSimple): BNWeight { return { - proofSize: a.proofSize - ? bnToBn( - isCompact(a.proofSize) - ? a.proofSize.unwrap() - : a.proofSize - ) - : BN_ZERO, + proofSize: bnToBn( + isCompact(a.proofSize) + ? a.proofSize.unwrap() + : a.proofSize + ), refTime: bnToBn( isCompact(a.refTime) ? a.refTime.unwrap() diff --git a/packages/react-hooks/src/useWeight.ts b/packages/react-hooks/src/useWeight.ts index 46ad675833..291b1854fa 100644 --- a/packages/react-hooks/src/useWeight.ts +++ b/packages/react-hooks/src/useWeight.ts @@ -33,7 +33,7 @@ export const ZERO_ACCOUNT = '0x9876543210abcdef9876543210abcdef9876543210abcdef9 const EMPTY_STATE: Partial = { encodedCallLength: 0, v1Weight: BN_ZERO, - v2Weight: { refTime: BN_ZERO }, + v2Weight: { proofSize: BN_ZERO, refTime: BN_ZERO }, weight: BN_ZERO };