Skip to content

Commit

Permalink
feat: merged context contract branch
Browse files Browse the repository at this point in the history
  • Loading branch information
alenmestrov committed Feb 3, 2025
2 parents a621d78 + add75aa commit 84798c0
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 124 deletions.
4 changes: 1 addition & 3 deletions contracts/stellar/context-config/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,4 @@ test_snapshots/
mock_proxy/test_snapshots/

# Res folder
res/

#
res/
2 changes: 1 addition & 1 deletion contracts/stellar/context-config/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ TARGET="${CARGO_TARGET_DIR:-../../../target}"

rustup target add wasm32-unknown-unknown

cargo build --target wasm32-unknown-unknown --profile app-release $extra_args
cargo build --target wasm32-unknown-unknown --profile app-release

mkdir -p res

Expand Down
13 changes: 7 additions & 6 deletions contracts/stellar/context-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
use calimero_context_config::stellar::stellar_types::StellarError;
use guard::Guard;
use soroban_sdk::{
contract, contractimpl, contracttype, symbol_short, Address, BytesN, Env, Map, Symbol,
contract, contractimpl, contracttype, log, symbol_short, Address, BytesN, Env, Map, Symbol,
};

mod guard;
mod mutate;
mod query;
mod sys;
// mod types;

// use types::Error;

const STORAGE_KEY_STATE: Symbol = symbol_short!("STATE");

Expand Down Expand Up @@ -60,10 +57,10 @@ impl OptionalBytes {
}

#[contract]
pub struct ContextContract;
pub struct ContextConfig;

#[contractimpl]
impl ContextContract {
impl ContextConfig {
pub fn __constructor(env: Env, owner: Address, ledger_id: Address) -> Result<(), StellarError> {
// Require authorization from deployer
owner.require_auth();
Expand All @@ -72,13 +69,17 @@ impl ContextContract {
return Err(StellarError::Unauthorized);
}

log!(&env, "Initializing storage contract");

let configs = ContextConfigs {
contexts: Map::new(&env),
proxy_code: OptionalBytes::None,
owner,
ledger_id,
};

log!(&env, "Initializing context contract");

env.storage()
.instance()
.set(&symbol_short!("STATE"), &configs);
Expand Down
4 changes: 2 additions & 2 deletions contracts/stellar/context-config/src/mutate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use calimero_context_config::stellar::stellar_types::{
use soroban_sdk::{contractimpl, Address, BytesN, Env, IntoVal, Map, Symbol, Vec};

use crate::guard::{Guard, GuardedValue};
use crate::{Context, ContextContract, ContextContractArgs, ContextContractClient};
use crate::{Context, ContextConfig, ContextConfigArgs, ContextConfigClient};

#[contractimpl]
impl ContextContract {
impl ContextConfig {
/// Process a signed mutation request
/// # Errors
/// Returns InvalidSignature if request signature is invalid
Expand Down
5 changes: 2 additions & 3 deletions contracts/stellar/context-config/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ use calimero_context_config::stellar::stellar_types::{
use soroban_sdk::{contractimpl, Address, BytesN, Env, Map, Vec};

use crate::guard::GuardedValue;
// use crate::types::{StellarApplication, StellarCapability, Error};
use crate::{Context, ContextContract, ContextContractArgs, ContextContractClient};
use crate::{Context, ContextConfig, ContextConfigArgs, ContextConfigClient};

#[contractimpl]
impl ContextContract {
impl ContextConfig {
/// Helper function to get context
fn get_context(env: &Env, context_id: BytesN<32>) -> Option<Context> {
Self::get_state(env).contexts.get(context_id)
Expand Down
4 changes: 2 additions & 2 deletions contracts/stellar/context-config/src/sys.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use calimero_context_config::stellar::stellar_types::StellarError;
use soroban_sdk::{contractimpl, log, Address, Bytes, BytesN, Env};

use crate::{ContextContract, ContextContractArgs, ContextContractClient, OptionalBytes};
use crate::{ContextConfig, ContextConfigArgs, ContextConfigClient, OptionalBytes};

#[contractimpl]
impl ContextContract {
impl ContextConfig {
/// Upgrades the context contract with new WASM code
/// # Arguments
/// * `new_wasm` - The new WASM bytecode
Expand Down
70 changes: 26 additions & 44 deletions contracts/stellar/context-config/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use calimero_context_config::stellar::stellar_types::{
StellarApplication, StellarCapability, StellarContextRequest, StellarContextRequestKind,
StellarRequest, StellarRequestKind, StellarSignedRequest, StellarSignedRequestPayload,
};
use calimero_context_config_stellar::{ContextContract, ContextContractClient};
use calimero_context_config_stellar::ContextConfigClient;
use ed25519_dalek::{Signer, SigningKey};
use soroban_sdk::testutils::Address as _;
use soroban_sdk::{log, vec, Address, Bytes, BytesN, Env, IntoVal};
use soroban_sdk::{vec, Address, Bytes, BytesN, Env, IntoVal};

fn create_signed_request(
signer_key: &SigningKey,
Expand All @@ -23,7 +23,7 @@ fn create_signed_request(
// Helper struct to manage test context
struct TestContext<'a> {
env: Env,
client: ContextContractClient<'a>,
client: ContextConfigClient<'a>,
context_key: SigningKey,
context_id: BytesN<32>,
}
Expand All @@ -33,17 +33,27 @@ impl<'a> TestContext<'a> {
let env = Env::default();
env.mock_all_auths();
let owner = Address::generate(&env);
let contract_id = env.register(
ContextContract,

let context_contract_wasm =
fs::read("../context-config/res/calimero_context_config_stellar.wasm")
.expect("Failed to read context contract WASM file");
let context_contract_wasm = Bytes::from_slice(&env, &context_contract_wasm);

let contract_id = env.deployer().upload_contract_wasm(context_contract_wasm);

let salt = BytesN::<32>::from_array(&env, &[0; 32]);
let contract_id = env.deployer().with_address(owner.clone(), salt).deploy_v2(
contract_id,
(
&owner,
owner.clone(),
Address::from_str(
&env,
"CDLZFC3SYJYDZT7K67VZ75HPJVIEUVNIXF47ZG2FB2RMQQVU2HHGCYSC",
),
),
);
let client = ContextContractClient::new(&env, &contract_id);

let client = ContextConfigClient::new(&env, &contract_id);

// Set up proxy code
let proxy_wasm = fs::read("../context-proxy/res/calimero_context_proxy_stellar.wasm")
Expand Down Expand Up @@ -103,11 +113,6 @@ impl<'a> TestContext<'a> {
#[test]
fn test_add_context() {
let ctx = TestContext::setup();
log!(
&ctx.env,
"Context contract address: {:?}",
ctx.client.address
);
let (_author_key, author_id) = ctx.generate_key();
let app = ctx.create_application(1);

Expand Down Expand Up @@ -143,7 +148,6 @@ fn test_member_management() {

// Verify members after authorized addition
let members = ctx.client.members(&ctx.context_id, &0u32, &10u32);
log!(&ctx.env, "Members after authorized addition: {:?}", members);
assert_eq!(
members.len(),
2,
Expand All @@ -170,7 +174,6 @@ fn test_member_management() {

// Verify members haven't changed
let members = ctx.client.members(&ctx.context_id, &0u32, &10u32);
log!(&ctx.env, "Final members list: {:?}", members);
assert_eq!(
members.len(),
2,
Expand Down Expand Up @@ -204,11 +207,7 @@ fn test_member_management() {

// Verify members haven't changed after failed removal
let members = ctx.client.members(&ctx.context_id, &0u32, &10u32);
log!(
&ctx.env,
"Members after failed removal attempt: {:?}",
members
);
assert_eq!(members.len(), 2, "Should still have both members");
assert_eq!(members.len(), 2, "Should still have both members");
assert!(
members.contains(&alice_id),
Expand All @@ -231,19 +230,12 @@ fn test_member_management() {

// Verify final membership after successful removal
let members = ctx.client.members(&ctx.context_id, &0u32, &10u32);
log!(
&ctx.env,
"Final members after authorized removal: {:?}",
members
);
assert_eq!(members.len(), 1, "Should have only Alice as member");
assert!(
members.contains(&alice_id),
"Alice should still be a member"
);
assert!(!members.contains(&bob_id), "Bob should have been removed");

log!(&ctx.env, "Member management test completed successfully");
}

#[test]
Expand Down Expand Up @@ -290,7 +282,14 @@ fn test_capability_management() {
let bob_privileges = ctx
.client
.privileges(&ctx.context_id, &vec![&ctx.env, bob_id.clone()]);
log!(&ctx.env, "Bob's privileges: {:?}", bob_privileges);
assert!(
bob_privileges.contains_key(bob_id.clone())
&& bob_privileges
.get(bob_id.clone())
.unwrap()
.contains(&StellarCapability::ManageMembers),
"Bob should have ManageMembers capability"
);

// Bob should now be able to add members
let add_member_request = StellarRequest {
Expand Down Expand Up @@ -333,11 +332,6 @@ fn test_capability_management() {
let bob_privileges = ctx
.client
.privileges(&ctx.context_id, &vec![&ctx.env, bob_id.clone()]);
log!(
&ctx.env,
"Bob's privileges after revocation: {:?}",
bob_privileges
);
assert!(
bob_privileges.is_empty() || !bob_privileges.contains_key(bob_id.clone()),
"Bob should have no capabilities after revocation"
Expand Down Expand Up @@ -368,11 +362,6 @@ fn test_capability_management() {
!members.contains(&david_id),
"David should not have been added"
);

log!(
&ctx.env,
"Capability management test completed successfully"
);
}

#[test]
Expand All @@ -389,7 +378,6 @@ fn test_application_update() {

// Verify initial application state
let app = ctx.client.application(&ctx.context_id);
log!(&ctx.env, "Initial application: {:?}", app);
assert_eq!(app.id, initial_app.id, "Initial application ID mismatch");
assert_eq!(
app.blob, initial_app.blob,
Expand All @@ -415,7 +403,6 @@ fn test_application_update() {

// Verify application hasn't changed
let app = ctx.client.application(&ctx.context_id);
log!(&ctx.env, "Application after failed update: {:?}", app);
assert_eq!(
app.id, initial_app.id,
"Application should not have changed"
Expand All @@ -440,7 +427,6 @@ fn test_application_update() {

// Verify application has been updated
let app = ctx.client.application(&ctx.context_id);
log!(&ctx.env, "Final application state: {:?}", app);
assert_eq!(
app.id, updated_app.id,
"Application should have been updated"
Expand All @@ -449,8 +435,6 @@ fn test_application_update() {
app.blob, updated_app.blob,
"Application should have been updated"
);

log!(&ctx.env, "Application update test completed successfully");
}

#[test]
Expand Down Expand Up @@ -593,6 +577,4 @@ fn test_query_endpoints() {
!proxy_address.to_string().is_empty(),
"Proxy address should be set"
);

log!(&ctx.env, "Query endpoints test completed successfully");
}
1 change: 0 additions & 1 deletion crates/context/config/src/stellar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::repr::{Repr, ReprBytes, ReprTransmute};
use crate::types::ProposalId;
use crate::{ProposalAction, ProposalWithApprovals, ProxyMutateRequest};

pub mod stellar_repr;
pub mod stellar_types;

#[derive(Clone, Debug, Eq, PartialEq)]
Expand Down
62 changes: 0 additions & 62 deletions crates/context/config/src/stellar/stellar_repr.rs

This file was deleted.

0 comments on commit 84798c0

Please sign in to comment.