Skip to content

Commit

Permalink
Implement mailbox process
Browse files Browse the repository at this point in the history
  • Loading branch information
yorhodes committed Dec 9, 2022
1 parent be2c3b4 commit dc7cf39
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
3 changes: 2 additions & 1 deletion hyperlane-mailbox/Forc.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
[project]
authors = ["Trevor Porter"]
authors = ["Trevor Porter", "Yorke Rhodes"]
entry = "main.sw"
license = "Apache-2.0"
name = "hyperlane-mailbox"

[dependencies]
hyperlane_message = { path = "../hyperlane-message" }
merkle = { path = "../merkle" }
multisig = { path = "../multisig-ism" }
12 changes: 12 additions & 0 deletions hyperlane-mailbox/src/interface.sw
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
library interface;

dep message;

use message::Message;
use multisig::MultisigMetadata;

abi Mailbox {
/// Dispatches a message to the destination domain and recipient.
/// Returns the message's ID.
Expand All @@ -12,6 +17,9 @@ abi Mailbox {
#[storage(read, write)]
fn dispatch(destination_domain: u32, recipient: b256, message_body: Vec<u8>) -> b256;

#[storage(read, write)]
fn process(metadata: MultisigMetadata, message: Message);

/// Returns the number of inserted leaves (i.e. messages) in the merkle tree.
#[storage(read)]
fn count() -> u32;
Expand All @@ -25,3 +33,7 @@ abi Mailbox {
#[storage(read)]
fn latest_checkpoint() -> (b256, u32);
}

abi MessageRecipient {
fn handle(origin: u32, sender: b256, message: Vec<u8>);
}
24 changes: 23 additions & 1 deletion hyperlane-mailbox/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ use std::{
logging::log,
};

use interface::Mailbox;
use interface::{Mailbox, MessageRecipient};
use merkle::StorageMerkleTree;
use hyperlane_message::EncodedMessage;

use multisig::{MultisigIsm, MultisigMetadata};

// Sway doesn't allow pow in a const.
// Equal to 2 KiB, or 2 * (2 ** 10).
const MAX_MESSAGE_BODY_BYTES: u64 = 2048;
Expand All @@ -25,6 +27,8 @@ const LOCAL_DOMAIN: u32 = 0x6675656cu32;
storage {
// A merkle tree that includes outbound message IDs as leaves.
merkle_tree: StorageMerkleTree = StorageMerkleTree {},
delivered: StorageMap<b256, bool> = StorageMap {},
default_ism: ContractId = ContractId::default(),
}

impl Mailbox for Contract {
Expand Down Expand Up @@ -64,6 +68,24 @@ impl Mailbox for Contract {
message_id
}

#[storage(read, write)]
fn process(metadata: MultisigMetadata, message: Message) {
require(message.version == VERSION, "!version");
require(message.destination == LOCAL_DOMAIN, "!destination");

let id = message.id();
require(storage.delivered.get(id) == false, "delivered");
storage.delivered.insert(id, true);

// TODO: defer to message.recipient.ism
require(storage.default_ism.verify(metadata, message), "!module");
abi(MessageRecipient, message.recipient).handle(message.origin, message.sender, message.body);

// TODO: investigate how to log dynamically sized data (because of the message body).
// https://github.com/hyperlane-xyz/fuel-contracts/issues/3
log(message);
}

/// Returns the number of inserted leaves (i.e. messages) in the merkle tree.
#[storage(read)]
fn count() -> u32 {
Expand Down

0 comments on commit dc7cf39

Please sign in to comment.