Skip to content

Commit

Permalink
Merge pull request #1379 from oasisprotocol/ptrus/bugfix/priority
Browse files Browse the repository at this point in the history
runtime-sdk: set priority instead of increasing it
  • Loading branch information
ptrus committed May 30, 2023
2 parents 75e9e52 + 2661f2f commit 733f7ca
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 34 deletions.
6 changes: 3 additions & 3 deletions runtime-sdk/src/modules/accounts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -927,11 +927,11 @@ impl module::TransactionHandler for Module {
// TODO: Emit event that fee has been paid.

let gas_price = tx.auth_info.fee.gas_price();
// Bump transaction priority.
<C::Runtime as Runtime>::Core::add_priority(
// Set transaction priority.
<C::Runtime as Runtime>::Core::set_priority(
ctx,
gas_price.try_into().unwrap_or(u64::MAX),
)?;
);
}

// Do not update nonces early during transaction checks. In case of checks, only do it after
Expand Down
13 changes: 4 additions & 9 deletions runtime-sdk/src/modules/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ pub trait API {
/// Configured minimum gas price.
fn min_gas_price<C: Context>(ctx: &mut C, denom: &token::Denomination) -> u128;

/// Increase transaction priority for the provided amount.
fn add_priority<C: Context>(ctx: &mut C, priority: u64) -> Result<(), Error>;
/// Sets the transaction priority to the provided amount.
fn set_priority<C: Context>(ctx: &mut C, priority: u64);

/// Takes and returns the stored transaction priority.
fn take_priority<C: Context>(ctx: &mut C) -> u64;
Expand Down Expand Up @@ -451,13 +451,8 @@ impl<Cfg: Config> API for Module<Cfg> {
.unwrap_or_default()
}

fn add_priority<C: Context>(ctx: &mut C, priority: u64) -> Result<(), Error> {
let p = ctx.value::<u64>(CONTEXT_KEY_PRIORITY).or_default();
let added_p = p.checked_add(priority).unwrap_or(u64::MAX);

ctx.value::<u64>(CONTEXT_KEY_PRIORITY).set(added_p);

Ok(())
fn set_priority<C: Context>(ctx: &mut C, priority: u64) {
ctx.value::<u64>(CONTEXT_KEY_PRIORITY).set(priority);
}

fn take_priority<C: Context>(ctx: &mut C) -> u64 {
Expand Down
28 changes: 6 additions & 22 deletions runtime-sdk/src/modules/core/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ fn test_approve_unverified_tx() {
}

#[test]
fn test_add_priority() {
fn test_set_priority() {
let mut mock = mock::Mock::default();
let mut ctx = mock.create_ctx();

Expand All @@ -844,34 +844,18 @@ fn test_add_priority() {
"default priority should be 0"
);

Core::add_priority(&mut ctx, 1).expect("adding priority should succeed");
Core::add_priority(&mut ctx, 11).expect("adding priority should succeed");
Core::set_priority(&mut ctx, 1);
Core::set_priority(&mut ctx, 11);

let tx = mock::transaction();
ctx.with_tx(0, 0, tx, |mut tx_ctx, _call| {
Core::add_priority(&mut tx_ctx, 10)
.expect("adding priority from tx context should succeed");
Core::set_priority(&mut tx_ctx, 10);
});

assert_eq!(
22,
10,
Core::take_priority(&mut ctx),
"adding priority should work"
);
}

#[test]
fn test_add_priority_overflow() {
let mut mock = mock::Mock::default();
let mut ctx = mock.create_ctx();

Core::add_priority(&mut ctx, u64::MAX).expect("adding priority should succeed");
Core::add_priority(&mut ctx, u64::MAX).expect("adding priority should succeed");

assert_eq!(
u64::MAX,
Core::take_priority(&mut ctx),
"adding priority should work"
"setting priority should work"
);
}

Expand Down

0 comments on commit 733f7ca

Please sign in to comment.