Skip to content

Commit

Permalink
feat: add macros for shared interfaces (#105)
Browse files Browse the repository at this point in the history
Co-authored-by: Christian Gorenflo <christian@axelar.network>
  • Loading branch information
milapsheth and cgorenflo authored Dec 10, 2024
1 parent bdf9443 commit 4f513f9
Show file tree
Hide file tree
Showing 20 changed files with 525 additions and 228 deletions.
36 changes: 24 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ edition = "2021"
rust-version = "1.81.0"

[workspace.dependencies]
soroban-sdk = { version = "22.0.0" }
soroban-token-sdk = { version = "22.0.0" }
soroban-sdk = { version = "22.0.2" }
soroban-token-sdk = { version = "22.0.2" }
cfg-if = { version = "1.0" }
axelar-soroban-std = { version = "^0.1.0", path = "packages/axelar-soroban-std" }
axelar-soroban-std = { version = "^0.1.0", path = "packages/axelar-soroban-std", features = ["derive"] }
axelar-soroban-std-derive = { version = "^0.1.0", path = "packages/axelar-soroban-std-derive" }
axelar-gas-service = { version = "^0.1.0", path = "contracts/axelar-gas-service" }
axelar-gateway = { version = "^0.1.0", path = "contracts/axelar-gateway" }
axelar-operators = { version = "^0.1.0", path = "contracts/axelar-operators" }
Expand All @@ -30,8 +31,6 @@ nursery = { level = "warn", priority = -1 }
too_many_arguments = "allow"

[workspace.lints.rust]
warnings = "deny"
deprecated = "allow"

[profile.release]
opt-level = "z"
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ cargo install --locked stellar-cli --features opt

```bash
cargo build
```

## Build wasm

```bash
cargo wasm

# OR

stellar contract build
```
Expand Down
38 changes: 3 additions & 35 deletions contracts/axelar-gas-service/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use soroban_sdk::{contract, contractimpl, token, Address, Bytes, BytesN, Env, String};
use soroban_sdk::{contract, contractimpl, token, Address, Bytes, Env, String};

use crate::error::ContractError;
use crate::event;
use crate::interface::AxelarGasServiceInterface;
use crate::storage_types::DataKey;
use axelar_soroban_std::interfaces::{MigratableInterface, OwnableInterface, UpgradableInterface};
use axelar_soroban_std::{ensure, interfaces, types::Token};
use axelar_soroban_std::{Ownable, Upgradable};

#[contract]
#[derive(Ownable, Upgradable)]
pub struct AxelarGasService;

#[contractimpl]
Expand All @@ -26,39 +27,6 @@ impl AxelarGasService {
const fn run_migration(_env: &Env, _migration_data: ()) {}
}

#[contractimpl]
impl MigratableInterface for AxelarGasService {
type MigrationData = ();
type Error = ContractError;

fn migrate(env: &Env, migration_data: ()) -> Result<(), ContractError> {
interfaces::migrate::<Self>(env, || Self::run_migration(env, migration_data))
.map_err(|_| ContractError::MigrationNotAllowed)
}
}

#[contractimpl]
impl UpgradableInterface for AxelarGasService {
fn version(env: &Env) -> String {
String::from_str(env, env!("CARGO_PKG_VERSION"))
}

fn upgrade(env: &Env, new_wasm_hash: BytesN<32>) {
interfaces::upgrade::<Self>(env, new_wasm_hash);
}
}

#[contractimpl]
impl OwnableInterface for AxelarGasService {
fn owner(env: &Env) -> Address {
interfaces::owner(env)
}

fn transfer_ownership(env: &Env, new_owner: Address) {
interfaces::transfer_ownership::<Self>(env, new_owner);
}
}

#[contractimpl]
impl AxelarGasServiceInterface for AxelarGasService {
fn pay_gas(
Expand Down
17 changes: 7 additions & 10 deletions contracts/axelar-gateway/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ mod tests {
let (env, signers, client) = setup_env(randint(0, 10), randint(1, 10));

let msg_hash: BytesN<32> = BytesN::random(&env);
let proof = generate_proof(&env, msg_hash.clone(), signers);
let proof = generate_proof(&env, msg_hash, signers);

let different_msg_hash: BytesN<32> = BytesN::random(&env);

Expand Down Expand Up @@ -595,16 +595,15 @@ mod tests {
let previous_signer_retention = randint(1, 5);
let (env, original_signers, client) = setup_env(previous_signer_retention, randint(1, 10));

// Proof from the first signer set should still be valid
let msg_hash: BytesN<32> = BytesN::random(&env);
let proof = generate_proof(&env, msg_hash.clone(), original_signers.clone());
let domain_separator = original_signers.domain_separator.clone();

let mut previous_signers = original_signers.clone();
let mut previous_signers = original_signers;

for _ in 0..previous_signer_retention {
let new_signers = generate_signers_set(
&env,
randint(1, 10),
original_signers.domain_separator.clone(),
);
let new_signers = generate_signers_set(&env, randint(1, 10), domain_separator.clone());

testutils::rotate_signers(&env, &client.address, new_signers.clone());

Expand All @@ -623,8 +622,6 @@ mod tests {
previous_signers = new_signers;
}

// Proof from the first signer set should still be valid
let proof = generate_proof(&env, msg_hash.clone(), original_signers.clone());
env.as_contract(&client.address, || {
assert!(!assert_ok!(auth::validate_proof(&env, &msg_hash, proof)));
})
Expand All @@ -647,7 +644,7 @@ mod tests {
}

// Proof from the first signer set should fail
let proof = generate_proof(&env, msg_hash.clone(), original_signers.clone());
let proof = generate_proof(&env, msg_hash.clone(), original_signers);

env.as_contract(&client.address, || {
assert_err!(
Expand Down
53 changes: 2 additions & 51 deletions contracts/axelar-gateway/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,15 @@ use crate::messaging_interface::AxelarGatewayMessagingInterface;
use crate::storage_types::{DataKey, MessageApprovalKey, MessageApprovalValue};
use crate::types::{CommandType, Message, Proof, WeightedSigners};
use crate::{auth, event};
use axelar_soroban_std::interfaces::{
migrate, MigratableInterface, OperatableInterface, OwnableInterface, UpgradableInterface,
};
use axelar_soroban_std::ttl::{INSTANCE_TTL_EXTEND_TO, INSTANCE_TTL_THRESHOLD};
use axelar_soroban_std::{ensure, interfaces};
use axelar_soroban_std::{ensure, interfaces, Operatable, Ownable, Upgradable};
use soroban_sdk::xdr::ToXdr;
use soroban_sdk::{contract, contractimpl, Address, Bytes, BytesN, Env, String, Vec};

const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");

#[contract]
#[derive(Ownable, Upgradable, Operatable)]
pub struct AxelarGateway;

#[contractimpl]
impl MigratableInterface for AxelarGateway {
type MigrationData = ();
type Error = ContractError;

fn migrate(env: &Env, migration_data: ()) -> Result<(), ContractError> {
migrate::<Self>(env, || Self::run_migration(env, migration_data))
.map_err(|_| ContractError::MigrationNotAllowed)
}
}

#[contractimpl]
impl UpgradableInterface for AxelarGateway {
fn version(env: &Env) -> String {
String::from_str(env, CONTRACT_VERSION)
}

// boilerplate necessary for the contractimpl macro to include function in the generated client
fn upgrade(env: &Env, new_wasm_hash: BytesN<32>) {
interfaces::upgrade::<Self>(env, new_wasm_hash);
}
}

#[contractimpl]
impl OwnableInterface for AxelarGateway {
fn owner(env: &Env) -> Address {
interfaces::owner(env)
}

fn transfer_ownership(env: &Env, new_owner: Address) {
interfaces::transfer_ownership::<Self>(env, new_owner);
}
}

#[contractimpl]
impl OperatableInterface for AxelarGateway {
fn operator(env: &Env) -> Address {
interfaces::operator(env)
}

fn transfer_operatorship(env: &Env, new_operator: Address) {
interfaces::transfer_operatorship::<Self>(env, new_operator);
}
}

#[contractimpl]
impl AxelarGateway {
/// Initialize the gateway
Expand Down
39 changes: 3 additions & 36 deletions contracts/axelar-operators/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::error::ContractError;
use crate::event;
use crate::storage_types::DataKey;
use axelar_soroban_std::interfaces::{MigratableInterface, OwnableInterface, UpgradableInterface};
use axelar_soroban_std::{ensure, interfaces};
use soroban_sdk::{contract, contractimpl, Address, BytesN, Env, String, Symbol, Val, Vec};
use axelar_soroban_std::{ensure, interfaces, Ownable, Upgradable};
use soroban_sdk::{contract, contractimpl, Address, Env, Symbol, Val, Vec};

#[contract]
#[derive(Ownable, Upgradable)]
pub struct AxelarOperators;

#[contractimpl]
Expand Down Expand Up @@ -86,36 +86,3 @@ impl AxelarOperators {
// Modify this function to add migration logic
const fn run_migration(_env: &Env, _migration_data: ()) {}
}

#[contractimpl]
impl MigratableInterface for AxelarOperators {
type MigrationData = ();
type Error = ContractError;

fn migrate(env: &Env, migration_data: ()) -> Result<(), ContractError> {
interfaces::migrate::<Self>(env, || Self::run_migration(env, migration_data))
.map_err(|_| ContractError::MigrationNotAllowed)
}
}

#[contractimpl]
impl UpgradableInterface for AxelarOperators {
fn version(env: &Env) -> String {
String::from_str(env, env!("CARGO_PKG_VERSION"))
}

fn upgrade(env: &Env, new_wasm_hash: BytesN<32>) {
interfaces::upgrade::<Self>(env, new_wasm_hash);
}
}

#[contractimpl]
impl OwnableInterface for AxelarOperators {
fn owner(env: &Env) -> Address {
interfaces::owner(env)
}

fn transfer_ownership(env: &Env, new_owner: Address) {
interfaces::transfer_ownership::<Self>(env, new_owner);
}
}
Loading

0 comments on commit 4f513f9

Please sign in to comment.