refactor cost calculation (#21062)
* - cache calculated transaction cost to allow sharing; - atomic cost tracking op; - only lock accounts for transactions eligible for current block; - moved qos service and stats reporting to its own model; - add cost_weight default to neutral (as 1), vote has zero weight; Co-authored-by: Tyera Eulberg <teulberg@gmail.com> * Update core/src/qos_service.rs Co-authored-by: Tyera Eulberg <teulberg@gmail.com> * Update core/src/qos_service.rs Co-authored-by: Tyera Eulberg <teulberg@gmail.com> Co-authored-by: Tyera Eulberg <teulberg@gmail.com>
This commit is contained in:
@@ -72,7 +72,7 @@ impl CostTracker {
|
||||
_transaction: &SanitizedTransaction,
|
||||
tx_cost: &TransactionCost,
|
||||
) -> Result<u64, CostTrackerError> {
|
||||
let cost = tx_cost.sum();
|
||||
let cost = tx_cost.sum() * tx_cost.cost_weight as u64;
|
||||
self.would_fit(&tx_cost.writable_accounts, &cost)?;
|
||||
self.add_transaction(&tx_cost.writable_accounts, &cost);
|
||||
Ok(self.block_cost)
|
||||
@@ -369,4 +369,26 @@ mod tests {
|
||||
assert_eq!(acct2, costliest_account);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_try_add_with_cost_weight() {
|
||||
let (mint_keypair, start_hash) = test_setup();
|
||||
let (tx, _keys, _cost) = build_simple_transaction(&mint_keypair, &start_hash);
|
||||
let tx = SanitizedTransaction::from_transaction_for_tests(tx);
|
||||
|
||||
let limit = 100u64;
|
||||
let mut testee = CostTracker::new(limit, limit);
|
||||
|
||||
let mut cost = TransactionCost {
|
||||
execution_cost: limit + 1,
|
||||
..TransactionCost::default()
|
||||
};
|
||||
|
||||
// cost exceed limit by 1, will not fit
|
||||
assert!(testee.try_add(&tx, &cost).is_err());
|
||||
|
||||
cost.cost_weight = 0u32;
|
||||
// setting cost_weight to zero will allow this tx
|
||||
assert!(testee.try_add(&tx, &cost).is_ok());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user