Skip to content
Merged
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
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ log = "0.4.6"
reqwest = { version = "0.12", default-features = false, features = ["json", "cookies"] }
secp256k1 = { version = "0.30.0", features = ["recovery"] }
tokio-util = { version = "0.7.7", features = ["codec"] }
tokio = { version = "1", features = ["time"] }
tokio = { version = "1", features = ["time", "sync"] }
bytes = "1"
futures = "0.3"
jsonrpc-core = "18"
Expand Down Expand Up @@ -58,7 +58,7 @@ async-iterator = "2.3.0"
[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2.16", features = ["js"] }
web-time = "1.1.0"
tokio_with_wasm = { version = "0.8.2", features = ["time"] }
tokio_with_wasm = { version = "0.8", features = ["time", "rt"] }

[features]
default = ["default-tls"]
Expand Down
5 changes: 2 additions & 3 deletions examples/script_unlocker_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ use std::collections::HashMap;
/// [CapacityDiff]: https://github.com/doitian/ckb-sdk-examples-capacity-diff
struct CapacityDiffUnlocker {}

#[cfg_attr(target_arch="wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
#[async_trait::async_trait]
impl ScriptUnlocker for CapacityDiffUnlocker {
// This works for any args
fn match_args(&self, _args: &[u8]) -> bool {
Expand Down Expand Up @@ -114,7 +113,7 @@ impl ScriptUnlocker for CapacityDiffUnlocker {
}

fn other_unlock_error(message: &str) -> UnlockError {
UnlockError::Other(std::io::Error::new(std::io::ErrorKind::Other, message).into())
UnlockError::Other(std::io::Error::other(message).into())
}

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/sudt_issue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn main() -> Result<(), Box<dyn StdErr>> {
let configuration = TransactionBuilderConfiguration::new_with_network(network_info.clone())?;

let issuer = Address::from_str("ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsq2qf8keemy2p5uu0g0gn8cd4ju23s5269qk8rg4r")?;
let iterator = InputIterator::new_with_address(&[issuer.clone()], &network_info);
let iterator = InputIterator::new_with_address(std::slice::from_ref(&issuer), &network_info);
let mut builder = SudtTransactionBuilder::new(configuration, iterator, &issuer, true)?;
builder.add_output(&issuer, 42);

Expand Down
2 changes: 1 addition & 1 deletion examples/sudt_send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() -> Result<(), Box<dyn StdErr>> {

let sender = Address::from_str("ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsq2qf8keemy2p5uu0g0gn8cd4ju23s5269qk8rg4r")?;
let receiver= Address::from_str("ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqv5dsed9par23x4g58seaw58j3ym5ml2hs8ztche")?;
let iterator = InputIterator::new_with_address(&[sender.clone()], &network_info);
let iterator = InputIterator::new_with_address(std::slice::from_ref(&sender), &network_info);
let mut builder = SudtTransactionBuilder::new(configuration, iterator, &sender, false)?;
builder.add_output(&receiver, 50);

Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.85.0
1.92.0
2 changes: 1 addition & 1 deletion src/pubsub/stream_codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl tokio_util::codec::Decoder for StreamCodec {

match str::from_utf8(line.as_ref()) {
Ok(_) => Ok(Some(line)),
Err(_) => Err(io::Error::new(io::ErrorKind::Other, "invalid UTF-8")),
Err(_) => Err(io::Error::other("invalid UTF-8")),
}
} else {
Ok(None)
Expand Down
8 changes: 2 additions & 6 deletions src/rpc/ckb_indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,17 @@ pub struct SearchKey {

#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, Hash)]
#[serde(rename_all = "snake_case")]
#[derive(Default)]
pub enum SearchMode {
// search with prefix
#[default]
Prefix,
// search with exact match
Exact,
// search with partial match
Partial,
}

impl Default for SearchMode {
fn default() -> Self {
Self::Prefix
}
}

#[derive(Serialize, Deserialize, Default, Clone, Debug)]
pub struct SearchKeyFilter {
pub script: Option<Script>,
Expand Down
61 changes: 37 additions & 24 deletions src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ pub enum RpcError {
Rpc(#[from] jsonrpc_core::Error),
#[error(transparent)]
Other(#[from] anyhow::Error),
#[error("oneshot channel closed")]
ChannelClosed,
}

#[cfg(not(target_arch = "wasm32"))]
Expand Down Expand Up @@ -206,7 +208,6 @@ macro_rules! jsonrpc_async {
Ok($struct_name { id: 0.into(), client })
}

#[cfg(not(target_arch="wasm32"))]
pub fn post<PARAM, RET>(&self, method:&str, params: PARAM)->impl std::future::Future<Output =Result<RET, $crate::rpc::RpcError>> + Send + 'static
where
PARAM:serde::ser::Serialize + Send + 'static,
Expand All @@ -227,28 +228,6 @@ macro_rules! jsonrpc_async {

self.client.post(params_fn)

}
#[cfg(target_arch="wasm32")]
pub fn post<PARAM, RET>(&self, method:&str, params: PARAM)->impl std::future::Future<Output =Result<RET, $crate::rpc::RpcError>> + 'static
where
PARAM:serde::ser::Serialize + Send + 'static,
RET: serde::de::DeserializeOwned + Send + 'static,
{
let id = self.id.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let method = serde_json::json!(method);

let params_fn = move || -> Result<_,_> {
let params = serde_json::to_value(params)?;
let mut req_json = serde_json::Map::new();
req_json.insert("id".to_owned(), serde_json::json!(id));
req_json.insert("jsonrpc".to_owned(), serde_json::json!("2.0"));
req_json.insert("method".to_owned(), method);
req_json.insert("params".to_owned(), params);
Ok(req_json)
};

self.client.post(params_fn)

}
$(
$(#[$attr])*
Expand Down Expand Up @@ -320,7 +299,7 @@ impl RpcClient {
}
}

pub fn post<PARAM, RET, T>(
fn post_inner<PARAM, RET, T>(
&self,
json_post_params: T,
) -> impl std::future::Future<Output = Result<RET, crate::rpc::RpcError>>
Expand All @@ -343,6 +322,40 @@ impl RpcClient {
}
}
}

#[cfg(not(target_arch = "wasm32"))]
pub fn post<PARAM, RET, T>(
&self,
json_post_params: T,
) -> impl std::future::Future<Output = Result<RET, crate::rpc::RpcError>> + Send
where
PARAM: serde::ser::Serialize + Send + 'static,
RET: serde::de::DeserializeOwned + Send + 'static,
T: FnOnce() -> Result<PARAM, crate::rpc::RpcError> + Send,
{
self.post_inner(json_post_params)
}

#[cfg(target_arch = "wasm32")]
pub fn post<PARAM, RET, T>(
&self,
json_post_params: T,
) -> impl std::future::Future<Output = Result<RET, crate::rpc::RpcError>> + Send
where
PARAM: serde::ser::Serialize + Send + 'static,
RET: serde::de::DeserializeOwned + Send + 'static,
T: FnOnce() -> Result<PARAM, crate::rpc::RpcError> + Send + 'static,
{
let (tx, rx) = tokio::sync::oneshot::channel();
let future = self.post_inner(json_post_params);

tokio_with_wasm::spawn(async move {
let result = future.await;
let _ = tx.send(result);
});

async move { rx.await.map_err(|_| crate::rpc::RpcError::ChannelClosed)? }
}
}

#[macro_export]
Expand Down
9 changes: 3 additions & 6 deletions src/test_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,7 @@ impl Context {
}
}

#[cfg_attr(target_arch="wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
#[async_trait::async_trait]
impl TransactionDependencyProvider for Context {
// For verify certain cell belong to certain transaction
async fn get_transaction_async(
Expand Down Expand Up @@ -458,8 +457,7 @@ impl TransactionDependencyProvider for Context {
}
}

#[cfg_attr(target_arch="wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
#[async_trait::async_trait]
impl HeaderDepResolver for Context {
async fn resolve_by_tx_async(
&self,
Expand Down Expand Up @@ -528,8 +526,7 @@ impl CellDepResolver for Context {
}
}

#[cfg_attr(target_arch="wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
#[async_trait::async_trait]
impl CellCollector for LiveCellsContext {
async fn collect_live_cells_async(
&mut self,
Expand Down
3 changes: 1 addition & 2 deletions src/tests/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ const CYCLE_BIN: &[u8] = include_bytes!("../test-data/cycle");
pub struct CycleUnlocker {
loops: u64,
}
#[cfg_attr(target_arch="wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
#[async_trait::async_trait]
impl ScriptUnlocker for CycleUnlocker {
fn match_args(&self, _args: &[u8]) -> bool {
true
Expand Down
18 changes: 11 additions & 7 deletions src/traits/default_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,7 @@ impl DefaultHeaderDepResolver {
}
}

#[cfg_attr(target_arch="wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
#[async_trait::async_trait]
impl HeaderDepResolver for DefaultHeaderDepResolver {
async fn resolve_by_tx_async(
&self,
Expand Down Expand Up @@ -380,7 +379,14 @@ impl DefaultCellCollector {
#[cfg(not(target_arch = "wasm32"))]
tokio::time::sleep(Duration::from_millis(50)).await;
#[cfg(target_arch = "wasm32")]
tokio_with_wasm::time::sleep(Duration::from_millis(50)).await;
{
let (tx, rx) = tokio::sync::oneshot::channel::<()>();
tokio_with_wasm::spawn(async move {
tokio_with_wasm::time::sleep(Duration::from_millis(50)).await;
let _ = tx.send(());
});
let _ = rx.await;
}
} else {
return Ok(());
}
Expand All @@ -398,8 +404,7 @@ impl DefaultCellCollector {
}
}

#[cfg_attr(target_arch="wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
#[async_trait::async_trait]
impl CellCollector for DefaultCellCollector {
async fn collect_live_cells_async(
&mut self,
Expand Down Expand Up @@ -597,8 +602,7 @@ impl DefaultTransactionDependencyProvider {
}
}

#[cfg_attr(target_arch="wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
#[async_trait::async_trait]
impl TransactionDependencyProvider for DefaultTransactionDependencyProvider {
async fn get_transaction_async(
&self,
Expand Down
9 changes: 3 additions & 6 deletions src/traits/dummy_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ use anyhow::anyhow;
#[derive(Clone, Default)]
pub struct DummyCellCollector;

#[cfg_attr(target_arch="wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
#[async_trait::async_trait]
impl CellCollector for DummyCellCollector {
async fn collect_live_cells_async(
&mut self,
Expand Down Expand Up @@ -49,8 +48,7 @@ impl CellCollector for DummyCellCollector {
#[derive(Default)]
pub struct DummyHeaderDepResolver;

#[cfg_attr(target_arch="wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
#[async_trait::async_trait]
impl HeaderDepResolver for DummyHeaderDepResolver {
async fn resolve_by_tx_async(
&self,
Expand All @@ -70,8 +68,7 @@ impl HeaderDepResolver for DummyHeaderDepResolver {
#[derive(Default)]
pub struct DummyTransactionDependencyProvider;

#[cfg_attr(target_arch="wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
#[async_trait::async_trait]
impl TransactionDependencyProvider for DummyTransactionDependencyProvider {
// For verify certain cell belong to certain transaction
async fn get_transaction_async(
Expand Down
9 changes: 3 additions & 6 deletions src/traits/light_client_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ impl LightClientHeaderDepResolver {
}
}

#[cfg_attr(target_arch="wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
#[async_trait::async_trait]
impl HeaderDepResolver for LightClientHeaderDepResolver {
async fn resolve_by_tx_async(
&self,
Expand Down Expand Up @@ -120,8 +119,7 @@ impl LightClientTransactionDependencyProvider {
}
}

#[cfg_attr(target_arch="wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
#[async_trait::async_trait]
impl TransactionDependencyProvider for LightClientTransactionDependencyProvider {
async fn get_transaction_async(
&self,
Expand Down Expand Up @@ -272,8 +270,7 @@ impl LightClientCellCollector {
}
}

#[cfg_attr(target_arch="wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
#[async_trait::async_trait]
impl CellCollector for LightClientCellCollector {
async fn collect_live_cells_async(
&mut self,
Expand Down
9 changes: 3 additions & 6 deletions src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ pub enum TransactionDependencyError {
/// * inputs
/// * cell_deps
/// * header_deps
#[cfg_attr(target_arch="wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
#[async_trait::async_trait]
pub trait TransactionDependencyProvider: Sync + Send {
async fn get_transaction_async(
&self,
Expand Down Expand Up @@ -433,8 +432,7 @@ impl CellQueryOptions {
}
}

#[cfg_attr(target_arch="wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
#[async_trait::async_trait]
pub trait CellCollector: DynClone + Send + Sync {
/// Collect live cells by query options, if `apply_changes` is true will
/// mark all collected cells as dead cells.
Expand Down Expand Up @@ -478,8 +476,7 @@ pub trait CellDepResolver: Send + Sync {
fn resolve(&self, script: &Script) -> Option<CellDep>;
}

#[cfg_attr(target_arch="wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
#[async_trait::async_trait]
pub trait HeaderDepResolver: Send + Sync {
/// Resolve header dep by trancation hash
async fn resolve_by_tx_async(
Expand Down
6 changes: 2 additions & 4 deletions src/traits/offchain_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ pub struct OffchainHeaderDepResolver {
pub by_number: HashMap<u64, HeaderView>,
}

#[cfg_attr(target_arch="wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
#[async_trait::async_trait]
impl HeaderDepResolver for OffchainHeaderDepResolver {
async fn resolve_by_tx_async(
&self,
Expand Down Expand Up @@ -229,8 +228,7 @@ impl OffchainTransactionDependencyProvider {
}
}

#[cfg_attr(target_arch="wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
#[async_trait::async_trait]
impl TransactionDependencyProvider for OffchainTransactionDependencyProvider {
// For verify certain cell belong to certain transaction
async fn get_transaction_async(
Expand Down
3 changes: 1 addition & 2 deletions src/transaction/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ pub use fee_calculator::FeeCalculator;
pub use simple::SimpleTransactionBuilder;

/// CKB transaction builder trait.
#[cfg_attr(target_arch="wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
#[async_trait::async_trait]
pub trait CkbTransactionBuilder {
#[cfg(not(target_arch = "wasm32"))]
fn build(
Expand Down
Loading
Loading