Struct Mempool

Source
pub struct Mempool {
    transactions: HashMap<u64, MempoolTransaction>,
    mempool_size: usize,
    max_mempool_size: usize,
    queue: Vec<Txid>,
    hasher: RandomState,
}
Expand description

Holds the transactions that we broadcasted and are still in the mempool.

Fields§

§transactions: HashMap<u64, MempoolTransaction>

A list of all transactions we currently have in the mempool.

Transactions are kept as a map of their transaction id to the transaction itself, we also keep track of when we added the transaction to the mempool to be able to remove stale transactions.

§mempool_size: usize

How much memory (in bytes) does the mempool currently use.

§max_mempool_size: usize

The maximum size of the mempool in bytes.

§queue: Vec<Txid>

A queue of transaction we know about, but we haven’t downloaded yet

§hasher: RandomState

A hasher that we use to compute the short transaction ids.

Implementations§

Source§

impl Mempool

Source

pub fn new(max_mempool_size: usize) -> Mempool

Creates a new mempool with a given maximum size

Source

pub fn list_unprocessed(&self) -> Vec<Txid>

List transactions we are pending to process.

Source

pub fn list_mempool(&self) -> Vec<Txid>

List all transactions we’ve accepted to the mempool.

This won’t count transactions that are still in the queue.

Source

pub fn get_block_template( &self, version: Version, prev_blockhash: BlockHash, time: u32, bits: CompactTarget, max_block_weight: u64, ) -> Block

Returns an unsolved block (with nonce 0) with as many transactions as we can fit into a block (up to max_block_weight).

Source

fn add_transaction_to_block( &self, block_transactions: &mut Vec<Transaction>, short_txid: u64, )

Utility method that grabs one transaction and all its dependencies, then adds them to a tx list.

Source

pub fn consume_block(&mut self, block: &Block) -> Vec<Txid>

Consume a block and remove all transactions that were included in it.

Source

fn is_already_spent(&self, outpoint: &OutPoint) -> bool

Checks if an outpoint is already spent in the mempool.

This can be used to find conflicts before adding a transaction to the mempool.

Source

fn check_for_conflicts( &self, transaction: &Transaction, ) -> Result<(), AcceptToMempoolError>

Checks if the transaction doesn’t have conflicting inputs or spends the same input twice.

Source

pub fn accept_to_mempool( &mut self, transaction: Transaction, ) -> Result<(), AcceptToMempoolError>

Accepts a transaction to mempool

This method will perform some context-less validations on a transaction, and then accept to our mempool. It assumes that we have validated this transaction’s proof.

§Errors
  • If we don’t have space left in our mempool
  • If the transaction conflicts with another mempool transaction
  • If it sepends the same input twice
  • If any amount check fails: if input amounts are less than output amounts or if it spends more than the theoretical maximum amount of Bitcoins
  • If either vIn or vOut are empty
  • If any script is larger than the maximum allowed size
Source

fn find_mempool_depends(&self, tx: &Transaction) -> Vec<u64>

From a transaction that is already in the mempool, computes which transaction it depends.

Source

pub fn get_from_mempool<'a>(&'a self, id: &Txid) -> Option<&'a Transaction>

Get a transaction from the mempool.

Source

pub fn get_stale(&mut self) -> Vec<Txid>

Get all transactions that were in the mempool for more than 1 hour, if any

Trait Implementations§

Source§

impl Debug for Mempool

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more