Skip to content

Commit

Permalink
fix: Account for polkadot-js changes; Harden createCalcFee (#376)
Browse files Browse the repository at this point in the history
* fix: Account for polkadot-js number serialization change; Add log message to calc

* Update mock api to use codec types for weightToFee

* fix spacing

* Update README.md

Co-authored-by: David <[email protected]>

* Cast to AbstractInt; Spacing

Co-authored-by: David <[email protected]>
  • Loading branch information
emostov and dvdplm committed Jan 7, 2021
1 parent a6fdda5 commit fdee04c
Show file tree
Hide file tree
Showing 11 changed files with 563 additions and 458 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ the console. Because this fee calculation happens in the statically compiled web
a re-compile with the proper environment variable set is necessary:

```bash
CALC_DEBUG=1 yarn
CALC_DEBUG=1 sh calc/build.sh
```

## Available endpoints
Expand Down
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.3"
version = "0.1.4"
authors = ["Parity Technologies <[email protected]>"]
edition = "2018"
license = "Apache-2.0"
Expand Down
73 changes: 73 additions & 0 deletions calc/pkg/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,71 @@ function getInt32Memory0() {
return cachegetInt32Memory0;
}

function debugString(val) {
// primitive types
const type = typeof val;
if (type == 'number' || type == 'boolean' || val == null) {
return `${val}`;
}
if (type == 'string') {
return `"${val}"`;
}
if (type == 'symbol') {
const description = val.description;
if (description == null) {
return 'Symbol';
} else {
return `Symbol(${description})`;
}
}
if (type == 'function') {
const name = val.name;
if (typeof name == 'string' && name.length > 0) {
return `Function(${name})`;
} else {
return 'Function';
}
}
// objects
if (Array.isArray(val)) {
const length = val.length;
let debug = '[';
if (length > 0) {
debug += debugString(val[0]);
}
for(let i = 1; i < length; i++) {
debug += ', ' + debugString(val[i]);
}
debug += ']';
return debug;
}
// Test for built-in
const builtInMatches = /\[object ([^\]]+)\]/.exec(toString.call(val));
let className;
if (builtInMatches.length > 1) {
className = builtInMatches[1];
} else {
// Failed to match the standard '[object ClassName]'
return toString.call(val);
}
if (className == 'Object') {
// we're a user defined class or Object
// JSON.stringify avoids problems with cycles, and is generally much
// easier than looping through ownProperties of `val`.
try {
return 'Object(' + JSON.stringify(val) + ')';
} catch (_) {
return 'Object';
}
}
// errors
if (val instanceof Error) {
return `${val.name}: ${val.message}\n${val.stack}`;
}
// TODO we could test for more things here, like `Set`s and `Map`s.
return className;
}

let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });

cachedTextDecoder.decode();
Expand Down Expand Up @@ -194,6 +259,14 @@ module.exports.__wbindgen_json_serialize = function(arg0, arg1) {
getInt32Memory0()[arg0 / 4 + 0] = ptr0;
};

module.exports.__wbindgen_debug_string = function(arg0, arg1) {
var ret = debugString(getObject(arg1));
var ptr0 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
getInt32Memory0()[arg0 / 4 + 1] = len0;
getInt32Memory0()[arg0 / 4 + 0] = ptr0;
};

module.exports.__wbindgen_throw = function(arg0, arg1) {
throw new Error(getStringFromWasm0(arg0, arg1));
};
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 <[email protected]>"
],
"description": "Off-chain calculations for @substrate/api-sidecar.",
"version": "0.1.3",
"version": "0.1.4",
"license": "Apache-2.0",
"repository": {
"type": "git",
Expand Down
4 changes: 4 additions & 0 deletions calc/src/calc_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ impl CalcFee {
spec_version: u32,
) -> Option<CalcFee> {
debug::setup();
info!(
"CalcFee::from_params({:#?}, {}, {}, {}, {}, {})",
polynomial, extrinsic_base_weight, multiplier, per_byte_fee, spec_name, spec_version
);

let polynomial: Vec<Coefficient> = {
let poly: Option<Vec<JSCoefficient>> = polynomial.into_serde().unwrap();
Expand Down
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
"test": "jest --silent"
},
"dependencies": {
"@polkadot/api": "^3.0.1",
"@polkadot/apps-config": "^0.71.2",
"@polkadot/util-crypto": "^5.0.1",
"@polkadot/api": "^3.3.1",
"@polkadot/apps-config": "^0.74.1",
"@polkadot/util-crypto": "^5.2.3",
"@substrate/calc": "^0.1.3",
"confmgr": "^1.0.6",
"express": "^4.17.1",
Expand All @@ -53,24 +53,24 @@
"@types/jest": "^26.0.19",
"@types/morgan": "^1.9.2",
"@types/triple-beam": "^1.3.2",
"@typescript-eslint/eslint-plugin": "4.10.0",
"@typescript-eslint/parser": "4.10.0",
"eslint": "^7.15.0",
"eslint-config-prettier": "^7.0.0",
"eslint-plugin-prettier": "^3.3.0",
"@typescript-eslint/eslint-plugin": "4.12.0",
"@typescript-eslint/parser": "4.12.0",
"eslint": "^7.17.0",
"eslint-config-prettier": "^7.1.0",
"eslint-plugin-prettier": "^3.3.1",
"eslint-plugin-simple-import-sort": "^7.0.0",
"jest": "^26.6.3",
"prettier": "^2.2.1",
"rimraf": "^3.0.2",
"standard-version": "^9.0.0",
"standard-version": "^9.1.0",
"ts-jest": "^26.4.4",
"tsc-watch": "^4.2.9",
"typescript": "^4.1.3"
},
"resolutions": {
"node-forge": ">=0.10.0",
"node-fetch": ">=2.6.1",
"prismjs": ">=1.21.1"
"prismjs": ">=1.23.0"
},
"keywords": [
"substrate",
Expand Down
30 changes: 17 additions & 13 deletions src/services/blocks/BlocksService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ApiPromise } from '@polkadot/api';
import { SignedBlockExtended } from '@polkadot/api-derive/type';
import { GenericCall, Struct } from '@polkadot/types';
import { AbstractInt } from '@polkadot/types/codec/AbstractInt';
import {
Block,
BlockHash,
Expand Down Expand Up @@ -222,15 +223,16 @@ export class BlocksService extends AbstractService {
tip,
} = extrinsic;
const hash = u8aToHex(blake2AsU8a(extrinsic.toU8a(), 256));
const call = block.registry.createType('Call', method);

return {
method: {
pallet: method.sectionName,
method: method.methodName,
pallet: method.section,
method: method.method,
},
signature: isSigned ? { signature, signer } : null,
nonce: isSigned ? nonce : null,
args: this.parseGenericCall(method, block.registry).args,
args: this.parseGenericCall(call, block.registry).args,
tip: isSigned ? tip : null,
hash,
info: {},
Expand Down Expand Up @@ -338,7 +340,8 @@ export class BlocksService extends AbstractService {
block: Block
) {
const perByte = api.consts.transactionPayment?.transactionByteFee;
const extrinsicBaseWeight = api.consts.system?.extrinsicBaseWeight;
const extrinsicBaseWeight = api.consts.system
?.extrinsicBaseWeight as AbstractInt;

let calcFee, specName, specVersion;
if (
Expand All @@ -360,9 +363,11 @@ export class BlocksService extends AbstractService {
const coefficients = api.consts.transactionPayment.weightToFee.map(
(c) => {
return {
coeffInteger: c.coeffInteger.toString(),
coeffFrac: c.coeffFrac,
degree: c.degree,
// Anything that could overflow Number.MAX_SAFE_INTEGER needs to be serialized
// to BigInt or string.
coeffInteger: c.coeffInteger.toString(10),
coeffFrac: c.coeffFrac.toNumber(),
degree: c.degree.toNumber(),
negative: c.negative,
};
}
Expand Down Expand Up @@ -392,9 +397,9 @@ export class BlocksService extends AbstractService {

calcFee = CalcFee.from_params(
coefficients,
BigInt(extrinsicBaseWeight.toString()),
multiplier.toString(),
perByte.toString(),
extrinsicBaseWeight.toBigInt(),
multiplier.toString(10),
perByte.toString(10),
specName,
specVersion
);
Expand Down Expand Up @@ -455,7 +460,6 @@ export class BlocksService extends AbstractService {
genericCall: GenericCall,
registry: Registry
): ISanitizedCall {
const { sectionName, methodName } = genericCall;
const newArgs = {};

// Pull out the struct of arguments to this call
Expand Down Expand Up @@ -503,8 +507,8 @@ export class BlocksService extends AbstractService {

return {
method: {
pallet: sectionName,
method: methodName,
pallet: genericCall.section,
method: genericCall.method,
},
args: newArgs,
};
Expand Down
6 changes: 3 additions & 3 deletions src/services/test-helpers/mock/mockApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,9 @@ export const mockApi = ({
transactionByteFee: polkadotRegistry.createType('Balance', 1000000),
weightToFee: [
{
coeffFrac: 80000000,
coeffInteger: 0,
degree: 1,
coeffFrac: polkadotRegistry.createType('Perbill', 80000000),
coeffInteger: polkadotRegistry.createType('Balance', 0),
degree: polkadotRegistry.createType('u8', 1),
negative: false,
},
],
Expand Down
Loading

0 comments on commit fdee04c

Please sign in to comment.