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

Eliminate contract index duplication between commit objects #404

Merged
merged 31 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
bc2760b
piecrust: added counter float test
miloszm Nov 7, 2024
eba7a0d
piecrust: encapsulation of contract index element
miloszm Nov 8, 2024
f727258
piecrust: commit store and base fields in commit
miloszm Nov 8, 2024
5734e3a
piecrust: populating only shallow elements in commit
miloszm Nov 8, 2024
9efaae3
piecrust: removed index redundancy
miloszm Nov 12, 2024
86ef718
piecrust: bumped version to 0.26.1-rc.1
miloszm Nov 13, 2024
c41f41f
piecrust: main index in commit store
miloszm Nov 13, 2024
57c72e5
piecrust: bumped version to 0.26.1-rc.2
miloszm Nov 13, 2024
2b0ac1c
piecrust: setting commit id and base before adding to commit store
miloszm Nov 13, 2024
7166e57
piecrust: eliminated hulk commit
miloszm Nov 14, 2024
0f4a3c5
piecrust: storing entire merkle
miloszm Nov 17, 2024
306bdc1
piecrust: introduced tree positions file
miloszm Nov 18, 2024
988d9f2
piecrust: bumped version to 0.26.1-rc.3
miloszm Nov 22, 2024
a649601
piecrust: added callstack api
miloszm Nov 25, 2024
1f5de1e
piecrust-uplink: added callstack api
miloszm Nov 25, 2024
f1507fe
piecrust: improved implementation and test
miloszm Nov 25, 2024
34b2bdf
piecrust: optimized callstack implementation
miloszm Nov 25, 2024
f52f027
piecrust: changed version to 0.27.0-rc.0
miloszm Nov 25, 2024
3719dbf
piecrust: bumped version to 0.27.0-rc.0
miloszm Nov 26, 2024
23c634a
piecrust-uplink: bumped version to 0.17.2-rc.0
miloszm Nov 26, 2024
b41d205
piecrust: updated uplink dependency to 0.17.2-rc.0
miloszm Nov 26, 2024
9b5b43e
piecrust: added test for ICCs not rolled back when panic
miloszm Nov 28, 2024
d7f8597
piecrust: optimisation of the merkle position file
miloszm Dec 2, 2024
2749b8e
piecrust: chunking for writing merkle position file
miloszm Dec 2, 2024
e1370be
Merge pull request #411 from dusk-network/issue-410
miloszm Dec 4, 2024
ffecfed
piecrust: bumped version to 0.27.0-rc.1
miloszm Dec 4, 2024
37faefc
piecrust: bumped version to 0.27.0
miloszm Dec 18, 2024
7826494
piecrust-uplink: merged serde changes
miloszm Dec 19, 2024
ef43c8e
piecrust-uplink: bumped version to 0.17.3
miloszm Dec 19, 2024
acd2956
piecrust: bumped uplink dependency to 0.17.3
miloszm Dec 19, 2024
2ed5eff
Merge branch 'main' into full-delta-index
miloszm Jan 9, 2025
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
2 changes: 2 additions & 0 deletions contracts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ members = [
"box",
"c-example",
"callcenter",
"callstack",
"initializer",
"counter",
"counter_float",
"debugger",
"double_counter",
"empty_initializer",
Expand Down
27 changes: 27 additions & 0 deletions contracts/callcenter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ impl Callcenter {
uplink::caller()
}

/// Return the entire call stack of this contract
pub fn return_callstack(&self) -> Vec<ContractId> {
uplink::callstack()
}

/// Make sure that the caller of this contract is the contract itself
pub fn call_self(&self) -> Result<bool, ContractError> {
let self_id = uplink::self_id();
Expand All @@ -90,6 +95,16 @@ impl Callcenter {
}
}

/// Return a call stack after calling itself n times
pub fn call_self_n_times(&self, n: u32) -> Vec<ContractId> {
let self_id = uplink::self_id();
match n {
0 => uplink::callstack(),
_ => uplink::call(self_id, "call_self_n_times", &(n - 1))
.expect("calling self should succeed")
}
}

/// Calls the `spend` function of the `contract` with no arguments, and the
/// given `gas_limit`, assuming the called function returns `()`. It will
/// then return the call's result itself.
Expand Down Expand Up @@ -133,6 +148,12 @@ unsafe fn call_self(arg_len: u32) -> u32 {
wrap_call(arg_len, |_: ()| STATE.call_self())
}

/// Expose `Callcenter::call_self_n_times()` to the host
#[no_mangle]
unsafe fn call_self_n_times(arg_len: u32) -> u32 {
wrap_call(arg_len, |n: u32| STATE.call_self_n_times(n))
}

/// Expose `Callcenter::call_spend_with_limit` to the host
#[no_mangle]
unsafe fn call_spend_with_limit(arg_len: u32) -> u32 {
Expand All @@ -153,6 +174,12 @@ unsafe fn return_caller(arg_len: u32) -> u32 {
wrap_call(arg_len, |_: ()| STATE.return_caller())
}

/// Expose `Callcenter::return_callstack()` to the host
#[no_mangle]
unsafe fn return_callstack(arg_len: u32) -> u32 {
wrap_call(arg_len, |_: ()| STATE.return_callstack())
}

/// Expose `Callcenter::delegate_query()` to the host
#[no_mangle]
unsafe fn delegate_query(arg_len: u32) -> u32 {
Expand Down
15 changes: 15 additions & 0 deletions contracts/callstack/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "callstack"
version = "0.1.0"
authors = [
"Milosz Muszynski <[email protected]>",
]
edition = "2021"

license = "MPL-2.0"

[dependencies]
piecrust-uplink = { path = "../../piecrust-uplink", features = ["abi", "dlmalloc"] }

[lib]
crate-type = ["cdylib", "rlib"]
34 changes: 34 additions & 0 deletions contracts/callstack/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) DUSK NETWORK. All rights reserved.

//! Contract which exposes the call stack

#![no_std]

extern crate alloc;

use piecrust_uplink as uplink;
use alloc::vec::Vec;
use uplink::ContractId;

/// Struct that describes the state of the contract
pub struct CallStack;

/// State of the Counter contract
static mut STATE: CallStack = CallStack;

impl CallStack {
/// Return the call stack
pub fn return_callstack(&self) -> Vec<ContractId> {
uplink::callstack()
}
}

/// Expose `CallStack::read_callstack()` to the host
#[no_mangle]
unsafe fn return_callstack(arg_len: u32) -> u32 {
uplink::wrap_call_unchecked(arg_len, |_: ()| STATE.return_callstack())
}
16 changes: 16 additions & 0 deletions contracts/counter_float/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "counter_float"
version = "0.1.0"
authors = [
"Kristoffer Ström <[email protected]>",
"Milosz Muszynski <[email protected]>",
]
edition = "2021"

license = "MPL-2.0"

[dependencies]
piecrust-uplink = { path = "../../piecrust-uplink", features = ["abi", "dlmalloc"] }

[lib]
crate-type = ["cdylib", "rlib"]
45 changes: 45 additions & 0 deletions contracts/counter_float/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) DUSK NETWORK. All rights reserved.

//! Contract to implement a simple counter that can be read and incremented by
//! one count.

#![no_std]

use piecrust_uplink as uplink;

/// Struct that describes the state of the Counter contract
pub struct Counter {
value: f64,
}

/// State of the Counter contract
static mut STATE: Counter = Counter { value: 0xfc as f64 };

impl Counter {
/// Read the value of the counter
pub fn read_value(&self) -> f64 {
self.value
}

/// Increment the value of the counter by 1
pub fn increment(&mut self) {
let value = self.value + 1.0;
self.value = value;
}
}

/// Expose `Counter::read_value()` to the host
#[no_mangle]
unsafe fn read_value(arg_len: u32) -> u32 {
uplink::wrap_call_unchecked(arg_len, |_: ()| STATE.read_value())
}

/// Expose `Counter::increment()` to the host
#[no_mangle]
unsafe fn increment(arg_len: u32) -> u32 {
uplink::wrap_call_unchecked(arg_len, |_: ()| STATE.increment())
}
29 changes: 29 additions & 0 deletions contracts/crossover/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,27 @@ impl Crossover {
self.set_crossover(value_to_set);
}

// Chain of ICC is not being rolled back when a callee panics and its panic
// is not propagated up the call chain.
pub fn check_iccs_dont_rollback(
&mut self,
contract: ContractId,
value_to_set: i32,
) {
self.set_crossover(value_to_set);

const ANY_VALUE_1: i32 = 5;
const ANY_VALUE_2: i32 = 6;

uplink::debug!("calling panicking contract {contract:?}");
uplink::call::<_, ()>(
contract,
"set_back_and_panic",
&(ANY_VALUE_1, ANY_VALUE_2),
)
.expect_err("should give an error on a panic");
}

// Sets the contract's value and then calls its caller's [`set_crossover`]
// call to set their value. The caller is assumed to be another crossover
// contract.
Expand Down Expand Up @@ -125,6 +146,14 @@ unsafe fn check_consistent_state_on_errors(arg_len: u32) -> u32 {
})
}

/// Expose `Crossover::check_iccs_dont_rollback()` to the host
#[no_mangle]
unsafe fn check_iccs_dont_rollback(arg_len: u32) -> u32 {
uplink::wrap_call(arg_len, |(contract, s)| {
STATE.check_iccs_dont_rollback(contract, s)
})
}

/// Expose `Crossover::set_back_and_panic()` to the host
#[no_mangle]
unsafe fn set_back_and_panic(arg_len: u32) -> u32 {
Expand Down
15 changes: 14 additions & 1 deletion piecrust-uplink/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.17.3] - 2024-12-19

## [0.17.2] - 2024-12-17

### Added

- Add serde `Serialize` and `Deserialize` implementations for `ContractId` and `Event` [#414]
- Add `serde`, `hex`, `base64` and `serde_json` optional dependencies [#414]
- Add `serde` feature [#414]

## [0.17.1] - 2024-09-24

### Changed
Expand Down Expand Up @@ -216,6 +226,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- First `piecrust-uplink` release

<!-- ISSUES -->
[#414]: https://github.com/dusk-network/piecrust/issues/414
[#375]: https://github.com/dusk-network/piecrust/issues/375
[#365]: https://github.com/dusk-network/piecrust/issues/365
[#357]: https://github.com/dusk-network/piecrust/issues/357
Expand All @@ -241,7 +252,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#136]: https://github.com/dusk-network/piecrust/issues/136

<!-- VERSIONS -->
[Unreleased]: https://github.com/dusk-network/piecrust/compare/uplink-0.17.1...HEAD
[Unreleased]: https://github.com/dusk-network/piecrust/compare/uplink-0.17.3...HEAD
[0.17.3]: https://github.com/dusk-network/piecrust/compare/uplink-0.17.2...uplink-0.17.3
[0.17.2]: https://github.com/dusk-network/piecrust/compare/uplink-0.17.1...uplink-0.17.2
[0.17.1]: https://github.com/dusk-network/piecrust/compare/uplink-0.17.0...uplink-0.17.1
[0.17.0]: https://github.com/dusk-network/piecrust/compare/uplink-0.16.0...uplink-0.17.0
[0.16.0]: https://github.com/dusk-network/piecrust/compare/uplink-0.15.0...uplink-0.16.0
Expand Down
10 changes: 9 additions & 1 deletion piecrust-uplink/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ categories = ["wasm", "no-std", "cryptography::cryptocurrencies"]
keywords = ["virtual", "machine", "smart", "contract", "wasm"]

repository = "https://github.com/dusk-network/piecrust"
version = "0.17.1"
version = "0.17.3"

edition = "2021"
license = "MPL-2.0"
Expand All @@ -16,10 +16,18 @@ license = "MPL-2.0"
rkyv = { version = "0.7", default-features = false, features = ["size_32", "alloc", "validation"] }
bytecheck = { version = "0.6", default-features = false }
dlmalloc = { version = "0.2", optional = true, features = ["global"] }
serde = { version = "1.0", optional = true }
hex = { version = "0.4" , optional = true }
base64 = { version = "0.22", optional = true }
serde_json = { version = "1.0", optional = true }

[dev-dependencies]
rand = "0.8"

[features]
abi = []
debug = []
serde = ["dep:serde", "serde_json", "hex", "base64"]

[package.metadata.docs.rs]
all-features = true
Expand Down
17 changes: 17 additions & 0 deletions piecrust-uplink/src/abi/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ mod ext {
pub fn feed(arg_len: u32);

pub fn caller() -> i32;
pub fn callstack() -> i32;
pub fn limit() -> u64;
pub fn spent() -> u64;
pub fn owner(contract_id: *const u8) -> i32;
Expand Down Expand Up @@ -289,6 +290,22 @@ pub fn caller() -> Option<ContractId> {
}
}

/// Returns IDs of all calling contracts present in the calling stack
pub fn callstack() -> Vec<ContractId> {
let n = unsafe { ext::callstack() };
with_arg_buf(|buf| {
let mut v = Vec::new();
for i in 0..n as usize {
let mut bytes = [0; CONTRACT_ID_BYTES];
bytes.copy_from_slice(
&buf[i * CONTRACT_ID_BYTES..(i + 1) * CONTRACT_ID_BYTES],
);
v.push(ContractId::from_bytes(bytes));
}
v
})
}

/// Returns the gas limit with which the contact was called.
pub fn limit() -> u64 {
unsafe { ext::limit() }
Expand Down
3 changes: 3 additions & 0 deletions piecrust-uplink/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ pub use types::*;
mod error;
pub use error::*;

#[cfg(feature = "serde")]
mod serde_support;

/// How many bytes to use for scratch space when serializing
pub const SCRATCH_BUF_BYTES: usize = 1024;

Expand Down
Loading