forest/message_pool/
config.rs

1// Copyright 2019-2025 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,
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 REPLACE_BY_FEE_RATIO: f64 = 1.25;
17const GAS_LIMIT_OVERESTIMATION: f64 = 1.25;
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: f64,
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,
39            prune_cooldown: PRUNE_COOLDOWN,
40            gas_limit_overestimation: GAS_LIMIT_OVERESTIMATION,
41        }
42    }
43}
44
45impl MpoolConfig {
46    #[cfg(test)]
47    /// Saves message pool `config` to the database, to easily reload.
48    pub fn save_config<DB: SettingsStore>(&self, store: &DB) -> Result<(), anyhow::Error> {
49        store.write_bin(MPOOL_CONFIG_KEY, &fvm_ipld_encoding::to_vec(&self)?)
50    }
51
52    /// Returns the low limit capacity of messages to allocate.
53    pub fn size_limit_low(&self) -> i64 {
54        self.size_limit_low
55    }
56
57    /// Returns slice of [Address]es to prioritize when selecting messages.
58    pub fn priority_addrs(&self) -> &[Address] {
59        &self.priority_addrs
60    }
61}
62
63impl MpoolConfig {
64    /// Load `config` from store, if exists. If there is no `config`, uses
65    /// default.
66    pub fn load_config<DB: SettingsStore>(store: &DB) -> Result<Self, anyhow::Error> {
67        match store.read_bin(MPOOL_CONFIG_KEY)? {
68            Some(v) => Ok(from_slice_with_fallback(&v)?),
69            None => Ok(Default::default()),
70        }
71    }
72}