diff --git a/Cargo.lock b/Cargo.lock index c585ecdadd..29e9084e12 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8644,10 +8644,6 @@ dependencies = [ [[package]] name = "mempool" version = "0.1.0" -dependencies = [ - "futures", - "tokio", -] [[package]] name = "merlin" @@ -12077,7 +12073,6 @@ dependencies = [ "logging", "logos-blockchain-binary-codec", "logos-blockchain-core", - "mempool", "mockall", "num-traits", "ping_core", @@ -12145,7 +12140,6 @@ dependencies = [ "libp2p 0.57.1", "logos-blockchain-core", "logos-blockchain-key-management-system-service", - "mempool", "sequencer_channel_signing_actor", "sequencer_core", "sequencer_slasher_actor", diff --git a/lez/mempool/Cargo.toml b/lez/mempool/Cargo.toml index 47550fc93b..7f67fbea9f 100644 --- a/lez/mempool/Cargo.toml +++ b/lez/mempool/Cargo.toml @@ -7,9 +7,3 @@ license = { workspace = true } [lints] workspace = true -[dependencies] -tokio = { workspace = true, features = ["sync"] } - -[dev-dependencies] -tokio = { workspace = true, features = ["rt-multi-thread", "macros"] } -futures.workspace = true diff --git a/lez/mempool/src/lib.rs b/lez/mempool/src/lib.rs index b76813c1e3..e561a9a470 100644 --- a/lez/mempool/src/lib.rs +++ b/lez/mempool/src/lib.rs @@ -1,130 +1,405 @@ -use tokio::sync::mpsc::{Receiver, Sender}; +use std::{ + cmp::Reverse, + collections::{BTreeMap, BinaryHeap, HashMap, HashSet}, + hash::Hash, + ops::Bound, + time::{Duration, Instant}, +}; + +/// Items one lane may hold, so a single signer cannot fill the pool. +pub const MAX_LANE_LENGTH: usize = 64; + +/// Why [`MemPool::insert`] turned an item away. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Rejected { + /// The pool holds `max_size` items and none of them can be evicted. + Full, + /// One of its lanes already holds [`MAX_LANE_LENGTH`] items. + LaneFull, + /// It does not outbid the item it would replace, or the one it would evict from a full pool. + Underpriced, + /// Its slots would order it both before and after another item, so neither could ever pop. + Cycle, +} + +/// A held item and the lane slots it takes. +struct Entry { + item: T, + slots: Vec<(G, N)>, + /// Whether `ready` currently holds it, so it is never enqueued twice. + queued: bool, + /// Given back and not yet unparked: it holds its slots but does not pop. + parked: bool, + born: Instant, +} -pub struct MemPool { - receiver: Receiver, - handle: MemPoolHandle, - front_buffer: Vec, +/// A bounded pool that pops the highest `priority` first, without ever +/// reordering a lane. +/// +/// - `priority`: the bid of an item; higher pops first, ties go to the earlier arrival. +/// - `slots_of`: the lane slots an item takes (one per signer, at its `nonce`). A lane pops in slot +/// order: an item is ready only once it holds the first slot of every lane it is in. An item in +/// no lane is always ready. +/// - `outbids`: whether a bid may replace the one holding a slot it takes, or evict the lowest lane +/// tail from a full pool. +/// +/// This is Kahn's algorithm with a priority heap, kept across pops: +/// - items are the nodes of a DAG +/// - lanes are its edges. +/// +/// A pop costs O(k·(k + log n)), `k` being the lanes of the popped item and `n` the ready items. +pub struct MemPool { + max_size: usize, + priority: fn(&T) -> K, + slots_of: fn(&T) -> Vec<(G, N)>, + outbids: fn(&K, &K) -> bool, + /// Arrival numbers: counting up from 0 for inserted items, down from -1 for given back ones. + next_arrival: i64, + next_given_back: i64, + items: HashMap>, + /// Per lane, arrivals by slot; only the first is eligible. + lanes: HashMap>, + /// Ready items: given back ones first, then the highest bid, earlier arrival breaks ties. + /// + /// An entry goes stale when an item takes a slot ahead of it, or is removed; [`Self::pop`] + /// skips those. + ready: BinaryHeap<(bool, K, Reverse)>, + /// Given back items waiting for [`Self::unpark`]. + parked: Vec, } impl MemPool { + /// A pool with no priorities nor lanes, simple arrival order (FIFO). #[must_use] - pub fn new(max_size: usize) -> Self { - let (sender, receiver) = tokio::sync::mpsc::channel(max_size); - let handle = MemPoolHandle::new(sender); - - Self { - receiver, - handle, - front_buffer: Vec::new(), - } + pub fn new_fifo(max_size: usize) -> Self { + Self::new(max_size, |_| (), |_| Vec::new(), |(), ()| false) } +} +impl MemPool { #[must_use] - pub const fn handle(&self) -> &MemPoolHandle { - &self.handle + pub fn new( + max_size: usize, + priority: fn(&T) -> K, + slots_of: fn(&T) -> Vec<(G, N)>, + outbids: fn(&K, &K) -> bool, + ) -> Self { + Self { + max_size, + priority, + slots_of, + outbids, + next_arrival: 0, + next_given_back: -1, + items: HashMap::new(), + lanes: HashMap::new(), + ready: BinaryHeap::new(), + parked: Vec::new(), + } } - /// Returns the total number of items in the mempool, including both the front buffer and the - /// channel. + /// Returns the number of items the pool holds, parked ones included. #[must_use] pub fn len(&self) -> usize { - self.front_buffer.len().saturating_add(self.receiver.len()) + self.items.len() } /// Returns true if the mempool is empty, false otherwise. #[must_use] pub fn is_empty(&self) -> bool { - self.front_buffer.is_empty() && self.receiver.is_empty() + self.items.is_empty() } - /// Pop an item from the mempool first checking the front buffer (LIFO) then the channel (FIFO). - pub fn pop(&mut self) -> Option { - use tokio::sync::mpsc::error::TryRecvError; + /// Adds an item at its slots, replacing whatever holds them if it outbids all of those. Into + /// a full pool, it takes the place of the lowest lane tail it outbids. + pub fn insert(&mut self, item: T) -> Result<(), Rejected> { + let slots = (self.slots_of)(&item); + let mut displaced = self.holders(&slots); + if slots.iter().any(|(lane, at)| { + self.lanes + .get(lane) + .is_some_and(|queue| queue.len() >= MAX_LANE_LENGTH && !queue.contains_key(at)) + }) { + return Err(Rejected::LaneFull); + } + if displaced.is_empty() && self.len() >= self.max_size { + displaced.push(self.eviction_candidate(&slots).ok_or(Rejected::Full)?); + } + let bid = (self.priority)(&item); + if !displaced.iter().all(|arrival| { + self.items + .get(arrival) + .is_some_and(|held| (self.outbids)(&bid, &(self.priority)(&held.item))) + }) { + return Err(Rejected::Underpriced); + } + if self.closes_cycle(&slots, &displaced) { + return Err(Rejected::Cycle); + } + for arrival in displaced { + self.remove(arrival); + } + let arrival = self.next_arrival; + self.next_arrival = arrival.saturating_add(1); + self.add(arrival, item, slots, false); + Ok(()) + } - // First check if there are any items in the front buffer (LIFO) - if let Some(item) = self.front_buffer.pop() { - return Some(item); + /// Gives an item back past the size bound, replacing whatever holds its slots. It holds them + /// but does not pop until [`Self::unpark`], then pops before anything inserted. Dropped if it + /// would close a cycle. + /// + /// Given back items pop among themselves by priority, ties last in first out, so hand a batch + /// back in reverse. + pub fn give_back(&mut self, item: T) { + let slots = (self.slots_of)(&item); + let holders = self.holders(&slots); + if self.closes_cycle(&slots, &holders) { + return; + } + for arrival in holders { + self.remove(arrival); } + let arrival = self.next_given_back; + self.next_given_back = arrival.saturating_sub(1); + self.add(arrival, item, slots, true); + self.parked.push(arrival); + } + + /// Lets everything given back pop again. + pub fn unpark(&mut self) { + for arrival in std::mem::take(&mut self.parked) { + if let Some(entry) = self.items.get_mut(&arrival) { + entry.parked = false; + } + self.enqueue_if_ready(arrival); + } + } + + /// Drops every item held for `ttl` or longer, given back ones counting from when they were + /// given back. O(n) per call. + pub fn expire(&mut self, ttl: Duration) { + let expired: Vec = self + .items + .iter() + .filter(|(_, entry)| entry.born.elapsed() >= ttl) + .map(|(&arrival, _)| arrival) + .collect(); + for arrival in expired { + self.remove(arrival); + } + } - // Otherwise, try to receive from the channel (FIFO) + /// Pops the ready item that goes first. + pub fn pop(&mut self) -> Option { + self.pop_unless(|_| false) + } - match self.receiver.try_recv() { - Ok(item) => Some(item), - Err(TryRecvError::Empty) => None, - Err(TryRecvError::Disconnected) => { - panic!("Mempool senders disconnected, cannot receive items, this is a bug") + /// Pops the ready item that goes first, parking in place, as if given back, those `wait` + /// holds back. Unlike a pop and a give back, a parked item keeps its age. + pub fn pop_unless(&mut self, mut wait: impl FnMut(&T) -> bool) -> Option { + loop { + let (_, _, Reverse(arrival)) = self.ready.pop()?; + let ready = self.is_ready(arrival); + let Some(entry) = self.items.get_mut(&arrival) else { + continue; + }; + entry.queued = false; + if !ready { + continue; } + if wait(&entry.item) { + entry.parked = true; + self.parked.push(arrival); + continue; + } + return self.remove(arrival); } } - /// Push an item to the front of the mempool (will be popped first). - pub fn push_front(&mut self, item: T) { - self.front_buffer.push(item); + fn add(&mut self, arrival: i64, item: T, slots: Vec<(G, N)>, parked: bool) { + for (lane, at) in &slots { + self.lanes + .entry(lane.clone()) + .or_default() + .insert(at.clone(), arrival); + } + let entry = Entry { + item, + slots, + queued: false, + parked, + born: Instant::now(), + }; + self.items.insert(arrival, entry); + self.enqueue_if_ready(arrival); } -} -pub struct MemPoolHandle { - sender: Sender, -} + fn remove(&mut self, arrival: i64) -> Option { + let entry = self.items.remove(&arrival)?; + for (lane, at) in &entry.slots { + if let Some(queue) = self.lanes.get_mut(lane) { + queue.remove(at); + if queue.is_empty() { + self.lanes.remove(lane); + } + } + } -impl Clone for MemPoolHandle { - fn clone(&self) -> Self { - Self { - sender: self.sender.clone(), + // whatever now heads those lanes may have become ready + for (lane, _) in &entry.slots { + if let Some(head) = self.head(lane) { + self.enqueue_if_ready(head); + } } + + Some(entry.item) + } + + fn head(&self, lane: &G) -> Option { + self.lanes + .get(lane) + .and_then(BTreeMap::first_key_value) + .map(|(_, &arrival)| arrival) + } + + /// The items holding any of `slots`, each once. + fn holders(&self, slots: &[(G, N)]) -> Vec { + let mut holders: Vec = slots + .iter() + .filter_map(|(lane, at)| self.lanes.get(lane)?.get(at).copied()) + .collect(); + holders.sort_unstable(); + holders.dedup(); + holders } -} -impl MemPoolHandle { - const fn new(sender: Sender) -> Self { - Self { sender } + /// The lowest bid among the inserted, unparked lane tails, latest arrival on ties: removing + /// it strands nothing. One ahead of `slots` in a lane they share is spared, as evicting it + /// would only leave a gap before them. O(n) per call. + fn eviction_candidate(&self, slots: &[(G, N)]) -> Option { + self.items + .iter() + .filter(|&(&arrival, entry)| { + arrival >= 0 + && !entry.parked + && !is_ahead(&entry.slots, slots) + && entry.slots.iter().all(|(lane, at)| { + self.lanes + .get(lane) + .and_then(BTreeMap::last_key_value) + .is_some_and(|(last, _)| last == at) + }) + }) + .min_by_key(|&(&arrival, entry)| ((self.priority)(&entry.item), Reverse(arrival))) + .map(|(&arrival, _)| arrival) } - /// Send an item to the mempool blocking if max size is reached. - pub async fn push(&self, item: T) -> Result<(), tokio::sync::mpsc::error::SendError> { - self.sender.send(item).await + /// Whether an item at `slots`, once `replaced` are gone, would come after something that + /// comes after it. + /// + /// Walks forward from the items behind it, looking for one ahead of it. An item in one lane + /// cannot close a cycle, so only one in several lanes pays for the walk. + fn closes_cycle(&self, slots: &[(G, N)], replaced: &[i64]) -> bool { + if slots.len() < 2 { + return false; + } + let next = |lane: &G, at: &N| { + self.lanes + .get(lane)? + .range((Bound::Excluded(at), Bound::Unbounded)) + .map(|(_, &arrival)| arrival) + .find(|arrival| !replaced.contains(arrival)) + }; + let mut behind: Vec = slots + .iter() + .filter_map(|(lane, at)| next(lane, at)) + .collect(); + let mut seen = HashSet::new(); + while let Some(arrival) = behind.pop() { + if !seen.insert(arrival) { + continue; + } + let Some(entry) = self.items.get(&arrival) else { + continue; + }; + if is_ahead(&entry.slots, slots) { + return true; + } + behind.extend(entry.slots.iter().filter_map(|(lane, at)| next(lane, at))); + } + false } - /// Send an item to the mempool, failing _immediately_ if it is full. - pub fn try_push(&self, item: T) -> Result<(), tokio::sync::mpsc::error::TrySendError> { - self.sender.try_send(item) + /// Whether the item is not parked and heads every lane it is in. + fn is_ready(&self, arrival: i64) -> bool { + self.items.get(&arrival).is_some_and(|entry| { + !entry.parked + && entry + .slots + .iter() + .all(|(lane, _)| self.head(lane) == Some(arrival)) + }) + } + + fn enqueue_if_ready(&mut self, arrival: i64) { + if !self.is_ready(arrival) { + return; + } + if let Some(entry) = self.items.get_mut(&arrival) + && !std::mem::replace(&mut entry.queued, true) + { + self.ready + .push((arrival < 0, (self.priority)(&entry.item), Reverse(arrival))); + } } } +/// Whether `slots` come before `others` in a lane they share. +fn is_ahead(slots: &[(G, N)], others: &[(G, N)]) -> bool { + slots.iter().any(|(lane, at)| { + others + .iter() + .any(|(other_lane, other_at)| lane == other_lane && at < other_at) + }) +} + #[cfg(test)] mod tests { - use futures::FutureExt as _; - use tokio::test; - use super::*; - #[test] - async fn mempool_new() { - let mut pool = MemPool::::new(10); - assert_eq!(pool.pop(), None); - assert_eq!(pool.len(), 0); - } + /// An item as `(priority, slots, tag)`. + type Laned = (u32, Vec<(char, u32)>, &'static str); - #[test] - async fn push_and_pop() { - let mut pool = MemPool::::new(10); + fn laned_pool(max_size: usize) -> MemPool { + MemPool::new( + max_size, + |&(priority, _, _)| priority, + |(_, slots, _)| slots.clone(), + |new, old| new > old, + ) + } - pool.handle().push(1).await.unwrap(); - assert_eq!(pool.len(), 1); + fn drain(pool: &mut MemPool) -> Vec<&'static str> { + std::iter::from_fn(|| pool.pop()) + .map(|(_, _, tag)| tag) + .collect() + } - let item = pool.pop(); - assert_eq!(item, Some(1)); + #[test] + fn mempool_new() { + let mut pool = MemPool::::new_fifo(10); assert_eq!(pool.pop(), None); assert_eq!(pool.len(), 0); } #[test] - async fn multiple_push_pop() { - let mut pool = MemPool::::new(10); + fn insert_and_pop_in_arrival_order() { + let mut pool = MemPool::new_fifo(10); - pool.handle().push(1).await.unwrap(); - pool.handle().push(2).await.unwrap(); - pool.handle().push(3).await.unwrap(); + pool.insert(1).unwrap(); + pool.insert(2).unwrap(); + pool.insert(3).unwrap(); assert_eq!(pool.len(), 3); assert_eq!(pool.pop(), Some(1)); @@ -135,49 +410,261 @@ mod tests { } #[test] - async fn max_size() { - let pool = MemPool::::new(2); + fn pops_highest_first_then_arrival_order() { + let mut pool = laned_pool(10); - pool.handle().push(1).await.unwrap(); - pool.handle().push(2).await.unwrap(); + // every item in its own lane + pool.insert((1, vec![('b', 0)], "b")).unwrap(); + pool.insert((5, vec![('c', 0)], "c")).unwrap(); + pool.insert((1, vec![('d', 0)], "d")).unwrap(); - // This should block if buffer is full - assert_eq!(pool.handle().push(3).now_or_never(), None); + assert_eq!(drain(&mut pool), vec!["c", "b", "d"]); } #[test] - async fn try_push_fails_when_full_without_blocking() { - let mut pool = MemPool::::new(1); + fn never_reorders_a_lane_and_waits_on_every_lane() { + let mut pool = laned_pool(10); + + // `p` is in no lane; alice's tipped `a1` waits behind `a0`; the + // sponsored `s` is in both lanes and waits behind `a0` even though + // bob's lane is free; `b1` waits behind `s`. + pool.insert((0, vec![], "p")).unwrap(); + pool.insert((0, vec![('A', 0)], "a0")).unwrap(); + pool.insert((5, vec![('A', 1)], "a1")).unwrap(); + pool.insert((9, vec![('A', 2), ('B', 0)], "s")).unwrap(); + pool.insert((3, vec![('B', 1)], "b1")).unwrap(); + + assert_eq!(drain(&mut pool), vec!["p", "a0", "a1", "s", "b1"]); + } - pool.handle().try_push(1).unwrap(); - assert!( - pool.handle().try_push(2).is_err(), - "full mempool must not accept" - ); + #[test] + fn orders_a_lane_by_slot_not_arrival() { + let mut pool = laned_pool(10); + + pool.insert((9, vec![('A', 1)], "a1")).unwrap(); + pool.insert((0, vec![('A', 0)], "a0")).unwrap(); + + assert_eq!(drain(&mut pool), vec!["a0", "a1"]); + } + + #[test] + fn enqueues_an_item_once_however_many_lanes_report_it() { + let mut pool = laned_pool(10); + + // `y` becomes the head of both lanes at once when `x` is picked + pool.insert((1, vec![('A', 0), ('B', 0)], "x")).unwrap(); + pool.insert((2, vec![('A', 1), ('B', 1)], "y")).unwrap(); + pool.insert((3, vec![('A', 2)], "z")).unwrap(); + + assert_eq!(drain(&mut pool), vec!["x", "y", "z"]); + } + + #[test] + fn orders_what_arrives_between_pops() { + let mut pool = laned_pool(10); + + pool.insert((1, vec![], "low")).unwrap(); + pool.insert((2, vec![], "mid")).unwrap(); + assert_eq!(pool.pop().unwrap().2, "mid"); + + pool.insert((9, vec![], "late")).unwrap(); + assert_eq!(drain(&mut pool), vec!["late", "low"]); + } + + #[test] + fn insert_fails_when_full() { + let mut pool = MemPool::new_fifo(1); + + pool.insert(1).unwrap(); + // a FIFO bid never outbids, so nothing is evicted + assert_eq!(pool.insert(2), Err(Rejected::Underpriced)); // Popping frees capacity again. assert_eq!(pool.pop(), Some(1)); - pool.handle().try_push(2).unwrap(); + pool.insert(2).unwrap(); assert_eq!(pool.pop(), Some(2)); } #[test] - async fn push_front() { - let mut pool = MemPool::::new(10); + fn replaces_a_slot_only_when_outbid_even_when_full() { + let mut pool = laned_pool(1); + + pool.insert((1, vec![('A', 0)], "old")).unwrap(); + assert_eq!( + pool.insert((1, vec![('A', 0)], "same")), + Err(Rejected::Underpriced) + ); + assert_eq!( + pool.insert((9, vec![('A', 1)], "next")), + Err(Rejected::Full) + ); + pool.insert((2, vec![('A', 0)], "new")).unwrap(); + + assert_eq!(pool.len(), 1); + assert_eq!(drain(&mut pool), vec!["new"]); + } + + #[test] + fn a_full_pool_evicts_the_lowest_lane_tail_it_is_outbid_by() { + let mut pool = laned_pool(3); + + // `a0` is no tail, so `b0` is the lowest one + pool.insert((1, vec![('A', 0)], "a0")).unwrap(); + pool.insert((5, vec![('A', 1)], "a1")).unwrap(); + pool.insert((2, vec![('B', 0)], "b0")).unwrap(); + pool.insert((3, vec![('C', 0)], "c")).unwrap(); + // now `c` is the lowest tail, and it is not outbid + assert_eq!( + pool.insert((0, vec![('D', 0)], "d")), + Err(Rejected::Underpriced) + ); + + assert_eq!(drain(&mut pool), vec!["c", "a0", "a1"]); + } + + #[test] + fn a_lane_holds_at_most_its_cap() { + let mut pool = laned_pool(MAX_LANE_LENGTH + 1); + + for at in 0..MAX_LANE_LENGTH { + let at = u32::try_from(at).unwrap(); + pool.insert((1, vec![('A', at)], "a")).unwrap(); + } + let next = u32::try_from(MAX_LANE_LENGTH).unwrap(); + assert_eq!( + pool.insert((1, vec![('A', next)], "over")), + Err(Rejected::LaneFull) + ); + // replacing does not lengthen the lane + pool.insert((2, vec![('A', 3)], "replacement")).unwrap(); + assert_eq!(pool.len(), MAX_LANE_LENGTH); + } + + #[test] + fn expires_what_was_held_for_the_ttl() { + let mut pool = laned_pool(10); + + pool.insert((1, vec![('A', 0)], "a0")).unwrap(); + pool.give_back((1, vec![('B', 0)], "b0")); + pool.expire(Duration::from_secs(60)); + assert_eq!(pool.len(), 2); + + pool.expire(Duration::ZERO); + assert!(pool.is_empty()); + assert!(pool.lanes.is_empty()); + pool.unpark(); + assert_eq!(pool.pop(), None); + } + + #[test] + fn an_item_held_back_at_pop_parks_in_place_holding_its_lanes() { + let mut pool = laned_pool(10); + + pool.insert((5, vec![('A', 0)], "wait")).unwrap(); + pool.insert((1, vec![('B', 0)], "go")).unwrap(); + pool.insert((0, vec![('A', 1)], "behind")).unwrap(); + + assert_eq!( + pool.pop_unless(|&(_, _, tag)| tag == "wait") + .map(|(_, _, tag)| tag), + Some("go") + ); + assert_eq!(pool.pop(), None); + assert_eq!(pool.len(), 2); + + pool.unpark(); + assert_eq!(drain(&mut pool), vec!["wait", "behind"]); + } + + #[test] + fn rejects_an_item_that_would_close_a_cycle() { + let mut pool = laned_pool(10); + + pool.insert((0, vec![('A', 2), ('B', 1)], "y")).unwrap(); + // ahead of `y` in `A`, behind it in `B` + assert_eq!( + pool.insert((0, vec![('A', 1), ('B', 2)], "x")), + Err(Rejected::Cycle) + ); + // through a third item: behind `y` in `A`, ahead of `z` in `C`, which is ahead of `y` + pool.insert((0, vec![('B', 0), ('C', 1)], "z")).unwrap(); + assert_eq!( + pool.insert((0, vec![('A', 3), ('C', 0)], "w")), + Err(Rejected::Cycle) + ); + pool.insert((0, vec![('A', 3), ('C', 2)], "v")).unwrap(); + + assert_eq!(drain(&mut pool), vec!["z", "y", "v"]); + } + + #[test] + fn a_given_back_item_replaces_what_holds_its_slots() { + let mut pool = laned_pool(10); - pool.handle().push(1).await.unwrap(); - pool.handle().push(2).await.unwrap(); + pool.insert((9, vec![('A', 0)], "resubmitted")).unwrap(); + pool.give_back((1, vec![('A', 0)], "orphan")); + pool.unpark(); - // Push items to the front - these should be popped first - pool.push_front(10); - pool.push_front(20); + assert_eq!(drain(&mut pool), vec!["orphan"]); + } - // Items pushed to front are popped in LIFO order + #[test] + fn given_back_items_wait_for_unpark_then_pop_first_in_reverse() { + let mut pool = MemPool::new_fifo(2); + + pool.insert(1).unwrap(); + pool.insert(2).unwrap(); + // past the bound + pool.give_back(10); + pool.give_back(20); + assert_eq!(pool.len(), 4); + + assert_eq!(pool.pop(), Some(1), "parked items do not pop"); + pool.unpark(); assert_eq!(pool.pop(), Some(20)); assert_eq!(pool.pop(), Some(10)); - // Original items are then popped in FIFO order - assert_eq!(pool.pop(), Some(1)); assert_eq!(pool.pop(), Some(2)); assert_eq!(pool.pop(), None); } + + #[test] + fn a_parked_item_holds_its_lanes() { + let mut pool = laned_pool(10); + + pool.insert((0, vec![('A', 0)], "a0")).unwrap(); + pool.insert((5, vec![('A', 1)], "a1")).unwrap(); + pool.insert((0, vec![('B', 0)], "b0")).unwrap(); + + // popping `a0` made `a1` ready; giving `a0` back must block it again + let a0 = pool.pop().unwrap(); + assert_eq!(a0.2, "a0"); + pool.give_back(a0); + assert_eq!(drain(&mut pool), vec!["b0"]); + + pool.unpark(); + assert_eq!(drain(&mut pool), vec!["a0", "a1"]); + assert!(pool.lanes.is_empty(), "drained lanes are dropped"); + } + + #[test] + fn a_batch_given_back_in_reverse_keeps_its_order() { + let mut pool = laned_pool(10); + + pool.insert((0, vec![('A', 0)], "a0")).unwrap(); + pool.insert((5, vec![('A', 1)], "a1")).unwrap(); + pool.insert((9, vec![('H', 0)], "h")).unwrap(); + pool.insert((1, vec![('A', 2)], "a2")).unwrap(); + + let taken: Vec<_> = std::iter::from_fn(|| pool.pop()).take(3).collect(); + assert_eq!( + taken.iter().map(|(_, _, tag)| *tag).collect::>(), + vec!["h", "a0", "a1"] + ); + for item in taken.into_iter().rev() { + pool.give_back(item); + } + pool.unpark(); + + assert_eq!(drain(&mut pool), vec!["h", "a0", "a1", "a2"]); + } } diff --git a/lez/sequencer/actors/executor/Cargo.toml b/lez/sequencer/actors/executor/Cargo.toml index 9d548f70f6..f41ab3f94e 100644 --- a/lez/sequencer/actors/executor/Cargo.toml +++ b/lez/sequencer/actors/executor/Cargo.toml @@ -17,7 +17,6 @@ actor = [ "dep:sharding_pool_actor", "dep:sequencer_core_metrics", "dep:sequencer_stake_core", - "dep:mempool", "dep:testnet_initial_state", "dep:cross_zone", "dep:programs", @@ -59,7 +58,6 @@ sequencer_core_metrics = { workspace = true, features = [ "record", ], optional = true } sequencer_stake_core = { workspace = true, optional = true } -mempool = { workspace = true, optional = true } testnet_initial_state = { workspace = true, optional = true } cross_zone = { workspace = true, optional = true } system_accounts = { workspace = true, optional = true } diff --git a/lez/sequencer/actors/executor/src/actor.rs b/lez/sequencer/actors/executor/src/actor.rs index 0191c248d8..721ccda52a 100644 --- a/lez/sequencer/actors/executor/src/actor.rs +++ b/lez/sequencer/actors/executor/src/actor.rs @@ -32,7 +32,7 @@ use sequencer_bedrock_actor::{ protocol::{ChannelEvent, ChannelEventKind, Ed25519Key, PublisherEvent}, }; use sequencer_channel_signing_actor::{ChannelSigningActor, protocol as channel_signing_protocol}; -use sequencer_core::{StakeConfigUpdate, config::SequencerConfig}; +use sequencer_core::{Rejected, StakeConfigUpdate, config::SequencerConfig}; use sequencer_slasher_actor::SlasherActor; use sequencer_storage_actor::StorageActorTrait; use sharding_pool_actor::{ @@ -350,7 +350,7 @@ impl Message for Execut }: Transaction, _ctx: &mut Context, ) -> Self::Reply { - let online = self.state.online()?; + let online = self.state.online_mut()?; let size = u64::try_from( borsh::object_length(&transaction) @@ -366,6 +366,11 @@ impl Message for Execut return Err(Error::TransactionTooLarge { size, max }); } + // Only the sequencer mints deposits, from their L1 events. + if sequencer_core::extract_bridge_deposit_id(&transaction).is_some() { + return Err(Error::BridgeDepositNotSequencer); + } + // Fee admission against the head state, before the mempool sees it. // Advisory (base fees and balances move), but everything it turns // away would have been refused by the block builder anyway. @@ -386,9 +391,15 @@ impl Message for Execut })?; online - .mempool_handle() - .try_push((origin.into(), transaction)) - .map_err(|_err| Error::MempoolIsFull)?; + .sequencer_mut() + .mempool_mut() + .insert((origin.into(), transaction)) + .map_err(|rejected| match rejected { + Rejected::Full => Error::MempoolIsFull, + Rejected::LaneFull => Error::MempoolLaneFull, + Rejected::Underpriced => Error::MempoolUnderpriced, + Rejected::Cycle => Error::MempoolNonceCycle, + })?; Ok(()) } } diff --git a/lez/sequencer/actors/executor/src/actor/state.rs b/lez/sequencer/actors/executor/src/actor/state.rs index ec99b59d5c..5cd6b6c7c4 100644 --- a/lez/sequencer/actors/executor/src/actor/state.rs +++ b/lez/sequencer/actors/executor/src/actor/state.rs @@ -30,6 +30,10 @@ const CHANNEL_PROBE_DELAY: Duration = Duration::from_secs(2); /// the start. const CHANNEL_PROBE_RETRIES: u32 = 29; +#[expect( + clippy::large_enum_variant, + reason = "One per executor, moved only on its rare state transitions" +)] pub enum State { /// An error or future cancellation happened during [`Self::modify()`]. Error(String), @@ -157,6 +161,14 @@ impl State { } } + pub const fn online_mut(&mut self) -> Result<&mut online::OnlineState> { + if let Self::Online(online) = self { + Ok(online) + } else { + Err(Error::NotOnline) + } + } + /// Modify internal state by value. /// /// In case of `f` failing or cancelling a [`Self::Error`] will be set as the current state. diff --git a/lez/sequencer/actors/executor/src/actor/state/online.rs b/lez/sequencer/actors/executor/src/actor/state/online.rs index 6697418308..c9807db28a 100644 --- a/lez/sequencer/actors/executor/src/actor/state/online.rs +++ b/lez/sequencer/actors/executor/src/actor/state/online.rs @@ -1,16 +1,12 @@ use chain_state::{ChainState, ChannelEntry}; -use common::{ - block::{BedrockStatus, Block}, - transaction::LeeTransaction, -}; +use common::block::{BedrockStatus, Block}; use kameo::actor::ActorRef; -use mempool::{MemPool, MemPoolHandle}; use sequencer_actors_common::SendErrorExt; use sequencer_bedrock_actor::{ BedrockActorTrait, protocol::{ChannelId, ChannelSeq, Ed25519Key, Ed25519PublicKey, PublishOutcome, Slot}, }; -use sequencer_core::{SequencerCore, TransactionOrigin, config::SequencerConfig}; +use sequencer_core::{SequencerCore, config::SequencerConfig}; use sequencer_storage_actor::{StorageActorTrait, protocol::AtomicUpdate}; use sharding_pool_actor::ShardingPoolActor; use tracing::info; @@ -28,7 +24,6 @@ mod turn; pub struct OnlineState { sequencer: SequencerCore, - mempool_handle: MemPoolHandle<(TransactionOrigin, LeeTransaction)>, /// Whose turn it is to write: blocks are only produced in ours. turn: Turn, // TODO: Remove this field @@ -80,10 +75,6 @@ impl OnlineState { &mut self.sequencer } - pub const fn mempool_handle(&self) -> &MemPoolHandle<(TransactionOrigin, LeeTransaction)> { - &self.mempool_handle - } - pub const fn is_our_turn(&self) -> bool { self.turn.is_ours() } @@ -198,8 +189,7 @@ impl OnlineState { info!(%reward_account, "Producer reward account (stake ownership)"); } - let mempool = MemPool::new(config.mempool_max_size); - let mempool_handle = mempool.handle().clone(); + let mempool = sequencer_core::new_mempool(config.mempool_max_size); let is_our_turn = bedrock_pool_ref .ask(sequencer_bedrock_actor::protocol::CheckIsOurTurn { @@ -229,7 +219,6 @@ impl OnlineState { Ok(Self { background_tasks: sequencer.background_task(), - mempool_handle, turn: Turn::new(is_our_turn), sequencer, }) diff --git a/lez/sequencer/actors/executor/src/actor/tests.rs b/lez/sequencer/actors/executor/src/actor/tests.rs index 0d55953cf0..766cf88359 100644 --- a/lez/sequencer/actors/executor/src/actor/tests.rs +++ b/lez/sequencer/actors/executor/src/actor/tests.rs @@ -96,6 +96,11 @@ pub(super) fn sequencer_config() -> (SequencerConfig, TempDir) { } fn test_transaction() -> LeeTransaction { + test_transaction_at(0) +} + +/// A transfer from the funded payer, signed at `payer_nonce`. +fn test_transaction_at(payer_nonce: u128) -> LeeTransaction { let key2 = PrivateKey::new_os_random(); let acc2 = AccountId::from(&PublicKey::new_from_private_key(&key2)); @@ -106,7 +111,7 @@ fn test_transaction() -> LeeTransaction { let payer = accounts[0].account_id; let payer_key = accounts[0].pub_sign_key.clone(); - let nonces = BTreeMap::from([(payer, 0_u128.into()), (acc2, 0_u128.into())]); + let nonces = BTreeMap::from([(payer, payer_nonce.into()), (acc2, 0_u128.into())]); let message = Message::try_new_with_fees( Actor::native_balance(payer), vec![Actor::native_balance(payer), Actor::native_balance(acc2)], @@ -831,8 +836,8 @@ async fn handle_transaction_fails_on_full_mempool() -> Result<()> { .await?; // Fill mempool - for _ in 0..mempool_max_size { - let tx = test_transaction(); + for payer_nonce in 0..mempool_max_size { + let tx = test_transaction_at(payer_nonce.try_into().unwrap()); executor .ask(protocol::Transaction { transaction: tx, @@ -842,7 +847,7 @@ async fn handle_transaction_fails_on_full_mempool() -> Result<()> { } // Now the mempool is full, the next transaction should fail - let tx = test_transaction(); + let tx = test_transaction_at(mempool_max_size.try_into().unwrap()); assert!(matches!( executor .ask(protocol::Transaction { @@ -1022,3 +1027,48 @@ async fn handle_transaction_rejects_a_transaction_too_large_for_any_block() -> R Ok(()) } + +#[test] +async fn handle_transaction_rejects_a_bridge_deposit() -> Result<()> { + let _res = logging::try_init(); + + let (config, _home) = sequencer_config(); + let storage_ref = MockStorageActor::spawn(prepare_mock_storage_with_empty_genesis()); + let executor = spawn_on_genesis( + config, + storage_ref, + spawn_bedrock_pool(prepare_mock_bedrock_on_genesis), + ) + .await?; + + let recipient_id = testnet_initial_state::initial_public_user_accounts()[0].account_id; + let receipt = Actor::new(recipient_id, programs::bridge_account_id()); + let message = Message::try_new( + receipt, + vec![receipt], + BTreeMap::new(), + bridge_core::Message::Deposit { + l1_deposit_op_id: [7; 32], + recipient_id, + amount: 1, + }, + )?; + let tx: LeeTransaction = + PublicTransaction::new(message, WitnessSet::from_raw_parts(Vec::new())).into(); + + let res = executor + .ask(protocol::Transaction { + transaction: tx, + origin: TransactionOrigin::Gossip, + }) + .await; + assert!( + matches!( + res.map_err(SendError::err), + Err(Some(crate::error::Error::BridgeDepositNotSequencer)) + ), + "a bridge deposit was admitted" + ); + + Ok(()) +} diff --git a/lez/sequencer/actors/executor/src/error.rs b/lez/sequencer/actors/executor/src/error.rs index b5dcf5d5bd..145887a9f3 100644 --- a/lez/sequencer/actors/executor/src/error.rs +++ b/lez/sequencer/actors/executor/src/error.rs @@ -57,6 +57,21 @@ pub enum Error { #[error("The mempool is full")] MempoolIsFull, + #[error( + "The transaction does not raise the tip per gas enough over the pending one it would \ + replace or evict" + )] + MempoolUnderpriced, + + #[error("One of the transaction's signers already has too many pending transactions")] + MempoolLaneFull, + + #[error("The transaction's nonces order it both before and after a pending transaction")] + MempoolNonceCycle, + + #[error("Only the sequencer mints bridge deposits, from their L1 events")] + BridgeDepositNotSequencer, + #[error("Transaction too large: size {size}, max {max}")] TransactionTooLarge { size: u64, max: u64 }, diff --git a/lez/sequencer/actors/gossip/Cargo.toml b/lez/sequencer/actors/gossip/Cargo.toml index 55f13ea20f..cf2cfb5146 100644 --- a/lez/sequencer/actors/gossip/Cargo.toml +++ b/lez/sequencer/actors/gossip/Cargo.toml @@ -57,7 +57,6 @@ libp2p = { workspace = true, optional = true } [dev-dependencies] lee = { workspace = true, features = ["test-utils"] } logos-blockchain-core.workspace = true -mempool.workspace = true sequencer_stake_core.workspace = true system_upgrader_core.workspace = true testnet_initial_state.workspace = true diff --git a/lez/sequencer/actors/gossip/src/actor/seen_cache.rs b/lez/sequencer/actors/gossip/src/actor/seen_cache.rs index ac2b50c7e4..6a022b7c6c 100644 --- a/lez/sequencer/actors/gossip/src/actor/seen_cache.rs +++ b/lez/sequencer/actors/gossip/src/actor/seen_cache.rs @@ -4,7 +4,7 @@ use common::HashType; /// Bounded, FIFO-eviction membership cache over transaction hashes. /// -/// The mempool is a plain channel with no dedup, so the gossip layer tracks +/// The mempool only dedups signed transactions, by nonce, so the gossip layer tracks /// recently seen transactions here to avoid re-admitting duplicates that /// arrive from multiple peers or echo back after a local publish. /// diff --git a/lez/sequencer/actors/gossip/src/actor/tests.rs b/lez/sequencer/actors/gossip/src/actor/tests.rs index 9bb9b8755e..6f54badf20 100644 --- a/lez/sequencer/actors/gossip/src/actor/tests.rs +++ b/lez/sequencer/actors/gossip/src/actor/tests.rs @@ -13,9 +13,8 @@ use kameo::{ }; use logos_blockchain_core::proofs::channel_multi_sig_proof::IndexedSignature; use logos_blockchain_key_management_system_service::keys::{Ed25519Key, Ed25519PublicKey}; -use mempool::{MemPool, MemPoolHandle}; use sequencer_channel_signing_actor::protocol::{Signature, Wire}; -use sequencer_core::{TransactionOrigin, config::GossipConfig}; +use sequencer_core::config::GossipConfig; use sequencer_slasher_actor::protocol::{Approval, Offence}; use sequencer_stake_core::SequencerKey; use system_upgrader_core::SignedApproval; @@ -36,9 +35,9 @@ struct TestNode { local_peer_id: libp2p::PeerId, } -/// The mempool and the gossip receivers a started node feeds. +/// The gossip receivers a started node feeds. struct NodeSinks { - mempool: MemPool<(TransactionOrigin, LeeTransaction)>, + transactions: mpsc::UnboundedReceiver, approvals: mpsc::UnboundedReceiver, configs: mpsc::UnboundedReceiver, system_approvals: mpsc::UnboundedReceiver, @@ -152,7 +151,6 @@ fn pubkey(secret: [u8; 32]) -> Ed25519PublicKey { Ed25519Key::from_bytes(&secret).public_key() } -/// An [`IngestSubmit`] that pushes straight into `mempool` unscreened. /// Forwards every `system_upgrader` approval the gossip actor accepts into a channel; stands in for /// the executor's approval pool. fn channel_system_approval_submit( @@ -164,16 +162,11 @@ fn channel_system_approval_submit( }) } -fn unscreened_mempool_submit( - mempool: MemPoolHandle<(TransactionOrigin, LeeTransaction)>, -) -> IngestSubmit { +/// An [`IngestSubmit`] that forwards every transaction unscreened; stands in for the executor. +fn unscreened_submit(sink: mpsc::UnboundedSender) -> IngestSubmit { Arc::new(move |tx| { - let mempool = mempool.clone(); - Box::pin(async move { - mempool - .try_push((TransactionOrigin::Gossip, tx)) - .context("mempool is full") - }) + let sink = sink.clone(); + Box::pin(async move { sink.send(tx).context("the test dropped its receiver") }) }) } @@ -188,8 +181,8 @@ fn test_config() -> GossipConfig { } } -fn test_mempool_handle() -> MemPoolHandle<(TransactionOrigin, LeeTransaction)> { - MemPool::new(1000).handle().clone() +fn test_submit() -> IngestSubmit { + unscreened_submit(mpsc::unbounded_channel().0) } fn test_approval_sink() -> kameo::actor::Recipient { @@ -230,8 +223,7 @@ async fn start_node(secret: [u8; 32], bootstrap: Vec) -> (Tes listen_addr: "/ip4/127.0.0.1/udp/0/quic-v1".parse().unwrap(), bootstrap_peers: bootstrap, }; - let mempool = MemPool::new(1000); - let mempool_handle = mempool.handle().clone(); + let (transaction_tx, transactions) = mpsc::unbounded_channel(); let (approval_tx, approvals) = mpsc::unbounded_channel(); let (config_tx, configs) = mpsc::unbounded_channel(); let (system_approval_tx, system_approvals) = mpsc::unbounded_channel(); @@ -242,7 +234,7 @@ async fn start_node(secret: [u8; 32], bootstrap: Vec) -> (Tes CHANNEL, Ed25519Key::from_bytes(&secret), sink_ref.recipient(), - unscreened_mempool_submit(mempool_handle), + unscreened_submit(transaction_tx), config_sink_ref.recipient(), channel_system_approval_submit(system_approval_tx), ) @@ -258,7 +250,7 @@ async fn start_node(secret: [u8; 32], bootstrap: Vec) -> (Tes local_peer_id, }, NodeSinks { - mempool, + transactions, approvals, configs, system_approvals, @@ -315,7 +307,7 @@ async fn new_binds_and_reports_listen_addr() { [1; 32], Ed25519Key::from_bytes(&[9; 32]), test_approval_sink(), - unscreened_mempool_submit(test_mempool_handle()), + test_submit(), test_config_sink(), channel_system_approval_submit(mpsc::unbounded_channel().0), ) @@ -334,7 +326,7 @@ async fn kill_stops_the_swarm_and_frees_the_socket() { [1; 32], Ed25519Key::from_bytes(&[9; 32]), test_approval_sink(), - unscreened_mempool_submit(test_mempool_handle()), + test_submit(), test_config_sink(), channel_system_approval_submit(mpsc::unbounded_channel().0), ) @@ -359,7 +351,7 @@ async fn kill_stops_the_swarm_and_frees_the_socket() { [1; 32], Ed25519Key::from_bytes(&[10; 32]), test_approval_sink(), - unscreened_mempool_submit(test_mempool_handle()), + test_submit(), test_config_sink(), channel_system_approval_submit(mpsc::unbounded_channel().0), ) @@ -423,9 +415,9 @@ async fn transaction_submitted_to_one_node_reaches_others() { assert!( wait_for(Duration::from_secs(30), async || { sinks_b - .mempool - .pop() - .is_some_and(|(_, received)| received.hash() == expected_hash) + .transactions + .try_recv() + .is_ok_and(|received| received.hash() == expected_hash) }) .await, "B never received the gossiped transaction" @@ -433,9 +425,9 @@ async fn transaction_submitted_to_one_node_reaches_others() { assert!( wait_for(Duration::from_secs(30), async || { sinks_c - .mempool - .pop() - .is_some_and(|(_, received)| received.hash() == expected_hash) + .transactions + .try_recv() + .is_ok_and(|received| received.hash() == expected_hash) }) .await, "C never received the gossiped transaction" @@ -474,9 +466,9 @@ async fn invalid_transaction_is_not_propagated() { assert!( wait_for(Duration::from_secs(30), async || { sinks_b - .mempool - .pop() - .is_some_and(|(_, received)| received.hash() == valid_hash) + .transactions + .try_recv() + .is_ok_and(|received| received.hash() == valid_hash) }) .await, "B never received the valid transaction; gossip link not live" @@ -486,8 +478,8 @@ async fn invalid_transaction_is_not_propagated() { tokio::time::sleep(Duration::from_secs(2)).await; assert!( - sinks_b.mempool.pop().is_none(), - "an invalidly-signed transaction must not reach the mempool" + sinks_b.transactions.try_recv().is_err(), + "an invalidly-signed transaction must not be submitted" ); drop((node_a, node_b)); } diff --git a/lez/sequencer/actors/rpc_server/src/actor/service.rs b/lez/sequencer/actors/rpc_server/src/actor/service.rs index 9c062a783f..784045f0a7 100644 --- a/lez/sequencer/actors/rpc_server/src/actor/service.rs +++ b/lez/sequencer/actors/rpc_server/src/actor/service.rs @@ -465,7 +465,8 @@ fn map_executor_error( ) } invalid @ (sequencer_executor_actor::error::Error::IncorrectFee(_) - | sequencer_executor_actor::error::Error::TransactionTooLarge { .. }) => { + | sequencer_executor_actor::error::Error::TransactionTooLarge { .. } + | sequencer_executor_actor::error::Error::BridgeDepositNotSequencer) => { // With its cause, which the sender is shown. let message = std::error::Error::source(&invalid).map_or_else( || invalid.to_string(), @@ -478,6 +479,27 @@ fn map_executor_error( "Mempool is full".to_owned(), None::<()>, ), + underpriced @ sequencer_executor_actor::error::Error::MempoolUnderpriced => { + ErrorObjectOwned::owned( + sequencer_service_rpc::MEMPOOL_UNDERPRICED_ERROR_CODE, + underpriced.to_string(), + None::<()>, + ) + } + cycle @ sequencer_executor_actor::error::Error::MempoolNonceCycle => { + ErrorObjectOwned::owned( + sequencer_service_rpc::MEMPOOL_NONCE_CYCLE_ERROR_CODE, + cycle.to_string(), + None::<()>, + ) + } + lane_full @ sequencer_executor_actor::error::Error::MempoolLaneFull => { + ErrorObjectOwned::owned( + sequencer_service_rpc::MEMPOOL_LANE_FULL_ERROR_CODE, + lane_full.to_string(), + None::<()>, + ) + } sequencer_executor_actor::error::Error::NotOnline => ErrorObjectOwned::owned( sequencer_service_rpc::SEQUENCER_IS_NOT_ONLINE_ERROR_CODE, "Sequencer is not online yet, try again later".to_owned(), diff --git a/lez/sequencer/core/src/lib.rs b/lez/sequencer/core/src/lib.rs index 82b811f741..d5fcc9409c 100644 --- a/lez/sequencer/core/src/lib.rs +++ b/lez/sequencer/core/src/lib.rs @@ -4,7 +4,7 @@ use std::{ collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque}, path::Path, sync::Arc, - time::Instant, + time::{Duration, Instant}, }; use anyhow::{Context as _, Result, anyhow}; @@ -33,6 +33,7 @@ use logos_blockchain_zone_sdk::{ sequencer::{DepositInfo, SequencerCheckpoint, WithdrawArg, WithdrawInfo}, }; use mempool::MemPool; +pub use mempool::Rejected; use num_bigint::BigUint; use sequencer_bedrock_actor::{ BedrockActorTrait, @@ -95,6 +96,13 @@ const RETIRE_DISPATCH_AFTER_FAILURES: u32 = 3; /// block; nothing is dropped. const MAX_DISPATCHES_PER_BLOCK: usize = 16; +/// How far, in percent, a transaction must raise the tip per declared gas of the pending one it +/// replaces, so a dust raise cannot churn the mempool. +const REPLACEMENT_BUMP_PERCENT: u128 = 10; + +/// How long a transaction may wait in the mempool, e.g. on a nonce gap that never fills. +const MEMPOOL_TTL: Duration = Duration::from_mins(10); + /// File in a sequencer home holding the Ed25519 key the node signs its channel inscriptions with, /// which is the key its stake accredits. pub const CHANNEL_SIGNING_KEY_FILE: &str = "channel_signing_key"; @@ -159,7 +167,7 @@ impl DeclaredGasBudget { } /// The origin of a transaction. -#[derive(Clone, Copy)] +#[derive(Debug, Clone, Copy)] pub enum TransactionOrigin { /// Basic transactions submitted by users via RPC. User, @@ -185,7 +193,8 @@ pub struct SequencerCore { /// Two-tier chain state: production builds on its head; the publisher's /// `on_follow` sink feeds adopted/orphaned/finalized peer blocks into it. chain: Arc>, - mempool: MemPool<(TransactionOrigin, LeeTransaction)>, + /// Ordered by tip per declared gas, one lane per signer's nonce sequence. + mempool: SequencerMemPool, sequencer_config: SequencerConfig, /// Cross-zone watchers, stopped when this sequencer is dropped. They hold a /// store handle, so leaving them running would keep the `RocksDB` lock held @@ -241,7 +250,7 @@ impl SequencerCore { )] pub async fn new( config: SequencerConfig, - mempool: MemPool<(TransactionOrigin, LeeTransaction)>, + mempool: SequencerMemPool, chain: ChainState, applied_seq: Option, channel_signing_key: Ed25519Key, @@ -580,25 +589,12 @@ impl SequencerCore { } } - /// Returns orphaned user txs to the mempool so the next on-turn production - /// re-includes them on the new head. - /// - /// TODO: a full mempool still drops the transaction; a durable resubmit - /// queue is a follow-up. - fn resubmit_orphaned(&self, txs: Vec) { - for tx in txs { - let tx_hash = tx.hash(); - if let Err(err) = self - .mempool - .handle() - .try_push((TransactionOrigin::User, tx)) - { - warn!( - %tx_hash, - error = format_args!("{:#}", anyhow!(err)), - "Dropping orphaned transaction on resubmit" - ); - } + /// Gives orphaned user txs back to the mempool, replacing any pending tx at their nonces, so + /// the next on-turn production re-includes them on the new head. They were included once, + /// so they go back past the size bound rather than being dropped. + fn resubmit_orphaned(&mut self, txs: Vec) { + for tx in txs.into_iter().rev() { + self.mempool.give_back((TransactionOrigin::User, tx)); } } @@ -798,7 +794,7 @@ impl SequencerCore { if publish_res.is_err() { // Nothing was inscribed, so the block's transactions go back in front. for (origin, tx) in mempool_transactions.into_iter().rev() { - self.mempool.push_front((origin, tx)); + self.mempool.give_back((origin, tx)); } } let outcome = match publish_res { @@ -934,7 +930,7 @@ impl SequencerCore { }); // Nothing was inscribed, so the block's transactions go back in front. for (origin, tx) in mempool_transactions.into_iter().rev() { - self.mempool.push_front((origin, tx)); + self.mempool.give_back((origin, tx)); } let observed_deposits = std::mem::take(&mut self.observed_deposits); @@ -1330,8 +1326,26 @@ impl SequencerCore { Some(receipt) } + /// Builds a block, giving back what it took from the mempool if it fails. async fn build_block_from_mempool(&mut self) -> Result { + let mut taken = Vec::new(); + let built = self.assemble_block(&mut taken).await; + if built.is_err() { + for (origin, tx) in taken.into_iter().rev() { + self.mempool.give_back((origin, tx)); + } + } + built + } + + /// Builds a block, collecting in `taken` the mempool transactions it includes. + async fn assemble_block( + &mut self, + taken: &mut Vec<(TransactionOrigin, LeeTransaction)>, + ) -> Result { let now = Instant::now(); + self.mempool.expire(MEMPOOL_TTL); + self.mempool.unpark(); // Decoded outside the chain lock, and read before it is taken: the usual // case is no delivery records at all, and decoding is the expensive part. @@ -1521,7 +1535,6 @@ impl SequencerCore { pending_deposits.push_back(tx); } let mut pinned_deposit_notes = Vec::new(); - let mut deferred_withdrawals = Vec::new(); let max_block_size = usize::try_from( committee_discovery::channel_params(&working_state) @@ -1575,7 +1588,6 @@ impl SequencerCore { // is not the same as a `Sequencer` origin: it says the transaction has a // record behind it and so needs no requeue, where the origin only says // it was not submitted by a user. - let mut mempool_transactions = Vec::new(); // `system_upgrader` changes in the mempool, returned by a reorg, wait for the one slot // before the fee. let mut upgrade_candidates = Vec::new(); @@ -1586,7 +1598,12 @@ impl SequencerCore { while let Some((origin, tx, from_store)) = pending_from_store .pop_front() .map(|tx| (TransactionOrigin::Sequencer, tx, true)) - .or_else(|| self.mempool.pop().map(|(origin, tx)| (origin, tx, false))) + .or_else(|| { + // One ahead of its signers' nonces waits, holding its lanes, for the gap to fill. + self.mempool + .pop_unless(|(_, tx)| awaits_nonces(&working_state, tx)) + .map(|(origin, tx)| (origin, tx, false)) + }) { let tx_hash = tx.hash(); @@ -1607,7 +1624,56 @@ impl SequencerCore { // A block pinning deposits publishes on an explicit parent, which // cannot carry withdrawals. if !pinned_deposit_notes.is_empty() && extract_bridge_withdraw_data(&tx).is_some() { - deferred_withdrawals.push((origin, tx)); + self.mempool.give_back((origin, tx)); + continue; + } + + // Declared-gas pre-screen, before the size check that serializes the + // whole block: a charged transaction whose signed limits do not fit + // this block's remaining budget is deferred with nothing executed — + // unless they exceed the caps outright, in which case it fits no + // budget and is dropped rather than deferred for ever (the RPC door + // screens these out, but gossip ingest does not). One that does not + // even classify (an unserializable transaction) falls through: the + // settlement below rejects it and it is dropped like any other + // failed application. + let charged_view = match chain_state::classify::classify(&tx, false) { + Ok(chain_state::classify::FeeClass::Charged(view)) => Some(view), + Ok(chain_state::classify::FeeClass::Exempt) | Err(_) => None, + }; + if let Some(view) = &charged_view + && !gas_budget.fits(view) + { + // A transaction whose declared gas exceeds the caps fits no + // budget, not even an empty one, so deferring it would stall the + // builder behind it for ever. The RPC door screens these out, + // but gossip ingest does not, so drop it here instead. + if !DeclaredGasBudget::fits_empty(view) { + warn!( + %tx_hash, + gas_limit = view.gas_limit(), + gas_stor = view.gas_stor(), + "Transaction declares gas beyond the block caps, dropping it rather than \ + stalling production" + ); + self.count_dispatch_failure(&tx).await; + continue; + } + warn!( + %tx_hash, + gas_limit = view.gas_limit(), + gas_stor = view.gas_stor(), + "Transaction deferred to next block: its declared gas would exceed the block \ + gas caps" + ); + // Smaller ones behind it may still fit, so the block keeps filling, + // but not from the store: what follows there may depend on it, and + // its record stays and is drained again on the next turn. + if from_store { + pending_from_store.clear(); + } else { + self.mempool.give_back((origin, tx)); + } continue; } @@ -1638,10 +1704,7 @@ impl SequencerCore { if block_size > max_block_size { // Would a block carrying nothing but this still be too big? Then // it does not fit in any block and deferring it defers it for - // ever. A deferred transaction is at the head of the queue every - // turn, so breaking here would stop production reaching anything - // behind it, including the whole mempool, permanently. Give up on - // it instead. + // ever, coming back first every turn. Give up on it instead. // // Measured on its own rather than from `block_size`, which also // counts whatever this block already holds: a transaction that @@ -1672,58 +1735,15 @@ impl SequencerCore { max_block_size, "Transaction deferred to next block: it would exceed the block size limit" ); - // Anything drained from the store needs no requeue: its record - // stays there and is drained again on the next turn. - if !from_store { - self.mempool.push_front((origin, tx)); + // Smaller ones behind it may still fit, so the block keeps filling, + // but not from the store: what follows there may depend on it, and + // its record stays and is drained again on the next turn. + if from_store { + pending_from_store.clear(); + } else { + self.mempool.give_back((origin, tx)); } - break; - } - - // Declared-gas pre-screen: a charged transaction whose signed - // limits do not fit this block's remaining budget is deferred with - // nothing executed, mirroring the size deferral above — unless they - // exceed the caps outright, in which case it fits no budget and is - // dropped rather than deferred for ever (the RPC door screens these - // out, but gossip ingest does not). One that does not even classify - // (an unserializable transaction) falls through: the settlement - // below rejects it and it is dropped like any other failed - // application. - let charged_view = match chain_state::classify::classify(&tx, false) { - Ok(chain_state::classify::FeeClass::Charged(view)) => Some(view), - Ok(chain_state::classify::FeeClass::Exempt) | Err(_) => None, - }; - if let Some(view) = &charged_view - && !gas_budget.fits(view) - { - // A transaction whose declared gas exceeds the caps fits no - // budget, not even an empty one, so deferring it would stall the - // builder behind it for ever. The RPC door screens these out, - // but gossip ingest does not, so drop it here instead. - if !DeclaredGasBudget::fits_empty(view) { - warn!( - %tx_hash, - gas_limit = view.gas_limit(), - gas_stor = view.gas_stor(), - "Transaction declares gas beyond the block caps, dropping it rather than \ - stalling production" - ); - self.count_dispatch_failure(&tx).await; - continue; - } - warn!( - %tx_hash, - gas_limit = view.gas_limit(), - gas_stor = view.gas_stor(), - "Transaction deferred to next block: its declared gas would exceed the block \ - gas caps" - ); - // Anything drained from the store needs no requeue: its record - // stays there and is drained again on the next turn. - if !from_store { - self.mempool.push_front((origin, tx)); - } - break; + continue; } let before_tx_apply = Instant::now(); @@ -1782,7 +1802,7 @@ impl SequencerCore { before_tx_apply.elapsed(), ); if !from_store { - mempool_transactions.push((origin, tx.clone())); + taken.push((origin, tx.clone())); } if let Some(notes) = pins.remove(&tx_hash) { pinned_deposit_notes.extend(notes); @@ -1806,9 +1826,6 @@ impl SequencerCore { break; } } - for (origin, tx) in deferred_withdrawals.into_iter().rev() { - self.mempool.push_front((origin, tx)); - } // At most one `system_upgrader` transaction, directly before the fee: the due `Apply` // first, then changes a threshold of collected approvals carries, then approved changes a @@ -1855,7 +1872,7 @@ impl SequencerCore { "system_upgrader transaction deferred: block is full" ); if !matches!(origin, TransactionOrigin::Sequencer) { - self.mempool.push_front((origin, tx)); + self.mempool.give_back((origin, tx)); } continue; } @@ -1872,7 +1889,7 @@ impl SequencerCore { ) { receipts.push(receipt); if !matches!(origin, TransactionOrigin::Sequencer) { - mempool_transactions.push((origin, tx.clone())); + taken.push((origin, tx.clone())); } valid_transactions.push(tx); break; @@ -1887,7 +1904,7 @@ impl SequencerCore { } for (origin, tx, _) in candidates { if !matches!(origin, TransactionOrigin::Sequencer) { - self.mempool.push_front((origin, tx)); + self.mempool.give_back((origin, tx)); } } if let Err(err) = self.persist_system_approvals().await { @@ -1943,7 +1960,7 @@ impl SequencerCore { block, withdrawals, parent, - mempool_transactions, + mempool_transactions: std::mem::take(taken), pinned_deposit_notes, }) } @@ -1954,6 +1971,10 @@ impl SequencerCore { f(self.chain.lock().await.head_state()) } + pub const fn mempool_mut(&mut self) -> &mut SequencerMemPool { + &mut self.mempool + } + #[must_use] pub const fn storage_ref(&self) -> &ActorRef { &self.storage_ref @@ -2274,6 +2295,10 @@ struct BlockWithMeta { /// Why a publish, pinned or not, did not go out. type PublishError = kameo::error::SendError<(), sequencer_bedrock_actor::error::Error>; +/// The sequencer's mempool: ordered by tip per declared gas, one lane per +/// signer, slotted by nonce. +pub type SequencerMemPool = MemPool<(TransactionOrigin, LeeTransaction), u128, AccountId, u128>; + /// A turn given up because the channel moved under the block while it was /// being built. #[derive(Debug, thiserror::Error)] @@ -2310,6 +2335,71 @@ fn checkpoint_bytes(checkpoint: &SequencerCheckpoint) -> Result> { .context("Failed to serialize zone-sdk checkpoint") } +#[must_use] +pub fn new_mempool(max_size: usize) -> SequencerMemPool { + MemPool::new( + max_size, + |(_, tx)| priority(tx), + |(_, tx)| nonce_slots(tx), + outbids, + ) +} + +/// Whether a replacement's priority clears the one it replaces by [`REPLACEMENT_BUMP_PERCENT`]. +fn outbids(new: &u128, old: &u128) -> bool { + let bump = old + .checked_div(100) + .expect("nonzero divisor") + .saturating_mul(REPLACEMENT_BUMP_PERCENT); + new > old && *new >= old.saturating_add(bump) +} + +/// Tip per declared gas, as a 64.64 fixed-point number. +fn tip_per_gas(tip: u64, gas_limit: u64) -> u128 { + u128::from(tip) + .checked_shl(64) + .and_then(|tip| tip.checked_div(u128::from(gas_limit.max(1)))) + .expect("a u64 shifted by 64 fits u128 and the divisor is nonzero") +} + +/// The order the mempool pops a transaction in, highest first: its tip per +/// declared gas; zero for exempt or unclassifiable ones. +fn priority(tx: &LeeTransaction) -> u128 { + match chain_state::classify::classify(tx, false) { + Ok(chain_state::classify::FeeClass::Charged(view)) => { + tip_per_gas(view.tip(), view.gas_limit()) + } + Ok(chain_state::classify::FeeClass::Exempt) | Err(_) => 0, + } +} + +/// The nonce each signer, a co-signing payer included, signs a transaction at. A private tx +/// signs for its public accounts the same way; a purely private one has none. +fn nonce_slots(tx: &LeeTransaction) -> Vec<(AccountId, u128)> { + let nonces = match tx { + LeeTransaction::Public(tx) => &tx.message().nonces, + LeeTransaction::PrivacyPreserving(tx) => &tx.message().nonces, + }; + nonces.iter().map(|(id, nonce)| (*id, nonce.0)).collect() +} + +/// Whether a transaction signs at a nonce one of its accounts has not reached yet, and at none +/// one has passed: it waits for the gap to fill rather than fails. +fn awaits_nonces(state: &lee::V03State, tx: &LeeTransaction) -> bool { + let mut ahead = false; + for (account_id, nonce) in nonce_slots(tx) { + let current = state + .get_account_by_id_ref(account_id) + .map_or(0, |account| account.nonce.0); + match nonce.cmp(¤t) { + std::cmp::Ordering::Less => return false, + std::cmp::Ordering::Greater => ahead = true, + std::cmp::Ordering::Equal => {} + } + } + ahead +} + /// Whether `deposit_op_id`'s mint is already reflected in `state` — the bridge /// owns its receipt PDA. The receipt is the exactly-once ledger the bridge /// program keeps. diff --git a/lez/sequencer/core/src/tests.rs b/lez/sequencer/core/src/tests.rs index 53e0e25b8c..77c196d4b2 100644 --- a/lez/sequencer/core/src/tests.rs +++ b/lez/sequencer/core/src/tests.rs @@ -2,7 +2,6 @@ use std::{ collections::{BTreeMap, BTreeSet, HashSet, VecDeque}, - pin::pin, sync::Arc, time::Duration, }; @@ -12,12 +11,13 @@ use chain_state::{ChainState, ChannelEntry}; use common::{ HashType, block::{BedrockStatus, Block, HashableBlockData}, + test_utils::create_transaction_native_token_transfer_with_fees, transaction::{FeePayee, LeeTransaction, clock_invocation, fee_invocation}, }; use kameo::{actor::Spawn as _, supervision::RestartPolicy}; use kameo_actors::{DeliveryStrategy, pubsub::PubSub}; use lee::{ - Account, AccountId, Actor, PrivateKey, PublicKey, PublicTransaction, RootCall, + Account, AccountId, Actor, FeeDeclaration, PrivateKey, PublicKey, PublicTransaction, RootCall, TransactionEntry, V03State, program::Program, test_utils::{TestPrivateKeys, init_witness, no_seal, signer_nonces}, @@ -44,7 +44,6 @@ use logos_blockchain_key_management_system_service::keys::{ Ed25519Key, Ed25519PublicKey, ZkPublicKey, }; use logos_blockchain_zone_sdk::{Slot, sequencer::DepositInfo}; -use mempool::{MemPool, MemPoolHandle}; use ping_core::{ReceiverMessage, ping_record_pda, receiver_config_account_id}; use sequencer_bedrock_actor::{ mock::{CannedChannel, MockBedrockActor, SharedChannel, checkpoint_at, mock_msg_of}, @@ -114,12 +113,7 @@ fn test_sequencer_key(seed: u8) -> sequencer_stake_core::SequencerKey { .expect("a channel signing public key is a valid Ed25519 public key") } -async fn start_sequencer( - config: SequencerConfig, -) -> ( - SequencerCore, - MemPoolHandle<(TransactionOrigin, LeeTransaction)>, -) { +async fn start_sequencer(config: SequencerConfig) -> SequencerCore { start_sequencer_on(config, CannedChannel::empty().share()).await } @@ -128,11 +122,8 @@ async fn start_sequencer( async fn start_sequencer_on( config: SequencerConfig, channel: SharedChannel, -) -> ( - SequencerCore, - MemPoolHandle<(TransactionOrigin, LeeTransaction)>, -) { - let (mut sequencer, mempool_handle) = start_sequencer_bare_on(config, channel).await; +) -> SequencerCore { + let mut sequencer = start_sequencer_bare_on(config, channel).await; if sequencer.chain().lock().await.head_tip().is_none() { let genesis = block_at(&sequencer, GENESIS_BLOCK_ID) .await @@ -155,16 +146,13 @@ async fn start_sequencer_on( }) .await .expect("the channel-signing actor should accept a view"); - (sequencer, mempool_handle) + sequencer } /// A sequencer whose channel reports nothing. async fn start_sequencer_bare( config: SequencerConfig, -) -> ( - SequencerCore, - MemPoolHandle<(TransactionOrigin, LeeTransaction)>, -) { +) -> SequencerCore { start_sequencer_bare_on(config, CannedChannel::empty().share()).await } @@ -175,10 +163,7 @@ async fn start_sequencer_bare( async fn start_sequencer_bare_on( config: SequencerConfig, channel: SharedChannel, -) -> ( - SequencerCore, - MemPoolHandle<(TransactionOrigin, LeeTransaction)>, -) { +) -> SequencerCore { let storage = StorageActor::new(&config.db_path()).expect("Failed to open database"); let storage_ref = StorageActor::spawn(storage); @@ -231,8 +216,6 @@ async fn start_sequencer_bare_on( let channel_signing_key = crate::load_or_create_signing_key(&config.home.join(crate::CHANNEL_SIGNING_KEY_FILE)) .unwrap(); - let mempool = MemPool::new(config.mempool_max_size); - let mempool_handle = mempool.handle().clone(); let slasher_ref = SlasherActor::spawn( SlasherActor::load( storage_ref.clone(), @@ -247,7 +230,8 @@ async fn start_sequencer_bare_on( crate::inscribed_block_deposits, )); - let sequencer = SequencerCore::new( + let mempool = crate::new_mempool(config.mempool_max_size); + SequencerCore::new( config, mempool, chain, @@ -260,8 +244,7 @@ async fn start_sequencer_bare_on( channel_signer, ) .await - .expect("Failed to start the sequencer"); - (sequencer, mempool_handle) + .expect("Failed to start the sequencer") } /// The stored block at `block_id`. @@ -486,31 +469,25 @@ fn create_signing_key_for_account2() -> lee::PrivateKey { initial_pub_accounts_private_keys()[1].pub_sign_key.clone() } -async fn common_setup() -> ( - SequencerCore, - MemPoolHandle<(TransactionOrigin, LeeTransaction)>, -) { +async fn common_setup() -> SequencerCore { let config = setup_sequencer_config(); common_setup_with_config(config).await } async fn common_setup_with_config( config: SequencerConfig, -) -> ( - SequencerCore, - MemPoolHandle<(TransactionOrigin, LeeTransaction)>, -) { - let (mut sequencer, mempool_handle) = start_sequencer(config).await; +) -> SequencerCore { + let mut sequencer = start_sequencer(config).await; let tx = common::test_utils::produce_dummy_empty_transaction(); - mempool_handle - .push((TransactionOrigin::User, tx)) - .await + sequencer + .mempool + .insert((TransactionOrigin::User, tx)) .unwrap(); sequencer.run_production_turn().await.unwrap(); - (sequencer, mempool_handle) + sequencer } fn tx_is_bridge_deposit( @@ -592,7 +569,7 @@ async fn a_charged_bridge_deposit_is_dropped_by_the_builder_bridge_guard() { // the builder drops any user-submitted deposit before settling. let config = setup_sequencer_config(); - let (mut sequencer, mempool_handle) = start_sequencer(config).await; + let mut sequencer = start_sequencer(config).await; let payer_key = initial_pub_accounts_private_keys()[0].pub_sign_key.clone(); let recipient_id = initial_public_user_accounts()[1].account_id; @@ -619,9 +596,9 @@ async fn a_charged_bridge_deposit_is_dropped_by_the_builder_bridge_guard() { "expected RestrictedAccountModification, got {err:?}" ); - mempool_handle - .push((TransactionOrigin::User, forged)) - .await + sequencer + .mempool + .insert((TransactionOrigin::User, forged)) .unwrap(); let block_id = sequencer.run_production_turn().await.unwrap(); @@ -650,7 +627,7 @@ async fn a_charged_bridge_deposit_is_dropped_by_the_builder_bridge_guard() { #[tokio::test] async fn a_reverted_withdraw_releases_nothing_on_l1() { - let (mut sequencer, mempool_handle) = common_setup().await; + let mut sequencer = common_setup().await; let payer = &initial_pub_accounts_private_keys()[0]; let root = Actor::new(payer.account_id, programs::bridge_account_id()); let message = lee::public_transaction::Message::try_new_with_fees( @@ -671,9 +648,9 @@ async fn a_reverted_withdraw_releases_nothing_on_l1() { let witness_set = lee::public_transaction::WitnessSet::for_message(&message, &[&payer.pub_sign_key]); let withdraw = LeeTransaction::Public(PublicTransaction::new(message, witness_set)); - mempool_handle - .push((TransactionOrigin::User, withdraw.clone())) - .await + sequencer + .mempool + .insert((TransactionOrigin::User, withdraw.clone())) .unwrap(); let built = sequencer.build_block_from_mempool().await.unwrap(); @@ -684,6 +661,98 @@ async fn a_reverted_withdraw_releases_nothing_on_l1() { assert!(built.withdrawals.is_empty()); } +#[tokio::test] +async fn the_builder_orders_by_tip_without_breaking_a_nonce_sequence() { + let mut sequencer = start_sequencer(setup_sequencer_config()).await; + + let accounts = initial_pub_accounts_private_keys(); + let transfer = |from: usize, nonce: u128, tip: u64| { + let (from_id, to_id) = (accounts[from].account_id, accounts[1 - from].account_id); + create_transaction_native_token_transfer_with_fees( + from_id, + nonce, + to_id, + 1, + &accounts[from].pub_sign_key, + FeeDeclaration::new(from_id, 2_000_000, tip, u128::MAX >> 1), + ) + }; + // Alice's tipped second transfer must wait behind her first; + // Bob's tip beats Alice's untipped head even though he arrived last. + let alice_first = transfer(0, 0, 0); + let alice_second = transfer(0, 1, 5); + let bob = transfer(1, 0, 3); + + for tx in [&alice_first, &alice_second, &bob] { + sequencer + .mempool + .insert((TransactionOrigin::User, tx.clone())) + .unwrap(); + } + + let block_id = sequencer.run_production_turn().await.unwrap(); + let block = block_at(&sequencer, block_id) + .await + .expect("produced block is stored"); + + let position = |needle: &LeeTransaction| { + block + .body + .transactions + .iter() + .position(|tx| tx == needle) + .expect("all three transfers are included") + }; + assert!(position(&bob) < position(&alice_first)); + assert!(position(&alice_first) < position(&alice_second)); +} + +#[test] +fn a_replacement_must_raise_the_priority_by_the_bump() { + assert!(crate::outbids(&110, &100)); + assert!(!crate::outbids(&109, &100)); + assert!(crate::outbids(&1, &0)); + assert!(!crate::outbids(&0, &0)); +} + +#[tokio::test] +async fn a_transaction_ahead_of_its_nonce_waits_for_the_gap() { + let mut sequencer = start_sequencer(setup_sequencer_config()).await; + let (acc1, acc2) = ( + initial_public_user_accounts()[0].account_id, + initial_public_user_accounts()[1].account_id, + ); + let transfer = |nonce| { + common::test_utils::create_transaction_native_token_transfer( + acc1, + nonce, + acc2, + 10, + &create_signing_key_for_account1(), + ) + }; + + let second = transfer(1); + sequencer + .mempool + .insert((TransactionOrigin::User, second.clone())) + .unwrap(); + let block_id = sequencer.run_production_turn().await.unwrap(); + assert_block_tail(&block_at(&sequencer, block_id).await.unwrap(), &[]); + assert_eq!(sequencer.mempool.len(), 1, "the gapped tx is kept"); + + let first = transfer(0); + sequencer + .mempool + .insert((TransactionOrigin::User, first.clone())) + .unwrap(); + let block_id = sequencer.run_production_turn().await.unwrap(); + assert_block_tail( + &block_at(&sequencer, block_id).await.unwrap(), + &[first, second], + ); +} + #[tokio::test] async fn an_exempt_public_bridge_deposit_is_dropped_by_the_builder_bridge_guard() { // An empty-witness bridge `Deposit` classifies as an exempt system @@ -693,7 +762,7 @@ async fn an_exempt_public_bridge_deposit_is_dropped_by_the_builder_bridge_guard( // public deposit straight through (the consensus replay gap is #809). let config = setup_sequencer_config(); - let (mut sequencer, mempool_handle) = start_sequencer(config).await; + let mut sequencer = start_sequencer(config).await; let recipient = DepositRecipient::Identified(lee::PublicAccountEvidence::Key( PublicKey::new_from_private_key(&initial_pub_accounts_private_keys()[1].pub_sign_key), @@ -707,9 +776,9 @@ async fn an_exempt_public_bridge_deposit_is_dropped_by_the_builder_bridge_guard( }) .expect("bridge deposit tx builds"); - mempool_handle - .push((TransactionOrigin::User, forged)) - .await + sequencer + .mempool + .insert((TransactionOrigin::User, forged)) .unwrap(); let block_id = sequencer.run_production_turn().await.unwrap(); @@ -846,7 +915,7 @@ async fn recorded_dispatches_are_drained_from_the_store_on_production() { let record = dispatch_record(7, ping_payload(&payload)); let key = record.message_key; - let (mut sequencer, _mempool_handle) = start_sequencer(cross_zone_test_config()).await; + let mut sequencer = start_sequencer(cross_zone_test_config()).await; assert_eq!( sequencer .storage_ref @@ -915,7 +984,7 @@ async fn recorded_dispatches_are_drained_from_the_store_on_production() { /// and paid for by a user is rejected, so no block may carry it. #[tokio::test] async fn settlement_rejects_a_dispatch_a_user_signed() { - let (sequencer, _mempool_handle) = start_sequencer(cross_zone_test_config()).await; + let sequencer = start_sequencer(cross_zone_test_config()).await; let state = sequencer.with_state(Clone::clone).await; let opening = chain_state::apply::opening_fee_state(&state); @@ -976,7 +1045,7 @@ async fn a_delivered_dispatch_is_skipped_on_the_next_turn() { let record = dispatch_record(11, ping_payload(b"once")); let key = record.message_key; - let (mut sequencer, _mempool_handle) = start_sequencer(cross_zone_test_config()).await; + let mut sequencer = start_sequencer(cross_zone_test_config()).await; sequencer .storage_ref .ask(AddPendingCrossZoneDispatches { @@ -1022,7 +1091,7 @@ async fn a_dispatch_that_never_executes_is_given_up_on_after_repeated_failures() // so without a give-up policy it would fail on every block for ever. let record = dispatch_record(13, b"odd".to_vec()); - let (mut sequencer, _mempool_handle) = start_sequencer(cross_zone_test_config()).await; + let mut sequencer = start_sequencer(cross_zone_test_config()).await; sequencer .storage_ref .ask(AddPendingCrossZoneDispatches { @@ -1112,7 +1181,7 @@ async fn a_redelivered_record_is_dropped_once_its_delivery_is_irreversible() { let record = dispatch_record(29, ping_payload(b"again")); let key = record.message_key; - let (mut sequencer, _mempool_handle) = start_sequencer(cross_zone_test_config()).await; + let mut sequencer = start_sequencer(cross_zone_test_config()).await; sequencer .storage_ref .ask(AddPendingCrossZoneDispatches { @@ -1175,7 +1244,7 @@ async fn a_delivery_still_reversible_keeps_its_record() { let record = dispatch_record(31, ping_payload(b"pending")); let key = record.message_key; - let (mut sequencer, _mempool_handle) = start_sequencer(cross_zone_test_config()).await; + let mut sequencer = start_sequencer(cross_zone_test_config()).await; sequencer .storage_ref .ask(AddPendingCrossZoneDispatches { @@ -1249,7 +1318,7 @@ async fn a_mempool_transaction_too_large_for_any_block_is_dropped() { .unwrap() .channel_params .max_block_size = 4 * 1024; - let (mut sequencer, mempool_handle) = start_sequencer(config).await; + let mut sequencer = start_sequencer(config).await; let segment_key = lee::PrivateKey::try_new([212; 32]).unwrap(); let segment = Actor::new( @@ -1278,9 +1347,9 @@ async fn a_mempool_transaction_too_large_for_any_block_is_dropped() { &create_signing_key_for_account1(), ); for tx in [oversized.clone(), user_tx.clone()] { - mempool_handle - .push((TransactionOrigin::User, tx)) - .await + sequencer + .mempool + .insert((TransactionOrigin::User, tx)) .unwrap(); } @@ -1298,9 +1367,9 @@ async fn a_mempool_transaction_too_large_for_any_block_is_dropped() { #[tokio::test] async fn a_delivery_too_large_for_any_block_does_not_stall_production() { // A store-drained transaction is at the head of the queue every turn, so one - // that cannot fit in any block would defer itself for ever and, because the - // deferral breaks the loop, stop production ever reaching the mempool behind - // it. The peer chooses the payload, so this is theirs to trigger. + // that cannot fit in any block would defer itself for ever and, because a + // store deferral stops the store drain, starve every delivery behind it. + // The peer chooses the payload, so this is theirs to trigger. let record = dispatch_record(41, ping_payload(&[7_u8; 8192])); let mut config = cross_zone_test_config(); @@ -1310,7 +1379,7 @@ async fn a_delivery_too_large_for_any_block_does_not_stall_production() { .unwrap() .channel_params .max_block_size = 4 * 1024; - let (mut sequencer, mempool_handle) = start_sequencer(config).await; + let mut sequencer = start_sequencer(config).await; sequencer .storage_ref .ask(AddPendingCrossZoneDispatches { @@ -1326,9 +1395,9 @@ async fn a_delivery_too_large_for_any_block_does_not_stall_production() { 10, &create_signing_key_for_account1(), ); - mempool_handle - .push((TransactionOrigin::User, user_tx.clone())) - .await + sequencer + .mempool + .insert((TransactionOrigin::User, user_tx.clone())) .unwrap(); // Production must get past it to the mempool in the very first block. @@ -1371,7 +1440,7 @@ async fn a_delivery_backlog_is_spread_across_blocks() { let mut config = cross_zone_test_config(); config.max_num_tx_in_block = backlog + 10; - let (mut sequencer, _mempool_handle) = start_sequencer(config).await; + let mut sequencer = start_sequencer(config).await; sequencer .storage_ref .ask(AddPendingCrossZoneDispatches { @@ -1412,7 +1481,7 @@ async fn a_delivery_backlog_is_spread_across_blocks() { #[tokio::test] async fn unused_declared_gas_is_recredited_after_settlement() { - let (mut sequencer, mempool_handle) = common_setup().await; + let mut sequencer = common_setup().await; let acc1 = initial_public_user_accounts()[0].account_id; let acc2 = initial_public_user_accounts()[1].account_id; let sign_key = create_signing_key_for_account1(); @@ -1429,9 +1498,9 @@ async fn unused_declared_gas_is_recredited_after_settlement() { }) .collect(); for tx in &transfers { - mempool_handle - .push((TransactionOrigin::User, tx.clone())) - .await + sequencer + .mempool + .insert((TransactionOrigin::User, tx.clone())) .unwrap(); } @@ -1447,7 +1516,7 @@ async fn unused_declared_gas_is_recredited_after_settlement() { #[tokio::test] async fn a_block_full_of_charged_gas_defers_the_rest() { - let (mut sequencer, mempool_handle) = common_setup().await; + let mut sequencer = common_setup().await; let acc1 = initial_public_user_accounts()[0].account_id; let acc2 = initial_public_user_accounts()[1].account_id; let sign_key = create_signing_key_for_account1(); @@ -1455,7 +1524,8 @@ async fn a_block_full_of_charged_gas_defers_the_rest() { // A reverted transfer settles first and is charged its whole declared budget — native // execution itself burns no cycles, so a settled transfer alone never fills a block. The // second transfer declares the full per-block cap: it fits an empty budget, but not on top - // of what the block already charged — deferred, not dropped. + // of what the block already charged — deferred, not dropped. It does not close the block: + // the third, queued behind it, still makes it. let transfers = vec![ common::test_utils::create_transaction_native_token_transfer_with_fees( acc1, @@ -1473,11 +1543,18 @@ async fn a_block_full_of_charged_gas_defers_the_rest() { &sign_key, lee::FeeDeclaration::new(acc1, fee_core::market::MAX_GAS_EXEC, 0, u128::MAX >> 1), ), + common::test_utils::create_transaction_native_token_transfer( + acc2, + 0, + acc1, + 10, + &create_signing_key_for_account2(), + ), ]; for tx in &transfers { - mempool_handle - .push((TransactionOrigin::User, tx.clone())) - .await + sequencer + .mempool + .insert((TransactionOrigin::User, tx.clone())) .unwrap(); } @@ -1488,10 +1565,10 @@ async fn a_block_full_of_charged_gas_defers_the_rest() { .await .unwrap() .unwrap(); - assert_block_tail(&block, &transfers[..1]); + assert_block_tail(&block, &[transfers[0].clone(), transfers[2].clone()]); // One receipt per user transaction; the reverted transfer is charged its whole budget. assert_eq!(block.body.receipts.len(), block.body.transactions.len() - 2); - let receipt = block.body.receipts.last().unwrap(); + let receipt = block.body.receipts.first().unwrap(); assert!(!receipt.success); assert_eq!(receipt.fee.gas_exec, fee_core::market::MAX_GAS_EXEC >> 1); @@ -1503,7 +1580,7 @@ async fn a_block_full_of_charged_gas_defers_the_rest() { .await .unwrap() .unwrap(); - assert_block_tail(&block, &transfers[1..]); + assert_block_tail(&block, &transfers[1..2]); } #[tokio::test] @@ -1513,7 +1590,7 @@ async fn an_over_cap_transaction_is_dropped_not_deferred() { // ingest does not, so the builder must DROP it — deferring it would stall // every subsequent block behind it. A normal transfer queued after it must // still make the block. - let (mut sequencer, mempool_handle) = common_setup().await; + let mut sequencer = common_setup().await; let acc1 = initial_public_user_accounts()[0].account_id; let acc2 = initial_public_user_accounts()[1].account_id; let sign_key = create_signing_key_for_account1(); @@ -1528,16 +1605,21 @@ async fn an_over_cap_transaction_is_dropped_not_deferred() { &sign_key, lee::FeeDeclaration::new(acc1, fee_core::market::MAX_GAS_EXEC + 1, 0, u128::MAX >> 1), ); - let normal = - common::test_utils::create_transaction_native_token_transfer(acc1, 0, acc2, 10, &sign_key); + let normal = common::test_utils::create_transaction_native_token_transfer( + acc2, + 0, + acc1, + 10, + &create_signing_key_for_account2(), + ); - mempool_handle - .push((TransactionOrigin::Gossip, over_cap)) - .await + sequencer + .mempool + .insert((TransactionOrigin::Gossip, over_cap)) .unwrap(); - mempool_handle - .push((TransactionOrigin::User, normal.clone())) - .await + sequencer + .mempool + .insert((TransactionOrigin::User, normal.clone())) .unwrap(); // The over-cap tx is dropped and the normal transfer still makes the block: @@ -1572,7 +1654,7 @@ fn transaction_pre_check_pass() { #[tokio::test] async fn transaction_pre_check_native_transfer_valid() { - let (_sequencer, _mempool_handle) = common_setup().await; + let _sequencer = common_setup().await; let acc1 = initial_public_user_accounts()[0].account_id; let acc2 = initial_public_user_accounts()[1].account_id; @@ -1588,7 +1670,7 @@ async fn transaction_pre_check_native_transfer_valid() { #[tokio::test] async fn transaction_pre_check_native_transfer_other_signature() { - let (sequencer, _mempool_handle) = common_setup().await; + let sequencer = common_setup().await; let acc1 = initial_public_user_accounts()[0].account_id; let acc2 = initial_public_user_accounts()[1].account_id; @@ -1616,7 +1698,7 @@ async fn transaction_pre_check_native_transfer_other_signature() { #[tokio::test] async fn transaction_pre_check_native_transfer_sent_too_much() { - let (sequencer, _mempool_handle) = common_setup().await; + let sequencer = common_setup().await; let acc1 = initial_public_user_accounts()[0].account_id; let acc2 = initial_public_user_accounts()[1].account_id; @@ -1653,7 +1735,7 @@ async fn transaction_pre_check_native_transfer_sent_too_much() { #[tokio::test] async fn transaction_execute_native_transfer() { - let (sequencer, _mempool_handle) = common_setup().await; + let sequencer = common_setup().await; let acc1 = initial_public_user_accounts()[0].account_id; let acc2 = initial_public_user_accounts()[1].account_id; @@ -1681,42 +1763,79 @@ async fn transaction_execute_native_transfer() { } #[tokio::test] -async fn push_tx_into_mempool_blocks_until_mempool_is_full() { +async fn push_tx_into_mempool_fails_until_a_turn_frees_it() { let config = SequencerConfig { mempool_max_size: 1, ..setup_sequencer_config() }; - let (mut sequencer, mempool_handle) = common_setup_with_config(config).await; + let mut sequencer = common_setup_with_config(config).await; let tx = common::test_utils::produce_dummy_empty_transaction(); // Fill the mempool - mempool_handle - .push((TransactionOrigin::User, tx.clone())) - .await + sequencer + .mempool + .insert((TransactionOrigin::User, tx.clone())) .unwrap(); - - // Check that pushing another transaction will block - let mut push_fut = pin!(mempool_handle.push((TransactionOrigin::User, tx.clone()))); - let poll = futures::poll!(push_fut.as_mut()); - assert!(poll.is_pending()); + let other = common::test_utils::create_transaction_native_token_transfer( + initial_public_user_accounts()[0].account_id, + 0, + initial_public_user_accounts()[1].account_id, + 10, + &create_signing_key_for_account1(), + ); + assert_eq!( + sequencer.mempool.insert((TransactionOrigin::User, other)), + Err(crate::Rejected::Underpriced), + "a full mempool evicts only what a tx outbids" + ); // Empty the mempool by producing a block sequencer.run_production_turn().await.unwrap(); - // Resolve the pending push - assert!(push_fut.await.is_ok()); + sequencer + .mempool + .insert((TransactionOrigin::User, tx)) + .unwrap(); +} + +/// Orphaned transactions were included once, so a full mempool does not drop them. +#[tokio::test] +async fn orphaned_transactions_are_requeued_past_a_full_mempool() { + let config = SequencerConfig { + mempool_max_size: 1, + ..setup_sequencer_config() + }; + let mut sequencer = start_sequencer(config).await; + sequencer + .mempool + .insert(( + TransactionOrigin::User, + common::test_utils::produce_dummy_empty_transaction(), + )) + .unwrap(); + let orphan = common::test_utils::create_transaction_native_token_transfer( + initial_public_user_accounts()[0].account_id, + 0, + initial_public_user_accounts()[1].account_id, + 10, + &create_signing_key_for_account1(), + ); + + sequencer.resubmit_orphaned(vec![orphan]); + + assert_eq!(sequencer.mempool.len(), 2); } #[tokio::test] async fn build_block_from_mempool() { - let (mut sequencer, mempool_handle) = common_setup().await; + let mut sequencer = common_setup().await; let genesis_height = sequencer.chain_height().await; let tx = common::test_utils::produce_dummy_empty_transaction(); - mempool_handle - .push((TransactionOrigin::User, tx)) - .await + sequencer + .mempool + .insert((TransactionOrigin::User, tx)) .unwrap(); let result = sequencer.build_block_from_mempool().await; @@ -1741,7 +1860,7 @@ fn live_channel(keys: Vec) -> LiveChannelConfig { #[tokio::test] async fn a_stake_only_moves_the_committee_once_it_has_finalized() { // Genesis stakes the bootstrap key, so the head wants it accredited already. - let (mut sequencer, _mempool_handle) = common_setup().await; + let mut sequencer = common_setup().await; let chain = sequencer.chain(); let finalized = MsgId::root(); @@ -1790,7 +1909,7 @@ async fn a_stake_only_moves_the_committee_once_it_has_finalized() { #[tokio::test] async fn replay_transactions_are_rejected_in_the_same_block() { - let (mut sequencer, mempool_handle) = common_setup().await; + let mut sequencer = common_setup().await; let acc1 = initial_public_user_accounts()[0].account_id; let acc2 = initial_public_user_accounts()[1].account_id; @@ -1801,17 +1920,17 @@ async fn replay_transactions_are_rejected_in_the_same_block() { acc1, 0, acc2, 100, &sign_key1, ); - let tx_original = tx.clone(); - let tx_replay = tx.clone(); - // Pushing two copies of the same tx to the mempool - mempool_handle - .push((TransactionOrigin::User, tx_original)) - .await - .unwrap(); - mempool_handle - .push((TransactionOrigin::User, tx_replay)) - .await + // The mempool holds one tx per signer nonce, so a copy does not replace the original + sequencer + .mempool + .insert((TransactionOrigin::User, tx.clone())) .unwrap(); + assert_eq!( + sequencer + .mempool + .insert((TransactionOrigin::User, tx.clone())), + Err(crate::Rejected::Underpriced) + ); // Create block sequencer.run_production_turn().await.unwrap(); @@ -1830,7 +1949,7 @@ async fn replay_transactions_are_rejected_in_the_same_block() { #[tokio::test] async fn replay_transactions_are_rejected_in_different_blocks() { - let (mut sequencer, mempool_handle) = common_setup().await; + let mut sequencer = common_setup().await; let acc1 = initial_public_user_accounts()[0].account_id; let acc2 = initial_public_user_accounts()[1].account_id; @@ -1842,9 +1961,9 @@ async fn replay_transactions_are_rejected_in_different_blocks() { ); // The transaction should be included the first time - mempool_handle - .push((TransactionOrigin::User, tx.clone())) - .await + sequencer + .mempool + .insert((TransactionOrigin::User, tx.clone())) .unwrap(); sequencer.run_production_turn().await.unwrap(); let block = sequencer @@ -1858,9 +1977,9 @@ async fn replay_transactions_are_rejected_in_different_blocks() { assert_block_tail(&block, std::slice::from_ref(&tx)); // Add same transaction should fail - mempool_handle - .push((TransactionOrigin::User, tx.clone())) - .await + sequencer + .mempool + .insert((TransactionOrigin::User, tx.clone())) .unwrap(); sequencer.run_production_turn().await.unwrap(); let block = sequencer @@ -1886,7 +2005,7 @@ async fn restart_from_storage() { // from `acc_1` to `acc_2`. The block created with that transaction will be kept stored in // the temporary directory for the block storage of this test. let storage_weak = { - let (mut sequencer, mempool_handle) = start_sequencer(config.clone()).await; + let mut sequencer = start_sequencer(config.clone()).await; let signing_key = create_signing_key_for_account1(); let tx = common::test_utils::create_transaction_native_token_transfer( @@ -1897,9 +2016,9 @@ async fn restart_from_storage() { &signing_key, ); - mempool_handle - .push((TransactionOrigin::User, tx.clone())) - .await + sequencer + .mempool + .insert((TransactionOrigin::User, tx.clone())) .unwrap(); sequencer.run_production_turn().await.unwrap(); let block = sequencer @@ -1917,7 +2036,7 @@ async fn restart_from_storage() { // Instantiating a new sequencer from the same config. This should load the existing block // with the above transaction and update the state to reflect that. - let (sequencer, _mempool_handle) = start_sequencer(config.clone()).await; + let sequencer = start_sequencer(config.clone()).await; let balance_acc_1 = sequencer .with_state(|s| { s.get_account_by_id(acc1_account_id) @@ -1947,7 +2066,7 @@ async fn restart_from_storage() { #[tokio::test] async fn get_pending_blocks() { let config = setup_sequencer_config(); - let (mut sequencer, _mempool_handle) = start_sequencer(config).await; + let mut sequencer = start_sequencer(config).await; sequencer.run_production_turn().await.unwrap(); sequencer.run_production_turn().await.unwrap(); sequencer.run_production_turn().await.unwrap(); @@ -1962,7 +2081,7 @@ async fn produce_block_with_correct_prev_meta_after_restart() { // Step 1: Create initial database with some block metadata let (storage_weak, expected_prev_meta) = { - let (mut sequencer, mempool_handle) = start_sequencer(config.clone()).await; + let mut sequencer = start_sequencer(config.clone()).await; let signing_key = create_signing_key_for_account1(); @@ -1975,9 +2094,9 @@ async fn produce_block_with_correct_prev_meta_after_restart() { &signing_key, ); - mempool_handle - .push((TransactionOrigin::User, tx)) - .await + sequencer + .mempool + .insert((TransactionOrigin::User, tx)) .unwrap(); sequencer.run_production_turn().await.unwrap(); @@ -1993,7 +2112,7 @@ async fn produce_block_with_correct_prev_meta_after_restart() { storage_weak.wait_for_shutdown_with_result(|_| ()).await; // Step 2: Restart sequencer from the same storage - let (mut sequencer, mempool_handle) = start_sequencer(config.clone()).await; + let mut sequencer = start_sequencer(config.clone()).await; // Step 3: Submit a new transaction let signing_key = create_signing_key_for_account1(); @@ -2005,9 +2124,9 @@ async fn produce_block_with_correct_prev_meta_after_restart() { &signing_key, ); - mempool_handle - .push((TransactionOrigin::User, tx.clone())) - .await + sequencer + .mempool + .insert((TransactionOrigin::User, tx.clone())) .unwrap(); // Step 4: Produce new block @@ -2032,7 +2151,7 @@ async fn produce_block_with_correct_prev_meta_after_restart() { #[tokio::test] async fn user_tx_that_chain_calls_clock_is_dropped() { - let (mut sequencer, mempool_handle) = common_setup().await; + let mut sequencer = common_setup().await; let clock_reader = test_programs::clock_reader(); @@ -2070,9 +2189,9 @@ async fn user_tx_that_chain_calls_clock_is_dropped() { segment_message, segment_witness_set, )); - mempool_handle - .push((TransactionOrigin::User, segment_tx)) - .await + sequencer + .mempool + .insert((TransactionOrigin::User, segment_tx)) .unwrap(); sequencer.run_production_turn().await.unwrap(); @@ -2102,9 +2221,9 @@ async fn user_tx_that_chain_calls_clock_is_dropped() { header_message, header_witness_set, )); - mempool_handle - .push((TransactionOrigin::User, deploy_tx)) - .await + sequencer + .mempool + .insert((TransactionOrigin::User, deploy_tx)) .unwrap(); sequencer.run_production_turn().await.unwrap(); @@ -2132,9 +2251,9 @@ async fn user_tx_that_chain_calls_clock_is_dropped() { timestamp: 0, block_id: tick_block_id, }); - mempool_handle - .push((TransactionOrigin::User, tick)) - .await + sequencer + .mempool + .insert((TransactionOrigin::User, tick)) .unwrap(); sequencer.run_production_turn().await.unwrap(); let block = block_at(&sequencer, tick_block_id) @@ -2144,9 +2263,9 @@ async fn user_tx_that_chain_calls_clock_is_dropped() { // The same transaction with a read-only message is included, so the setup is sound. let read = user_tx(&clock_core::Message::ReadBlockId { granularity: 1 }); - mempool_handle - .push((TransactionOrigin::User, read.clone())) - .await + sequencer + .mempool + .insert((TransactionOrigin::User, read.clone())) .unwrap(); sequencer.run_production_turn().await.unwrap(); let block = block_at(&sequencer, tick_block_id + 1) @@ -2157,7 +2276,7 @@ async fn user_tx_that_chain_calls_clock_is_dropped() { #[tokio::test] async fn block_production_aborts_when_clock_account_data_is_corrupted() { - let (mut sequencer, mempool_handle) = common_setup().await; + let mut sequencer = common_setup().await; // Corrupt the clock account's clock actor state so deserialization fails. let clock_program_id = programs::clock_account_id(); @@ -2177,9 +2296,9 @@ async fn block_production_aborts_when_clock_account_data_is_corrupted() { // Push a dummy transaction so the mempool is non-empty. let tx = common::test_utils::produce_dummy_empty_transaction(); - mempool_handle - .push((TransactionOrigin::User, tx)) - .await + sequencer + .mempool + .insert((TransactionOrigin::User, tx)) .unwrap(); // Block production must fail because the appended clock tx cannot execute. @@ -2577,7 +2696,7 @@ fn resubmittable_txs_of_blocks_without_user_txs_is_empty() { #[tokio::test] async fn follow_update_persists_the_checkpoint_with_its_effects() { let config = setup_sequencer_config(); - let (mut sequencer, _mempool_handle) = start_sequencer(config).await; + let mut sequencer = start_sequencer(config).await; let genesis_meta = sequencer .storage_ref .ask(GetLatestBlockMeta) @@ -2612,7 +2731,7 @@ async fn follow_update_persists_the_checkpoint_with_its_effects() { async fn a_failed_publish_leaves_the_head_and_the_pin_alone() { let config = setup_sequencer_config(); let channel = CannedChannel::empty().share(); - let (mut sequencer, mempool_handle) = start_sequencer_on(config, channel.clone()).await; + let mut sequencer = start_sequencer_on(config, channel.clone()).await; let first = sequencer.run_production_turn().await.unwrap(); let pin = sequencer.chain().lock().await.pin(); @@ -2621,8 +2740,9 @@ async fn a_failed_publish_leaves_the_head_and_the_pin_alone() { publish_fails: true, ..CannedChannel::empty() }); - mempool_handle - .push(( + sequencer + .mempool + .insert(( TransactionOrigin::User, common::test_utils::create_transaction_native_token_transfer( initial_public_user_accounts()[0].account_id, @@ -2632,7 +2752,6 @@ async fn a_failed_publish_leaves_the_head_and_the_pin_alone() { &create_signing_key_for_account1(), ), )) - .await .unwrap(); let failed = sequencer.run_production_turn().await; assert!(failed.is_err(), "the canned publish failure must surface"); @@ -2648,7 +2767,7 @@ async fn a_failed_publish_leaves_the_head_and_the_pin_alone() { async fn a_block_is_refused_when_the_channel_tip_moved_under_it() { let config = setup_sequencer_config(); let channel = CannedChannel::empty().share(); - let (mut sequencer, _mempool_handle) = start_sequencer_on(config, channel.clone()).await; + let mut sequencer = start_sequencer_on(config, channel.clone()).await; let first = sequencer.run_production_turn().await.unwrap(); @@ -2677,7 +2796,7 @@ async fn a_block_is_refused_when_the_channel_tip_moved_under_it() { async fn production_chains_on_a_garbage_entry_at_the_tip() { let config = setup_sequencer_config(); let channel = CannedChannel::empty().share(); - let (mut sequencer, _mempool_handle) = start_sequencer_on(config, channel.clone()).await; + let mut sequencer = start_sequencer_on(config, channel.clone()).await; let first = sequencer.run_production_turn().await.unwrap(); @@ -2717,7 +2836,7 @@ async fn production_chains_on_a_garbage_entry_at_the_tip() { async fn a_conflict_dropping_our_newest_block_rewinds_the_head_and_the_pin() { let config = setup_sequencer_config(); let channel = CannedChannel::empty().share(); - let (mut sequencer, _mempool_handle) = start_sequencer_on(config, channel.clone()).await; + let mut sequencer = start_sequencer_on(config, channel.clone()).await; sequencer.run_production_turn().await.unwrap(); let dropped = sequencer.run_production_turn().await.unwrap(); @@ -2752,7 +2871,7 @@ async fn a_conflict_dropping_our_newest_block_rewinds_the_head_and_the_pin() { #[tokio::test] async fn a_conflict_dropping_a_garbage_entry_rewinds_only_the_pin() { let config = setup_sequencer_config(); - let (mut sequencer, _mempool_handle) = start_sequencer(config).await; + let mut sequencer = start_sequencer(config).await; let first = sequencer.run_production_turn().await.unwrap(); let kept = sequencer.chain().lock().await.view().to_vec(); @@ -2793,7 +2912,7 @@ async fn a_conflict_dropping_a_garbage_entry_rewinds_only_the_pin() { #[tokio::test] async fn the_pin_stays_on_a_finalized_entry() { let config = setup_sequencer_config(); - let (mut sequencer, _mempool_handle) = start_sequencer(config).await; + let mut sequencer = start_sequencer(config).await; sequencer.run_production_turn().await.unwrap(); let block2 = block_at(&sequencer, 2).await.unwrap(); @@ -2826,7 +2945,7 @@ async fn the_pin_stays_on_a_finalized_entry() { #[tokio::test] async fn follow_update_records_deposits_for_the_production_drain() { let config = setup_sequencer_config(); - let (mut sequencer, _mempool_handle) = start_sequencer(config).await; + let mut sequencer = start_sequencer(config).await; let recipient = DepositRecipient::Identified(lee::PublicAccountEvidence::Key( PublicKey::new_from_private_key(&initial_pub_accounts_private_keys()[0].pub_sign_key), @@ -2912,8 +3031,7 @@ async fn an_observed_deposit_mints_in_a_block_pinned_on_its_notes() { ..CannedChannel::empty() }; let pins = Arc::clone(&channel.pins); - let (mut sequencer, _mempool_handle) = - start_sequencer_on(setup_sequencer_config(), channel.share()).await; + let mut sequencer = start_sequencer_on(setup_sequencer_config(), channel.share()).await; let op_id = [0x5d; 32]; sequencer @@ -2940,7 +3058,6 @@ async fn an_observed_deposit_mints_in_a_block_pinned_on_its_notes() { /// that deposit and block. async fn sequencer_minting_an_observed_deposit() -> ( SequencerCore, - MemPoolHandle<(TransactionOrigin, LeeTransaction)>, DepositInfo, Block, ) { @@ -2949,8 +3066,7 @@ async fn sequencer_minting_an_observed_deposit() -> ( channel_notes: HashSet::from([note]), ..CannedChannel::empty() }; - let (mut sequencer, mempool_handle) = - start_sequencer_on(setup_sequencer_config(), channel.share()).await; + let mut sequencer = start_sequencer_on(setup_sequencer_config(), channel.share()).await; let deposit = deposit_info([0x5d; 32], 5, &[note]); sequencer .on_observed_deposits(std::slice::from_ref(&deposit)) @@ -2960,7 +3076,7 @@ async fn sequencer_minting_an_observed_deposit() -> ( .await .expect("produced block is stored"); - (sequencer, mempool_handle, deposit, block) + (sequencer, deposit, block) } /// Whether this node signs a peer's pin of `block` on `deposit`'s notes. @@ -3039,8 +3155,7 @@ async fn signs_peer_pin( #[tokio::test] async fn a_peer_pin_of_an_observed_deposit_is_signed() { - let (sequencer, _mempool_handle, deposit, block) = - sequencer_minting_an_observed_deposit().await; + let (sequencer, deposit, block) = sequencer_minting_an_observed_deposit().await; assert!(signs_peer_pin(&sequencer, &block, &deposit).await); } @@ -3049,8 +3164,7 @@ async fn a_peer_pin_of_an_observed_deposit_is_signed() { /// finalize. #[tokio::test] async fn a_peer_pin_of_a_deposit_final_on_l1_is_still_signed() { - let (mut sequencer, _mempool_handle, deposit, block) = - sequencer_minting_an_observed_deposit().await; + let (mut sequencer, deposit, block) = sequencer_minting_an_observed_deposit().await; sequencer .on_channel_update(&ChannelUpdate { checkpoint: checkpoint_at(mock_msg_of(&block)), @@ -3064,8 +3178,7 @@ async fn a_peer_pin_of_a_deposit_final_on_l1_is_still_signed() { #[tokio::test] async fn a_peer_pin_of_a_deposit_minted_in_a_final_block_is_not_signed() { - let (mut sequencer, _mempool_handle, deposit, block) = - sequencer_minting_an_observed_deposit().await; + let (mut sequencer, deposit, block) = sequencer_minting_an_observed_deposit().await; sequencer .on_channel_update(&ChannelUpdate { checkpoint: checkpoint_at(mock_msg_of(&block)), @@ -3087,8 +3200,7 @@ async fn a_pin_short_of_signatures_leaves_its_deposit_to_finality() { ..CannedChannel::empty() }; let pins = Arc::clone(&channel.pins); - let (mut sequencer, mempool_handle) = - start_sequencer_on(setup_sequencer_config(), channel.share()).await; + let mut sequencer = start_sequencer_on(setup_sequencer_config(), channel.share()).await; let silent_peer = Ed25519Key::from_bytes(&[8; 32]).public_key(); sequencer .channel_signer @@ -3107,9 +3219,9 @@ async fn a_pin_short_of_signatures_leaves_its_deposit_to_finality() { 10, &create_signing_key_for_account1(), ); - mempool_handle - .push((TransactionOrigin::User, user_tx.clone())) - .await + sequencer + .mempool + .insert((TransactionOrigin::User, user_tx.clone())) .unwrap(); let op_id = [0x6a; 32]; @@ -3164,8 +3276,7 @@ async fn deposits_past_the_pin_input_limit_mint_in_the_next_block() { ..CannedChannel::empty() }; let pins = Arc::clone(&channel.pins); - let (mut sequencer, _mempool_handle) = - start_sequencer_on(setup_sequencer_config(), channel.share()).await; + let mut sequencer = start_sequencer_on(setup_sequencer_config(), channel.share()).await; let (first, second) = ([0x6b; 32], [0x6c; 32]); sequencer @@ -3204,8 +3315,7 @@ async fn assert_failed_pin_leaves_its_deposit_to_finality(channel: CannedChannel ..channel }; let pins = Arc::clone(&channel.pins); - let (mut sequencer, _mempool_handle) = - start_sequencer_on(setup_sequencer_config(), channel.share()).await; + let mut sequencer = start_sequencer_on(setup_sequencer_config(), channel.share()).await; let op_id = [0x6d; 32]; sequencer @@ -3249,8 +3359,7 @@ async fn a_pin_that_fails_to_publish_leaves_its_deposit_to_finality() { async fn an_observed_deposit_off_the_branch_is_not_minted() { let channel = CannedChannel::empty(); let pins = Arc::clone(&channel.pins); - let (mut sequencer, _mempool_handle) = - start_sequencer_on(setup_sequencer_config(), channel.share()).await; + let mut sequencer = start_sequencer_on(setup_sequencer_config(), channel.share()).await; let op_id = [0x5e; 32]; sequencer @@ -3277,8 +3386,7 @@ async fn a_pinned_deposit_is_not_minted_again_once_final() { ..CannedChannel::empty() }; let pins = Arc::clone(&channel.pins); - let (mut sequencer, _mempool_handle) = - start_sequencer_on(setup_sequencer_config(), channel.share()).await; + let mut sequencer = start_sequencer_on(setup_sequencer_config(), channel.share()).await; let op_id = [0x5f; 32]; let deposit = deposit_info(op_id, 5, &[note]); @@ -3370,7 +3478,7 @@ async fn reorg_to_genesis( #[tokio::test] async fn a_final_deposit_mints_in_the_next_block_and_not_again() { - let (mut sequencer, _mempool_handle) = start_sequencer(setup_sequencer_config()).await; + let mut sequencer = start_sequencer(setup_sequencer_config()).await; let op_id = [0x61; 32]; sequencer @@ -3410,8 +3518,7 @@ async fn an_observed_deposit_is_not_minted_again_next_turn() { ..CannedChannel::empty() }; let pins = Arc::clone(&channel.pins); - let (mut sequencer, _mempool_handle) = - start_sequencer_on(setup_sequencer_config(), channel.share()).await; + let mut sequencer = start_sequencer_on(setup_sequencer_config(), channel.share()).await; let op_id = [0x62; 32]; sequencer @@ -3443,8 +3550,7 @@ async fn an_observed_deposit_with_a_note_off_the_branch_is_not_minted() { ..CannedChannel::empty() }; let pins = Arc::clone(&channel.pins); - let (mut sequencer, _mempool_handle) = - start_sequencer_on(setup_sequencer_config(), channel.share()).await; + let mut sequencer = start_sequencer_on(setup_sequencer_config(), channel.share()).await; let op_id = [0x66; 32]; sequencer @@ -3471,8 +3577,7 @@ async fn an_observed_deposit_with_a_note_off_the_branch_is_not_minted() { async fn an_observed_deposit_without_notes_is_minted_only_once_final() { let channel = CannedChannel::empty(); let pins = Arc::clone(&channel.pins); - let (mut sequencer, _mempool_handle) = - start_sequencer_on(setup_sequencer_config(), channel.share()).await; + let mut sequencer = start_sequencer_on(setup_sequencer_config(), channel.share()).await; let op_id = [0x67; 32]; let deposit = deposit_info(op_id, 5, &[]); @@ -3506,8 +3611,7 @@ async fn an_observed_deposit_without_notes_is_minted_only_once_final() { #[tokio::test] async fn an_orphaned_final_deposit_mint_is_minted_again_once() { let channel = CannedChannel::empty().share(); - let (mut sequencer, _mempool_handle) = - start_sequencer_on(setup_sequencer_config(), channel.clone()).await; + let mut sequencer = start_sequencer_on(setup_sequencer_config(), channel.clone()).await; let balance_before = native_balance(&sequencer, deposit_recipient()).await; let op_id = [0x63; 32]; @@ -3549,8 +3653,7 @@ async fn an_orphaned_pinned_deposit_is_minted_again_pinned_on_its_notes() { }; let pins = Arc::clone(&channel.pins); let shared = channel.clone().share(); - let (mut sequencer, _mempool_handle) = - start_sequencer_on(setup_sequencer_config(), shared.clone()).await; + let mut sequencer = start_sequencer_on(setup_sequencer_config(), shared.clone()).await; let balance_before = native_balance(&sequencer, deposit_recipient()).await; let op_id = [0x64; 32]; @@ -3590,8 +3693,7 @@ async fn an_orphaned_pinned_deposit_whose_notes_left_the_branch_is_not_minted_ag }; let pins = Arc::clone(&channel.pins); let shared = channel.share(); - let (mut sequencer, _mempool_handle) = - start_sequencer_on(setup_sequencer_config(), shared.clone()).await; + let mut sequencer = start_sequencer_on(setup_sequencer_config(), shared.clone()).await; let balance_before = native_balance(&sequencer, deposit_recipient()).await; let op_id = [0x65; 32]; @@ -3637,8 +3739,7 @@ async fn a_minted_deposit_whose_notes_its_pin_spent_stays_observed() { }; let pins = Arc::clone(&channel.pins); let shared = channel.clone().share(); - let (mut sequencer, _mempool_handle) = - start_sequencer_on(setup_sequencer_config(), shared.clone()).await; + let mut sequencer = start_sequencer_on(setup_sequencer_config(), shared.clone()).await; let op_id = [0x6e; 32]; sequencer @@ -3672,7 +3773,7 @@ async fn a_minted_deposit_whose_notes_its_pin_spent_stays_observed() { #[tokio::test] async fn follow_adopted_peer_block_applies_and_persists() { let config = setup_sequencer_config(); - let (mut sequencer, _mempool_handle) = start_sequencer(config.clone()).await; + let mut sequencer = start_sequencer(config.clone()).await; let genesis_meta = sequencer .storage_ref .ask(GetLatestBlockMeta) @@ -3721,7 +3822,7 @@ async fn follow_adopted_peer_block_applies_and_persists() { #[tokio::test] async fn an_extension_repeating_our_own_entry_is_deduped() { let config = setup_sequencer_config(); - let (mut sequencer, mempool_handle) = start_sequencer(config).await; + let mut sequencer = start_sequencer(config).await; let acc1 = initial_public_user_accounts()[0].account_id; let acc2 = initial_public_user_accounts()[1].account_id; @@ -3732,9 +3833,9 @@ async fn an_extension_repeating_our_own_entry_is_deduped() { 10, &create_signing_key_for_account1(), ); - mempool_handle - .push((TransactionOrigin::User, tx)) - .await + sequencer + .mempool + .insert((TransactionOrigin::User, tx)) .unwrap(); sequencer.run_production_turn().await.unwrap(); let view_msgs = @@ -3770,7 +3871,7 @@ async fn an_extension_repeating_our_own_entry_is_deduped() { #[tokio::test] async fn a_conflict_dropping_our_block_reverts_it_and_requeues_its_user_txs() { let config = setup_sequencer_config(); - let (mut sequencer, mempool_handle) = start_sequencer(config).await; + let mut sequencer = start_sequencer(config).await; let acc1 = initial_public_user_accounts()[0].account_id; let acc2 = initial_public_user_accounts()[1].account_id; @@ -3781,9 +3882,9 @@ async fn a_conflict_dropping_our_block_reverts_it_and_requeues_its_user_txs() { 10, &create_signing_key_for_account1(), ); - mempool_handle - .push((TransactionOrigin::User, tx.clone())) - .await + sequencer + .mempool + .insert((TransactionOrigin::User, tx.clone())) .unwrap(); sequencer.run_production_turn().await.unwrap(); let kept = sequencer.chain().lock().await.view()[..1].to_vec(); @@ -3806,6 +3907,7 @@ async fn a_conflict_dropping_our_block_reverts_it_and_requeues_its_user_txs() { initial_public_user_accounts()[0].balance, "the dropped transfer must be reverted from the head" ); + sequencer.mempool.unpark(); let (origin, requeued) = sequencer .mempool .pop() @@ -3823,7 +3925,7 @@ async fn a_conflict_after_finalization_requeues_nothing() { // A finalized block is below the view, so a conflict that no longer names // it reverts nothing and requeues nothing. let config = setup_sequencer_config(); - let (mut sequencer, mempool_handle) = start_sequencer(config).await; + let mut sequencer = start_sequencer(config).await; let acc1 = initial_public_user_accounts()[0].account_id; let acc2 = initial_public_user_accounts()[1].account_id; @@ -3834,9 +3936,9 @@ async fn a_conflict_after_finalization_requeues_nothing() { 10, &create_signing_key_for_account1(), ); - mempool_handle - .push((TransactionOrigin::User, tx)) - .await + sequencer + .mempool + .insert((TransactionOrigin::User, tx)) .unwrap(); sequencer.run_production_turn().await.unwrap(); let block2 = block_at(&sequencer, 2).await.unwrap(); @@ -3870,6 +3972,7 @@ async fn a_conflict_after_finalization_requeues_nothing() { initial_public_user_accounts()[1].balance + 10, "the finalized transfer stands" ); + sequencer.mempool.unpark(); assert!( sequencer.mempool.pop().is_none(), "a transaction that is already irreversible must not be requeued" @@ -3879,12 +3982,12 @@ async fn a_conflict_after_finalization_requeues_nothing() { #[tokio::test] async fn follow_finalized_own_block_moves_final_tier_and_marks_store() { let config = setup_sequencer_config(); - let (mut sequencer, mempool_handle) = start_sequencer(config).await; + let mut sequencer = start_sequencer(config).await; let tx = common::test_utils::produce_dummy_empty_transaction(); - mempool_handle - .push((TransactionOrigin::User, tx)) - .await + sequencer + .mempool + .insert((TransactionOrigin::User, tx)) .unwrap(); sequencer.run_production_turn().await.unwrap(); let block2 = block_at(&sequencer, 2).await.unwrap(); @@ -3918,7 +4021,7 @@ async fn follow_finalized_delivery_drops_its_pending_record() { let record = dispatch_record(17, ping_payload(b"settled")); let key = record.message_key; - let (mut sequencer, _mempool_handle) = start_sequencer(cross_zone_test_config()).await; + let mut sequencer = start_sequencer(cross_zone_test_config()).await; sequencer .storage_ref .ask(AddPendingCrossZoneDispatches { @@ -3960,7 +4063,7 @@ async fn a_parked_finalized_block_does_not_drop_a_dispatch_record() { let key = record.message_key; let delivery = dispatch_tx(19, ping_payload(b"parked")); - let (mut sequencer, mempool_handle) = start_sequencer(cross_zone_test_config()).await; + let mut sequencer = start_sequencer(cross_zone_test_config()).await; sequencer .storage_ref .ask(AddPendingCrossZoneDispatches { @@ -3970,9 +4073,9 @@ async fn a_parked_finalized_block_does_not_drop_a_dispatch_record() { .unwrap(); let tx = common::test_utils::produce_dummy_empty_transaction(); - mempool_handle - .push((TransactionOrigin::User, tx)) - .await + sequencer + .mempool + .insert((TransactionOrigin::User, tx)) .unwrap(); sequencer.run_production_turn().await.unwrap(); @@ -4003,7 +4106,7 @@ async fn a_parked_finalized_block_does_not_drop_a_dispatch_record() { #[tokio::test] async fn follow_finalized_backfill_block_is_applied_and_marked_finalized() { let config = setup_sequencer_config(); - let (mut sequencer, _mempool_handle) = start_sequencer(config).await; + let mut sequencer = start_sequencer(config).await; let genesis_meta = sequencer .storage_ref .ask(GetLatestBlockMeta) @@ -4043,7 +4146,7 @@ async fn restart_restores_the_view_and_recovers_from_a_conflict() { // Produce block 2 (a user transfer), then "crash" before it finalizes. let (storage_weak, tx, genesis_entry) = { - let (mut sequencer, mempool_handle) = start_sequencer(config.clone()).await; + let mut sequencer = start_sequencer(config.clone()).await; let tx = common::test_utils::create_transaction_native_token_transfer( acc1, 0, @@ -4051,9 +4154,9 @@ async fn restart_restores_the_view_and_recovers_from_a_conflict() { 10, &create_signing_key_for_account1(), ); - mempool_handle - .push((TransactionOrigin::User, tx.clone())) - .await + sequencer + .mempool + .insert((TransactionOrigin::User, tx.clone())) .unwrap(); sequencer.run_production_turn().await.unwrap(); let genesis_entry = sequencer.chain().lock().await.view()[0].clone(); @@ -4063,7 +4166,7 @@ async fn restart_restores_the_view_and_recovers_from_a_conflict() { // Restart: nothing is finalized, so block 2 must come back in the view, // not final — the L1 can still drop it. - let (mut sequencer, _mempool_handle) = start_sequencer(config.clone()).await; + let mut sequencer = start_sequencer(config.clone()).await; assert_eq!(sequencer.chain_height().await, 2); // The channel replaces block 2 with a competing empty block 2'. @@ -4100,6 +4203,7 @@ async fn restart_restores_the_view_and_recovers_from_a_conflict() { ); let stored = block_at(&sequencer, 2).await.unwrap(); assert_eq!(stored.header.hash, block2_prime.header.hash); + sequencer.mempool.unpark(); let (origin, requeued) = sequencer .mempool .pop() @@ -4115,7 +4219,7 @@ async fn a_conflict_keeping_our_block_requeues_nothing() { let config = setup_sequencer_config(); let acc1 = initial_public_user_accounts()[0].account_id; let acc2 = initial_public_user_accounts()[1].account_id; - let (mut sequencer, mempool_handle) = start_sequencer(config).await; + let mut sequencer = start_sequencer(config).await; let tx = common::test_utils::create_transaction_native_token_transfer( acc1, @@ -4124,9 +4228,9 @@ async fn a_conflict_keeping_our_block_requeues_nothing() { 10, &create_signing_key_for_account1(), ); - mempool_handle - .push((TransactionOrigin::User, tx)) - .await + sequencer + .mempool + .insert((TransactionOrigin::User, tx)) .unwrap(); sequencer.run_production_turn().await.unwrap(); let block2 = block_at(&sequencer, 2).await.unwrap(); @@ -4156,6 +4260,7 @@ async fn a_conflict_keeping_our_block_requeues_nothing() { head_tip.hash, block2.header.hash, "the block stays on the head" ); + sequencer.mempool.unpark(); assert!( sequencer.mempool.pop().is_none(), "a kept block must not requeue its transactions" @@ -4169,11 +4274,11 @@ async fn restart_reanchors_on_the_persisted_final_snapshot() { // Produce block 2 and follow its finalization, which persists the final // snapshot; then "crash". let storage_weak = { - let (mut sequencer, mempool_handle) = start_sequencer(config.clone()).await; + let mut sequencer = start_sequencer(config.clone()).await; let tx = common::test_utils::produce_dummy_empty_transaction(); - mempool_handle - .push((TransactionOrigin::User, tx)) - .await + sequencer + .mempool + .insert((TransactionOrigin::User, tx)) .unwrap(); sequencer.run_production_turn().await.unwrap(); let block2 = block_at(&sequencer, 2).await.unwrap(); @@ -4190,7 +4295,7 @@ async fn restart_reanchors_on_the_persisted_final_snapshot() { // Restart: the final tier re-anchors on the snapshot instead of treating // the whole stored chain as final. - let (sequencer, _mempool_handle) = start_sequencer(config.clone()).await; + let sequencer = start_sequencer(config.clone()).await; let chain = sequencer.chain(); let chain = chain.lock().await; assert_eq!(chain.final_tip().expect("final tip set").block_id, 2); @@ -4200,7 +4305,7 @@ async fn restart_reanchors_on_the_persisted_final_snapshot() { #[tokio::test] async fn a_publish_on_a_pin_the_view_moved_past_is_dropped() { let config = setup_sequencer_config(); - let (mut sequencer, _mempool_handle) = start_sequencer(config).await; + let mut sequencer = start_sequencer(config).await; let genesis_meta = sequencer .storage_ref .ask(GetLatestBlockMeta) @@ -4244,7 +4349,7 @@ async fn a_publish_on_a_pin_the_view_moved_past_is_dropped() { #[tokio::test] async fn a_published_block_that_does_not_apply_moves_only_the_pin() { let config = setup_sequencer_config(); - let (sequencer, _mempool_handle) = start_sequencer(config).await; + let sequencer = start_sequencer(config).await; let pin = sequencer.chain().lock().await.pin(); // The head moved under us: our block's parent is no longer the tip. @@ -4266,7 +4371,7 @@ async fn a_published_block_that_does_not_apply_moves_only_the_pin() { #[tokio::test] async fn follow_update_persists_blocks_meta_and_state_atomically() { let config = setup_sequencer_config(); - let (mut sequencer, _mempool_handle) = start_sequencer(config.clone()).await; + let mut sequencer = start_sequencer(config.clone()).await; let genesis_meta = sequencer .storage_ref .ask(GetLatestBlockMeta) @@ -4350,7 +4455,7 @@ async fn follow_update_persists_blocks_meta_and_state_atomically() { #[tokio::test] async fn a_foreign_block_below_the_final_tip_settles_no_deposit_record() { let config = setup_sequencer_config(); - let (mut sequencer, _mempool_handle) = start_sequencer(config).await; + let mut sequencer = start_sequencer(config).await; // Final tier at block 2. sequencer.run_production_turn().await.unwrap(); @@ -4422,7 +4527,7 @@ async fn a_foreign_block_below_the_final_tip_settles_no_deposit_record() { #[tokio::test] async fn a_finalized_competitor_requeues_the_user_txs_of_our_block() { let config = setup_sequencer_config(); - let (mut sequencer, mempool_handle) = start_sequencer(config).await; + let mut sequencer = start_sequencer(config).await; let acc1 = initial_public_user_accounts()[0].account_id; let acc2 = initial_public_user_accounts()[1].account_id; @@ -4433,9 +4538,9 @@ async fn a_finalized_competitor_requeues_the_user_txs_of_our_block() { 10, &create_signing_key_for_account1(), ); - mempool_handle - .push((TransactionOrigin::User, tx.clone())) - .await + sequencer + .mempool + .insert((TransactionOrigin::User, tx.clone())) .unwrap(); sequencer.run_production_turn().await.unwrap(); let genesis = sequencer.chain().lock().await.view()[0].clone(); @@ -4456,6 +4561,7 @@ async fn a_finalized_competitor_requeues_the_user_txs_of_our_block() { let head_tip = sequencer.chain().lock().await.head_tip().unwrap(); assert_eq!(head_tip.hash, peer_block.header.hash); + sequencer.mempool.unpark(); let (origin, requeued) = sequencer .mempool .pop() @@ -4469,7 +4575,7 @@ async fn a_finalized_competitor_requeues_the_user_txs_of_our_block() { #[tokio::test] async fn a_block_back_on_the_head_is_stored_again() { let config = setup_sequencer_config(); - let (mut sequencer, _mempool_handle) = start_sequencer(config).await; + let mut sequencer = start_sequencer(config).await; sequencer.run_production_turn().await.unwrap(); let view = sequencer.chain().lock().await.view().to_vec(); @@ -4500,7 +4606,7 @@ async fn a_block_back_on_the_head_is_stored_again() { async fn a_restart_without_a_view_follows_the_channel_from_the_final_tier() { let config = setup_sequencer_config(); let (storage_weak, view) = { - let (mut sequencer, _mempool_handle) = start_sequencer(config.clone()).await; + let mut sequencer = start_sequencer(config.clone()).await; sequencer.run_production_turn().await.unwrap(); let view = sequencer.chain().lock().await.view().to_vec(); sequencer @@ -4519,7 +4625,7 @@ async fn a_restart_without_a_view_follows_the_channel_from_the_final_tier() { }; storage_weak.wait_for_shutdown_with_result(|_| ()).await; - let (mut sequencer, _mempool_handle) = start_sequencer_bare(config).await; + let mut sequencer = start_sequencer_bare(config).await; { let chain = sequencer.chain(); let chain = chain.lock().await; @@ -6293,7 +6399,7 @@ fn a_private_genesis_allocation_is_a_pending_credit_its_owner_receives_once() { #[tokio::test] async fn a_slash_lands_over_a_pending_partial_unstake() { // The release is due in the block the slash is proposed for; it must not pre-empt it. - let (mut sequencer, _mempool_handle) = common_setup_with_config(setup_sequencer_config()).await; + let mut sequencer = common_setup_with_config(setup_sequencer_config()).await; let amount = 2 * system_accounts::DEFAULT_MINIMUM_SEQUENCER_STAKE; let offender = test_sequencer_key(0x44); let (offender_id, offender_key) = committee_ownership(0x44); @@ -6391,14 +6497,10 @@ async fn a_slash_lands_over_a_pending_partial_unstake() { async fn sequencer_with_a_full_exit( channel: CannedChannel, seed: u8, -) -> ( - SequencerCore, - MemPoolHandle<(TransactionOrigin, LeeTransaction)>, -) { - let (sequencer, mempool_handle) = - start_sequencer_on(setup_sequencer_config(), channel.share()).await; +) -> SequencerCore { + let sequencer = start_sequencer_on(setup_sequencer_config(), channel.share()).await; set_up_a_full_exit(&sequencer, seed).await; - (sequencer, mempool_handle) + sequencer } /// Gives the head `seed`'s key with its whole stake pending release. @@ -6441,7 +6543,7 @@ async fn a_full_exit_is_left_out_until_its_removal_is_final() { pending_removals: [test_sequencer_key(0x46)].into(), ..CannedChannel::empty() }; - let (mut sequencer, _mempool_handle) = sequencer_with_a_full_exit(channel, 0x46).await; + let mut sequencer = sequencer_with_a_full_exit(channel, 0x46).await; let built = sequencer.build_block_from_mempool().await.unwrap(); @@ -6453,8 +6555,7 @@ async fn a_full_exit_is_left_out_until_its_removal_is_final() { #[tokio::test] async fn a_full_exit_lands_once_its_removal_is_final() { - let (mut sequencer, _mempool_handle) = - sequencer_with_a_full_exit(CannedChannel::empty(), 0x46).await; + let mut sequencer = sequencer_with_a_full_exit(CannedChannel::empty(), 0x46).await; let built = sequencer.build_block_from_mempool().await.unwrap(); @@ -6466,8 +6567,7 @@ async fn a_full_exit_lands_once_its_removal_is_final() { #[tokio::test] async fn a_block_releasing_a_stake_before_its_removal_is_final_is_invalid() { - let (mut sequencer, _mempool_handle) = - sequencer_with_a_full_exit(CannedChannel::empty(), 0x46).await; + let mut sequencer = sequencer_with_a_full_exit(CannedChannel::empty(), 0x46).await; let built = sequencer.build_block_from_mempool().await.unwrap(); let mut state = sequencer.chain().lock().await.head_state().clone(); @@ -6495,8 +6595,7 @@ async fn a_block_releasing_a_stake_before_its_removal_is_final_is_invalid() { async fn a_held_block_finalized_by_its_child_is_judged_from_final_blocks() { let leaving = test_sequencer_key(0x46); let channel = CannedChannel::empty().share(); - let (mut sequencer, _mempool_handle) = - start_sequencer_on(setup_sequencer_config(), channel.clone()).await; + let mut sequencer = start_sequencer_on(setup_sequencer_config(), channel.clone()).await; // Finalized first, so the stake below is on the final state too. let bootstrap = sequencer.chain().lock().await.view().to_vec(); sequencer @@ -6545,8 +6644,7 @@ async fn a_slash_of_a_fully_pending_stake_is_not_a_release() { pending_removals: [test_sequencer_key(0x44)].into(), ..CannedChannel::empty() }; - let (mut sequencer, _mempool_handle) = - start_sequencer_on(setup_sequencer_config(), channel.share()).await; + let mut sequencer = start_sequencer_on(setup_sequencer_config(), channel.share()).await; let amount = 2 * system_accounts::DEFAULT_MINIMUM_SEQUENCER_STAKE; let offender = test_sequencer_key(0x44); let (offender_id, offender_key) = committee_ownership(0x44); @@ -6996,7 +7094,7 @@ async fn finalize_genesis( #[tokio::test] async fn a_finalized_block_that_does_not_apply_is_reported() { - let (mut sequencer, _mempool_handle) = start_sequencer(setup_sequencer_config()).await; + let mut sequencer = start_sequencer(setup_sequencer_config()).await; let (genesis, genesis_entry) = finalize_genesis(&mut sequencer).await; // The final tip again is a re-delivery, not an offence. @@ -7015,7 +7113,7 @@ async fn a_finalized_block_that_does_not_apply_is_reported() { #[tokio::test] async fn a_finalized_block_with_a_wrong_id_is_reported() { - let (mut sequencer, _mempool_handle) = start_sequencer(setup_sequencer_config()).await; + let mut sequencer = start_sequencer(setup_sequencer_config()).await; let (genesis, genesis_entry) = finalize_genesis(&mut sequencer).await; let skips_ahead = common::test_utils::produce_dummy_block( @@ -7029,7 +7127,7 @@ async fn a_finalized_block_with_a_wrong_id_is_reported() { #[tokio::test] async fn a_finalized_block_off_the_lineage_is_not_reported() { - let (mut sequencer, _mempool_handle) = start_sequencer(setup_sequencer_config()).await; + let mut sequencer = start_sequencer(setup_sequencer_config()).await; let (genesis, _) = finalize_genesis(&mut sequencer).await; // Chained on an entry that is neither final nor held: a re-delivery. @@ -7054,7 +7152,7 @@ async fn adopt(sequencer: &mut SequencerCore, en #[tokio::test] async fn a_stale_adopted_entry_finalized_below_the_tier_is_not_reported() { - let (mut sequencer, _mempool_handle) = start_sequencer(setup_sequencer_config()).await; + let mut sequencer = start_sequencer(setup_sequencer_config()).await; let (_, genesis_entry) = finalize_genesis(&mut sequencer).await; sequencer.run_production_turn().await.unwrap(); let block2 = block_at(&sequencer, 2).await.unwrap(); @@ -7075,7 +7173,7 @@ async fn a_stale_adopted_entry_finalized_below_the_tier_is_not_reported() { #[tokio::test] async fn a_held_finalized_block_with_a_wrong_id_is_reported() { - let (mut sequencer, _mempool_handle) = start_sequencer(setup_sequencer_config()).await; + let mut sequencer = start_sequencer(setup_sequencer_config()).await; let (genesis, genesis_entry) = finalize_genesis(&mut sequencer).await; let skips_ahead = common::test_utils::produce_dummy_block( @@ -7091,7 +7189,7 @@ async fn a_held_finalized_block_with_a_wrong_id_is_reported() { #[tokio::test] async fn the_first_finalized_block_is_not_reported() { - let (mut sequencer, _mempool_handle) = start_sequencer(setup_sequencer_config()).await; + let mut sequencer = start_sequencer(setup_sequencer_config()).await; // Nothing is final yet, so there is no tip it failed to follow. let invalid = common::test_utils::produce_dummy_block( @@ -7281,7 +7379,7 @@ fn a_genesis_supply_names_its_recipient_in_the_deposit_form() { #[tokio::test] async fn a_turn_on_a_view_with_a_hole_is_skipped() { let config = setup_sequencer_config(); - let (mut sequencer, mempool_handle) = start_sequencer(config.clone()).await; + let mut sequencer = start_sequencer(config.clone()).await; let genesis_meta = sequencer .storage_ref .ask(GetLatestBlockMeta) @@ -7300,8 +7398,9 @@ async fn a_turn_on_a_view_with_a_hole_is_skipped() { }) .await; let pin = sequencer.chain().lock().await.pin(); - mempool_handle - .push(( + sequencer + .mempool + .insert(( TransactionOrigin::User, common::test_utils::create_transaction_native_token_transfer( initial_public_user_accounts()[0].account_id, @@ -7311,7 +7410,6 @@ async fn a_turn_on_a_view_with_a_hole_is_skipped() { &create_signing_key_for_account1(), ), )) - .await .unwrap(); let skipped = sequencer.run_production_turn().await; @@ -7452,7 +7550,7 @@ async fn an_approved_change_has_room_in_a_full_block() { .unwrap() .channel_params .max_block_size = sequencer_stake_core::MIN_MAX_BLOCK_SIZE; - let (mut sequencer, mempool_handle) = common_setup_with_config(config).await; + let mut sequencer = common_setup_with_config(config).await; // `clock`'s upgrade is pending, so a Cancel of it applies; a second accredited member joins // the node, since a threshold is at least two approvals. @@ -7559,9 +7657,9 @@ async fn an_approved_change_has_room_in_a_full_block() { }) .collect(); for tx in &transfers { - mempool_handle - .push((TransactionOrigin::User, tx.clone())) - .await + sequencer + .mempool + .insert((TransactionOrigin::User, tx.clone())) .unwrap(); } @@ -7630,3 +7728,14 @@ fn a_peer_genesis_hash_round_trips_as_hex() { .contains("\"0202020202020202020202020202020202020202020202020202020202020202\"") ); } + +#[test] +fn tip_per_gas_ranks_by_tip_over_declared_gas() { + use crate::tip_per_gas; + + // 3 over 100k gas outbids 5 over 10M gas + assert!(tip_per_gas(3, 100_000) > tip_per_gas(5, 10_000_000)); + assert_eq!(tip_per_gas(1, 2), tip_per_gas(2, 4)); + // a zero gas limit does not divide by zero + assert_eq!(tip_per_gas(7, 0), tip_per_gas(7, 1)); +} diff --git a/lez/sequencer/service/rpc/src/lib.rs b/lez/sequencer/service/rpc/src/lib.rs index 7ec7a13450..8f91decbd9 100644 --- a/lez/sequencer/service/rpc/src/lib.rs +++ b/lez/sequencer/service/rpc/src/lib.rs @@ -26,6 +26,18 @@ pub const PRIVATE_FEE_BELOW_HEAD_ERROR_CODE: i32 = -31901; /// from the channel. pub const SEQUENCER_IS_NOT_ONLINE_ERROR_CODE: i32 = -31902; +/// Error code of a transaction turned away because it does not raise the tip per gas enough over +/// the pending one it would replace (same signer nonce) or evict (full mempool). +pub const MEMPOOL_UNDERPRICED_ERROR_CODE: i32 = -31903; + +/// Error code of a transaction turned away because its nonces order it both before and after a +/// pending transaction, so neither could ever be included. +pub const MEMPOOL_NONCE_CYCLE_ERROR_CODE: i32 = -31904; + +/// Error code of a transaction turned away because one of its signers already has the most +/// pending transactions a signer may have. +pub const MEMPOOL_LANE_FULL_ERROR_CODE: i32 = -31905; + /// Type alias for RPC client. Only available when `client` feature is enabled. /// /// It's cheap to clone this client, so it can be cloned and shared across the application. diff --git a/test_fixtures/fixtures/prebuilt_sequencer_db.dump b/test_fixtures/fixtures/prebuilt_sequencer_db.dump index ab6e7bd7a4..c878c8fc4a 100644 Binary files a/test_fixtures/fixtures/prebuilt_sequencer_db.dump and b/test_fixtures/fixtures/prebuilt_sequencer_db.dump differ