Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions backend/src/db/seeds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ pub enum SeedError {
/// Human-readable reason.
reason: String,
},
/// Multiple seed errors collected while running multiple steps.
#[error("Multiple seed errors: {0:?}")]
Multiple(Vec<SeedError>),
}

// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -210,21 +213,31 @@ pub async fn seed_feature_flags(pool: &PgPool) -> Result<usize, SeedError> {
/// Returns the first [`SeedError`] encountered, if any.
pub async fn run_all(pool: &PgPool) -> Result<(), SeedError> {
info!("Starting database seed");
// Run each step and collect errors so one failing step doesn't abort the rest.
let mut errors: Vec<SeedError> = Vec::new();

seed_users(pool).await.map_err(|e| SeedError::StepFailed {
step: "seed_users".to_string(),
reason: e.to_string(),
})?;
if let Err(e) = seed_users(pool).await {
errors.push(SeedError::StepFailed {
step: "seed_users".to_string(),
reason: e.to_string(),
});
}

seed_feature_flags(pool)
.await
.map_err(|e| SeedError::StepFailed {
if let Err(e) = seed_feature_flags(pool).await {
errors.push(SeedError::StepFailed {
step: "seed_feature_flags".to_string(),
reason: e.to_string(),
})?;
});
}

info!("Database seed complete");
Ok(())
if errors.is_empty() {
info!("Database seed complete");
Ok(())
} else if errors.len() == 1 {
Err(errors.remove(0))
} else {
Err(SeedError::Multiple(errors))
}
}

// ---------------------------------------------------------------------------
Expand Down
82 changes: 82 additions & 0 deletions backend/src/services/cache.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;

#[derive(Clone)]
struct CacheEntry {
value: String,
expires_at: Option<Instant>,
}

#[derive(Clone, Default)]
pub struct MultiLevelCache {
// Simple in-memory local layer for fast reads. This is a mock; a real
// implementation would add Redis or another remote layer.
local: Arc<RwLock<HashMap<String, CacheEntry>>>,
}

impl MultiLevelCache {
pub fn new() -> Self {
Self {
local: Arc::new(RwLock::new(HashMap::new())),
}
}

pub async fn get(&self, key: &str) -> Option<String> {
let now = Instant::now();
// Check local cache
{
let read = self.local.read().await;
if let Some(entry) = read.get(key) {
if let Some(exp) = entry.expires_at {
if now >= exp {
return None;
}
}
return Some(entry.value.clone());
}
}

// Remote layer would be queried here in a production implementation.
// For this mock we return None on miss.
None
}

pub async fn set(&self, key: String, value: String, ttl: Option<Duration>) {
let expires_at = ttl.map(|t| Instant::now() + t);
let entry = CacheEntry { value, expires_at };
let mut write = self.local.write().await;
write.insert(key, entry);
}

pub async fn invalidate(&self, key: &str) {
let mut write = self.local.write().await;
write.remove(key);
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::time::Duration;

#[tokio::test]
async fn basic_set_get() {
let cache = MultiLevelCache::new();
cache.set("k".to_string(), "v".to_string(), None).await;
let v = cache.get("k").await;
assert_eq!(v.as_deref(), Some("v"));
}

#[tokio::test]
async fn ttl_expires() {
let cache = MultiLevelCache::new();
cache
.set("k2".to_string(), "v2".to_string(), Some(Duration::from_millis(1)))
.await;
tokio::time::sleep(Duration::from_millis(5)).await;
let v = cache.get("k2").await;
assert!(v.is_none());
}
}
62 changes: 62 additions & 0 deletions backend/src/services/transformers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use serde_json::{Map, Value};

pub struct DataTransformer;

impl DataTransformer {
pub fn new() -> Self {
Self {}
}

/// Apply a simple normalization pipeline to a JSON value.
/// - If the value is an object, convert all top-level keys to lowercase.
/// - Optionally extract a nested field by dot-path using `extract_field`.
pub fn transform(&self, mut input: Value) -> Value {
if let Value::Object(map) = &mut input {
let mut normalized = Map::new();
for (k, v) in map.drain() {
normalized.insert(k.to_lowercase(), v);
}
return Value::Object(normalized);
}
input
}

/// Extract a nested field from `value` using a dot-separated path like "a.b.c".
pub fn extract_field<'a>(value: &'a Value, path: &str) -> Option<&'a Value> {
let mut current = value;
for part in path.split('.') {
match current {
Value::Object(map) => {
current = map.get(part)?;
}
_ => return None,
}
}
Some(current)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn normalize_keys() {
let t = DataTransformer::new();
let input: Value = serde_json::json!({ "Name": "Alice", "AGE": 30 });
let out = t.transform(input);
if let Value::Object(map) = out {
assert!(map.contains_key("name"));
assert!(map.contains_key("age"));
} else {
panic!("expected object")
}
}

#[test]
fn extract_dot_path() {
let v: Value = serde_json::json!({ "a": { "b": { "c": 1 } } });
let got = DataTransformer::extract_field(&v, "a.b.c").unwrap();
assert_eq!(got, &Value::Number(1.into()));
}
}
13 changes: 13 additions & 0 deletions examples/multisig/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "crucible-example-multisig"
version.workspace = true
edition.workspace = true

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
soroban-sdk = { workspace = true }

[dev-dependencies]
crucible = { path = "../../contracts/crucible" }
115 changes: 115 additions & 0 deletions examples/multisig/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#![no_std]
#![allow(deprecated)]
use soroban_sdk::{contract, contractimpl, contracttype, symbol_short, Address, Bytes, Env, Vec};

#[contracttype]
#[derive(Clone)]
pub struct Proposal {
pub proposer: Address,
pub tx: Bytes,
pub approvals: Vec<Address>,
pub executed: bool,
}

#[contracttype]
#[derive(Clone)]
pub enum DataKey {
Owners,
Threshold,
NextId,
Proposal(u64),
}

#[contract]
#[derive(Default)]
pub struct MultiSig;

#[contractimpl]
impl MultiSig {
pub fn initialize(env: Env, owners: Vec<Address>, threshold: u32) {
if env.storage().instance().has(&DataKey::Owners) {
panic!("already initialized");
}
if owners.is_empty() || threshold == 0 || (threshold as usize) > owners.len() {
panic!("invalid owners/threshold");
}
env.storage().instance().set(&DataKey::Owners, &owners);
env.storage().instance().set(&DataKey::Threshold, &threshold);
env.storage().instance().set(&DataKey::NextId, &1u64);
env.events().publish((symbol_short!("initialized"),), ());
}

pub fn propose(env: Env, proposer: Address, tx: Bytes) -> u64 {
// proposer must be an owner
let owners: Vec<Address> = env.storage().instance().get(&DataKey::Owners).unwrap();
if !owners.iter().any(|a| a == &proposer) {
panic!("only owner can propose");
}
proposer.require_auth();

let mut id: u64 = 1;
if env.storage().instance().has(&DataKey::NextId) {
id = env.storage().instance().get(&DataKey::NextId).unwrap();
}

let proposal = Proposal {
proposer: proposer.clone(),
tx: tx.clone(),
approvals: Vec::new(&env),
executed: false,
};

env.storage().instance().set(&DataKey::Proposal(id), &proposal);
env.storage().instance().set(&DataKey::NextId, &(id + 1));
env.events().publish((symbol_short!("proposed"),), id);
id
}

pub fn approve(env: Env, approver: Address, id: u64) {
let owners: Vec<Address> = env.storage().instance().get(&DataKey::Owners).unwrap();
if !owners.iter().any(|a| a == &approver) {
panic!("only owner can approve");
}
approver.require_auth();

let mut p: Proposal = env.storage().instance().get(&DataKey::Proposal(id)).unwrap();
if p.executed {
panic!("proposal already executed");
}
// check for duplicate approval
if p.approvals.iter().any(|a| a == &approver) {
return; // idempotent
}
p.approvals.push_back(approver.clone());
env.storage().instance().set(&DataKey::Proposal(id), &p);
env.events().publish((symbol_short!("approved"),), (id, approver));
}

pub fn execute(env: Env, executor: Address, id: u64) {
let owners: Vec<Address> = env.storage().instance().get(&DataKey::Owners).unwrap();
if !owners.iter().any(|a| a == &executor) {
panic!("only owner can execute");
}
executor.require_auth();

let threshold: u32 = env.storage().instance().get(&DataKey::Threshold).unwrap();
let mut p: Proposal = env.storage().instance().get(&DataKey::Proposal(id)).unwrap();
if p.executed {
panic!("proposal already executed");
}
let approvals = p.approvals.len();
if (approvals as u32) < threshold {
panic!("not enough approvals");
}
p.executed = true;
env.storage().instance().set(&DataKey::Proposal(id), &p);
env.events().publish((symbol_short!("executed"),), id);
}

pub fn get_proposal(env: Env, id: u64) -> Proposal {
env.storage().instance().get(&DataKey::Proposal(id)).unwrap()
}
}

#[cfg(test)]
mod test;
Loading