recall_ipc_api 0.1.2

IPC common types and utils
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// Copyright 2022-2024 Protocol Labs
// SPDX-License-Identifier: MIT

//! Type conversion for IPC Agent struct with solidity contract struct

use crate::address::IPCAddress;
use crate::checkpoint::BottomUpMsgBatch;
use crate::checkpoint::{consensus, BottomUpCheckpoint, CompressedActivityRollup};
use crate::cross::{IpcEnvelope, IpcMsgKind};
use crate::staking::StakingChange;
use crate::staking::StakingChangeRequest;
use crate::subnet::{Asset, AssetKind};
use crate::subnet_id::SubnetID;
use crate::{eth_to_fil_amount, ethers_address_to_fil_address};
use anyhow::anyhow;
use ethers::types::U256;
use fvm_shared::address::{Address, Payload};
use fvm_shared::clock::ChainEpoch;
use fvm_shared::econ::TokenAmount;
use recall_ipc_actors_abis::{
    checkpointing_facet, gateway_getter_facet, gateway_manager_facet, gateway_messenger_facet,
    lib_gateway, register_subnet_facet, subnet_actor_activity_facet,
    subnet_actor_checkpointing_facet, subnet_actor_diamond, subnet_actor_getter_facet,
    top_down_finality_facet, xnet_messaging_facet,
};

/// The type conversion for IPC structs to evm solidity contracts. We need this convenient macro because
/// the abigen is creating the same struct but under different modules. This save a lot of
/// code.
macro_rules! base_type_conversion {
    ($module:ident) => {
        impl TryFrom<&SubnetID> for $module::SubnetID {
            type Error = anyhow::Error;

            fn try_from(subnet: &SubnetID) -> Result<Self, Self::Error> {
                Ok($module::SubnetID {
                    root: subnet.root_id(),
                    route: subnet_id_to_evm_addresses(subnet)?,
                })
            }
        }

        impl TryFrom<$module::SubnetID> for SubnetID {
            type Error = anyhow::Error;

            fn try_from(value: $module::SubnetID) -> Result<Self, Self::Error> {
                let children = value
                    .route
                    .iter()
                    .map(ethers_address_to_fil_address)
                    .collect::<anyhow::Result<Vec<_>>>()?;
                Ok(SubnetID::new(value.root, children))
            }
        }
    };
}

/// Implement the cross network message types. To use this macro, make sure the $module has already
/// implemented the base types.
macro_rules! cross_msg_types {
    ($module:ident) => {
        impl TryFrom<IPCAddress> for $module::Ipcaddress {
            type Error = anyhow::Error;

            fn try_from(value: IPCAddress) -> Result<Self, Self::Error> {
                Ok($module::Ipcaddress {
                    subnet_id: $module::SubnetID::try_from(&value.subnet()?)?,
                    raw_address: $module::FvmAddress::try_from(value.raw_addr()?)?,
                })
            }
        }

        impl TryFrom<$module::Ipcaddress> for IPCAddress {
            type Error = anyhow::Error;

            fn try_from(value: $module::Ipcaddress) -> Result<Self, Self::Error> {
                let addr = Address::try_from(value.raw_address)?;
                let i = IPCAddress::new(&SubnetID::try_from(value.subnet_id)?, &addr)?;
                Ok(i)
            }
        }

        impl TryFrom<IpcEnvelope> for $module::IpcEnvelope {
            type Error = anyhow::Error;

            fn try_from(value: IpcEnvelope) -> Result<Self, Self::Error> {
                let val = fil_to_eth_amount(&value.value)?;

                let c = $module::IpcEnvelope {
                    kind: value.kind as u8,
                    from: $module::Ipcaddress::try_from(value.from).map_err(|e| {
                        anyhow!("cannot convert `from` ipc address msg due to: {e:}")
                    })?,
                    to: $module::Ipcaddress::try_from(value.to)
                        .map_err(|e| anyhow!("cannot convert `to`` ipc address due to: {e:}"))?,
                    value: val,
                    local_nonce: value.local_nonce,
                    original_nonce: value.original_nonce,
                    message: ethers::core::types::Bytes::from(value.message),
                };
                Ok(c)
            }
        }

        impl TryFrom<$module::IpcEnvelope> for IpcEnvelope {
            type Error = anyhow::Error;

            fn try_from(value: $module::IpcEnvelope) -> Result<Self, Self::Error> {
                let s = IpcEnvelope {
                    from: IPCAddress::try_from(value.from)?,
                    to: IPCAddress::try_from(value.to)?,
                    value: eth_to_fil_amount(&value.value)?,
                    kind: IpcMsgKind::try_from(value.kind)?,
                    message: value.message.to_vec(),
                    local_nonce: value.local_nonce,
                    original_nonce: value.original_nonce,
                };
                Ok(s)
            }
        }
    };
}

/// The type conversion between different bottom up checkpoint definition in ethers and sdk
macro_rules! bottom_up_checkpoint_conversion {
    ($module:ident) => {
        impl TryFrom<consensus::AggregatedStats> for $module::AggregatedStats {
            type Error = anyhow::Error;

            fn try_from(c: consensus::AggregatedStats) -> Result<Self, Self::Error> {
                Ok($module::AggregatedStats {
                    total_active_validators: c.total_active_validators,
                    total_num_blocks_committed: c.total_num_blocks_committed,
                })
            }
        }

        impl TryFrom<CompressedActivityRollup> for $module::CompressedActivityRollup {
            type Error = anyhow::Error;

            fn try_from(c: CompressedActivityRollup) -> Result<Self, Self::Error> {
                Ok($module::CompressedActivityRollup {
                    consensus: c.consensus.try_into()?,
                })
            }
        }

        impl From<$module::CompressedActivityRollup> for CompressedActivityRollup {
            fn from(value: $module::CompressedActivityRollup) -> Self {
                CompressedActivityRollup {
                    consensus: consensus::CompressedSummary {
                        stats: consensus::AggregatedStats {
                            total_active_validators: value.consensus.stats.total_active_validators,
                            total_num_blocks_committed: value
                                .consensus
                                .stats
                                .total_num_blocks_committed,
                        },
                        data_root_commitment: value.consensus.data_root_commitment.to_vec(),
                    },
                }
            }
        }

        impl TryFrom<consensus::CompressedSummary> for $module::CompressedSummary {
            type Error = anyhow::Error;

            fn try_from(c: consensus::CompressedSummary) -> Result<Self, Self::Error> {
                Ok($module::CompressedSummary {
                    stats: c
                        .stats
                        .try_into()
                        .map_err(|_| anyhow!("cannot convert aggregated stats"))?,
                    data_root_commitment: c
                        .data_root_commitment
                        .try_into()
                        .map_err(|_| anyhow!("cannot convert bytes32"))?,
                })
            }
        }

        impl TryFrom<BottomUpCheckpoint> for $module::BottomUpCheckpoint {
            type Error = anyhow::Error;

            fn try_from(checkpoint: BottomUpCheckpoint) -> Result<Self, Self::Error> {
                Ok($module::BottomUpCheckpoint {
                    subnet_id: $module::SubnetID::try_from(&checkpoint.subnet_id)?,
                    block_height: ethers::core::types::U256::from(checkpoint.block_height),
                    block_hash: vec_to_bytes32(checkpoint.block_hash)?,
                    next_configuration_number: checkpoint.next_configuration_number,
                    msgs: checkpoint
                        .msgs
                        .into_iter()
                        .map($module::IpcEnvelope::try_from)
                        .collect::<Result<Vec<_>, _>>()?,
                    activity: checkpoint.activity_rollup.try_into()?,
                })
            }
        }

        impl TryFrom<$module::BottomUpCheckpoint> for BottomUpCheckpoint {
            type Error = anyhow::Error;

            fn try_from(value: $module::BottomUpCheckpoint) -> Result<Self, Self::Error> {
                Ok(BottomUpCheckpoint {
                    subnet_id: SubnetID::try_from(value.subnet_id)?,
                    block_height: value.block_height.as_u128() as ChainEpoch,
                    block_hash: value.block_hash.to_vec(),
                    next_configuration_number: value.next_configuration_number,
                    msgs: value
                        .msgs
                        .into_iter()
                        .map(IpcEnvelope::try_from)
                        .collect::<Result<Vec<_>, _>>()?,
                    activity_rollup: value.activity.into(),
                })
            }
        }
    };
}

/// The type conversion between different bottom up message batch definition in ethers and sdk
macro_rules! bottom_up_msg_batch_conversion {
    ($module:ident) => {
        impl TryFrom<BottomUpMsgBatch> for $module::BottomUpMsgBatch {
            type Error = anyhow::Error;

            fn try_from(batch: BottomUpMsgBatch) -> Result<Self, Self::Error> {
                Ok($module::BottomUpMsgBatch {
                    subnet_id: $module::SubnetID::try_from(&batch.subnet_id)?,
                    block_height: ethers::core::types::U256::from(batch.block_height),
                    msgs: batch
                        .msgs
                        .into_iter()
                        .map($module::IpcEnvelope::try_from)
                        .collect::<Result<Vec<_>, _>>()?,
                })
            }
        }
    };
}

/// The type conversion between different asset token types
macro_rules! asset_conversion {
    ($module:ident) => {
        impl TryFrom<Asset> for $module::Asset {
            type Error = anyhow::Error;

            fn try_from(value: Asset) -> Result<Self, Self::Error> {
                let token_address = if let Some(token_address) = value.token_address {
                    payload_to_evm_address(token_address.payload())?
                } else {
                    ethers::types::Address::zero()
                };

                Ok(Self {
                    kind: value.kind as u8,
                    token_address,
                })
            }
        }

        impl TryFrom<$module::Asset> for Asset {
            type Error = anyhow::Error;

            fn try_from(value: $module::Asset) -> Result<Self, Self::Error> {
                let token_address = if value.token_address == ethers::types::Address::zero() {
                    None
                } else {
                    Some(ethers_address_to_fil_address(&value.token_address)?)
                };

                Ok(Self {
                    kind: AssetKind::try_from(value.kind)?,
                    token_address,
                })
            }
        }
    };
}

base_type_conversion!(xnet_messaging_facet);
base_type_conversion!(subnet_actor_getter_facet);
base_type_conversion!(gateway_manager_facet);
base_type_conversion!(subnet_actor_checkpointing_facet);
base_type_conversion!(gateway_getter_facet);
base_type_conversion!(gateway_messenger_facet);
base_type_conversion!(lib_gateway);
base_type_conversion!(subnet_actor_activity_facet);
base_type_conversion!(checkpointing_facet);

cross_msg_types!(gateway_getter_facet);
cross_msg_types!(xnet_messaging_facet);
cross_msg_types!(gateway_messenger_facet);
cross_msg_types!(lib_gateway);
cross_msg_types!(subnet_actor_checkpointing_facet);
cross_msg_types!(checkpointing_facet);

bottom_up_checkpoint_conversion!(checkpointing_facet);
bottom_up_checkpoint_conversion!(gateway_getter_facet);
bottom_up_checkpoint_conversion!(subnet_actor_checkpointing_facet);
bottom_up_msg_batch_conversion!(gateway_getter_facet);

asset_conversion!(subnet_actor_diamond);
asset_conversion!(register_subnet_facet);
asset_conversion!(subnet_actor_getter_facet);

impl TryFrom<u8> for AssetKind {
    type Error = anyhow::Error;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(AssetKind::Native),
            1 => Ok(AssetKind::ERC20),
            _ => Err(anyhow!("invalid kind {value}")),
        }
    }
}

/// Convert the ipc SubnetID type to a vec of evm addresses. It extracts all the children addresses
/// in the subnet id and turns them as a vec of evm addresses.
pub fn subnet_id_to_evm_addresses(
    subnet: &SubnetID,
) -> anyhow::Result<Vec<ethers::types::Address>> {
    let children = subnet.children();
    children
        .iter()
        .map(|addr| payload_to_evm_address(addr.payload()))
        .collect::<anyhow::Result<_>>()
}

/// Util function to convert Fil address payload to evm address. Only delegated address is supported.
pub fn payload_to_evm_address(payload: &Payload) -> anyhow::Result<ethers::types::Address> {
    match payload {
        Payload::Delegated(delegated) => {
            let slice = delegated.subaddress();
            Ok(ethers::types::Address::from_slice(&slice[0..20]))
        }
        _ => Err(anyhow!("address provided is not delegated")),
    }
}

/// Converts a Fil TokenAmount into an ethers::U256 amount.
pub fn fil_to_eth_amount(amount: &TokenAmount) -> anyhow::Result<U256> {
    let str = amount.atto().to_string();
    Ok(U256::from_dec_str(&str)?)
}

impl TryFrom<StakingChange> for top_down_finality_facet::StakingChange {
    type Error = anyhow::Error;

    fn try_from(value: StakingChange) -> Result<Self, Self::Error> {
        Ok(top_down_finality_facet::StakingChange {
            op: value.op as u8,
            payload: ethers::core::types::Bytes::from(value.payload),
            validator: payload_to_evm_address(value.validator.payload())?,
        })
    }
}

impl TryFrom<StakingChangeRequest> for top_down_finality_facet::StakingChangeRequest {
    type Error = anyhow::Error;

    fn try_from(value: StakingChangeRequest) -> Result<Self, Self::Error> {
        Ok(top_down_finality_facet::StakingChangeRequest {
            change: top_down_finality_facet::StakingChange::try_from(value.change)?,
            configuration_number: value.configuration_number,
        })
    }
}

pub fn vec_to_bytes32(v: Vec<u8>) -> anyhow::Result<[u8; 32]> {
    if v.len() != 32 {
        return Err(anyhow!("invalid length"));
    }

    let mut r = [0u8; 32];
    r.copy_from_slice(&v);

    Ok(r)
}

#[cfg(test)]
mod tests {
    use crate::evm::subnet_id_to_evm_addresses;
    use crate::subnet_id::SubnetID;
    use fvm_shared::address::Address;
    use recall_ipc_types::EthAddress;
    use std::str::FromStr;

    #[test]
    fn test_subnet_id_to_evm_addresses() {
        let eth_addr = EthAddress::from_str("0x0000000000000000000000000000000000000000").unwrap();
        let addr = Address::from(eth_addr);
        let addr2 = Address::from_str("f410ffzyuupbyl2uiucmzr3lu3mtf3luyknthaz4xsrq").unwrap();

        let id = SubnetID::new(0, vec![addr, addr2]);

        let addrs = subnet_id_to_evm_addresses(&id).unwrap();

        let a =
            ethers::types::Address::from_str("0x0000000000000000000000000000000000000000").unwrap();
        let b =
            ethers::types::Address::from_str("0x2e714a3c385ea88a09998ed74db265dae9853667").unwrap();

        assert_eq!(addrs, vec![a, b]);
    }
}