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

Update fee calculation logic #50

Merged
merged 23 commits into from
Jun 8, 2020
Merged

Update fee calculation logic #50

merged 23 commits into from
Jun 8, 2020

Conversation

danforbes
Copy link
Contributor

Closes #45

Copy link
Member

@athei athei left a comment

Choose a reason for hiding this comment

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

I do not feel qualified to do a proper review here as I have minimal experience with javascript or even typescript. So take my comments with a grain of salt.

Someone also needs to check if all calculations are done saturating rather than overflowing. I would assume that the plain mul and add functions have overflowing semantics.

src/ApiHandler.ts Show resolved Hide resolved
src/ApiHandler.ts Outdated Show resolved Hide resolved
src/ApiHandler.ts Outdated Show resolved Hide resolved
src/ApiHandler.ts Outdated Show resolved Hide resolved
src/ApiHandler.ts Outdated Show resolved Hide resolved
src/ApiHandler.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@amaury1093 amaury1093 left a comment

Choose a reason for hiding this comment

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

Code lgtm overall, added questions/nits here and there

src/ApiHandler.ts Outdated Show resolved Hide resolved
src/ApiHandler.ts Outdated Show resolved Hide resolved
src/main.ts Outdated Show resolved Hide resolved
src/ApiHandler.ts Outdated Show resolved Hide resolved
src/ApiHandler.ts Outdated Show resolved Hide resolved
src/ApiHandler.ts Outdated Show resolved Hide resolved
src/ApiHandler.ts Outdated Show resolved Hide resolved
src/ApiHandler.ts Outdated Show resolved Hide resolved
@danforbes danforbes force-pushed the update-fees branch 5 times, most recently from ba3646d to 8b4a95d Compare May 21, 2020 18:42
src/ApiHandler.ts Outdated Show resolved Hide resolved
@danforbes danforbes force-pushed the update-fees branch 2 times, most recently from ed16438 to f238657 Compare May 22, 2020 13:59
src/ApiHandler.ts Outdated Show resolved Hide resolved
src/ApiHandler.ts Outdated Show resolved Hide resolved
src/ApiHandler.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@amaury1093 amaury1093 left a comment

Choose a reason for hiding this comment

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

lgtm 👍

I have some doubts on this line:

const targetedFeeAdjustment = (await api.query.transactionPayment.nextFeeMultiplier.at(parentHash)).div(perBill); // FIXME This might round the number

To convert multiplier (a Fixed128) into Perbill, we do .div(1e9). But this conversion might come with precision loss.

const extrinsicBaseWeight = api.consts.system.extrinsicBaseWeight;
// Here we assume that `weightToFee` Vec only has 1 element, which is of
// degree 1. FIXME Implement polynomial.
const coefficients = api.consts.transactionPayment.weightToFee[0];
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm getting Internal Error: TypeError: Cannot read property '0' of undefined here. Not latest binary, but latest block.

Copy link
Member

Choose a reason for hiding this comment

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

This field is a recent addition. You need to update your node.

Copy link
Contributor

Choose a reason for hiding this comment

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

@athei still having the same issue on stable Kusama 0.7.33 release, fully synced.

Copy link
Member

Choose a reason for hiding this comment

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

The change is not tagged yet. You need to build master.

Copy link
Member

@athei athei left a comment

Choose a reason for hiding this comment

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

This still has issues. Our naive javascript PerBill and Fixed128 implementations do not exactly match the rust implementation.

For that reason we will delegate the actual computation to a wasm blob that uses the real Rust types for arithmetic. sidecar just calls into that blob and supplies all data.

I take over that PR if that is ok @danforbes .

Also substrate is currently buggy and does not apply negative fee adjustments: paritytech/substrate#6145

@amaury1093
Copy link
Contributor

Is there a reason why we cannot do the calculations with BNs?

@athei
Copy link
Member

athei commented May 26, 2020

You can do that sure. But just look at the PerBill Rust implementation. Good look replicating that so that it works in all corner cases.

Doing that in wasm is just way easier.

@maciejhirsz
Copy link
Contributor

Riot is acting up and it seems I can't join channels. WASM is fine by me.

@amaury1093
Copy link
Contributor

amaury1093 commented May 26, 2020

@athei makes sense. Note that https://github.com/polkadot-js/wasm exists, the idea was to have one repo for wasm so that we don't replicate rust->wasm+js_glue_code tooling. If this is something not-sidecar-specific, it might be a good idea to put it there.

@athei
Copy link
Member

athei commented May 26, 2020

Riot is acting up and it seems I can't join channels. WASM is fine by me.

I cannot accept nor decline your DM invitation. Something is seriously wrong. Try restarting :D

@athei makes sense. Note that https://github.com/polkadot-js/wasm exists, the idea was to have one repo for wasm so that we don't replicate rust->wasm+js_glue_code tooling. If this is something not-sidecar-specific, it might be a good idea to put it there.

How would I include the wasm blob into the sidecar then? Via npm.js?

@amaury1093
Copy link
Contributor

Via npm.js?

Yes.

@athei
Copy link
Member

athei commented May 27, 2020

I added the code that uses a rust module to calculate the fees. It works for me and yields the correct result. I also rebased it onto the current master.

CI is currently failing because it does not build the rust package nor does it has the required toolchain installed. I will fix this soon.

Copy link
Member

@athei athei left a comment

Choose a reason for hiding this comment

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

CI is fixed. It builds the rust project now. Let me just approve my own changes :D.

We should still wait for more tests and approvals before merging this.

serde_derive = { version = "1", default_features = false }
serde = { version = "1", default_features = false }
console_error_panic_hook = { version = "0.1", optional = true }
sp-arithmetic = { git = "https://github.com/paritytech/substrate", default_features = false }
Copy link
Member

Choose a reason for hiding this comment

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

We should replace that by a released version as soon as that is possible. The current release on crates.io (2.0.0-rc2) has a broken Fixed128 impl that does not work with negative coefficients on unsigned Balances.

@joepetrowski joepetrowski merged commit f5b8427 into master Jun 8, 2020
@joepetrowski joepetrowski deleted the update-fees branch June 8, 2020 19:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update fees for refund and polynomial calculation
5 participants