Skip to content

Commit

Permalink
fix: discriminate extrinsic_base_weight based on dispatch class (#414)
Browse files Browse the repository at this point in the history
fix: discriminate `extrinsic_base_weight` based on dispatch class
  • Loading branch information
TarikGul committed Feb 9, 2021
1 parent 4454294 commit ff98c76
Show file tree
Hide file tree
Showing 13 changed files with 642 additions and 505 deletions.
2 changes: 1 addition & 1 deletion calc/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion calc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "calc"
version = "0.1.4"
version = "0.2.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
license = "Apache-2.0"
Expand Down
6 changes: 3 additions & 3 deletions calc/pkg/calc.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ export class CalcFee {
free(): void;
/**
* @param {any} polynomial
* @param {BigInt} extrinsic_base_weight
* @param {string} multiplier
* @param {string} per_byte_fee
* @param {string} spec_name
* @param {number} spec_version
* @returns {CalcFee | undefined}
*/
static from_params(polynomial: any, extrinsic_base_weight: BigInt, multiplier: string, per_byte_fee: string, spec_name: string, spec_version: number): CalcFee | undefined;
static from_params(polynomial: any, multiplier: string, per_byte_fee: string, spec_name: string, spec_version: number): CalcFee | undefined;
/**
* @param {BigInt} weight
* @param {number} len
* @param {BigInt} extrinsic_base_weight
* @returns {string}
*/
calc_fee(weight: BigInt, len: number): string;
calc_fee(weight: BigInt, len: number, extrinsic_base_weight: BigInt): string;
}
/**
*/
Expand Down
24 changes: 12 additions & 12 deletions calc/pkg/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,25 +147,21 @@ class CalcFee {
}
/**
* @param {any} polynomial
* @param {BigInt} extrinsic_base_weight
* @param {string} multiplier
* @param {string} per_byte_fee
* @param {string} spec_name
* @param {number} spec_version
* @returns {CalcFee | undefined}
*/
static from_params(polynomial, extrinsic_base_weight, multiplier, per_byte_fee, spec_name, spec_version) {
static from_params(polynomial, multiplier, per_byte_fee, spec_name, spec_version) {
try {
uint64CvtShim[0] = extrinsic_base_weight;
const low0 = u32CvtShim[0];
const high0 = u32CvtShim[1];
var ptr1 = passStringToWasm0(multiplier, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var ptr0 = passStringToWasm0(multiplier, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
var ptr1 = passStringToWasm0(per_byte_fee, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len1 = WASM_VECTOR_LEN;
var ptr2 = passStringToWasm0(per_byte_fee, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var ptr2 = passStringToWasm0(spec_name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len2 = WASM_VECTOR_LEN;
var ptr3 = passStringToWasm0(spec_name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len3 = WASM_VECTOR_LEN;
var ret = wasm.calcfee_from_params(addBorrowedObject(polynomial), low0, high0, ptr1, len1, ptr2, len2, ptr3, len3, spec_version);
var ret = wasm.calcfee_from_params(addBorrowedObject(polynomial), ptr0, len0, ptr1, len1, ptr2, len2, spec_version);
return ret === 0 ? undefined : CalcFee.__wrap(ret);
} finally {
heap[stack_pointer++] = undefined;
Expand All @@ -174,16 +170,20 @@ class CalcFee {
/**
* @param {BigInt} weight
* @param {number} len
* @param {BigInt} extrinsic_base_weight
* @returns {string}
*/
calc_fee(weight, len) {
calc_fee(weight, len, extrinsic_base_weight) {
try {
const retptr = wasm.__wbindgen_export_2.value - 16;
wasm.__wbindgen_export_2.value = retptr;
uint64CvtShim[0] = weight;
const low0 = u32CvtShim[0];
const high0 = u32CvtShim[1];
wasm.calcfee_calc_fee(retptr, this.ptr, low0, high0, len);
uint64CvtShim[0] = extrinsic_base_weight;
const low1 = u32CvtShim[0];
const high1 = u32CvtShim[1];
wasm.calcfee_calc_fee(retptr, this.ptr, low0, high0, len, low1, high1);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
return getStringFromWasm0(r0, r1);
Expand Down
4 changes: 2 additions & 2 deletions calc/pkg/calc_bg.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
/* eslint-disable */
export const memory: WebAssembly.Memory;
export function __wbg_calcfee_free(a: number): void;
export function calcfee_from_params(a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number): number;
export function calcfee_calc_fee(a: number, b: number, c: number, d: number, e: number): void;
export function calcfee_from_params(a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number): number;
export function calcfee_calc_fee(a: number, b: number, c: number, d: number, e: number, f: number, g: number): void;
export function __wbg_calcpayout_free(a: number): void;
export function calcpayout_from_params(a: number, b: number, c: number): number;
export function calcpayout_calc_payout(a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number): void;
Expand Down
Binary file modified calc/pkg/calc_bg.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion calc/pkg/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"Parity Technologies <admin@parity.io>"
],
"description": "Off-chain calculations for @substrate/api-sidecar.",
"version": "0.1.4",
"version": "0.2.0",
"license": "Apache-2.0",
"repository": {
"type": "git",
Expand Down
22 changes: 12 additions & 10 deletions calc/src/calc_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,22 @@ pub struct CalcFee {
polynomial: Vec<Coefficient>,
multiplier: Multiplier,
per_byte_fee: Balance,
base_fee: Balance,
adjust_len_fee: bool,
}

#[wasm_bindgen]
impl CalcFee {
pub fn from_params(
polynomial: &JsValue,
extrinsic_base_weight: Weight,
multiplier: &str,
per_byte_fee: &str,
spec_name: &str,
spec_version: u32,
) -> Option<CalcFee> {
debug::setup();
info!(
"CalcFee::from_params({:#?}, {}, {}, {}, {}, {})",
polynomial, extrinsic_base_weight, multiplier, per_byte_fee, spec_name, spec_version
"CalcFee::from_params({:#?}, {}, {}, {}, {})",
polynomial, multiplier, per_byte_fee, spec_name, spec_version
);

let polynomial: Vec<Coefficient> = {
Expand All @@ -126,7 +124,6 @@ impl CalcFee {
};
let multiplier = Multiplier::new(multiplier, spec_name, spec_version)?;
let per_byte_fee = Balance::from_str(per_byte_fee).unwrap();
let base_fee = weight_to_fee(&extrinsic_base_weight, &polynomial);
let adjust_len_fee = if let Multiplier::V2(_) = &multiplier {
false
} else {
Expand All @@ -136,7 +133,6 @@ impl CalcFee {
polynomial,
multiplier,
per_byte_fee,
base_fee,
adjust_len_fee,
};
info!(
Expand All @@ -146,9 +142,15 @@ impl CalcFee {
Some(calc)
}

pub fn calc_fee(&self, weight: Weight, len: u32) -> String {
pub fn calc_fee(
&self,
weight: Weight,
len: u32,
extrinsic_base_weight: Weight,
) -> String {
let unadjusted_len_fee = self.per_byte_fee.saturating_mul(len.into());
let unadjusted_weight_fee = weight_to_fee(&weight, &self.polynomial);
let base_fee = weight_to_fee(&extrinsic_base_weight, &self.polynomial);

let (len_fee, adjustable_fee) = if self.adjust_len_fee {
(0, unadjusted_len_fee.saturating_add(unadjusted_weight_fee))
Expand All @@ -157,20 +159,20 @@ impl CalcFee {
};
let adjusted_fee = self.multiplier.calc(adjustable_fee);

let result = self
.base_fee
let result = base_fee
.saturating_add(len_fee)
.saturating_add(adjusted_fee);

info!(
"calc_fee: ({}, {}) -> len_fee: {} weight_fee: {} adjustable_fee: {} \
adjusted_fee: {} result: {}",
adjusted_fee: {} base_fee: {} result: {}",
weight,
len,
unadjusted_len_fee,
unadjusted_weight_fee,
adjustable_fee,
adjusted_fee,
base_fee,
result
);

Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
"test": "jest --silent"
},
"dependencies": {
"@polkadot/api": "^3.7.3",
"@polkadot/apps-config": "^0.78.1",
"@polkadot/util-crypto": "^5.4.4",
"@substrate/calc": "^0.1.3",
"@polkadot/api": "^3.8.1",
"@polkadot/apps-config": "^0.79.1",
"@polkadot/util-crypto": "^5.6.2",
"@substrate/calc": "^0.2.0",
"confmgr": "^1.0.6",
"express": "^4.17.1",
"express-winston": "^4.0.5",
Expand All @@ -53,8 +53,8 @@
"@types/jest": "^26.0.20",
"@types/morgan": "^1.9.2",
"@types/triple-beam": "^1.3.2",
"@typescript-eslint/eslint-plugin": "4.14.2",
"@typescript-eslint/parser": "4.14.2",
"@typescript-eslint/eslint-plugin": "4.15.0",
"@typescript-eslint/parser": "4.15.0",
"eslint": "^7.19.0",
"eslint-config-prettier": "^7.2.0",
"eslint-plugin-prettier": "^3.3.1",
Expand All @@ -63,9 +63,9 @@
"prettier": "^2.2.1",
"rimraf": "^3.0.2",
"standard-version": "^9.1.0",
"ts-jest": "^26.5.0",
"ts-jest": "^26.5.1",
"tsc-watch": "^4.2.9",
"typescript": "^4.1.3"
"typescript": "^4.1.4"
},
"resolutions": {
"node-forge": ">=0.10.0",
Expand Down
64 changes: 59 additions & 5 deletions src/services/blocks/BlocksService.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
/* eslint-disable @typescript-eslint/no-unsafe-call */
import { ApiPromise } from '@polkadot/api';
import { AugmentedConst } from '@polkadot/api/types/consts';
import { RpcPromiseResult } from '@polkadot/api/types/rpc';
import { GenericExtrinsic } from '@polkadot/types';
import { GenericCall } from '@polkadot/types/generic';
import { BlockHash, Hash, SignedBlock } from '@polkadot/types/interfaces';
import {
BalanceOf,
BlockHash,
Hash,
SignedBlock,
} from '@polkadot/types/interfaces';
import { BadRequest } from 'http-errors';

import { sanitizeNumbers } from '../../sanitize/sanitizeNumbers';
Expand All @@ -12,6 +18,7 @@ import {
kusamaRegistry,
polkadotRegistry,
} from '../../test-helpers/registries';
import { IExtrinsic } from '../../types/responses/';
import {
blockHash789629,
getBlock,
Expand All @@ -32,6 +39,13 @@ type GetBlock = RpcPromiseResult<
(hash?: string | BlockHash | Uint8Array | undefined) => Promise<SignedBlock>
>;

/**
* Interface for the reponse in `fetchBlock` test suite
*/
interface ResponseObj {
extrinsics: IExtrinsic[];
}

/**
* BlockService mock
*/
Expand Down Expand Up @@ -113,6 +127,44 @@ describe('BlocksService', () => {

expect(block.finalized).toEqual(undefined);
});

it('Return an error with a null calcFee when perByte is undefined', async () => {
mockApi.consts.transactionPayment.transactionByteFee = (undefined as unknown) as BalanceOf &
AugmentedConst<'promise'>;

const configuredBlocksService = new BlocksService(mockApi);

// fetchBlock options
const options = {
eventDocs: true,
extrinsicDocs: true,
checkFinalized: false,
queryFinalizedHead: false,
omitFinalizedTag: false,
};

const response = sanitizeNumbers(
await configuredBlocksService.fetchBlock(
blockHash789629,
options
)
);

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const responseObj: ResponseObj = JSON.parse(
JSON.stringify(response)
);

// Revert mockApi back to its original setting that was changed above.
mockApi.consts.transactionPayment.transactionByteFee = polkadotRegistry.createType(
'Balance',
1000000
) as BalanceOf & AugmentedConst<'promise'>;

expect(responseObj.extrinsics[3].info).toEqual({
error: 'Fee calculation not supported for 16#polkadot',
});
});
});

describe('createCalcFee & calc_fee', () => {
Expand All @@ -124,7 +176,9 @@ describe('BlocksService', () => {
mockBlock789629
);

expect(calcFee?.calc_fee(BigInt(399480000), 534)).toBe('544000000');
expect(
calcFee?.calc_fee(BigInt(399480000), 534, BigInt(125000000))
).toBe('544000000');
});

it('calculates partialFee for utility.batch in polkadot block 789629', async () => {
Expand All @@ -135,9 +189,9 @@ describe('BlocksService', () => {
mockBlock789629
);

expect(calcFee?.calc_fee(BigInt(941325000000), 1247)).toBe(
'1257000075'
);
expect(
calcFee?.calc_fee(BigInt(941325000000), 1247, BigInt(125000000))
).toBe('1257000075');
});
});

Expand Down
Loading

0 comments on commit ff98c76

Please sign in to comment.