From 1187021f9c37832ee18be4f5698e0e769030f825 Mon Sep 17 00:00:00 2001 From: Keshinro Tanitoluwa Joseph Date: Sun, 31 May 2026 16:46:47 +0100 Subject: [PATCH 1/2] Fix compilation errors in job_registry contract for SC-REP-044 - Add missing error variants (CollateralAlreadyReleased, CollateralNotFound, BidIndexOutOfBounds, BidWindowClosed) - Add collateral_released field to BidRecord struct - Add Bids variant to DataKey enum - Add missing helper functions for IPFS CID validation (is_valid_base58_char, is_valid_base32_char) - Remove duplicate release_collateral functions - Update bid creation to include collateral_released field --- contracts/job_registry/src/lib.rs | 97 +++++-------------------------- 1 file changed, 15 insertions(+), 82 deletions(-) diff --git a/contracts/job_registry/src/lib.rs b/contracts/job_registry/src/lib.rs index 5262859..81ac965 100644 --- a/contracts/job_registry/src/lib.rs +++ b/contracts/job_registry/src/lib.rs @@ -35,6 +35,10 @@ pub enum JobRegistryError { JobExpired = 16, JobNotExpired = 17, InvalidCollateral = 18, + CollateralAlreadyReleased = 19, + CollateralNotFound = 20, + BidIndexOutOfBounds = 21, + BidWindowClosed = 22, } #[contracttype] @@ -74,6 +78,7 @@ pub struct BidRecord { pub freelancer: Address, pub proposal_hash: Bytes, pub collateral_stroops: i128, + pub collateral_released: bool, } #[contracttype] @@ -85,6 +90,7 @@ pub enum DataKey { Bid(u64, u32), BidIndex(u64, Address), Deliverable(u64), + Bids(u64), } #[contract] @@ -300,6 +306,7 @@ impl JobRegistryContract { freelancer: freelancer.clone(), proposal_hash, collateral_stroops, + collateral_released: false, }); env.storage().persistent().set(&bids_key, &bids); @@ -852,6 +859,14 @@ fn validate_hash(env: &Env, hash: &Bytes) { validate_ipfs_cid(env, hash); } +fn is_valid_base58_char(c: u8) -> bool { + matches!(c, b'1'..=b'9' | b'A'..=b'H' | b'J'..=b'N' | b'P'..=b'Z' | b'a'..=b'k' | b'm'..=b'z') +} + +fn is_valid_base32_char(c: u8) -> bool { + matches!(c, b'A'..=b'Z' | b'2'..=b'7' | b'=') +} + fn validate_ipfs_cid(env: &Env, hash: &Bytes) { let len = hash.len(); if len == 46 { @@ -943,88 +958,6 @@ fn post_job_with_id( .set(&DataKey::BidCount(job_id), &0u32); } -fn release_collateral(env: &Env, job_id: u64, freelancer: Address, _slash: bool) { - let _job: JobRecord = env - .storage() - .persistent() - .get(&DataKey::Job(job_id)) - .unwrap_or_else(|| panic_with_error!(env, JobRegistryError::JobNotFound)); - - let bids_key = DataKey::Bids(job_id); - let bids: Vec = env - .storage() - .persistent() - .get(&bids_key) - .unwrap_or_else(|| panic_with_error!(env, JobRegistryError::CollateralNotFound)); - - let mut updated_bids: Vec = Vec::new(env); - let mut found = false; - - for bid in bids.iter() { - if bid.freelancer == freelancer { - found = true; - if bid.collateral_released { - panic_with_error!(env, JobRegistryError::CollateralAlreadyReleased); - } - let mut updated = bid.clone(); - updated.collateral_released = true; - updated_bids.push_back(updated); - } else { - updated_bids.push_back(bid.clone()); - } - } - - if !found { - panic_with_error!(env, JobRegistryError::CollateralNotFound); - } - - env.storage().persistent().set(&bids_key, &updated_bids); -} - -fn release_collateral(env: &Env, job_id: u64, freelancer: Address, slash: bool) { - let bids_key = DataKey::Bids(job_id); - let mut bids: Vec = env - .storage() - .persistent() - .get(&bids_key) - .unwrap_or_else(|| panic_with_error!(env, JobRegistryError::BidNotFound)); - - let mut updated = false; - for i in 0..bids.len() { - let mut bid = bids.get(i).unwrap(); - if bid.freelancer == freelancer { - if bid.collateral_released { - panic_with_error!( - env, - JobRegistryError::CollateralAlreadyReleased - ); - } - bid.collateral_released = true; - bids.set(i, bid); - updated = true; - break; - } - } - - if !updated { - panic_with_error!(env, JobRegistryError::BidNotFound); - } - - env.storage().persistent().set(&bids_key, &bids); - - if slash { - env.events().publish( - (symbol_short!("slash"), job_id), - freelancer, - ); - } else { - env.events().publish( - (symbol_short!("release"), job_id), - freelancer, - ); - } -} - fn release_collateral(env: &Env, job_id: u64, freelancer: Address, slash: bool) { let bids_key = DataKey::Bids(job_id); let mut bids: Vec = env From d1c039fdbd0746f88fc7ea7ca8438a7e5f0d64fd Mon Sep 17 00:00:00 2001 From: Keshinro Tanitoluwa Joseph Date: Mon, 1 Jun 2026 09:53:33 +0100 Subject: [PATCH 2/2] Fix duplicate Bids variant in DataKey enum --- contracts/job_registry/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/contracts/job_registry/src/lib.rs b/contracts/job_registry/src/lib.rs index 49344dc..baf3e74 100644 --- a/contracts/job_registry/src/lib.rs +++ b/contracts/job_registry/src/lib.rs @@ -91,7 +91,6 @@ pub enum DataKey { Bid(u64, u32), BidIndex(u64, Address), Deliverable(u64), - Bids(u64), } #[contract]