Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Next Next commit
Add debug build of calc-fee that outputs more information
  • Loading branch information
athei committed Jun 18, 2020
commit 4cd8bbf5a944b72dad262b7c21e3142aa37b5296
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ yarn
yarn start
```

### Fee Calculation Debugging

It is possible to get more information about the fee calculation process logged to
the console. Because this fee calculation happens in the statically compiled web assembly part
a re-compile with the proper environment variable set is necessary:

```
FEE_DEBUG=1 yarn
```

### Available paths

Block IDs may take two forms: a non-negative decimal integer that denotes the block _height_ **or**
Expand Down
31 changes: 31 additions & 0 deletions calc-fee/Cargo.lock

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

4 changes: 3 additions & 1 deletion calc-fee/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ description = "Calculate extrinsic fees off-chain"
crate-type = ["cdylib"]

[features]
debug = ["console_error_panic_hook"]
debug = ["console_error_panic_hook", "console_log"]

[dependencies]
wasm-bindgen = { version = "0.2", default_features = false, features = ["serde-serialize"] }
serde_derive = { version = "1", default_features = false }
serde = { version = "1", default_features = false }
console_error_panic_hook = { version = "0.1", optional = true }
console_log = { version = "0.2.0", optional = true }
log = "0.4.8"

[dependencies.sp-arithmetic]
version = "2.0.0-rc3"
Expand Down
10 changes: 10 additions & 0 deletions calc-fee/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

if [ -z ${FEE_DEBUG} ]; then
wasm-pack build --target nodejs --scope polkadot "$SCRIPT_DIR"
else
echo "Fee debugging enabled"
wasm-pack build --target nodejs --scope polkadot "$SCRIPT_DIR" -- --features debug
fi
8 changes: 8 additions & 0 deletions calc-fee/src/debug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#[cfg(feature = "debug")]
pub fn setup() {
console_error_panic_hook::set_once();
console_log::init_with_level(log::Level::Debug).ok();
}

#[cfg(not(feature = "debug"))]
pub fn setup() {}
46 changes: 28 additions & 18 deletions calc-fee/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
mod panic;
mod debug;

use core::str::FromStr;
use log::info;
use serde_derive::Deserialize;
use sp_arithmetic::{FixedI128, FixedPointNumber, Perbill};
use sp_arithmetic_legacy::Fixed128 as Fixed128Legacy;
use wasm_bindgen::prelude::*;
use core::str::FromStr;

type Balance = u128;
type Weight = u64;
Expand All @@ -18,13 +19,15 @@ pub struct JSCoefficient {
degree: u8,
}

#[derive(Debug)]
struct Coefficient {
coeff_integer: Balance,
coeff_frac: Perbill,
negative: bool,
degree: u8,
}

#[derive(Debug)]
enum Multiplier {
Current((FixedI128, bool)),
Legacy(Fixed128Legacy),
Expand All @@ -48,13 +51,14 @@ impl Multiplier {
} else {
mult.0.saturating_mul_acc_int(balance)
}
},
}
Self::Legacy(mult) => mult.saturated_multiply_accumulate(balance),
}
}
}

#[wasm_bindgen]
#[derive(Debug)]
pub struct CalcFee {
polynomial: Vec<Coefficient>,
multiplier: Multiplier,
Expand All @@ -72,44 +76,50 @@ impl CalcFee {
fixed128_bug: bool,
fixed128_legacy: bool,
) -> Self {
panic::set_hook();
debug::setup();

let polynomial: Vec<Coefficient> = {
let poly: Vec<JSCoefficient> = polynomial.into_serde().unwrap();
poly.iter().map(|c|
Coefficient {
poly.iter()
.map(|c| Coefficient {
coeff_integer: Balance::from_str(&c.coeffInteger).unwrap(),
coeff_frac: Perbill::from_parts(c.coeffFrac),
negative: c.negative,
degree: c.degree
}
)
.collect()
degree: c.degree,
})
.collect()
};
let multiplier = Multiplier::new(
i128::from_str(multiplier).unwrap(),
fixed128_legacy,
fixed128_bug
fixed128_bug,
);
let per_byte_fee = Balance::from_str(per_byte_fee).unwrap();
let base_fee = weight_to_fee(&extrinsic_base_weight, &polynomial);
Self {
let calc = Self {
polynomial,
multiplier,
per_byte_fee,
base_fee,
}
};
info!("CalcFee::withParams -> {:#?}", calc);
calc
}

pub fn calc_fee(&self, weight: Weight, len: u32) -> String {
panic::set_hook();

let len_fee = self.per_byte_fee.saturating_mul(len.into());
let unadjusted_weight_fee = weight_to_fee(&weight, &self.polynomial);
let weight_fee = weight_to_fee(&weight, &self.polynomial);

let adjustable_fee = len_fee.saturating_add(unadjusted_weight_fee);
let adjustable_fee = len_fee.saturating_add(weight_fee);
let adjusted_fee = self.multiplier.calc(adjustable_fee);

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

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

result.to_string()
}
}

Expand Down
4 changes: 0 additions & 4 deletions calc-fee/src/panic.rs

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "",
"main": "index.js",
"scripts": {
"preinstall": "wasm-pack build --target nodejs --scope polkadot calc-fee",
"preinstall": "./calc-fee/build.sh",
"postinstall": "yarn upgrade @polkadot/calc-fee",
"build": "tsc",
"lint": "tsc && eslint . --ext ts",
Expand Down
3 changes: 3 additions & 0 deletions src/ApiHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ export default class ApiHandler {
]);

const { parentHash, number, stateRoot, extrinsicsRoot } = block.header;

api.registry.setMetadata(await api.rpc.state.getMetadata(parentHash));

const onInitialize = { events: [] as SanitizedEvent[] };
const onFinalize = { events: [] as SanitizedEvent[] };

Expand Down