Skip to content

Commit

Permalink
feat: generate wallet ffi header file automatically (#4183)
Browse files Browse the repository at this point in the history
Description
---
Adds a `build.rs` and `cbindgen` to generate the header file automatically. I was not sure of the best location for this, so kept it the same as the previous `wallet.h` file, but renamed the file to match the crate (i.e. `tari_wallet_ffi.h`)


Motivation and Context
---
Currently, the `wallet.h` header file was manually edited. This obviously could lead to user error. Most notably, the `*_get_timestamp` methods were returning `c_longlong` but the header file indicated that they were using `c_ulonglong`. I'm not sure how long it has been like this, or which is correct, so I've changed the method to use `c_ulonglong` to match the previous header file.  

How Has This Been Tested?
---
Tested locally, but also asked @TruszczynskiA to compile iOs with the new header
  • Loading branch information
stringhandler authored Jun 13, 2022
1 parent 3de33bc commit 665f1f7
Show file tree
Hide file tree
Showing 10 changed files with 3,384 additions and 1,135 deletions.
22 changes: 21 additions & 1 deletion Cargo.lock

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

10 changes: 4 additions & 6 deletions base_layer/wallet_ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ version = "0.32.3"
edition = "2018"

[dependencies]
tari_core = { version = "^0.32", path = "../../base_layer/core", default-features = false, features = ["transactions"]}
tari_common = {path="../../common"}
tari_common_types = {path="../common_types"}
tari_comms = { version = "^0.32", path = "../../comms/core", features = ["c_integration"]}
Expand Down Expand Up @@ -40,12 +41,6 @@ security-framework = "2.4.2"
security-framework = "2.4.2"
# </workaround>

[dependencies.tari_core]
path = "../../base_layer/core"
version = "^0.32"
default-features = false
features = ["transactions"]

[lib]
crate-type = ["staticlib","cdylib"]

Expand All @@ -56,3 +51,6 @@ tari_key_manager = { version = "^0.32", path = "../key_manager" }
tari_common_types = { version = "^0.32", path = "../../base_layer/common_types"}
tari_test_utils = { version = "^0.32", path = "../../infrastructure/test_utils"}
tari_service_framework = { path = "../../base_layer/service_framework" }

[build-dependencies]
cbindgen = "0.24.3"
49 changes: 49 additions & 0 deletions base_layer/wallet_ffi/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2022. The Tari Project
// SPDX-License-Identifier: BSD-3-Clause

use std::{env, path::PathBuf};

use cbindgen::{Config, Language, ParseConfig, Style};

fn main() {
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();

let package_name = env::var("CARGO_PKG_NAME").unwrap();
let output_file = PathBuf::from(&crate_dir)
.join(format!("{}.h", package_name))
.display()
.to_string();

let config = Config {
language: Language::C,
header: Some("// Copyright 2022. The Tari Project\n// SPDX-License-Identifier: BSD-3-Clause".to_string()),
parse: ParseConfig {
parse_deps: true,
include: Some(vec![
"tari_core".to_string(),
"tari_common_types".to_string(),
"tari_crypto".to_string(),
"tari_p2p".to_string(),
"tari_wallet".to_string(),
]),
..Default::default()
},
style: Style::Tag,
..Default::default()
};

cbindgen::generate_with_config(&crate_dir, config)
.unwrap()
.write_to_file(&output_file);
}

// /// Find the location of the `target/` directory. Note that this may be
// /// overridden by `cmake`, so we also need to check the `CARGO_TARGET_DIR`
// /// variable.
// fn target_dir() -> PathBuf {
// if let Ok(target) = env::var("CARGO_TARGET_DIR") {
// PathBuf::from(target)
// } else {
// PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).join("target")
// }
// }
96 changes: 0 additions & 96 deletions base_layer/wallet_ffi/cbindgen.toml

This file was deleted.

4 changes: 2 additions & 2 deletions base_layer/wallet_ffi/mobile_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ if [ -n "${DEPENDENCIES}" ] && [ -n "${PKG_PATH}" ] && [ "${BUILD_IOS}" -eq 1 ]
if [ "${CARGO_CLEAN}" -eq "1" ]; then
cargo clean >> "${IOS_LOG_PATH}/cargo.txt" 2>&1
fi
cp wallet.h "${DEPENDENCIES}/MobileWallet/TariLib/"
cp tari_wallet_ffi.h "${DEPENDENCIES}/MobileWallet/TariLib/"
export PKG_CONFIG_PATH=${PKG_PATH}
# shellcheck disable=SC2028
echo "\t${CYAN}Building Wallet FFI${NC}"
Expand Down Expand Up @@ -424,7 +424,7 @@ EOF
else
cargo ndk --target ${PLATFORMABI} --android-platform ${LEVEL} -- build --release > "${ANDROID_LOG_PATH}/cargo_${PLATFORMABI}_${LEVEL}.txt" 2>&1
fi
cp wallet.h "${DEPENDENCIES}/"
cp tari_wallet_ffi.h "${DEPENDENCIES}/"
rm -rf .cargo
cd ../..
cd target || exit
Expand Down
21 changes: 1 addition & 20 deletions base_layer/wallet_ffi/src/callback_handler.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,5 @@
// Copyright 2019. The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// SPDX-License-Identifier: BSD-3-Clause

//! # Wallet Callback Handler
//! This CallbackHandler will monitor event streams from the various wallet services and on relevant events will call
Expand Down
21 changes: 1 addition & 20 deletions base_layer/wallet_ffi/src/callback_handler_tests.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,5 @@
// Copyright 2019. The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// SPDX-License-Identifier: BSD-3-Clause

#[cfg(test)]
mod test {
Expand Down
39 changes: 10 additions & 29 deletions base_layer/wallet_ffi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,5 @@
// Copyright 2019. The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// SPDX-License-Identifier: BSD-3-Clause

//! # LibWallet API Definition
//! This module contains the Rust backend implementations of the functionality that a wallet for the Tari Base Layer
Expand Down Expand Up @@ -86,7 +67,7 @@ use std::{

use chrono::{DateTime, Local};
use error::LibWalletError;
use libc::{c_char, c_int, c_longlong, c_uchar, c_uint, c_ulonglong, c_ushort};
use libc::{c_char, c_int, c_uchar, c_uint, c_ulonglong, c_ushort};
use log::{LevelFilter, *};
use log4rs::{
append::{
Expand Down Expand Up @@ -239,15 +220,15 @@ pub struct TariWallet {

#[allow(unused)]
#[derive(Debug, Clone)]
#[repr(C)]
// #[repr(C)]
pub struct Utxo {
commitment: Commitment,
value: c_ulonglong,
}

#[allow(unused)]
#[derive(Debug, Clone)]
#[repr(C)]
// #[repr(C)]
pub struct GetUtxosView {
outputs: Vec<Utxo>,
unlisted_dust_sum: c_ulonglong,
Expand Down Expand Up @@ -2437,15 +2418,15 @@ pub unsafe extern "C" fn completed_transaction_get_fee(
pub unsafe extern "C" fn completed_transaction_get_timestamp(
transaction: *mut TariCompletedTransaction,
error_out: *mut c_int,
) -> c_longlong {
) -> c_ulonglong {
let mut error = 0;
ptr::swap(error_out, &mut error as *mut c_int);
if transaction.is_null() {
error = LibWalletError::from(InterfaceError::NullError("transaction".to_string())).code;
ptr::swap(error_out, &mut error as *mut c_int);
return 0;
}
(*transaction).timestamp.timestamp() as c_longlong
(*transaction).timestamp.timestamp() as c_ulonglong
}

/// Gets the message of a TariCompletedTransaction
Expand Down Expand Up @@ -2738,15 +2719,15 @@ pub unsafe extern "C" fn pending_outbound_transaction_get_fee(
pub unsafe extern "C" fn pending_outbound_transaction_get_timestamp(
transaction: *mut TariPendingOutboundTransaction,
error_out: *mut c_int,
) -> c_longlong {
) -> c_ulonglong {
let mut error = 0;
ptr::swap(error_out, &mut error as *mut c_int);
if transaction.is_null() {
error = LibWalletError::from(InterfaceError::NullError("transaction".to_string())).code;
ptr::swap(error_out, &mut error as *mut c_int);
return 0;
}
(*transaction).timestamp.timestamp() as c_longlong
(*transaction).timestamp.timestamp() as c_ulonglong
}

/// Gets the message of a TariPendingOutboundTransaction
Expand Down Expand Up @@ -2945,15 +2926,15 @@ pub unsafe extern "C" fn pending_inbound_transaction_get_amount(
pub unsafe extern "C" fn pending_inbound_transaction_get_timestamp(
transaction: *mut TariPendingInboundTransaction,
error_out: *mut c_int,
) -> c_longlong {
) -> c_ulonglong {
let mut error = 0;
ptr::swap(error_out, &mut error as *mut c_int);
if transaction.is_null() {
error = LibWalletError::from(InterfaceError::NullError("transaction".to_string())).code;
ptr::swap(error_out, &mut error as *mut c_int);
return 0;
}
(*transaction).timestamp.timestamp() as c_longlong
(*transaction).timestamp.timestamp() as c_ulonglong
}

/// Gets the message of a TariPendingInboundTransaction
Expand Down
Loading

0 comments on commit 665f1f7

Please sign in to comment.