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

Prioritize Substrate txns by tip-per-effective-gas #1957

Merged
merged 30 commits into from Jan 31, 2023

Conversation

notlesh
Copy link
Contributor

@notlesh notlesh commented Nov 16, 2022

What does it do?

This modifies our substrate txn priority-override to compute a "tip per effective gas" metric. This should make the prioritization mechanism comparable to what is currently being done in Frontier (which is solely based on the priority fee paid after considering base fee).

Frontier's calculation currently takes a transaction's maxFeePerGas and maxPriorityFeePerGas as well as the current block's baseFee into account, so it basically calculates the effective tip per-gas at any point in time.

There are probably some issues related to the txpool prioritization when it comes to txn size which are not addressed by this PR. They are discussed in the comments but I'm considering them out-of-scope and want to keep this PR minimal.

@notlesh notlesh added B5-clientnoteworthy Changes should be mentioned in any downstream projects' release notes D9-needsaudit👮 PR contains changes to fund-managing logic that should be properly reviewed and externally audited labels Nov 16, 2022
@notlesh notlesh added the B7-runtimenoteworthy Changes should be noted in any runtime-upgrade release notes label Nov 16, 2022
@notlesh notlesh added the not-breaking Does not need to be mentioned in breaking changes label Jan 6, 2023
from: charleth.address,
privateKey: CHARLETH_PRIVATE_KEY,
maxFeePerGas: HIGH_MAX_FEE_PER_GAS,
maxPriorityFeePerGas: "0x" + TIP_PER_GAS_0.toString(16),
Copy link
Contributor

@nbaztec nbaztec Jan 30, 2023

Choose a reason for hiding this comment

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

nToHex(TIP_PER_GAS_0) exists in polkadotjs for this operation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, thanks, this is definitely the cumbersome way to do it.

I was bothered enough by it that I made createTransaction optionally take a bigint here: #2066 -- I should take advantage of that in this PR :)

});

expect(transferExts.length).to.eq(4);
expect(transferExts[0].method.section).to.eq("balances");
Copy link
Contributor

Choose a reason for hiding this comment

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

We should also assert that the correct TX type was ordered in accordance with the Tip and not just the type (since there are 2 of each we can't be sure that the appropriate tx was picked first)

Co-authored-by: Nisheeth Barthwal <nbaztec@gmail.com>
Co-authored-by: Nisheeth Barthwal <nbaztec@gmail.com>
notlesh and others added 2 commits January 30, 2023 08:02
Co-authored-by: Nisheeth Barthwal <nbaztec@gmail.com>
Co-authored-by: Nisheeth Barthwal <nbaztec@gmail.com>
notlesh and others added 2 commits January 30, 2023 08:05
Co-authored-by: Nisheeth Barthwal <nbaztec@gmail.com>
Co-authored-by: Nisheeth Barthwal <nbaztec@gmail.com>
Comment on lines +553 to +560
[...topDelegators].map((account, i) => {
// add a tip such that the delegation ordering will be preserved, e.g. the first txns sent
// will have the highest tip
let tip = BigInt(topDelegators.length - i + 1) * MILLIGLMR;
return context.polkadotApi.tx.parachainStaking
.delegate(alith.address, MIN_GLMR_DELEGATOR + 1n * GLMR, i + 1, 1)
.signAsync(account, { tip });
})
Copy link
Contributor

@nbaztec nbaztec Jan 30, 2023

Choose a reason for hiding this comment

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

This can be simplified byusing tip-- as be build the tx:

let tip = 1 * GLMR;
await expectOk(
      context.createBlock(
        [...topDelegators].map((account, i) => {
          // add a tip such that the delegation ordering will be preserved, e.g. the first txns sent
          // will have the highest tip
          return context.polkadotApi.tx.parachainStaking
            .delegate(alith.address, MIN_GLMR_DELEGATOR + 1n * GLMR, i + 1, 1)
            .signAsync(account, { tip: tip-- });
        })
      )
    );

[...bottomDelegators].map((account, i) => {
// add a tip such that the delegation ordering will be preserved, e.g. the first txns sent
// will have the highest tip
let tip = BigInt(topDelegators.length + (bottomDelegators.length - i) + 1) * MILLIGLMR;
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above

// lossy, we have to handle the case where a very low weight maps to zero gas.
let effective_gas_price = if effective_gas > 0 {
fee / effective_gas
let tip_per_gas = if effective_gas > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

We should also add an associated rust test for this validation function (if possible)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added some tests, but they're not in good shape. The call to Executive::validate_transaction early in the fn fails on an unsigned UncheckedExtrinsic except for an Ethereum one, and signing one is proving difficult.

I'll revisit this again with a fresh mind, but we may also want to consider creating a task to do these tests later.

In any case, I agree that they're a good idea...

@notlesh notlesh merged commit 3585380 into master Jan 31, 2023
@notlesh notlesh deleted the notlesh-fix-txpool-fairness branch January 31, 2023 17:15
@notlesh notlesh added D1-audited👍 PR contains changes to fund-managing logic that has been properly reviewed and externally audited and removed D9-needsaudit👮 PR contains changes to fund-managing logic that should be properly reviewed and externally audited labels Feb 22, 2023
imstar15 pushed a commit to OAK-Foundation/moonbeam that referenced this pull request May 16, 2023
…on#1957)

* Prioritize Substrate txns by tip-per-effective-gas

* s/<spaces>/<tabs>/

* fmt

* Remove unused import

* Remove useless calls to setBlocksPerRound

* wtf

* Replace createBlock([]) with one block per extrinsic

* A more performant workaround for txn ordering

* prettier

* Include some basic tests

* Fix test

* Add txn replacement tests

* prettier

* Don't assume nonce is 0

* Update tests/tests/test-txpool/test-txpool-fairness.ts

Co-authored-by: Nisheeth Barthwal <nbaztec@gmail.com>

* Update tests/tests/test-txpool/test-txpool-fairness.ts

Co-authored-by: Nisheeth Barthwal <nbaztec@gmail.com>

* Update tests/tests/test-txpool/test-txpool-fairness.ts

Co-authored-by: Nisheeth Barthwal <nbaztec@gmail.com>

* Update tests/tests/test-txpool/test-txpool-fairness.ts

Co-authored-by: Nisheeth Barthwal <nbaztec@gmail.com>

* Update tests/tests/test-staking/test-staking-locks.ts

Co-authored-by: Nisheeth Barthwal <nbaztec@gmail.com>

* Update tests/tests/test-staking/test-staking-locks.ts

Co-authored-by: Nisheeth Barthwal <nbaztec@gmail.com>

* Add some (failing) tests around validate_transaction

* Remove failing tests

* Remove unused imports

* Attempt to break up extremely long import line

* Another attempt

* Respect my authoritah

---------

Co-authored-by: Nisheeth Barthwal <nbaztec@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
B5-clientnoteworthy Changes should be mentioned in any downstream projects' release notes B7-runtimenoteworthy Changes should be noted in any runtime-upgrade release notes D1-audited👍 PR contains changes to fund-managing logic that has been properly reviewed and externally audited not-breaking Does not need to be mentioned in breaking changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants