Skip to content

Commit 189b2d2

Browse files
authored
fix: correctly handle v1 weights for historical blocks (#12065)
1 parent 0a49d34 commit 189b2d2

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

packages/page-explorer/src/BlockInfo/Summary.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,20 @@ function accumulateWeights (
3030
const totalRefTime = new BN(0);
3131
const totalProofSize = new BN(0);
3232

33+
if (!weight) {
34+
return { totalProofSize, totalRefTime };
35+
}
36+
3337
(['normal', 'operational', 'mandatory'] as const).forEach((cls) => {
34-
totalRefTime.iadd(weight?.[cls].refTime.toBn() ?? new BN(0));
35-
totalProofSize.iadd(weight?.[cls].proofSize.toBn() ?? new BN(0));
38+
const classWeight = weight[cls];
39+
40+
if (classWeight) {
41+
// convertWeight handles both V1 and V2 weights
42+
const { v2Weight } = convertWeight(classWeight as V2Weight);
43+
44+
totalRefTime.iadd(isBn(v2Weight.refTime) ? v2Weight.refTime : v2Weight.refTime.toBn());
45+
totalProofSize.iadd(isBn(v2Weight.proofSize) ? v2Weight.proofSize : v2Weight.proofSize.toBn());
46+
}
3647
});
3748

3849
return { totalProofSize, totalRefTime };

packages/react-hooks/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ export interface Preimage extends PreimageBytes, PreimageStatus {
216216

217217
export interface V2WeightConstruct {
218218
refTime: BN | ICompact<INumber>;
219-
proofSize?: BN | ICompact<INumber>;
219+
proofSize: BN | ICompact<INumber>;
220220
}
221221

222222
export interface WeightResult {

packages/react-hooks/src/useTxBatch.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,11 @@ interface Known {
3232
// converts a weight construct to only contain BN values
3333
function bnWeight (a: WeightSimple): BNWeight {
3434
return {
35-
proofSize: a.proofSize
36-
? bnToBn(
37-
isCompact(a.proofSize)
38-
? a.proofSize.unwrap()
39-
: a.proofSize
40-
)
41-
: BN_ZERO,
35+
proofSize: bnToBn(
36+
isCompact(a.proofSize)
37+
? a.proofSize.unwrap()
38+
: a.proofSize
39+
),
4240
refTime: bnToBn(
4341
isCompact(a.refTime)
4442
? a.refTime.unwrap()

packages/react-hooks/src/useWeight.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const ZERO_ACCOUNT = '0x9876543210abcdef9876543210abcdef9876543210abcdef9
3333
const EMPTY_STATE: Partial<Result> = {
3434
encodedCallLength: 0,
3535
v1Weight: BN_ZERO,
36-
v2Weight: { refTime: BN_ZERO },
36+
v2Weight: { proofSize: BN_ZERO, refTime: BN_ZERO },
3737
weight: BN_ZERO
3838
};
3939

0 commit comments

Comments
 (0)