diff --git a/apps/freenet-email-app/contracts/inbox/src/lib.rs b/apps/freenet-email-app/contracts/inbox/src/lib.rs index d89573acb..b6979c153 100644 --- a/apps/freenet-email-app/contracts/inbox/src/lib.rs +++ b/apps/freenet-email-app/contracts/inbox/src/lib.rs @@ -395,13 +395,6 @@ impl ContractInterface for Inbox { } } - fn validate_delta( - _parameters: Parameters<'static>, - delta: StateDelta<'static>, - ) -> Result { - Ok(UpdateInbox::try_from(delta).ok().is_some()) - } - fn update_state( parameters: Parameters<'static>, state: State<'static>, diff --git a/apps/freenet-email-app/web/container/src/lib.rs b/apps/freenet-email-app/web/container/src/lib.rs index 18ffad32f..7c92e8f2f 100644 --- a/apps/freenet-email-app/web/container/src/lib.rs +++ b/apps/freenet-email-app/web/container/src/lib.rs @@ -13,14 +13,7 @@ impl ContractInterface for Contract { ) -> Result { Ok(ValidateResult::Valid) } - - fn validate_delta( - _parameters: Parameters<'static>, - _delta: StateDelta<'static>, - ) -> Result { - Ok(true) - } - + fn update_state( _parameters: Parameters<'static>, state: State<'static>, diff --git a/apps/freenet-microblogging/contracts/posts/src/lib.rs b/apps/freenet-microblogging/contracts/posts/src/lib.rs index 3ef747768..43f8964f9 100644 --- a/apps/freenet-microblogging/contracts/posts/src/lib.rs +++ b/apps/freenet-microblogging/contracts/posts/src/lib.rs @@ -115,14 +115,6 @@ impl ContractInterface for PostsFeed { PostsFeed::try_from(state).map(|_| ValidateResult::Valid) } - fn validate_delta( - _parameters: Parameters<'static>, - delta: StateDelta<'static>, - ) -> Result { - serde_json::from_slice::>(&delta).map_err(|_| ContractError::InvalidDelta)?; - Ok(true) - } - fn update_state( parameters: Parameters<'static>, state: State<'static>, @@ -262,24 +254,6 @@ mod test { Ok(()) } - #[test] - fn validate_delta() -> Result<(), Box> { - let json = r#"[ - { - "author": "IDG", - "date": "2022-05-10T00:00:00Z", - "title": "Lore ipsum", - "content": "..." - } - ]"#; - let valid = PostsFeed::validate_delta( - [].as_ref().into(), - StateDelta::from(json.as_bytes().to_vec()), - )?; - assert!(valid); - Ok(()) - } - #[test] fn update_state() -> Result<(), Box> { let state = r#"{"messages":[{"author":"IDG","content":"...", diff --git a/apps/freenet-microblogging/web/container/src/lib.rs b/apps/freenet-microblogging/web/container/src/lib.rs index aa6f1111b..fd4d80283 100644 --- a/apps/freenet-microblogging/web/container/src/lib.rs +++ b/apps/freenet-microblogging/web/container/src/lib.rs @@ -15,13 +15,6 @@ impl ContractInterface for Contract { Ok(ValidateResult::Valid) } - fn validate_delta( - _parameters: Parameters<'static>, - _delta: StateDelta<'static>, - ) -> Result { - Ok(true) - } - fn update_state( _parameters: Parameters<'static>, state: State<'static>, diff --git a/apps/freenet-ping/contracts/ping/src/lib.rs b/apps/freenet-ping/contracts/ping/src/lib.rs index 61c9a66a8..8767d6713 100644 --- a/apps/freenet-ping/contracts/ping/src/lib.rs +++ b/apps/freenet-ping/contracts/ping/src/lib.rs @@ -20,21 +20,6 @@ impl ContractInterface for Contract { Ok(ValidateResult::Valid) } - fn validate_delta( - _parameters: Parameters<'static>, - delta: StateDelta<'static>, - ) -> Result { - let bytes = delta.as_ref(); - // allow empty delta - if bytes.is_empty() { - return Ok(true); - } - let _ = serde_json::from_slice::(bytes) - .map_err(|e| ContractError::Deser(e.to_string()))?; - - Ok(true) - } - fn update_state( parameters: Parameters<'static>, state: State<'static>, diff --git a/crates/core/src/contract/executor/runtime.rs b/crates/core/src/contract/executor/runtime.rs index 6461cc0cc..54dcb9645 100644 --- a/crates/core/src/contract/executor/runtime.rs +++ b/crates/core/src/contract/executor/runtime.rs @@ -89,20 +89,6 @@ impl ContractExecutor for Executor { vec![UpdateData::State(incoming_state.clone().into())] } Either::Right(delta) => { - let valid = self - .runtime - .validate_delta(&key, ¶ms, &delta) - .map_err(|err| { - if remove_if_fail { - let _ = self.runtime.contract_store.remove_contract(&key); - } - ExecutorError::other(err) - })?; - if !valid { - return Err(ExecutorError::request(StdContractError::invalid_update( - key, - ))); - } // todo: forward delta like we are doing with puts vec![UpdateData::Delta(delta)] } diff --git a/crates/core/src/wasm_runtime/contract.rs b/crates/core/src/wasm_runtime/contract.rs index c65aa05c6..074067727 100644 --- a/crates/core/src/wasm_runtime/contract.rs +++ b/crates/core/src/wasm_runtime/contract.rs @@ -19,16 +19,6 @@ pub(crate) trait ContractRuntimeInterface { related: &RelatedContracts<'_>, ) -> RuntimeResult; - /// Verify that a delta is valid - at least as much as possible. The goal is to prevent DDoS of - /// a contract by sending a large number of invalid delta updates. This allows peers - /// to verify a delta before forwarding it. - fn validate_delta( - &mut self, - key: &ContractKey, - parameters: &Parameters<'_>, - delta: &StateDelta<'_>, - ) -> RuntimeResult; - /// Determine whether this delta is a valid update for this contract. If it is, return the modified state, /// else return error. /// @@ -116,47 +106,6 @@ impl ContractRuntimeInterface for super::Runtime { Ok(is_valid) } - fn validate_delta<'a>( - &mut self, - key: &ContractKey, - parameters: &Parameters<'a>, - delta: &StateDelta<'a>, - ) -> RuntimeResult { - // todo: if we keep this hot in memory on next calls overwrite the buffer with new delta - let req_bytes = parameters.size() + delta.size(); - let running = self.prepare_contract_call(key, parameters, req_bytes)?; - let linear_mem = self.linear_mem(&running.instance)?; - - let param_buf_ptr = { - let mut param_buf = self.init_buf(&running.instance, parameters)?; - param_buf.write(parameters)?; - param_buf.ptr() - }; - let delta_buf_ptr = { - let mut delta_buf = self.init_buf(&running.instance, delta)?; - delta_buf.write(delta)?; - delta_buf.ptr() - }; - - let validate_func: TypedFunction<(i64, i64), FfiReturnTy> = running - .instance - .exports - .get_typed_function(&self.wasm_store, "validate_delta")?; - let is_valid = unsafe { - ContractInterfaceResult::from_raw( - validate_func.call( - &mut self.wasm_store, - param_buf_ptr as i64, - delta_buf_ptr as i64, - )?, - &linear_mem, - ) - .unwrap_validate_delta_res(linear_mem) - .map_err(Into::::into)? - }; - Ok(is_valid) - } - fn update_state( &mut self, key: &ContractKey, diff --git a/crates/core/src/wasm_runtime/tests/contract.rs b/crates/core/src/wasm_runtime/tests/contract.rs index 70ebaf898..6b4266f2c 100644 --- a/crates/core/src/wasm_runtime/tests/contract.rs +++ b/crates/core/src/wasm_runtime/tests/contract.rs @@ -37,34 +37,6 @@ fn validate_state() -> Result<(), Box> { Ok(()) } -#[test] -fn validate_delta() -> Result<(), Box> { - let TestSetup { - contract_store, - delegate_store, - secrets_store, - contract_key, - temp_dir, - } = super::setup_test_contract(TEST_CONTRACT_1)?; - let mut runtime = Runtime::build(contract_store, delegate_store, secrets_store, false).unwrap(); - - let is_valid = runtime.validate_delta( - &contract_key, - &Parameters::from([].as_ref()), - &StateDelta::from([1, 2, 3, 4].as_ref()), - )?; - assert!(is_valid); - - let not_valid = !runtime.validate_delta( - &contract_key, - &Parameters::from([].as_ref()), - &StateDelta::from([1, 0, 0, 1].as_ref()), - )?; - assert!(not_valid); - std::mem::drop(temp_dir); - Ok(()) -} - #[test] fn update_state() -> Result<(), Box> { let TestSetup { diff --git a/modules/antiflood-tokens/contracts/token-allocation-record/src/lib.rs b/modules/antiflood-tokens/contracts/token-allocation-record/src/lib.rs index a5260cb81..b90326a48 100644 --- a/modules/antiflood-tokens/contracts/token-allocation-record/src/lib.rs +++ b/modules/antiflood-tokens/contracts/token-allocation-record/src/lib.rs @@ -28,25 +28,6 @@ impl ContractInterface for TokenAllocContract { Ok(ValidateResult::Valid) } - /// The contract verifies that the release times for a tier matches the tier. - /// - /// For example, a 15:30 UTC release time isn't permitted for hour_1 tier, but 15:00 UTC is permitted. - fn validate_delta( - parameters: Parameters<'static>, - delta: StateDelta<'static>, - ) -> Result { - let assigned_token = TokenAssignment::try_from(delta)?; - let params = TokenDelegateParameters::try_from(parameters)?; - #[allow(clippy::redundant_clone)] - let verifying_key = VerifyingKey::::new(params.generator_public_key.clone()); - let verification = assigned_token.is_valid(&verifying_key); - if verification.is_err() { - log_verification_err(¶ms.generator_public_key, "validate delta"); - } - log_succesful_ver(¶ms.generator_public_key, "validate delta"); - Ok(verification.is_ok()) - } - fn update_state( parameters: Parameters<'static>, state: State<'static>, diff --git a/stdlib b/stdlib index f3d68931c..c900428d6 160000 --- a/stdlib +++ b/stdlib @@ -1 +1 @@ -Subproject commit f3d68931ccbc9df37038b0246baf8ffeb7d693d2 +Subproject commit c900428d67506986ec358b7adab4e064cc8b8206 diff --git a/tests/test-app-1/container/src/lib.rs b/tests/test-app-1/container/src/lib.rs index 897ec084f..8761c769c 100644 --- a/tests/test-app-1/container/src/lib.rs +++ b/tests/test-app-1/container/src/lib.rs @@ -12,13 +12,6 @@ impl ContractInterface for Contract { Ok(ValidateResult::Valid) } - fn validate_delta( - _parameters: Parameters<'static>, - _delta: StateDelta<'static>, - ) -> Result { - Ok(true) - } - fn update_state( _parameters: Parameters<'static>, state: State<'static>, diff --git a/tests/test-app-1/deps/src/lib.rs b/tests/test-app-1/deps/src/lib.rs index 897ec084f..8761c769c 100644 --- a/tests/test-app-1/deps/src/lib.rs +++ b/tests/test-app-1/deps/src/lib.rs @@ -12,13 +12,6 @@ impl ContractInterface for Contract { Ok(ValidateResult::Valid) } - fn validate_delta( - _parameters: Parameters<'static>, - _delta: StateDelta<'static>, - ) -> Result { - Ok(true) - } - fn update_state( _parameters: Parameters<'static>, state: State<'static>, diff --git a/tests/test-contract-1/src/lib.rs b/tests/test-contract-1/src/lib.rs index a41afc2ef..af1b232cd 100644 --- a/tests/test-contract-1/src/lib.rs +++ b/tests/test-contract-1/src/lib.rs @@ -23,20 +23,6 @@ impl ContractInterface for Contract { } } - fn validate_delta( - _parameters: Parameters<'static>, - delta: StateDelta<'static>, - ) -> Result { - let bytes = delta.as_ref(); - if bytes.len() == 4 && bytes == [1, 2, 3, 4] { - Ok(true) - } else if bytes.len() != 4 { - Err(ContractError::InvalidDelta) - } else { - Ok(false) - } - } - fn update_state( _parameters: Parameters<'static>, state: State<'static>,