Skip to main content

forest/message_pool/
config.rs

1// Copyright 2019-2026 ChainSafe Systems
2// SPDX-License-Identifier: Apache-2.0, MIT
3
4use std::time::Duration;
5
6use crate::{
7    db::{SettingsStore, setting_keys::MPOOL_CONFIG_KEY},
8    shim::{address::Address, percent::Percent},
9    utils::encoding::from_slice_with_fallback,
10};
11use serde::{Deserialize, Serialize};
12
13const SIZE_LIMIT_LOW: i64 = 20000;
14const SIZE_LIMIT_HIGH: i64 = 30000;
15const PRUNE_COOLDOWN: Duration = Duration::from_secs(60); // 1 minute
16const GAS_LIMIT_OVERESTIMATION: f64 = 1.25;
17pub const REPLACE_BY_FEE_RATIO_DEFAULT: Percent = Percent(125);
18
19/// Configuration available for the [`crate::message_pool::MessagePool`].
20///
21/// [MessagePool]: crate::message_pool::MessagePool
22#[derive(Clone, Serialize, Deserialize)]
23pub struct MpoolConfig {
24    pub priority_addrs: Vec<Address>,
25    pub size_limit_high: i64,
26    pub size_limit_low: i64,
27    pub replace_by_fee_ratio: Percent,
28    pub prune_cooldown: Duration,
29    pub gas_limit_overestimation: f64,
30}
31
32impl Default for MpoolConfig {
33    fn default() -> Self {
34        Self {
35            priority_addrs: vec![],
36            size_limit_high: SIZE_LIMIT_HIGH,
37            size_limit_low: SIZE_LIMIT_LOW,
38            replace_by_fee_ratio: REPLACE_BY_FEE_RATIO_DEFAULT,
39            prune_cooldown: PRUNE_COOLDOWN,
40            gas_limit_overestimation: GAS_LIMIT_OVERESTIMATION,
41        }
42    }
43}
44
45impl MpoolConfig {
46    /// Returns the low limit capacity of messages to allocate.
47    pub fn size_limit_low(&self) -> i64 {
48        self.size_limit_low
49    }
50
51    /// Returns slice of [Address]es to prioritize when selecting messages.
52    pub fn priority_addrs(&self) -> &[Address] {
53        &self.priority_addrs
54    }
55}
56
57impl MpoolConfig {
58    /// Load `config` from store, if exists. If there is no `config`, uses
59    /// default.
60    pub fn load_config<DB: SettingsStore>(store: &DB) -> anyhow::Result<Self> {
61        match store.read_bin(MPOOL_CONFIG_KEY)? {
62            Some(v) => Ok(from_slice_with_fallback(&v)?),
63            None => Ok(Default::default()),
64        }
65    }
66}