Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: discriminate extrinsic_base_weight based on dispatch class #414

Merged
merged 33 commits into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
c540824
feat: update wasm pkg with updated calc_fee
TarikGul Feb 1, 2021
13e8c51
feat: remove extrinsic_baseweight from from_params, and add it to cal…
TarikGul Feb 1, 2021
8618bfe
feat: working version minus testing
TarikGul Feb 1, 2021
9e3cb87
feat: update @substrate/calc for local dev
TarikGul Feb 1, 2021
3d77f75
fix: update test to take in extrinsicBaseweight param
TarikGul Feb 1, 2021
533a502
fix: update packages, lint
TarikGul Feb 1, 2021
f5c1a51
fix: add getParentParentHash, remove block param from createCalc, opt…
TarikGul Feb 2, 2021
f1cf416
fix: fix parameters for calc_fee tests
TarikGul Feb 2, 2021
1159516
fix: comments, cleanup
TarikGul Feb 2, 2021
d4a2d41
fix: logging for calc_fee
TarikGul Feb 2, 2021
1c7477a
fix: package import
TarikGul Feb 2, 2021
ccda584
Update src/services/blocks/BlocksService.ts
TarikGul Feb 4, 2021
3fb18ef
Update src/services/blocks/BlocksService.ts
TarikGul Feb 4, 2021
6708ddd
Update src/services/blocks/BlocksService.ts
TarikGul Feb 4, 2021
92c7342
fix: adjust params for createCalcFee
TarikGul Feb 4, 2021
ff6b58c
fix: add ICalcFee response type for createCalcFee
TarikGul Feb 4, 2021
516a786
fix: update structure to save on api calls, add field to createCalcFe…
TarikGul Feb 4, 2021
d0ab4e7
feat: update @substrate/calc versioning
TarikGul Feb 4, 2021
3abf658
fix: package.json @substrate/calc versioning
TarikGul Feb 4, 2021
94efd8b
fix: revert version in package.json for testing
TarikGul Feb 4, 2021
014251c
fix: packaging
TarikGul Feb 4, 2021
c95712f
Update src/types/responses/Block.ts
TarikGul Feb 4, 2021
cd4190d
fix: order of variable bug
TarikGul Feb 4, 2021
b59bc64
fix: return a dispatchInfo with null parital fee if extrinsicBaseWeig…
TarikGul Feb 5, 2021
4f31a5f
fix: add tests for partialFee
TarikGul Feb 5, 2021
6a21a5c
fix: remove isPartialFeeNull and set calcFee to null instead
TarikGul Feb 5, 2021
c80f1bd
fix: add tests for calcFee being null
TarikGul Feb 5, 2021
04832d1
fix: change type for calcFee in ICalcFee
TarikGul Feb 5, 2021
0d45a1f
fix: remove mockApiCreateCalcFeeEdgeCase
TarikGul Feb 5, 2021
0ad9477
Update src/types/responses/Block.ts
TarikGul Feb 6, 2021
949e451
fix: update packages
TarikGul Feb 9, 2021
162302d
fix: merge
TarikGul Feb 9, 2021
1865770
fix: update packages
TarikGul Feb 9, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the Cargo.toml version should get bumped to "0.2.0" since this is a breaking change, but we are on major 0 so we just bump minor. After bumping in Cargo.toml you should be able to run build.sh and the package.json should update.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rad, thanks for explaining. Yea was a little confused at first with the versioning.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"@polkadot/api": "^3.6.4",
"@polkadot/apps-config": "^0.77.1",
"@polkadot/util-crypto": "^5.4.4",
"@substrate/calc": "^0.1.3",
"@substrate/calc": "file:./calc/pkg",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a reminder that this should be change to "^0.2.0" before it gets merged.

"confmgr": "^1.0.6",
"express": "^4.17.1",
"express-winston": "^4.0.5",
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