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

feat: prevent mempool panic #6239

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
49 changes: 47 additions & 2 deletions base_layer/core/src/mempool/priority/prioritized_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ pub struct FeePriority(Vec<u8>);

impl FeePriority {
pub fn new(transaction: &Transaction, insert_epoch: u64, weight: u64) -> Result<Self, TransactionError> {
let fee_per_byte = transaction.body.get_total_fee()?.as_u64().saturating_mul(1000) / weight;
let fee_per_byte = transaction
.body
.get_total_fee()?
.as_u64()
.saturating_mul(1000)
.checked_div(weight)
.ok_or(TransactionError::ZeroWeight)?;
// Big-endian used here, the MSB is in the starting index. The ordering for Vec<u8> is taken from elements left
// to right and the unconfirmed pool expects the lowest priority to be sorted lowest to highest in the
// BTreeMap
Expand Down Expand Up @@ -95,7 +101,13 @@ impl PrioritizedTransaction {
Ok(Self {
key,
priority: FeePriority::new(&transaction, insert_epoch, weight)?,
fee_per_byte: transaction.body.get_total_fee()?.as_u64().saturating_mul(1000) / weight,
fee_per_byte: transaction
.body
.get_total_fee()?
.as_u64()
.saturating_mul(1000)
.checked_div(weight)
.ok_or(TransactionError::ZeroWeight)?,
weight,
transaction,
dependent_output_hashes: dependent_outputs.unwrap_or_default(),
Expand Down Expand Up @@ -162,4 +174,37 @@ mod tests {

assert!(p2 > p1);
}

#[test]
fn prioritized_from_empty_transaction() {
let weighting = TransactionWeight::latest();
match PrioritizedTransaction::new(
0,
&weighting,
Arc::new(Transaction::new(
vec![],
vec![],
vec![],
Default::default(),
Default::default(),
)),
None,
) {
Ok(_) => panic!("Empty transaction should not be valid"),
Err(e) => assert_eq!(e, TransactionError::ZeroWeight),
}
}

#[test]
fn fee_priority_with_zero_weight() {
let weight = 0;
match FeePriority::new(
&Transaction::new(vec![], vec![], vec![], Default::default(), Default::default()),
SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs(),
weight,
) {
Ok(_) => panic!("Empty transaction should not be valid"),
Err(e) => assert_eq!(e, TransactionError::ZeroWeight),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ pub enum TransactionError {
EncryptedDataError(String),
#[error("Ledger device error: {0}")]
LedgerDeviceError(#[from] LedgerDeviceError),
#[error("Transaction has a zero weight, not possible")]
ZeroWeight,
}

impl From<KeyManagerServiceError> for TransactionError {
Expand Down
7 changes: 7 additions & 0 deletions base_layer/core/src/transactions/weight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,11 @@ mod test {
);
}
}

#[test]
fn empty_body_weight() {
let weighting = TransactionWeight::latest();
let body = AggregateBody::empty();
assert_eq!(weighting.calculate_body(&body).unwrap(), 0);
}
}
Loading