Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove validate delta #1274

Merged
merged 2 commits into from
Oct 19, 2024
Merged
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
7 changes: 0 additions & 7 deletions apps/freenet-email-app/contracts/inbox/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,13 +395,6 @@ impl ContractInterface for Inbox {
}
}

fn validate_delta(
_parameters: Parameters<'static>,
delta: StateDelta<'static>,
) -> Result<bool, ContractError> {
Ok(UpdateInbox::try_from(delta).ok().is_some())
}

fn update_state(
parameters: Parameters<'static>,
state: State<'static>,
Expand Down
9 changes: 1 addition & 8 deletions apps/freenet-email-app/web/container/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,7 @@ impl ContractInterface for Contract {
) -> Result<ValidateResult, ContractError> {
Ok(ValidateResult::Valid)
}

fn validate_delta(
_parameters: Parameters<'static>,
_delta: StateDelta<'static>,
) -> Result<bool, ContractError> {
Ok(true)
}


fn update_state(
_parameters: Parameters<'static>,
state: State<'static>,
Expand Down
26 changes: 0 additions & 26 deletions apps/freenet-microblogging/contracts/posts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool, ContractError> {
serde_json::from_slice::<Vec<Post>>(&delta).map_err(|_| ContractError::InvalidDelta)?;
Ok(true)
}

fn update_state(
parameters: Parameters<'static>,
state: State<'static>,
Expand Down Expand Up @@ -262,24 +254,6 @@ mod test {
Ok(())
}

#[test]
fn validate_delta() -> Result<(), Box<dyn std::error::Error>> {
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<dyn std::error::Error>> {
let state = r#"{"messages":[{"author":"IDG","content":"...",
Expand Down
7 changes: 0 additions & 7 deletions apps/freenet-microblogging/web/container/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ impl ContractInterface for Contract {
Ok(ValidateResult::Valid)
}

fn validate_delta(
_parameters: Parameters<'static>,
_delta: StateDelta<'static>,
) -> Result<bool, ContractError> {
Ok(true)
}

fn update_state(
_parameters: Parameters<'static>,
state: State<'static>,
Expand Down
15 changes: 0 additions & 15 deletions apps/freenet-ping/contracts/ping/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,6 @@ impl ContractInterface for Contract {
Ok(ValidateResult::Valid)
}

fn validate_delta(
_parameters: Parameters<'static>,
delta: StateDelta<'static>,
) -> Result<bool, ContractError> {
let bytes = delta.as_ref();
// allow empty delta
if bytes.is_empty() {
return Ok(true);
}
let _ = serde_json::from_slice::<Ping>(bytes)
.map_err(|e| ContractError::Deser(e.to_string()))?;

Ok(true)
}

fn update_state(
parameters: Parameters<'static>,
state: State<'static>,
Expand Down
14 changes: 0 additions & 14 deletions crates/core/src/contract/executor/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,6 @@ impl ContractExecutor for Executor<Runtime> {
vec![UpdateData::State(incoming_state.clone().into())]
}
Either::Right(delta) => {
let valid = self
.runtime
.validate_delta(&key, &params, &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)]
}
Expand Down
51 changes: 0 additions & 51 deletions crates/core/src/wasm_runtime/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,6 @@ pub(crate) trait ContractRuntimeInterface {
related: &RelatedContracts<'_>,
) -> RuntimeResult<ValidateResult>;

/// 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<bool>;

/// Determine whether this delta is a valid update for this contract. If it is, return the modified state,
/// else return error.
///
Expand Down Expand Up @@ -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<bool> {
// 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::<ContractExecError>::into)?
};
Ok(is_valid)
}

fn update_state(
&mut self,
key: &ContractKey,
Expand Down
28 changes: 0 additions & 28 deletions crates/core/src/wasm_runtime/tests/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,34 +37,6 @@ fn validate_state() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

#[test]
fn validate_delta() -> Result<(), Box<dyn std::error::Error>> {
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<dyn std::error::Error>> {
let TestSetup {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool, ContractError> {
let assigned_token = TokenAssignment::try_from(delta)?;
let params = TokenDelegateParameters::try_from(parameters)?;
#[allow(clippy::redundant_clone)]
let verifying_key = VerifyingKey::<Sha256>::new(params.generator_public_key.clone());
let verification = assigned_token.is_valid(&verifying_key);
if verification.is_err() {
log_verification_err(&params.generator_public_key, "validate delta");
}
log_succesful_ver(&params.generator_public_key, "validate delta");
Ok(verification.is_ok())
}

fn update_state(
parameters: Parameters<'static>,
state: State<'static>,
Expand Down
16 changes: 4 additions & 12 deletions tests/test-app-1/Cargo.lock

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

2 changes: 1 addition & 1 deletion tests/test-app-1/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ panic = 'abort'
strip = true

[workspace.dependencies]
freenet-stdlib = { version = "0.0.7", default-features = false }
freenet-stdlib = { path = "../../stdlib/rust", default-features = false }
7 changes: 0 additions & 7 deletions tests/test-app-1/container/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ impl ContractInterface for Contract {
Ok(ValidateResult::Valid)
}

fn validate_delta(
_parameters: Parameters<'static>,
_delta: StateDelta<'static>,
) -> Result<bool, ContractError> {
Ok(true)
}

fn update_state(
_parameters: Parameters<'static>,
state: State<'static>,
Expand Down
7 changes: 0 additions & 7 deletions tests/test-app-1/deps/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ impl ContractInterface for Contract {
Ok(ValidateResult::Valid)
}

fn validate_delta(
_parameters: Parameters<'static>,
_delta: StateDelta<'static>,
) -> Result<bool, ContractError> {
Ok(true)
}

fn update_state(
_parameters: Parameters<'static>,
state: State<'static>,
Expand Down
14 changes: 0 additions & 14 deletions tests/test-contract-1/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,6 @@ impl ContractInterface for Contract {
}
}

fn validate_delta(
_parameters: Parameters<'static>,
delta: StateDelta<'static>,
) -> Result<bool, ContractError> {
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>,
Expand Down
Loading