Skip to content

Commit

Permalink
remove unnecessary CrossMessageValidationOutcome type.
Browse files Browse the repository at this point in the history
  • Loading branch information
raulk committed Jan 2, 2025
1 parent 5a92d52 commit 12088f8
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 62 deletions.
7 changes: 2 additions & 5 deletions contracts/contracts/errors/IPCErrors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ error AlreadyRegisteredSubnet();
error AlreadyInSet();
error CannotConfirmFutureChanges();
error CannotReleaseZero();
error CannotSendCrossMsgToItself();
error CheckpointAlreadyExists();
error BatchAlreadyExists();
error MaxMsgsPerBatchExceeded();
Expand Down Expand Up @@ -84,17 +83,15 @@ error MissingActivityCommitment();
error ValidatorAlreadyClaimed();
error InvalidActivityProof();
error NotOwner();
error CommonParentDoesNotExist();
error UnroutableMessage(string reason);

enum InvalidXnetMessageReason {
Sender,
DstSubnet,
Nonce,
Value,
Kind,
CannotSendToItself,
CommonParentNotExist,
ReflexiveSend,
NoRoute,
IncompatibleSupplySource
}

Expand Down
17 changes: 5 additions & 12 deletions contracts/contracts/gateway/GatewayMessengerFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import {GatewayActorModifiers} from "../lib/LibGatewayActorStorage.sol";
import {IpcEnvelope, CallMsg, IpcMsgKind} from "../structs/CrossNet.sol";
import {IPCMsgType} from "../enums/IPCMsgType.sol";
import {Subnet, SubnetID, AssetKind, IPCAddress, Asset} from "../structs/Subnet.sol";
import {InvalidXnetMessage, InvalidXnetMessageReason, CannotSendCrossMsgToItself, MethodNotAllowed, UnroutableMessage} from "../errors/IPCErrors.sol";
import {InvalidXnetMessage, InvalidXnetMessageReason, MethodNotAllowed} from "../errors/IPCErrors.sol";
import {SubnetIDHelper} from "../lib/SubnetIDHelper.sol";
import {LibGateway, CrossMessageValidationOutcome} from "../lib/LibGateway.sol";
import {LibGateway} from "../lib/LibGateway.sol";
import {FilAddress} from "fevmate/contracts/utils/FilAddress.sol";
import {AssetHelper} from "../lib/AssetHelper.sol";
import {CrossMsgHelper} from "../lib/CrossMsgHelper.sol";
Expand Down Expand Up @@ -61,16 +61,9 @@ contract GatewayMessengerFacet is GatewayActorModifiers {
nonce: 0 // nonce will be updated by LibGateway.commitValidatedCrossMessage
});

(CrossMessageValidationOutcome outcome, IPCMsgType applyType) = committed.validateCrossMessage();

if (outcome != CrossMessageValidationOutcome.Valid) {
if (outcome == CrossMessageValidationOutcome.InvalidDstSubnet) {
revert InvalidXnetMessage(InvalidXnetMessageReason.DstSubnet);
} else if (outcome == CrossMessageValidationOutcome.CannotSendToItself) {
revert CannotSendCrossMsgToItself();
} else if (outcome == CrossMessageValidationOutcome.CommonParentNotExist) {
revert UnroutableMessage("no common parent");
}
(bool valid, InvalidXnetMessageReason reason, IPCMsgType applyType) = committed.validateCrossMessage();
if (!valid) {
revert InvalidXnetMessage(reason);
}

if (applyType == IPCMsgType.TopDown) {
Expand Down
5 changes: 3 additions & 2 deletions contracts/contracts/lib/CrossMsgHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import {IPCMsgType} from "../enums/IPCMsgType.sol";
import {SubnetID, IPCAddress} from "../structs/Subnet.sol";
import {SubnetIDHelper} from "../lib/SubnetIDHelper.sol";
import {FvmAddressHelper} from "../lib/FvmAddressHelper.sol";
import {LibGateway, CrossMessageValidationOutcome} from "../lib/LibGateway.sol";
import {LibGateway} from "../lib/LibGateway.sol";
import {FvmAddress} from "../structs/FvmAddress.sol";
import {FilAddress} from "fevmate/contracts/utils/FilAddress.sol";
import {Address} from "@openzeppelin/contracts/utils/Address.sol";
import {Asset} from "../structs/Subnet.sol";
import {AssetHelper} from "./AssetHelper.sol";
import {IIpcHandler} from "../../sdk/interfaces/IIpcHandler.sol";
import "../errors/IPCErrors.sol";

/// @title Helper library for manipulating IpcEnvelope-related structs
library CrossMsgHelper {
Expand Down Expand Up @@ -224,7 +225,7 @@ library CrossMsgHelper {
return true;
}

function validateCrossMessage(IpcEnvelope memory crossMsg) internal view returns (CrossMessageValidationOutcome, IPCMsgType) {
function validateCrossMessage(IpcEnvelope memory crossMsg) internal view returns (bool, InvalidXnetMessageReason, IPCMsgType) {
return LibGateway.checkCrossMessage(crossMsg);
}
}
56 changes: 16 additions & 40 deletions contracts/contracts/lib/LibGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,14 @@ import {SubnetID, Subnet, AssetKind, Asset} from "../structs/Subnet.sol";
import {SubnetActorGetterFacet} from "../subnet/SubnetActorGetterFacet.sol";
import {CallMsg, IpcMsgKind, IpcEnvelope, OutcomeType, BottomUpMsgBatch, BottomUpMsgBatch, BottomUpCheckpoint, ParentFinality} from "../structs/CrossNet.sol";
import {Membership} from "../structs/Subnet.sol";
import {CannotSendCrossMsgToItself, MethodNotAllowed, MaxMsgsPerBatchExceeded, InvalidXnetMessage ,OldConfigurationNumber, NotRegisteredSubnet, InvalidActorAddress, ParentFinalityAlreadyCommitted, InvalidXnetMessageReason, UnroutableMessage} from "../errors/IPCErrors.sol";
import "../errors/IPCErrors.sol";
import {CrossMsgHelper} from "../lib/CrossMsgHelper.sol";
import {FilAddress} from "fevmate/contracts/utils/FilAddress.sol";
import {SubnetIDHelper} from "../lib/SubnetIDHelper.sol";
import {AssetHelper} from "../lib/AssetHelper.sol";
import {ISubnetActor} from "../interfaces/ISubnetActor.sol";
import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";

// Validation outcomes for cross messages
enum CrossMessageValidationOutcome {
Valid,
InvalidDstSubnet,
CannotSendToItself,
CommonParentNotExist,
IncompatibleSupplySource
}

library LibGateway {
using SubnetIDHelper for SubnetID;
using CrossMsgHelper for IpcEnvelope;
Expand Down Expand Up @@ -447,12 +438,12 @@ library LibGateway {
// should increase the appliedNonce to allow the execution of the next message
// of the batch (this is way we have this after the nonce logic).
if (!crossMsg.to.subnetId.equals(s.networkName)) {
CrossMessageValidationOutcome outcome = validateCrossMessage(crossMsg);
if (outcome != CrossMessageValidationOutcome.Valid) {
(bool valid, InvalidXnetMessageReason reason) = validateCrossMessage(crossMsg);
if (!valid) {
sendReceipt(
crossMsg,
OutcomeType.SystemErr,
abi.encodeWithSelector(InvalidXnetMessage.selector, validationOutcomeToInvalidXnetMsgReason(outcome))
abi.encodeWithSelector(InvalidXnetMessage.selector, reason)
);
return;
}
Expand Down Expand Up @@ -608,24 +599,24 @@ library LibGateway {
}

/// @notice Validates a cross message before committing it.
function validateCrossMessage(IpcEnvelope memory envelope) internal view returns (CrossMessageValidationOutcome) {
(CrossMessageValidationOutcome outcome, ) = checkCrossMessage(envelope);
return outcome;
function validateCrossMessage(IpcEnvelope memory envelope) internal view returns (bool, InvalidXnetMessageReason) {
(bool valid, InvalidXnetMessageReason reason, ) = checkCrossMessage(envelope);
return (valid, reason);
}

/// @notice Validates a cross message and returns the applyType if the message is valid
function checkCrossMessage(IpcEnvelope memory envelope) internal view returns (CrossMessageValidationOutcome, IPCMsgType applyType) {
function checkCrossMessage(IpcEnvelope memory envelope) internal view returns (bool valid, InvalidXnetMessageReason reason, IPCMsgType applyType) {
SubnetID memory toSubnetId = envelope.to.subnetId;
if (toSubnetId.isEmpty()) {
return (CrossMessageValidationOutcome.InvalidDstSubnet, applyType);
return (false, InvalidXnetMessageReason.DstSubnet, applyType);
}

GatewayActorStorage storage s = LibGatewayActorStorage.appStorage();
SubnetID memory currentNetwork = s.networkName;

// We cannot send a cross message to the same subnet.
if (toSubnetId.equals(currentNetwork)) {
return (CrossMessageValidationOutcome.CannotSendToItself, applyType);
return (false, InvalidXnetMessageReason.ReflexiveSend, applyType);
}

// Lowest common ancestor subnet
Expand All @@ -637,23 +628,23 @@ library LibGateway {
if (applyType == IPCMsgType.TopDown || isLCA) {
(bool foundChildSubnetId, SubnetID memory childSubnetId) = toSubnetId.down(currentNetwork);
if (!foundChildSubnetId) {
return (CrossMessageValidationOutcome.InvalidDstSubnet, applyType);
return (false, InvalidXnetMessageReason.DstSubnet, applyType);
}

(bool foundSubnet,) = LibGateway.getSubnet(childSubnetId);
if (!foundSubnet) {
return (CrossMessageValidationOutcome.InvalidDstSubnet, applyType);
return (false, InvalidXnetMessageReason.DstSubnet, applyType);
}
} else {
SubnetID memory commonParent = toSubnetId.commonParent(currentNetwork);
if (commonParent.isEmpty()) {
return (CrossMessageValidationOutcome.CommonParentNotExist, applyType);
return (false, InvalidXnetMessageReason.NoRoute, applyType);
}
}

// starting/ending subnet, no need check supply sources
if (envelope.from.subnetId.equals(currentNetwork) || envelope.to.subnetId.equals(currentNetwork)) {
return (CrossMessageValidationOutcome.Valid, applyType);
return (true, reason, applyType);
}

bool supplySourcesCompatible = checkSubnetsSupplyCompatible({
Expand All @@ -665,25 +656,10 @@ library LibGateway {
});

if (!supplySourcesCompatible) {
return (CrossMessageValidationOutcome.IncompatibleSupplySource, applyType);
}

return (CrossMessageValidationOutcome.Valid, applyType);
}

// Function to map CrossMessageValidationOutcome to InvalidXnetMessageReason
function validationOutcomeToInvalidXnetMsgReason(CrossMessageValidationOutcome outcome) internal pure returns (InvalidXnetMessageReason) {
if (outcome == CrossMessageValidationOutcome.InvalidDstSubnet) {
return InvalidXnetMessageReason.DstSubnet;
} else if (outcome == CrossMessageValidationOutcome.CannotSendToItself) {
return InvalidXnetMessageReason.CannotSendToItself;
} else if (outcome == CrossMessageValidationOutcome.CommonParentNotExist) {
return InvalidXnetMessageReason.CommonParentNotExist;
} else if (outcome == CrossMessageValidationOutcome.IncompatibleSupplySource) {
return InvalidXnetMessageReason.IncompatibleSupplySource;
return (false, InvalidXnetMessageReason.IncompatibleSupplySource, applyType);
}

revert("Unhandled validation outcome");
return (true, reason, applyType);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion contracts/test/integration/GatewayDiamond.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ contract GatewayActorDiamondTest is Test, IntegrationTestBase, SubnetWithNativeT
registerSubnet(DEFAULT_COLLATERAL_AMOUNT, caller);
SubnetID memory destinationSubnet = gatewayDiamond.getter().getNetworkName();

vm.expectRevert(CannotSendCrossMsgToItself.selector);
vm.expectRevert(abi.encodeWithSelector(InvalidXnetMessage.selector, InvalidXnetMessageReason.ReflexiveSend));
gatewayDiamond.messenger().sendContractXnetMessage{value: 1}(
TestUtils.newXnetCallMsg(
IPCAddress({
Expand Down
4 changes: 2 additions & 2 deletions contracts/test/integration/MultiSubnet.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ contract MultiSubnetTest is Test, IntegrationTestBase {
address tokenAddress,
address rootGatewayAddress,
SubnetID memory rootSubnetName
) internal returns (TestSubnetDefinition memory tokenSubnet) {
) internal returns (TestSubnetDefinition memory) {
SubnetActorDiamond rootTokenSubnetActor = createSubnetActor(
defaultSubnetActorParamsWith(rootGatewayAddress, rootSubnetName, tokenAddress)
);
Expand All @@ -124,7 +124,7 @@ contract MultiSubnetTest is Test, IntegrationTestBase {
SubnetID memory tokenSubnetName = SubnetID({root: ROOTNET_CHAINID, route: tokenSubnetPath});
GatewayDiamond tokenSubnetGateway = createGatewayDiamond(gatewayParams(tokenSubnetName));

tokenSubnet = TestSubnetDefinition({
return TestSubnetDefinition({
gateway: tokenSubnetGateway,
gatewayAddr: address(tokenSubnetGateway),
id: tokenSubnetName,
Expand Down

0 comments on commit 12088f8

Please sign in to comment.