Skip to content

Commit

Permalink
Refactor mailbox contracts to use PDAs where appropriate.
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Sloboda committed Jan 12, 2023
1 parent 7f7a5aa commit c8290a9
Show file tree
Hide file tree
Showing 10 changed files with 872 additions and 392 deletions.
342 changes: 204 additions & 138 deletions rust/sealevel/client/src/main.rs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ mod test {

use std::{fs::File, io::Read, path::PathBuf};

use sha3::{Digest, digest::Update, Keccak256};
use sha3::{digest::Update, Digest, Keccak256};

// From ethers-rs
fn hash_message<S>(message: S) -> H256
Expand Down
172 changes: 137 additions & 35 deletions rust/sealevel/hyperlane-core/src/accumulator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ pub mod incremental;
pub mod merkle;

use crate::types::H256;
use sha3::{Digest, Keccak256};
use sha3::digest::Update;
use sha3::{Digest, Keccak256};

/// Tree depth
pub const TREE_DEPTH: usize = 32;
Expand All @@ -24,41 +24,143 @@ pub(super) fn hash_concat(left: impl AsRef<[u8]>, right: impl AsRef<[u8]>) -> H2

// Can't have writable static data in solana bpf so must pre-compute the table.
pub static ZERO_HASHES: [H256; TREE_DEPTH + 1] = [
H256([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
H256([173, 50, 40, 182, 118, 247, 211, 205, 66, 132, 165, 68, 63, 23, 241, 150, 43, 54, 228, 145, 179, 10, 64, 178, 64, 88, 73, 229, 151, 186, 95, 181]),
H256([180, 193, 25, 81, 149, 124, 111, 143, 100, 44, 74, 246, 28, 214, 178, 70, 64, 254, 198, 220, 127, 198, 7, 238, 130, 6, 169, 158, 146, 65, 13, 48]),
H256([33, 221, 185, 163, 86, 129, 92, 63, 172, 16, 38, 182, 222, 197, 223, 49, 36, 175, 186, 219, 72, 92, 155, 165, 163, 227, 57, 138, 4, 183, 186, 133]),
H256([229, 135, 105, 179, 42, 27, 234, 241, 234, 39, 55, 90, 68, 9, 90, 13, 31, 182, 100, 206, 45, 211, 88, 231, 252, 191, 183, 140, 38, 161, 147, 68]),
H256([14, 176, 30, 191, 201, 237, 39, 80, 12, 212, 223, 201, 121, 39, 45, 31, 9, 19, 204, 159, 102, 84, 13, 126, 128, 5, 129, 17, 9, 225, 207, 45]),
H256([136, 124, 34, 189, 135, 80, 211, 64, 22, 172, 60, 102, 181, 255, 16, 45, 172, 221, 115, 246, 176, 20, 231, 16, 181, 30, 128, 34, 175, 154, 25, 104]),
H256([255, 215, 1, 87, 228, 128, 99, 252, 51, 201, 122, 5, 15, 127, 100, 2, 51, 191, 100, 108, 201, 141, 149, 36, 198, 185, 43, 207, 58, 181, 111, 131]),
H256([152, 103, 204, 95, 127, 25, 107, 147, 186, 225, 226, 126, 99, 32, 116, 36, 69, 210, 144, 242, 38, 56, 39, 73, 139, 84, 254, 197, 57, 247, 86, 175]),
H256([206, 250, 212, 229, 8, 192, 152, 185, 167, 225, 216, 254, 177, 153, 85, 251, 2, 186, 150, 117, 88, 80, 120, 113, 9, 105, 211, 68, 15, 80, 84, 224]),
H256([249, 220, 62, 127, 224, 22, 224, 80, 239, 242, 96, 51, 79, 24, 165, 212, 254, 57, 29, 130, 9, 35, 25, 245, 150, 79, 46, 46, 183, 193, 195, 165]),
H256([248, 177, 58, 73, 226, 130, 246, 9, 195, 23, 168, 51, 251, 141, 151, 109, 17, 81, 124, 87, 29, 18, 33, 162, 101, 210, 90, 247, 120, 236, 248, 146]),
H256([52, 144, 198, 206, 235, 69, 10, 236, 220, 130, 226, 130, 147, 3, 29, 16, 199, 215, 59, 248, 94, 87, 191, 4, 26, 151, 54, 10, 162, 197, 217, 156]),
H256([193, 223, 130, 217, 196, 184, 116, 19, 234, 226, 239, 4, 143, 148, 180, 211, 85, 76, 234, 115, 217, 43, 15, 122, 249, 110, 2, 113, 198, 145, 226, 187]),
H256([92, 103, 173, 215, 198, 202, 243, 2, 37, 106, 222, 223, 122, 177, 20, 218, 10, 207, 232, 112, 212, 73, 163, 164, 137, 247, 129, 214, 89, 232, 190, 204]),
H256([218, 123, 206, 159, 78, 134, 24, 182, 189, 47, 65, 50, 206, 121, 140, 220, 122, 96, 231, 225, 70, 10, 114, 153, 227, 198, 52, 42, 87, 150, 38, 210]),
H256([39, 51, 229, 15, 82, 110, 194, 250, 25, 162, 43, 49, 232, 237, 80, 242, 60, 209, 253, 249, 76, 145, 84, 237, 58, 118, 9, 162, 241, 255, 152, 31]),
H256([225, 211, 181, 200, 7, 178, 129, 228, 104, 60, 198, 214, 49, 92, 249, 91, 154, 222, 134, 65, 222, 252, 179, 35, 114, 241, 193, 38, 227, 152, 239, 122]),
H256([90, 45, 206, 10, 138, 127, 104, 187, 116, 86, 15, 143, 113, 131, 124, 44, 46, 187, 203, 247, 255, 251, 66, 174, 24, 150, 241, 63, 124, 116, 121, 160]),
H256([180, 106, 40, 182, 245, 85, 64, 248, 148, 68, 246, 61, 224, 55, 142, 61, 18, 27, 224, 158, 6, 204, 157, 237, 28, 32, 230, 88, 118, 211, 106, 160]),
H256([198, 94, 150, 69, 100, 71, 134, 182, 32, 226, 221, 42, 214, 72, 221, 252, 191, 74, 126, 91, 26, 58, 78, 207, 231, 246, 70, 103, 163, 240, 183, 226]),
H256([244, 65, 133, 136, 237, 53, 162, 69, 140, 255, 235, 57, 185, 61, 38, 241, 141, 42, 177, 59, 220, 230, 174, 229, 142, 123, 153, 53, 158, 194, 223, 217]),
H256([90, 156, 22, 220, 0, 214, 239, 24, 183, 147, 58, 111, 141, 198, 92, 203, 85, 102, 113, 56, 119, 111, 125, 234, 16, 16, 112, 220, 135, 150, 227, 119]),
H256([77, 248, 79, 64, 174, 12, 130, 41, 208, 214, 6, 158, 92, 143, 57, 167, 194, 153, 103, 122, 9, 211, 103, 252, 123, 5, 227, 188, 56, 14, 230, 82]),
H256([205, 199, 37, 149, 247, 76, 123, 16, 67, 208, 225, 255, 186, 183, 52, 100, 140, 131, 141, 251, 5, 39, 217, 113, 182, 2, 188, 33, 108, 150, 25, 239]),
H256([10, 191, 90, 201, 116, 161, 237, 87, 244, 5, 10, 165, 16, 221, 156, 116, 245, 8, 39, 123, 57, 215, 151, 59, 178, 223, 204, 197, 238, 176, 97, 141]),
H256([184, 205, 116, 4, 111, 243, 55, 240, 167, 191, 44, 142, 3, 225, 15, 100, 44, 24, 134, 121, 141, 113, 128, 106, 177, 232, 136, 217, 229, 238, 135, 208]),
H256([131, 140, 86, 85, 203, 33, 198, 203, 131, 49, 59, 90, 99, 17, 117, 223, 244, 150, 55, 114, 204, 233, 16, 129, 136, 179, 74, 200, 124, 129, 196, 30]),
H256([102, 46, 228, 221, 45, 215, 178, 188, 112, 121, 97, 177, 230, 70, 196, 4, 118, 105, 220, 182, 88, 79, 13, 141, 119, 13, 175, 93, 126, 125, 235, 46]),
H256([56, 138, 178, 14, 37, 115, 209, 113, 168, 129, 8, 231, 157, 130, 14, 152, 242, 108, 11, 132, 170, 139, 47, 74, 164, 150, 141, 187, 129, 142, 163, 34]),
H256([147, 35, 124, 80, 186, 117, 238, 72, 95, 76, 34, 173, 242, 247, 65, 64, 11, 223, 141, 106, 156, 199, 223, 126, 202, 229, 118, 34, 22, 101, 215, 53]),
H256([132, 72, 129, 139, 180, 174, 69, 98, 132, 158, 148, 158, 23, 172, 22, 224, 190, 22, 104, 142, 21, 107, 92, 241, 94, 9, 140, 98, 124, 0, 86, 169]),
H256([39, 174, 91, 160, 141, 114, 145, 201, 108, 140, 189, 220, 193, 72, 191, 72, 166, 214, 140, 121, 116, 185, 67, 86, 245, 55, 84, 239, 97, 113, 215, 87]),
H256([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0,
]),
H256([
173, 50, 40, 182, 118, 247, 211, 205, 66, 132, 165, 68, 63, 23, 241, 150, 43, 54, 228, 145,
179, 10, 64, 178, 64, 88, 73, 229, 151, 186, 95, 181,
]),
H256([
180, 193, 25, 81, 149, 124, 111, 143, 100, 44, 74, 246, 28, 214, 178, 70, 64, 254, 198,
220, 127, 198, 7, 238, 130, 6, 169, 158, 146, 65, 13, 48,
]),
H256([
33, 221, 185, 163, 86, 129, 92, 63, 172, 16, 38, 182, 222, 197, 223, 49, 36, 175, 186, 219,
72, 92, 155, 165, 163, 227, 57, 138, 4, 183, 186, 133,
]),
H256([
229, 135, 105, 179, 42, 27, 234, 241, 234, 39, 55, 90, 68, 9, 90, 13, 31, 182, 100, 206,
45, 211, 88, 231, 252, 191, 183, 140, 38, 161, 147, 68,
]),
H256([
14, 176, 30, 191, 201, 237, 39, 80, 12, 212, 223, 201, 121, 39, 45, 31, 9, 19, 204, 159,
102, 84, 13, 126, 128, 5, 129, 17, 9, 225, 207, 45,
]),
H256([
136, 124, 34, 189, 135, 80, 211, 64, 22, 172, 60, 102, 181, 255, 16, 45, 172, 221, 115,
246, 176, 20, 231, 16, 181, 30, 128, 34, 175, 154, 25, 104,
]),
H256([
255, 215, 1, 87, 228, 128, 99, 252, 51, 201, 122, 5, 15, 127, 100, 2, 51, 191, 100, 108,
201, 141, 149, 36, 198, 185, 43, 207, 58, 181, 111, 131,
]),
H256([
152, 103, 204, 95, 127, 25, 107, 147, 186, 225, 226, 126, 99, 32, 116, 36, 69, 210, 144,
242, 38, 56, 39, 73, 139, 84, 254, 197, 57, 247, 86, 175,
]),
H256([
206, 250, 212, 229, 8, 192, 152, 185, 167, 225, 216, 254, 177, 153, 85, 251, 2, 186, 150,
117, 88, 80, 120, 113, 9, 105, 211, 68, 15, 80, 84, 224,
]),
H256([
249, 220, 62, 127, 224, 22, 224, 80, 239, 242, 96, 51, 79, 24, 165, 212, 254, 57, 29, 130,
9, 35, 25, 245, 150, 79, 46, 46, 183, 193, 195, 165,
]),
H256([
248, 177, 58, 73, 226, 130, 246, 9, 195, 23, 168, 51, 251, 141, 151, 109, 17, 81, 124, 87,
29, 18, 33, 162, 101, 210, 90, 247, 120, 236, 248, 146,
]),
H256([
52, 144, 198, 206, 235, 69, 10, 236, 220, 130, 226, 130, 147, 3, 29, 16, 199, 215, 59, 248,
94, 87, 191, 4, 26, 151, 54, 10, 162, 197, 217, 156,
]),
H256([
193, 223, 130, 217, 196, 184, 116, 19, 234, 226, 239, 4, 143, 148, 180, 211, 85, 76, 234,
115, 217, 43, 15, 122, 249, 110, 2, 113, 198, 145, 226, 187,
]),
H256([
92, 103, 173, 215, 198, 202, 243, 2, 37, 106, 222, 223, 122, 177, 20, 218, 10, 207, 232,
112, 212, 73, 163, 164, 137, 247, 129, 214, 89, 232, 190, 204,
]),
H256([
218, 123, 206, 159, 78, 134, 24, 182, 189, 47, 65, 50, 206, 121, 140, 220, 122, 96, 231,
225, 70, 10, 114, 153, 227, 198, 52, 42, 87, 150, 38, 210,
]),
H256([
39, 51, 229, 15, 82, 110, 194, 250, 25, 162, 43, 49, 232, 237, 80, 242, 60, 209, 253, 249,
76, 145, 84, 237, 58, 118, 9, 162, 241, 255, 152, 31,
]),
H256([
225, 211, 181, 200, 7, 178, 129, 228, 104, 60, 198, 214, 49, 92, 249, 91, 154, 222, 134,
65, 222, 252, 179, 35, 114, 241, 193, 38, 227, 152, 239, 122,
]),
H256([
90, 45, 206, 10, 138, 127, 104, 187, 116, 86, 15, 143, 113, 131, 124, 44, 46, 187, 203,
247, 255, 251, 66, 174, 24, 150, 241, 63, 124, 116, 121, 160,
]),
H256([
180, 106, 40, 182, 245, 85, 64, 248, 148, 68, 246, 61, 224, 55, 142, 61, 18, 27, 224, 158,
6, 204, 157, 237, 28, 32, 230, 88, 118, 211, 106, 160,
]),
H256([
198, 94, 150, 69, 100, 71, 134, 182, 32, 226, 221, 42, 214, 72, 221, 252, 191, 74, 126, 91,
26, 58, 78, 207, 231, 246, 70, 103, 163, 240, 183, 226,
]),
H256([
244, 65, 133, 136, 237, 53, 162, 69, 140, 255, 235, 57, 185, 61, 38, 241, 141, 42, 177, 59,
220, 230, 174, 229, 142, 123, 153, 53, 158, 194, 223, 217,
]),
H256([
90, 156, 22, 220, 0, 214, 239, 24, 183, 147, 58, 111, 141, 198, 92, 203, 85, 102, 113, 56,
119, 111, 125, 234, 16, 16, 112, 220, 135, 150, 227, 119,
]),
H256([
77, 248, 79, 64, 174, 12, 130, 41, 208, 214, 6, 158, 92, 143, 57, 167, 194, 153, 103, 122,
9, 211, 103, 252, 123, 5, 227, 188, 56, 14, 230, 82,
]),
H256([
205, 199, 37, 149, 247, 76, 123, 16, 67, 208, 225, 255, 186, 183, 52, 100, 140, 131, 141,
251, 5, 39, 217, 113, 182, 2, 188, 33, 108, 150, 25, 239,
]),
H256([
10, 191, 90, 201, 116, 161, 237, 87, 244, 5, 10, 165, 16, 221, 156, 116, 245, 8, 39, 123,
57, 215, 151, 59, 178, 223, 204, 197, 238, 176, 97, 141,
]),
H256([
184, 205, 116, 4, 111, 243, 55, 240, 167, 191, 44, 142, 3, 225, 15, 100, 44, 24, 134, 121,
141, 113, 128, 106, 177, 232, 136, 217, 229, 238, 135, 208,
]),
H256([
131, 140, 86, 85, 203, 33, 198, 203, 131, 49, 59, 90, 99, 17, 117, 223, 244, 150, 55, 114,
204, 233, 16, 129, 136, 179, 74, 200, 124, 129, 196, 30,
]),
H256([
102, 46, 228, 221, 45, 215, 178, 188, 112, 121, 97, 177, 230, 70, 196, 4, 118, 105, 220,
182, 88, 79, 13, 141, 119, 13, 175, 93, 126, 125, 235, 46,
]),
H256([
56, 138, 178, 14, 37, 115, 209, 113, 168, 129, 8, 231, 157, 130, 14, 152, 242, 108, 11,
132, 170, 139, 47, 74, 164, 150, 141, 187, 129, 142, 163, 34,
]),
H256([
147, 35, 124, 80, 186, 117, 238, 72, 95, 76, 34, 173, 242, 247, 65, 64, 11, 223, 141, 106,
156, 199, 223, 126, 202, 229, 118, 34, 22, 101, 215, 53,
]),
H256([
132, 72, 129, 139, 180, 174, 69, 98, 132, 158, 148, 158, 23, 172, 22, 224, 190, 22, 104,
142, 21, 107, 92, 241, 94, 9, 140, 98, 124, 0, 86, 169,
]),
H256([
39, 174, 91, 160, 141, 114, 145, 201, 108, 140, 189, 220, 193, 72, 191, 72, 166, 214, 140,
121, 116, 185, 67, 86, 245, 55, 84, 239, 97, 113, 215, 87,
]),
];
pub static INITIAL_ROOT: H256 = H256([39, 174, 91, 160, 141, 114, 145, 201, 108, 140, 189, 220, 193, 72, 191, 72, 166, 214, 140, 121, 116, 185, 67, 86, 245, 55, 84, 239, 97, 113, 215, 87]);
pub static INITIAL_ROOT: H256 = H256([
39, 174, 91, 160, 141, 114, 145, 201, 108, 140, 189, 220, 193, 72, 191, 72, 166, 214, 140, 121,
116, 185, 67, 86, 245, 55, 84, 239, 97, 113, 215, 87,
]);

#[cfg(test)]
mod test {
Expand Down
14 changes: 6 additions & 8 deletions rust/sealevel/hyperlane-core/src/types/message.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::types::H256;
use sha3::{Digest, Keccak256};
use sha3::digest::Update;
use sha3::{Digest, Keccak256};

use crate::{HyperlaneError, Decode, Encode};
use crate::{Decode, Encode, HyperlaneError};

const HYPERLANE_MESSAGE_PREFIX_LEN: usize = 77;

Expand Down Expand Up @@ -124,7 +124,7 @@ impl HyperlaneMessage {
}

// Intnded for sealevel vm "event" (log message).
pub fn format(&self) ->Result<String, HyperlaneError> {
pub fn format(&self) -> Result<String, HyperlaneError> {
let mut serialized = vec![];
self.write_to(&mut serialized)?;
let encoded = bs58::encode(serialized).into_string();
Expand All @@ -133,11 +133,9 @@ impl HyperlaneMessage {

// Intended for sealevel vm "event" (log message).
pub fn parse(formatted: &[u8]) -> Result<Self, HyperlaneError> {
let decoded = bs58::decode(formatted)
.into_vec()
.map_err(|err| {
HyperlaneError::IoError(std::io::Error::new(std::io::ErrorKind::InvalidData, err))
})?;
let decoded = bs58::decode(formatted).into_vec().map_err(|err| {
HyperlaneError::IoError(std::io::Error::new(std::io::ErrorKind::InvalidData, err))
})?;
Self::read_from(&mut std::io::Cursor::new(decoded))
}
}
Expand Down
22 changes: 19 additions & 3 deletions rust/sealevel/programs/ism/rubber-stamp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,41 @@
// #![deny(missing_docs)] // FIXME
#![deny(unsafe_code)]

use std::str::FromStr as _;

use hyperlane_sealevel_mailbox::instruction::IsmInstruction;
use solana_program::{
account_info::AccountInfo,
account_info::{next_account_info, AccountInfo},
entrypoint,
entrypoint::ProgramResult,
msg,
program_error::ProgramError,
pubkey::Pubkey,
};

solana_program::declare_id!("6TCwgXydobJUEqabm7e6SL4FMdiFDvp1pmYoL6xXmRJq");
solana_program::declare_id!("YpYBDE5EsueaooNiYjgQ5PWcX9EB7kBpo3uufdDeLi7");

#[cfg(not(feature = "no-entrypoint"))]
entrypoint!(process_instruction);

const AUTHORITY: &str = "7E7dbtWMktZB7rSkBecaayfhTaaavBKqKCjNzvspwycH";

pub fn process_instruction(
_program_id: &Pubkey,
_accounts: &[AccountInfo],
accounts: &[AccountInfo],
instruction_data: &[u8],
) -> ProgramResult {
let accounts_iter = &mut accounts.iter();
let hyperlane_mailbox_auth = next_account_info(accounts_iter)?;
if hyperlane_mailbox_auth.key != &Pubkey::from_str(AUTHORITY).unwrap() {
return Err(ProgramError::InvalidArgument);
}
if !hyperlane_mailbox_auth.is_signer {
return Err(ProgramError::MissingRequiredSignature);
}
if accounts_iter.next().is_some() {
return Err(ProgramError::InvalidArgument);
}
let _ixn = IsmInstruction::from_instruction_data(instruction_data)?;
msg!("hyperlane-sealevel-ism-rubber-stamp: LGTM!");
Ok(())
Expand Down
Loading

0 comments on commit c8290a9

Please sign in to comment.