Skip to content

Ipc/x #9

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

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
toolchain: 1.85.0
override: true

- name: Cache Rust dependencies
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"


[dependencies]
ckb-testtool = "0.14.1"
ckb-testtool = "0.15.0"
serde_json = "1.0"
bytes = "1.5.0"
serde = { version = "1.0.209", features = ["derive"] }
Expand All @@ -16,7 +16,7 @@ hex = "0.4.3"
syn = "2.0.98"

[dev-dependencies]
ckb-std = "0.16.4"
ckb-std = "0.17.0"
bitcoin = { version = "0.31", features = ["serde"] }
log = "0.4"
env_logger = "0.11"
Expand Down
7 changes: 7 additions & 0 deletions demo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ members = [
"contracts/first-contract",
"contracts/ckb-script-ipc-demo",
"contracts/ckb-script-ipc-test",
"contracts/exec-contract",
"tests",
"contracts/test_invalid_request",
"contracts/test_loop_request",
"contracts/test_single_loop_request",
"contracts/test_large_data_handling",
"contracts/test_serialize",
"contracts/test_serialize_complex"
]

[profile.release]
Expand Down
2 changes: 1 addition & 1 deletion demo/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ TOP := $(cur_dir)
# RUSTFLAGS that are likely to be tweaked by developers. For example,
# while we enable debug logs by default here, some might want to strip them
# for minimal code size / consumed cycles.
CUSTOM_RUSTFLAGS := -C debug-assertions
CUSTOM_RUSTFLAGS := -C target-feature=+zba,+zbb,+zbc,+zbs,-a -C debug-assertions=yes
# Additional cargo args to append here. For example, one can use
# make test CARGO_ARGS="-- --nocapture" so as to inspect data emitted to
# stdout in unit tests
Expand Down
9 changes: 4 additions & 5 deletions demo/contracts/ckb-script-ipc-demo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ version = "1.0.0"
edition = "2021"

[dependencies]
ckb-std = { version = "0.16", default-features = false, features = ["allocator", "ckb-types", "dummy-atomic", "log"]}
#ckb-script-ipc-common = { path = "../../crates/ckb-script-ipc-common" }
ckb-script-ipc-common = "1.0.0"

ckb-std = { version = "0.17.0", default-features = false, features = ["allocator", "ckb-types", "dummy-atomic", "log"]}
ckb-script-ipc-common = { version = "1.0.3", features = ["enable-logging"] }

serde = { version = "1.0", default-features = false, features = ["derive"] }
ckb-script-ipc = "1.0.0"
ckb-script-ipc = { version = "1.0.3" }
log = "0.4.25"

[[bin]]
name = "bin1"
Expand Down
2 changes: 1 addition & 1 deletion demo/contracts/ckb-script-ipc-demo/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ TOP := $(cur_dir)
# RUSTFLAGS that are likely to be tweaked by developers. For example,
# while we enable debug logs by default here, some might want to strip them
# for minimal code size / consumed cycles.
CUSTOM_RUSTFLAGS := --cfg debug_assertions
CUSTOM_RUSTFLAGS := -C debug-assertions=yes
# RUSTFLAGS that are less likely to be tweaked by developers. Most likely
# one would want to keep the default values here.
FULL_RUSTFLAGS := -C target-feature=+zba,+zbb,+zbc,+zbs,-a $(CUSTOM_RUSTFLAGS)
Expand Down
49 changes: 43 additions & 6 deletions demo/contracts/ckb-script-ipc-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ version = "1.0.0"
edition = "2021"

[dependencies]
ckb-std = { version = "0.16", default-features = false, features = ["allocator", "ckb-types", "dummy-atomic", "log"]}
#ckb-script-ipc-common = { path = "../../crates/ckb-script-ipc-common" }
ckb-script-ipc-common = "1.0.0"

ckb-std = { version = "0.17.0", default-features = false, features = ["allocator", "ckb-types", "dummy-atomic", "log"]}
#ckb-script-ipc-common = { version = "1.0.1", features = ["enable-logging"] }
ckb-script-ipc-common = { version = "1.0.3" }
serde = { version = "1.0", default-features = false, features = ["derive"] }
ckb-script-ipc = "1.0.0"
ckb-script-ipc = { version = "1.0.3" }
log = "0.4.25"

[[bin]]
Expand All @@ -18,4 +17,42 @@ path = "src/client.rs"

[[bin]]
name = "ckb-script-ipc-test-serve"
path = "src/server.rs"
path = "src/server.rs"

[[bin]]
name = "large_data_server"
path = "src/large_data_server.rs"

[[bin]]
name = "large_data_client"
path = "src/large_data_client.rs"

[[bin]]
name = "large_data_client2"
path = "src/large_data_client_2.rs"

[[bin]]
name = "boundary_server"
path = "src/boundary_server.rs"

[[bin]]
name = "boundary_client"
path = "src/boundary_client.rs"

[[bin]]
name = "linked_server"
path = "src/linked_server.rs"


[[bin]]
name = "linked_client"
path = "src/linked_client.rs"

[[bin]]
name = "complex_chain_call_client"
path = "src/complex_chain_call_client.rs"

[[bin]]
name = "chain_error_propagation_client"
path = "src/chain_error_propagation_client.rs"

4 changes: 2 additions & 2 deletions demo/contracts/ckb-script-ipc-test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ TOP := $(cur_dir)
# RUSTFLAGS that are likely to be tweaked by developers. For example,
# while we enable debug logs by default here, some might want to strip them
# for minimal code size / consumed cycles.
CUSTOM_RUSTFLAGS := --cfg debug_assertions
CUSTOM_RUSTFLAGS := -C debug-assertions=yes
# RUSTFLAGS that are less likely to be tweaked by developers. Most likely
# one would want to keep the default values here.
FULL_RUSTFLAGS := -C target-feature=+zba,+zbb,+zbc,+zbs,-a $(CUSTOM_RUSTFLAGS)
Expand All @@ -25,7 +25,7 @@ BUILD_DIR :=
# likely match the crate name, which is also the name of the final binary.
# However if this is not the case, you can tweak this variable. As the name hints,
# more than one binary is supported here.
BINARIES := "ckb-script-ipc-test-serve" "ckb-script-ipc-test-client"
BINARIES := "ckb-script-ipc-test-serve" "ckb-script-ipc-test-client" "large_data_server" "large_data_client" "large_data_client2" "boundary_server" "boundary_client" "linked_server" "linked_client" "complex_chain_call_client" "chain_error_propagation_client"

ifeq (release,$(MODE))
MODE_ARGS := --release
Expand Down
93 changes: 93 additions & 0 deletions demo/contracts/ckb-script-ipc-test/src/boundary_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#![no_std]
#![cfg_attr(not(test), no_main)]

#[cfg(test)]
extern crate alloc;

pub mod error;
use ckb_std::debug;

use crate::error::Error;
use alloc::ffi::CString;
use alloc::string::String;
use alloc::vec;
use alloc::vec::Vec;
use ckb_script_ipc_common::spawn::spawn_server;
#[cfg(not(test))]
use ckb_std::default_alloc;
use ckb_std::high_level::{load_cell_data, QueryIter};
use ckb_std::logger;
use ckb_std::syscalls::current_cycles;
use ckb_std::{ckb_constants::Source, log::info};

pub mod def;
use crate::def::{BoundaryStruct, TestBoundaryClient};

#[cfg(not(test))]
ckb_std::entry!(program_entry);
#[cfg(not(test))]
default_alloc!();

pub fn program_entry() -> i8 {
drop(logger::init());
match client_run() {
Ok(_) => 0,
Err(e) => e as i8,
}
}

pub fn client_run() -> Result<(), Error> {
info!("client run started");

let (read_pipe, write_pipe) = spawn_server(
0,
Source::CellDep,
&[CString::new("demo").unwrap().as_ref()],
)
.map_err(|_| Error::CkbSysError)?;

// new client
let mut client = TestBoundaryClient::new(read_pipe, write_pipe);

let data_length = collect_outputs_amount().unwrap();

// invoke
let ret = client.test(vec![
BoundaryStruct::min_value(),
BoundaryStruct::max_value(),
]);
info!("IPC response: {:?}", ret);

Ok(())
}
const UDT_LEN: usize = 16;

fn generate_rand_str(len: usize) -> String {
let mut s = String::new();
for _ in 0..len {
s.push((current_cycles() as u8 % 26 + 97) as char);
}
s
}

fn collect_outputs_amount() -> Result<u128, u8> {
// With the sum of all input UDT tokens gathered, let's now iterate through
// output cells to grab the sum of all output UDT tokens.
let mut buf = [0u8; UDT_LEN];
debug!(
"QueryIter:{:?}",
QueryIter::new(load_cell_data, Source::GroupOutput).count()
);
let udt_list = QueryIter::new(load_cell_data, Source::GroupOutput)
.map(|data| {
if data.len() == UDT_LEN {
buf.copy_from_slice(&data);
// u128 is 16 bytes
Ok(u128::from_le_bytes(buf))
} else {
Err(9)
}
})
.collect::<Result<Vec<_>, u8>>()?;
Ok(udt_list.into_iter().sum::<u128>())
}
69 changes: 69 additions & 0 deletions demo/contracts/ckb-script-ipc-test/src/boundary_server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#![no_std]
#![cfg_attr(not(test), no_main)]

#[cfg(test)]
extern crate alloc;

pub mod def;
pub mod error;

use alloc::vec::Vec;

use crate::def::{BoundaryStruct, TestBoundary};
use ckb_script_ipc_common::spawn::run_server;

use crate::error::Error;
#[cfg(not(test))]
use ckb_std::default_alloc;
use ckb_std::logger;

#[cfg(not(test))]
ckb_std::entry!(program_entry);
#[cfg(not(test))]
default_alloc!();

pub fn program_entry() -> i8 {
drop(logger::init());
match server_run() {
Ok(_) => 0,
Err(e) => e as i8,
}
}

pub struct BoundaryServer {
pub data: usize,
}

impl BoundaryServer {
fn new() -> Self {
BoundaryServer { data: 2 }
}
}

impl TestBoundary for BoundaryServer {
// method implementation
fn test(&mut self, vec: Vec<BoundaryStruct>) -> Vec<BoundaryStruct> {
self.data += 1;
return vec;
}
}

// impl World for LargeDataServer {
// // method implementation
// fn hello(&mut self, name: String) -> Result<String, u64> {
// self.data += 1;
// if name == "error" {
// Err(1)
// } else {
// Ok(format!("hello, {}", name))
// }
// }
// fn get_data(&mut self) -> usize {
// return self.data;
// }
// }

pub fn server_run() -> Result<(), Error> {
let world = BoundaryServer::new();
run_server(world.server()).map_err(|_| Error::ServerError)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#![no_std]
#![cfg_attr(not(test), no_main)]

#[cfg(test)]
extern crate alloc;
use crate::def::WorldClient;

pub mod def;
pub mod error;

use alloc::ffi::CString;
use ckb_script_ipc_common::spawn::spawn_server;
use ckb_std::{ckb_constants::Source, debug, log::info};

use crate::error::Error;
#[cfg(not(test))]
use ckb_std::default_alloc;
use ckb_std::logger;

#[cfg(not(test))]
ckb_std::entry!(program_entry);
#[cfg(not(test))]
default_alloc!();

pub fn program_entry() -> i8 {
drop(logger::init());
match client_run() {
Ok(_) => 0,
Err(e) => e as i8,
}
}

pub fn client_run() -> Result<(), Error> {
info!("client run started");

// server can be spawned by any process which wants to start it.
// here it is invoked by client
let (read_pipe, write_pipe) = spawn_server(
0,
Source::CellDep,
&[CString::new("demo").unwrap().as_ref()],
)
.map_err(|_| Error::CkbSysError)?;

// new client
let mut client = WorldClient::new(read_pipe, write_pipe);
info!("call hello");

// invoke
// let ret = client.hello("world \0\n\\n\\\\\''''' ///@@".into()).unwrap();
let ret = match client.hello("world".into()) {
Ok(ok) => ok,
Err(err) => {
debug!("error: {:?}", err);
return Err(Error::CkbSysError);
}
};

Ok(())
}
Loading
Loading