-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement contract TTL extension functions (#24)
* Add live-until cells for contracts and code * Implement extend ttl host functions * add test * Set Version: 0.1.17 * add fuzz test * fix side conditions and refactor --------- Co-authored-by: devops <[email protected]>
- Loading branch information
1 parent
05c4936
commit 0b2d0f2
Showing
10 changed files
with
261 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.1.16 | ||
0.1.17 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" | |
|
||
[tool.poetry] | ||
name = "ksoroban" | ||
version = "0.1.16" | ||
version = "0.1.17" | ||
description = "K tooling for the Soroban platform" | ||
authors = [ | ||
"Runtime Verification, Inc. <[email protected]>", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
|
||
setExitCode(1) | ||
|
||
uploadWasm( b"test-wasm", | ||
;; #![no_std] | ||
;; use soroban_sdk::{contract, contractimpl, Env}; | ||
;; | ||
;; #[contract] | ||
;; pub struct IncrementContract; | ||
;; | ||
;; #[contractimpl] | ||
;; impl IncrementContract { | ||
;; pub fn extend_ttl(env: Env, threshold: u32, extend_to: u32) { | ||
;; env.storage().instance().extend_ttl(threshold, extend_to); | ||
;; } | ||
;; } | ||
(module $soroban_increment_contract.wasm | ||
(type (;0;) (func (param i64 i64) (result i64))) | ||
(type (;1;) (func)) | ||
(import "l" "8" (func $_ZN17soroban_env_guest5guest6ledger45extend_current_contract_instance_and_code_ttl17h6e6604048593c195E (type 0))) | ||
(func $extend_ttl (type 0) (param i64 i64) (result i64) | ||
block ;; label = @1 | ||
local.get 0 | ||
i64.const 255 | ||
i64.and | ||
i64.const 4 | ||
i64.ne | ||
br_if 0 (;@1;) | ||
local.get 1 | ||
i64.const 255 | ||
i64.and | ||
i64.const 4 | ||
i64.ne | ||
br_if 0 (;@1;) | ||
local.get 0 | ||
i64.const -4294967292 | ||
i64.and | ||
local.get 1 | ||
i64.const -4294967292 | ||
i64.and | ||
call $_ZN17soroban_env_guest5guest6ledger45extend_current_contract_instance_and_code_ttl17h6e6604048593c195E | ||
drop | ||
i64.const 2 | ||
return | ||
end | ||
unreachable | ||
unreachable) | ||
(func $_ (type 1)) | ||
(memory (;0;) 16) | ||
(global $__stack_pointer (mut i32) (i32.const 1048576)) | ||
(global (;1;) i32 (i32.const 1048576)) | ||
(global (;2;) i32 (i32.const 1048576)) | ||
(export "memory" (memory 0)) | ||
(export "extend_ttl" (func $extend_ttl)) | ||
(export "_" (func $_)) | ||
(export "__data_end" (global 1)) | ||
(export "__heap_base" (global 2))) | ||
) | ||
|
||
setAccount(Account(b"test-account"), 9876543210) | ||
|
||
deployContract( | ||
Account(b"test-account"), | ||
Contract(b"test-sc"), | ||
b"test-wasm", | ||
.List | ||
) | ||
|
||
callTx( | ||
Account(b"test-caller"), | ||
Contract(b"test-sc"), | ||
"extend_ttl", | ||
ListItem(U32(100)) ListItem(U32(100)), | ||
Void | ||
) | ||
|
||
setExitCode(0) |
15 changes: 15 additions & 0 deletions
15
src/tests/integration/data/soroban/contracts/test_ttl/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[package] | ||
name = "test_ttl" | ||
version = "0.0.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[lib] | ||
crate-type = ["cdylib"] | ||
doctest = false | ||
|
||
[dependencies] | ||
soroban-sdk = { workspace = true } | ||
|
||
[dev-dependencies] | ||
soroban-sdk = { workspace = true, features = ["testutils"] } |
18 changes: 18 additions & 0 deletions
18
src/tests/integration/data/soroban/contracts/test_ttl/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
A quick example of a contract that can be ran with `ksoroban test` | ||
|
||
You will need to have the stellar cli utils installed: | ||
https://developers.stellar.org/docs/build/smart-contracts/getting-started/setup | ||
|
||
And the soroban semantics kompiled: | ||
``` | ||
kdist build soroban-semantics.llvm | ||
``` | ||
|
||
And then (from this directory): | ||
|
||
```sh | ||
soroban contract build --out-dir output | ||
ksoroban test output/test_adder.wasm | ||
``` | ||
|
||
`ksoroban test` should exit successfully |
44 changes: 44 additions & 0 deletions
44
src/tests/integration/data/soroban/contracts/test_ttl/src/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#![no_std] | ||
use soroban_sdk::{contract, contractimpl, Env, Val}; | ||
|
||
#[contract] | ||
pub struct TtlContract; | ||
|
||
extern "C" { | ||
fn kasmer_set_ledger_sequence(x : u64); | ||
} | ||
|
||
fn set_ledger_sequence(x: u32) { | ||
unsafe { | ||
kasmer_set_ledger_sequence(Val::from_u32(x).to_val().get_payload()); | ||
} | ||
} | ||
|
||
#[contractimpl] | ||
impl TtlContract { | ||
|
||
pub fn test_ttl( | ||
env: Env, | ||
init_live_until: u32, | ||
seq: u32, | ||
threshold: u32, | ||
extend_to: u32 | ||
) -> bool { | ||
|
||
// Given: | ||
// contract is still alive and extend_ttl inputs are valid | ||
if seq <= init_live_until && threshold <= extend_to { | ||
env.storage().instance().extend_ttl(0, init_live_until); | ||
set_ledger_sequence(seq); | ||
|
||
// When: | ||
env.storage().instance().extend_ttl(threshold, extend_to); | ||
} | ||
|
||
// Since there is no getter function for the TTL value, we cannot verify | ||
// if `extend_ttl` works as expected. | ||
// Currently, we only check if the function runs without errors. | ||
// Consider adding a custom hook to retrieve the TTL value for more thorough testing. | ||
true | ||
} | ||
} |