Skip to main content

forest/message/
chain_message.rs

1// Copyright 2019-2026 ChainSafe Systems
2// SPDX-License-Identifier: Apache-2.0, MIT
3
4use super::*;
5use crate::message::signed_message::SignedMessage;
6use crate::shim::message::MethodNum;
7use crate::shim::{address::Address, econ::TokenAmount, message::Message};
8use ambassador::Delegate;
9use fvm_ipld_encoding::RawBytes;
10use get_size2::GetSize;
11use serde::{Deserialize, Serialize};
12use spire_enum::prelude::delegated_enum;
13use std::sync::Arc;
14
15/// `Enum` to encapsulate signed and unsigned messages. Useful when working with
16/// both types
17#[delegated_enum]
18#[derive(
19    Clone, Debug, Serialize, Deserialize, Hash, Eq, PartialEq, GetSize, derive_more::From, Delegate,
20)]
21#[delegate(MessageRead)]
22#[serde(untagged)]
23pub enum ChainMessage {
24    Unsigned(Arc<Message>),
25    Signed(Arc<SignedMessage>),
26}
27
28impl From<Message> for ChainMessage {
29    fn from(msg: Message) -> Self {
30        Arc::new(msg).into()
31    }
32}
33
34impl From<SignedMessage> for ChainMessage {
35    fn from(msg: SignedMessage) -> Self {
36        Arc::new(msg).into()
37    }
38}
39
40impl ChainMessage {
41    pub fn message(&self) -> &Message {
42        match self {
43            Self::Unsigned(m) => m,
44            Self::Signed(sm) => sm.message(),
45        }
46    }
47
48    pub fn cid(&self) -> cid::Cid {
49        delegate_chain_message!(self.cid())
50    }
51
52    /// Tests if a message is equivalent to another replacing message.
53    /// A replacing message is a message with a different CID,
54    /// any of Gas values, and different signature, but with all
55    /// other parameters matching (source/destination, nonce, parameters, etc.)
56    /// See <https://github.com/filecoin-project/lotus/blob/813d133c24295629ef442fc3aa60e6e6b2101226/chain/types/message.go#L138>
57    pub fn equal_call(&self, other: &Self) -> bool {
58        self.message().equal_call(other.message())
59    }
60}
61
62impl MessageReadWrite for ChainMessage {
63    fn set_gas_limit(&mut self, amount: u64) {
64        delegate_chain_message!(self => |i| Arc::make_mut(i).set_gas_limit(amount))
65    }
66
67    fn set_sequence(&mut self, sequence: u64) {
68        delegate_chain_message!(self => |i| Arc::make_mut(i).set_sequence(sequence))
69    }
70
71    fn set_gas_fee_cap(&mut self, cap: TokenAmount) {
72        delegate_chain_message!(self => |i| Arc::make_mut(i).set_gas_fee_cap(cap))
73    }
74
75    fn set_gas_premium(&mut self, prem: TokenAmount) {
76        delegate_chain_message!(self => |i| Arc::make_mut(i).set_gas_premium(prem))
77    }
78}