This repository has been archived by the owner on Jan 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGatewayMessengerFacet.sol
125 lines (104 loc) · 4.88 KB
/
GatewayMessengerFacet.sol
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
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.19;
import {GatewayActorModifiers} from "../lib/LibGatewayActorStorage.sol";
import {BURNT_FUNDS_ACTOR} from "../constants/Constants.sol";
import {CrossMsg, StorableMsg} from "../structs/Checkpoint.sol";
import {IPCMsgType} from "../enums/IPCMsgType.sol";
import {SubnetID} from "../structs/Subnet.sol";
import {InvalidCrossMsgFromSubnet, InvalidCrossMsgDstSubnet, CannotSendCrossMsgToItself, InvalidCrossMsgValue} from "../errors/IPCErrors.sol";
import {SubnetIDHelper} from "../lib/SubnetIDHelper.sol";
import {LibGateway} from "../lib/LibGateway.sol";
import {StorableMsgHelper} from "../lib/StorableMsgHelper.sol";
import {FilAddress} from "fevmate/utils/FilAddress.sol";
contract GatewayMessengerFacet is GatewayActorModifiers {
using FilAddress for address payable;
using SubnetIDHelper for SubnetID;
using StorableMsgHelper for StorableMsg;
/**
* @dev sends an arbitrary cross-message from the local subnet to the destination subnet.
*
* IMPORTANT: `msg.value` is expected to equal to the value sent in `crossMsg.value` plus the cross-messaging fee.
*
* @param crossMsg - a cross-message to send
*/
function sendCrossMessage(CrossMsg calldata crossMsg) external payable validFee(crossMsg.message.fee) {
if (crossMsg.message.value != msg.value - crossMsg.message.fee) {
revert InvalidCrossMsgValue();
}
// We disregard the "to" of the message that will be verified in the _commitCrossMessage().
// The caller is the one set as the "from" of the message
if (!crossMsg.message.from.subnetId.equals(s.networkName)) {
revert InvalidCrossMsgFromSubnet();
}
// commit cross-message for propagation
(bool shouldBurn, ) = _commitCrossMessage(crossMsg);
_crossMsgSideEffects({v: crossMsg.message.value, shouldBurn: shouldBurn});
}
/**
* @dev propagates the populated cross net message for the given cid
* @param msgCid - the cid of the cross-net message
*/
function propagate(bytes32 msgCid) external payable {
CrossMsg storage crossMsg = s.postbox[msgCid];
validateFee(crossMsg.message.fee);
(bool shouldBurn, ) = _commitCrossMessage(crossMsg);
// We must delete the message first to prevent potential re-entrancies,
// and as the message is deleted and we don't have a reference to the object
// anymore, we need to pull the data from the message to trigger the side-effects.
uint256 v = crossMsg.message.value;
delete s.postbox[msgCid];
_crossMsgSideEffects({v: v, shouldBurn: shouldBurn});
uint256 feeRemainder = msg.value - s.minCrossMsgFee;
if (feeRemainder > 0) {
payable(msg.sender).sendValue(feeRemainder);
}
}
/**
* @dev Commit the cross message to storage. It outputs a flag signaling
* if the committed messages was bottom-up and some funds need to be
* burnt or if a top-down message fee needs to be distributed.
*
* It also validates that destination subnet ID is not empty
* and not equal to the current network.
*/
function _commitCrossMessage(
CrossMsg memory crossMessage
) internal returns (bool shouldBurn, bool shouldDistributeRewards) {
SubnetID memory to = crossMessage.message.to.subnetId;
if (to.isEmpty()) {
revert InvalidCrossMsgDstSubnet();
}
// destination is the current network, you are better off with a good old message, no cross needed
if (to.equals(s.networkName)) {
revert CannotSendCrossMsgToItself();
}
SubnetID memory from = crossMessage.message.from.subnetId;
IPCMsgType applyType = crossMessage.message.applyType(s.networkName);
// slither-disable-next-line uninitialized-local
bool shouldCommitBottomUp;
if (applyType == IPCMsgType.BottomUp) {
shouldCommitBottomUp = !to.commonParent(from).equals(s.networkName);
}
if (shouldCommitBottomUp) {
LibGateway.commitBottomUpMsg(crossMessage);
return (shouldBurn = crossMessage.message.value > 0, shouldDistributeRewards = false);
}
if (applyType == IPCMsgType.TopDown) {
++s.appliedTopDownNonce;
}
LibGateway.commitTopDownMsg(crossMessage);
return (shouldBurn = false, shouldDistributeRewards = true);
}
/**
* @dev Performs transaction side-effects from the commitment of a cross-net message. It burns funds
* and propagates the corresponding rewards.
*
* @param v - the value of the committed cross-net message
* @param shouldBurn - flag if the message should burn funds
*/
function _crossMsgSideEffects(uint256 v, bool shouldBurn) internal {
if (shouldBurn) {
payable(BURNT_FUNDS_ACTOR).sendValue(v);
}
}
}