Skip to content

Commit

Permalink
Merge pull request Electric-Coin-Company#129 from Electric-Coin-Compa…
Browse files Browse the repository at this point in the history
…ny/ffi-0.7.4

FFI 0.7.4
  • Loading branch information
str4d authored Mar 28, 2024
2 parents 76daeb5 + e2d8763 commit e6b0c9b
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ and this library adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## 0.7.4 - 2024-03-28

### Added
- `zcashlc_put_orchard_subtree_roots`

## 0.7.3 - 2024-03-27

- Updates to `zcash_client_backend 0.12.1` to fix a bug in note selection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,29 @@ bool zcashlc_put_sapling_subtree_roots(const uint8_t *db_data,
const struct FfiSubtreeRoots *roots,
uint32_t network_id);

/**
* Adds a sequence of Orchard subtree roots to the data store.
*
* Returns true if the subtrees could be stored, false otherwise. When false is returned,
* caller should check for errors.
*
* # Safety
*
* - `db_data` must be non-null and valid for reads for `db_data_len` bytes, and it must have an
* alignment of `1`. Its contents must be a string representing a valid system path in the
* operating system's preferred representation.
* - The memory referenced by `db_data` must not be mutated for the duration of the function call.
* - The total size `db_data_len` must be no larger than `isize::MAX`. See the safety
* documentation of `pointer::offset`.
* - `roots` must be non-null and initialized.
* - The memory referenced by `roots` must not be mutated for the duration of the function call.
*/
bool zcashlc_put_orchard_subtree_roots(const uint8_t *db_data,
uintptr_t db_data_len,
uint64_t start_index,
const struct FfiSubtreeRoots *roots,
uint32_t network_id);

/**
* Updates the wallet's view of the blockchain.
*
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,29 @@ bool zcashlc_put_sapling_subtree_roots(const uint8_t *db_data,
const struct FfiSubtreeRoots *roots,
uint32_t network_id);

/**
* Adds a sequence of Orchard subtree roots to the data store.
*
* Returns true if the subtrees could be stored, false otherwise. When false is returned,
* caller should check for errors.
*
* # Safety
*
* - `db_data` must be non-null and valid for reads for `db_data_len` bytes, and it must have an
* alignment of `1`. Its contents must be a string representing a valid system path in the
* operating system's preferred representation.
* - The memory referenced by `db_data` must not be mutated for the duration of the function call.
* - The total size `db_data_len` must be no larger than `isize::MAX`. See the safety
* documentation of `pointer::offset`.
* - `roots` must be non-null and initialized.
* - The memory referenced by `roots` must not be mutated for the duration of the function call.
*/
bool zcashlc_put_orchard_subtree_roots(const uint8_t *db_data,
uintptr_t db_data_len,
uint64_t start_index,
const struct FfiSubtreeRoots *roots,
uint32_t network_id);

/**
* Updates the wallet's view of the blockchain.
*
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,29 @@ bool zcashlc_put_sapling_subtree_roots(const uint8_t *db_data,
const struct FfiSubtreeRoots *roots,
uint32_t network_id);

/**
* Adds a sequence of Orchard subtree roots to the data store.
*
* Returns true if the subtrees could be stored, false otherwise. When false is returned,
* caller should check for errors.
*
* # Safety
*
* - `db_data` must be non-null and valid for reads for `db_data_len` bytes, and it must have an
* alignment of `1`. Its contents must be a string representing a valid system path in the
* operating system's preferred representation.
* - The memory referenced by `db_data` must not be mutated for the duration of the function call.
* - The total size `db_data_len` must be no larger than `isize::MAX`. See the safety
* documentation of `pointer::offset`.
* - `roots` must be non-null and initialized.
* - The memory referenced by `roots` must not be mutated for the duration of the function call.
*/
bool zcashlc_put_orchard_subtree_roots(const uint8_t *db_data,
uintptr_t db_data_len,
uint64_t start_index,
const struct FfiSubtreeRoots *roots,
uint32_t network_id);

/**
* Updates the wallet's view of the blockchain.
*
Expand Down
Binary file not shown.
3 changes: 2 additions & 1 deletion rust/Cargo.lock

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

3 changes: 2 additions & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "libzcashlc"
version = "0.7.3"
version = "0.7.4"
authors = [
"Jack Grigg <jack@electriccoin.co>",
"Francisco Gindre",
Expand All @@ -14,6 +14,7 @@ build = "build.rs"
[dependencies]
hex = "0.4"
memuse = "0.2.1"
orchard = "0.8"
prost = "0.12"
rusqlite = "0.29"
sapling = { package = "sapling-crypto", version = "0.1", default-features = false }
Expand Down
52 changes: 52 additions & 0 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1807,6 +1807,58 @@ pub unsafe extern "C" fn zcashlc_put_sapling_subtree_roots(
unwrap_exc_or(res, false)
}

/// Adds a sequence of Orchard subtree roots to the data store.
///
/// Returns true if the subtrees could be stored, false otherwise. When false is returned,
/// caller should check for errors.
///
/// # Safety
///
/// - `db_data` must be non-null and valid for reads for `db_data_len` bytes, and it must have an
/// alignment of `1`. Its contents must be a string representing a valid system path in the
/// operating system's preferred representation.
/// - The memory referenced by `db_data` must not be mutated for the duration of the function call.
/// - The total size `db_data_len` must be no larger than `isize::MAX`. See the safety
/// documentation of `pointer::offset`.
/// - `roots` must be non-null and initialized.
/// - The memory referenced by `roots` must not be mutated for the duration of the function call.
#[no_mangle]
pub unsafe extern "C" fn zcashlc_put_orchard_subtree_roots(
db_data: *const u8,
db_data_len: usize,
start_index: u64,
roots: *const FfiSubtreeRoots,
network_id: u32,
) -> bool {
let res = catch_panic(|| {
let network = parse_network(network_id)?;
let mut db_data = unsafe { wallet_db(db_data, db_data_len, network)? };

let roots = unsafe { roots.as_ref().unwrap() };
let roots_slice: &[FfiSubtreeRoot] = unsafe { slice::from_raw_parts(roots.ptr, roots.len) };

let roots = roots_slice
.iter()
.map(|r| {
let root_hash_bytes =
unsafe { slice::from_raw_parts(r.root_hash_ptr, r.root_hash_ptr_len) };
let root_hash = orchard::tree::MerkleHashOrchard::read(root_hash_bytes)?;

Ok(CommitmentTreeRoot::from_parts(
BlockHeight::from_u32(r.completing_block_height),
root_hash,
))
})
.collect::<anyhow::Result<Vec<_>>>()?;

db_data
.put_orchard_subtree_roots(start_index, &roots)
.map(|()| true)
.map_err(|e| anyhow!("Error while storing Orchard subtree roots: {}", e))
});
unwrap_exc_or(res, false)
}

/// Updates the wallet's view of the blockchain.
///
/// This method is used to provide the wallet with information about the state of the blockchain,
Expand Down

0 comments on commit e6b0c9b

Please sign in to comment.