Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: relayer function call keys #792

Merged
merged 10 commits into from
Jul 18, 2023
233 changes: 127 additions & 106 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ bitflags = { version = "1", default-features = false }
bn = { version = "0.5", package = "zeropool-bn", default-features = false }
borsh = { version = "0.10", default-features = false }
borsh-compat = { version = "0.9", package = "borsh", default-features = false }
bs58 = { version = "0.5", default-features = false, features = ["alloc", "sha2"] }
bstr = "1"
byte-slice-cast = { version = "1", default-features = false }
criterion = "0.5"
Expand Down
69 changes: 32 additions & 37 deletions engine-sdk/src/near_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ use crate::io::StorageIntermediate;
use crate::prelude::NearGas;
use crate::promise::PromiseId;
use aurora_engine_types::account_id::AccountId;
use aurora_engine_types::parameters::{
NearPublicKey, PromiseAction, PromiseBatchAction, PromiseCreateArgs,
};
use aurora_engine_types::parameters::{PromiseAction, PromiseBatchAction, PromiseCreateArgs};
use aurora_engine_types::public_key::PublicKey;
use aurora_engine_types::types::PromiseResult;
use aurora_engine_types::H256;

Expand Down Expand Up @@ -418,36 +417,32 @@ impl crate::promise::PromiseHandler for Runtime {
receiver_id,
function_names,
} => {
feature_gated!("all-promise-actions", {
joshuajbouw marked this conversation as resolved.
Show resolved Hide resolved
let pk: RawPublicKey = public_key.into();
let pk_bytes = pk.as_bytes();
let allowance = allowance.as_u128();
let allowance_addr = core::ptr::addr_of!(allowance);
let receiver_id = receiver_id.as_bytes();
let function_names = function_names.as_bytes();
exports::promise_batch_action_add_key_with_function_call(
id,
pk_bytes.len() as _,
pk_bytes.as_ptr() as _,
*nonce,
allowance_addr as _,
receiver_id.len() as _,
receiver_id.as_ptr() as _,
function_names.len() as _,
function_names.as_ptr() as _,
);
});
let pk: RawPublicKey = public_key.into();
let pk_bytes = pk.as_bytes();
let allowance = allowance.as_u128();
let allowance_addr = core::ptr::addr_of!(allowance);
let receiver_id = receiver_id.as_bytes();
let function_names = function_names.as_bytes();
exports::promise_batch_action_add_key_with_function_call(
id,
pk_bytes.len() as _,
pk_bytes.as_ptr() as _,
*nonce,
allowance_addr as _,
receiver_id.len() as _,
receiver_id.as_ptr() as _,
function_names.len() as _,
function_names.as_ptr() as _,
);
}
PromiseAction::DeleteKey { public_key } => {
feature_gated!("all-promise-actions", {
let pk: RawPublicKey = public_key.into();
let pk_bytes = pk.as_bytes();
exports::promise_batch_action_delete_key(
id,
pk_bytes.len() as _,
pk_bytes.as_ptr() as _,
);
});
let pk: RawPublicKey = public_key.into();
let pk_bytes = pk.as_bytes();
exports::promise_batch_action_delete_key(
id,
pk_bytes.len() as _,
pk_bytes.as_ptr() as _,
);
}
PromiseAction::DeleteAccount { beneficiary_id } => {
feature_gated!("all-promise-actions", {
Expand Down Expand Up @@ -508,18 +503,18 @@ impl RawPublicKey {
}
}

impl<'a> From<&'a NearPublicKey> for RawPublicKey {
fn from(key: &'a NearPublicKey) -> Self {
impl<'a> From<&'a PublicKey> for RawPublicKey {
fn from(key: &'a PublicKey) -> Self {
match key {
NearPublicKey::Ed25519(bytes) => {
PublicKey::Ed25519(_) => {
let mut buf = [0u8; 33];
buf[1..33].copy_from_slice(bytes);
buf[1..33].copy_from_slice(key.key_data());
Self::Ed25519(buf)
}
NearPublicKey::Secp256k1(bytes) => {
PublicKey::Secp256k1(_) => {
let mut buf = [0u8; 65];
buf[0] = 0x01;
buf[1..65].copy_from_slice(bytes);
buf[1..65].copy_from_slice(key.key_data());
Self::Secp256k1(buf)
}
}
Expand Down
1 change: 1 addition & 0 deletions engine-standalone-storage/src/relayer_db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ mod test {
owner_id: "aurora".parse().unwrap(),
upgrade_delay_blocks: 0,
is_paused: false,
key_manager: None,
};

// Initialize engine and connector states in storage.
Expand Down
18 changes: 18 additions & 0 deletions engine-standalone-storage/src/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,24 @@ fn non_submit_execute<'db, M: ModExpAlgorithm + 'static>(
prev.is_paused = false;
state::set_state(&mut io, &prev)?;

None
}
TransactionKind::SetKeyManager(args) => {
let mut prev = state::get_state(&io)?;

prev.key_manager = args.key_manager.clone();
state::set_state(&mut io, &prev)?;

None
}
TransactionKind::AddRelayerKey(args) => {
engine::add_function_call_key(&mut io, &args.public_key);

None
}
TransactionKind::RemoveRelayerKey(args) => {
engine::remove_function_call_key(&mut io, &args.public_key)?;

None
}
};
Expand Down
20 changes: 20 additions & 0 deletions engine-standalone-storage/src/sync/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ pub enum TransactionKind {
PauseContract,
/// Resume the contract
ResumeContract,
/// Set the relayer key manager
SetKeyManager(parameters::RelayerKeyManagerArgs),
/// Add a new relayer public function call access key
AddRelayerKey(parameters::RelayerKeyArgs),
/// Remove the relayer public function call access key
RemoveRelayerKey(parameters::RelayerKeyArgs),
/// Sentinel kind for cases where a NEAR receipt caused a
/// change in Aurora state, but we failed to parse the Action.
Unknown,
Expand Down Expand Up @@ -360,6 +366,9 @@ impl TransactionKind {
Self::FundXccSubAccound(_) => Self::no_evm_execution("fund_xcc_sub_account"),
Self::PauseContract => Self::no_evm_execution("pause_contract"),
Self::ResumeContract => Self::no_evm_execution("resume_contract"),
Self::SetKeyManager(_) => Self::no_evm_execution("set_key_manager"),
Self::AddRelayerKey(_) => Self::no_evm_execution("add_relayer_key"),
Self::RemoveRelayerKey(_) => Self::no_evm_execution("remove_relayer_key"),
}
}

Expand Down Expand Up @@ -531,6 +540,9 @@ enum BorshableTransactionKind<'a> {
SetUpgradeDelayBlocks(Cow<'a, parameters::SetUpgradeDelayBlocksArgs>),
PauseContract,
ResumeContract,
SetKeyManager(Cow<'a, parameters::RelayerKeyManagerArgs>),
AddRelayerKey(Cow<'a, parameters::RelayerKeyArgs>),
RemoveRelayerKey(Cow<'a, parameters::RelayerKeyArgs>),
}

impl<'a> From<&'a TransactionKind> for BorshableTransactionKind<'a> {
Expand Down Expand Up @@ -579,6 +591,9 @@ impl<'a> From<&'a TransactionKind> for BorshableTransactionKind<'a> {
}
TransactionKind::PauseContract => Self::PauseContract,
TransactionKind::ResumeContract => Self::ResumeContract,
TransactionKind::SetKeyManager(x) => Self::SetKeyManager(Cow::Borrowed(x)),
TransactionKind::AddRelayerKey(x) => Self::AddRelayerKey(Cow::Borrowed(x)),
TransactionKind::RemoveRelayerKey(x) => Self::RemoveRelayerKey(Cow::Borrowed(x)),
}
}
}
Expand Down Expand Up @@ -646,6 +661,11 @@ impl<'a> TryFrom<BorshableTransactionKind<'a>> for TransactionKind {
}
BorshableTransactionKind::PauseContract => Ok(Self::PauseContract),
BorshableTransactionKind::ResumeContract => Ok(Self::ResumeContract),
BorshableTransactionKind::SetKeyManager(x) => Ok(Self::SetKeyManager(x.into_owned())),
BorshableTransactionKind::AddRelayerKey(x) => Ok(Self::AddRelayerKey(x.into_owned())),
BorshableTransactionKind::RemoveRelayerKey(x) => {
Ok(Self::RemoveRelayerKey(x.into_owned()))
}
}
}
}
1 change: 1 addition & 0 deletions engine-tests/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod pause_contract;
mod prepaid_gas_precompile;
mod promise_results_precompile;
mod random;
mod relayer_keys;
mod repro;
pub mod sanity;
mod self_destruct_state;
Expand Down
Loading
Loading