Skip to content

Commit

Permalink
wip: fix rent charge and add test contract
Browse files Browse the repository at this point in the history
  • Loading branch information
jayz22 committed Sep 6, 2023
1 parent 3d6c35d commit 04ec695
Show file tree
Hide file tree
Showing 21 changed files with 68 additions and 10 deletions.
3 changes: 2 additions & 1 deletion soroban-env-host/src/e2e_invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,10 @@ pub fn extract_rent_changes(ledger_changes: &Vec<LedgerEntryChange>) -> Vec<Ledg
&entry_change.expiration_change,
&entry_change.encoded_new_value,
) {
// Skip non-bumped entries as well.
// Skip the entry if 1. it is not bumped and 2. the entry size has not increased
if expiration_change.old_expiration_ledger
>= expiration_change.new_expiration_ledger
&& entry_change.old_entry_size_bytes >= encoded_new_value.len() as u32
{
return None;
}
Expand Down
44 changes: 40 additions & 4 deletions soroban-env-host/src/test/invocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ use std::rc::Rc;

use expect_test::expect;
use soroban_env_common::{
xdr::{self, ScErrorCode},
Env, EnvBase, TryFromVal, Val,
xdr::{
self, ContractDataDurability, LedgerKey, LedgerKeyContractData, ScErrorCode, ScSymbol,
ScVal,
},
Env, EnvBase, TryFromVal, U64Small, Val,
};

use crate::{
events::HostEvent, xdr::ScErrorType, ContractFunctionSet, Error, Host, HostError, Symbol, Tag,
budget::AsBudget, events::HostEvent, xdr::ScErrorType, ContractFunctionSet, Error, Host,
HostError, Symbol, Tag,
};
use soroban_test_wasms::{ADD_I32, ALLOC, ERR, INVOKE_CONTRACT, VEC};
use soroban_test_wasms::{ADD_I32, ALLOC, CONTRACT_STORAGE, ERR, INVOKE_CONTRACT, VEC};

#[test]
fn invoke_single_contract_function() -> Result<(), HostError> {
Expand Down Expand Up @@ -273,3 +277,35 @@ fn wasm_invoke_return_err_variants() -> Result<(), HostError> {
}
Ok(())
}

#[test]
fn invoke_single_contract_function2() -> Result<(), HostError> {
let host = Host::test_host_with_recording_footprint();
let contract_id_obj = host.register_test_contract_wasm(CONTRACT_STORAGE);
// put_persistent
let func = host.symbol_new_from_slice("put_persistent")?;
let key = Symbol::try_from_small_str("a")?;
let mut args = host.vec_new()?;
args = host.vec_push_back(args, key.into())?;
args = host.vec_push_back(args, U64Small::try_from(1u64).unwrap().into())?;
let _res = host.call(contract_id_obj, func.into(), args)?;

// increase_entry_size
let func = host.symbol_new_from_slice("increase_entry_size")?;
let mut args = host.vec_new()?;
args = host.vec_push_back(args, key.into())?;
let _res = host.call(contract_id_obj, func.into(), args)?;

// let v = host.get_contract_data(key.into(), soroban_env_common::StorageType::Persistent)?;
// println!("{:?}", v);

let k = Rc::new(LedgerKey::ContractData(LedgerKeyContractData {
key: ScVal::Symbol(ScSymbol::try_from("a").unwrap()),
durability: ContractDataDurability::Persistent,
contract: xdr::ScAddress::Contract(host.contract_id_from_address(contract_id_obj)?),
}));
let v = host.with_mut_storage(|s| s.get(&k, host.as_budget()))?;
println!("{:?}", v);

Ok(())
}
31 changes: 26 additions & 5 deletions soroban-test-wasms/wasm-workspace/contract_data/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,15 @@ impl Contract {
e.storage().persistent().get(&key).unwrap()
}

pub fn bump_persistent(e: Env, key: Symbol, low_expiration_watermark: u32, high_expiration_watermark: u32) {
e.storage().persistent().bump(&key, low_expiration_watermark, high_expiration_watermark)
pub fn bump_persistent(
e: Env,
key: Symbol,
low_expiration_watermark: u32,
high_expiration_watermark: u32,
) {
e.storage()
.persistent()
.bump(&key, low_expiration_watermark, high_expiration_watermark)
}

pub fn put_temporary(e: Env, key: Symbol, val: u64) {
Expand All @@ -42,8 +49,15 @@ impl Contract {
e.storage().temporary().get(&key).unwrap()
}

pub fn bump_temporary(e: Env, key: Symbol, low_expiration_watermark: u32, high_expiration_watermark: u32) {
e.storage().temporary().bump(&key, low_expiration_watermark, high_expiration_watermark)
pub fn bump_temporary(
e: Env,
key: Symbol,
low_expiration_watermark: u32,
high_expiration_watermark: u32,
) {
e.storage()
.temporary()
.bump(&key, low_expiration_watermark, high_expiration_watermark)
}

pub fn put_instance(e: Env, key: Symbol, val: u64) {
Expand All @@ -63,6 +77,13 @@ impl Contract {
}

pub fn bump_instance(e: Env, low_expiration_watermark: u32, high_expiration_watermark: u32) {
e.storage().instance().bump(low_expiration_watermark, high_expiration_watermark)
e.storage()
.instance()
.bump(low_expiration_watermark, high_expiration_watermark)
}

pub fn increase_entry_size(e: Env, key: Symbol) {
let u: u64 = e.storage().persistent().get(&key).unwrap();
e.storage().persistent().set(&key, &u128::from(u))
}
}
Binary file modified soroban-test-wasms/wasm-workspace/opt/auth_test_contract.wasm
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_add_f32.wasm
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_add_i32.wasm
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_alloc.wasm
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_complex.wasm
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_contract_data.wasm
Binary file not shown.
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_err.wasm
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_fannkuch.wasm
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_fib.wasm
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_hostile.wasm
Binary file not shown.
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_linear_memory.wasm
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_vec.wasm
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 04ec695

Please sign in to comment.