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

feat: add new macro to support async client #132

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
13 changes: 8 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
[package]
name = "ckb-sdk"
version = "3.5.0"
authors = [ "Linfeng Qian <[email protected]>", "Nervos Core Dev <[email protected]>" ]
authors = [
"Linfeng Qian <[email protected]>",
"Nervos Core Dev <[email protected]>",
]
edition = "2018"
license = "MIT"
description = "Rust SDK for CKB"
Expand All @@ -17,17 +20,18 @@ anyhow = "1.0.63"
bech32 = "0.8.1"
derive-getters = "0.2.1"
log = "0.4.6"
reqwest = { version = "0.11", default-features = false, features = [ "json", "blocking" ] }
reqwest = { version = "0.12", default-features = false, features = ["json"] }
secp256k1 = { version = "0.29.0", features = ["recovery"] }
tokio-util = { version = "0.7.7", features = ["codec"] }
tokio = { version = "1" }
tokio = { version = "1", features = ["full"] }
bytes = "1"
futures = "0.3"
jsonrpc-core = "18"
parking_lot = "0.12"
lru = "0.7.1"
dashmap = "5.4"
dyn-clone = "1.0"
async-trait = "0.1"

ckb-types = "0.119.0"
ckb-dao-utils = "0.119.0"
Expand All @@ -47,7 +51,6 @@ ckb-mock-tx-types = { version = "0.119.0" }
ckb-chain-spec = "0.119.0"

sparse-merkle-tree = "0.6.1"
lazy_static = "1.3.0"

[features]
default = ["default-tls"]
Expand All @@ -57,7 +60,7 @@ rustls-tls = ["reqwest/rustls-tls"]
test = []

[dev-dependencies]
clap = { version = "=4.4.18", features = [ "derive" ] } # TODO clap v4.5 requires rustc v1.74.0+
clap = { version = "4.4.18", features = ["derive"] }
httpmock = "0.6"
async-global-executor = "2.3.1"
hex = "0.4"
7 changes: 4 additions & 3 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ ignore = [
#{ id = "RUSTSEC-0000-0000", reason = "you can specify a reason the advisory is ignored" },
#"[email protected]", # you can also ignore yanked crate versions if you wish
#{ crate = "[email protected]", reason = "you can specify why you are ignoring the yanked crate"
"RUSTSEC-2024-0370" # proc-macro-error's maintainer seems to be unreachable, ignore this
"RUSTSEC-2024-0370", # proc-macro-error's maintainer seems to be unreachable, ignore this
"RUSTSEC-2024-0384", # instant is no longer maintained, ignore this
]
# If this is true, then cargo deny will use the git executable to fetch advisory database.
# If this is false, then it uses a built-in git library.
Expand All @@ -97,8 +98,8 @@ allow = [
"ISC",
"MIT",
"Unicode-DFS-2016",
"BSL-1.0", # xxhash-rust 0.8.10

"BSL-1.0", # xxhash-rust 0.8.10
"Unicode-3.0",
#"MIT",
#"Apache-2.0",
#"Apache-2.0 WITH LLVM-exception",
Expand Down
19 changes: 11 additions & 8 deletions examples/script_unlocker_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ use std::collections::HashMap;
/// [CapacityDiff]: https://github.com/doitian/ckb-sdk-examples-capacity-diff
struct CapacityDiffUnlocker {}

#[async_trait::async_trait]
impl ScriptUnlocker for CapacityDiffUnlocker {
// This works for any args
fn match_args(&self, _args: &[u8]) -> bool {
true
}

fn unlock(
async fn unlock_async(
&self,
tx: &TransactionView,
script_group: &ScriptGroup,
Expand All @@ -45,12 +46,14 @@ impl ScriptUnlocker for CapacityDiffUnlocker {

let mut total = 0i64;
for i in &script_group.input_indices {
let cell = tx_dep_provider.get_cell(
&tx.inputs()
.get(*i)
.ok_or_else(|| other_unlock_error("input index out of bound"))?
.previous_output(),
)?;
let cell = tx_dep_provider
.get_cell_async(
&tx.inputs()
.get(*i)
.ok_or_else(|| other_unlock_error("input index out of bound"))?
.previous_output(),
)
.await?;
let capacity: u64 = cell.capacity().unpack();
total -= capacity as i64;
}
Expand All @@ -71,7 +74,7 @@ impl ScriptUnlocker for CapacityDiffUnlocker {

// This is called before balancer. It's responsible to fill witness for inputs added manually
// by users.
fn fill_placeholder_witness(
async fn fill_placeholder_witness_async(
&self,
tx: &TransactionView,
script_group: &ScriptGroup,
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.75.0
1.81.0
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub mod test_util;
#[cfg(test)]
mod tests;

pub use rpc::{CkbRpcClient, IndexerRpcClient, RpcError};
pub use rpc::{CkbRpcAsyncClient, CkbRpcClient, IndexerRpcAsyncClient, IndexerRpcClient, RpcError};
pub use types::{
Address, AddressPayload, AddressType, CodeHashIndex, HumanCapacity, NetworkInfo, NetworkType,
OldAddress, OldAddressFormat, ScriptGroup, ScriptGroupType, ScriptId, Since, SinceType,
Expand Down
105 changes: 105 additions & 0 deletions src/rpc/ckb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,117 @@ crate::jsonrpc!(pub struct CkbRpcClient {
pub fn calculate_dao_maximum_withdraw(&self, out_point: OutPoint, kind: DaoWithdrawingCalculationKind) -> Capacity;
});

crate::jsonrpc_async!(pub struct CkbRpcAsyncClient {
// Chain
pub fn get_block(&self, hash: H256) -> Option<BlockView>;
pub fn get_block_by_number(&self, number: BlockNumber) -> Option<BlockView>;
pub fn get_block_hash(&self, number: BlockNumber) -> Option<H256>;
pub fn get_block_filter(&self, block_hash: H256) -> Option<BlockFilter>;
pub fn get_current_epoch(&self) -> EpochView;
pub fn get_epoch_by_number(&self, number: EpochNumber) -> Option<EpochView>;
pub fn get_header(&self, hash: H256) -> Option<HeaderView>;
pub fn get_header_by_number(&self, number: BlockNumber) -> Option<HeaderView>;
pub fn get_live_cell(&self, out_point: OutPoint, with_data: bool) -> CellWithStatus;
pub fn get_tip_block_number(&self) -> BlockNumber;
pub fn get_tip_header(&self) -> HeaderView;
pub fn get_transaction(&self, hash: H256) -> Option<TransactionWithStatusResponse>;
pub fn get_transaction_proof(
&self,
tx_hashes: Vec<H256>,
block_hash: Option<H256>
) -> TransactionProof;
pub fn verify_transaction_proof(&self, tx_proof: TransactionProof) -> Vec<H256>;
pub fn get_transaction_and_witness_proof(&self, tx_hashes: Vec<H256>,
block_hash: Option<H256>) -> TransactionAndWitnessProof;
pub fn verify_transaction_and_witness_proof(&self, tx_proof: TransactionAndWitnessProof) -> Vec<H256>;
pub fn get_fork_block(&self, block_hash: H256) -> Option<BlockView>;
pub fn get_consensus(&self) -> Consensus;
pub fn get_deployments_info(&self) -> DeploymentsInfo;
pub fn get_block_median_time(&self, block_hash: H256) -> Option<Timestamp>;
pub fn get_block_economic_state(&self, block_hash: H256) -> Option<BlockEconomicState>;
pub fn estimate_cycles(&self, tx: Transaction)-> EstimateCycles;
pub fn get_fee_rate_statics(&self, target:Option<Uint64>) -> Option<FeeRateStatistics>;
pub fn get_fee_rate_statistics(&self, target:Option<Uint64>) -> Option<FeeRateStatistics>;

// Indexer
pub fn get_indexer_tip(&self) -> Option<Tip>;
pub fn get_cells(&self, search_key: SearchKey, order: Order, limit: Uint32, after: Option<JsonBytes>) -> Pagination<Cell>;
pub fn get_transactions(&self, search_key: SearchKey, order: Order, limit: Uint32, after: Option<JsonBytes>) -> Pagination<Tx>;
pub fn get_cells_capacity(&self, search_key: SearchKey) -> Option<CellsCapacity>;

// Net
pub fn get_banned_addresses(&self) -> Vec<BannedAddr>;
pub fn get_peers(&self) -> Vec<RemoteNode>;
pub fn local_node_info(&self) -> LocalNode;
pub fn set_ban(
&self,
address: String,
command: String,
ban_time: Option<Timestamp>,
absolute: Option<bool>,
reason: Option<String>
) -> ();
pub fn sync_state(&self) -> SyncState;
pub fn set_network_active(&self, state: bool) -> ();
pub fn add_node(&self, peer_id: String, address: String) -> ();
pub fn remove_node(&self, peer_id: String) -> ();
pub fn clear_banned_addresses(&self) -> ();
pub fn ping_peers(&self) -> ();

// Pool
pub fn send_transaction(&self, tx: Transaction, outputs_validator: Option<OutputsValidator>) -> H256;
pub fn remove_transaction(&self, tx_hash: H256) -> bool;
pub fn tx_pool_info(&self) -> TxPoolInfo;
pub fn get_pool_tx_detail_info(&self, tx_hash: H256) -> PoolTxDetailInfo;
pub fn clear_tx_pool(&self) -> ();
pub fn get_raw_tx_pool(&self, verbose: Option<bool>) -> RawTxPool;
pub fn tx_pool_ready(&self) -> bool;
pub fn test_tx_pool_accept(&self, tx: Transaction, outputs_validator: Option<OutputsValidator>) -> EntryCompleted;
pub fn clear_tx_verify_queue(&self) -> ();

// Stats
pub fn get_blockchain_info(&self) -> ChainInfo;

// Miner
pub fn get_block_template(&self, bytes_limit: Option<Uint64>, proposals_limit: Option<Uint64>, max_version: Option<Version>) -> BlockTemplate;
pub fn submit_block(&self, _work_id: String, _data: Block) -> H256;

// Alert
pub fn send_alert(&self, alert: Alert) -> ();

// IntegrationTest
pub fn process_block_without_verify(&self, data: Block, broadcast: bool) -> Option<H256>;
pub fn truncate(&self, target_tip_hash: H256) -> ();
pub fn generate_block(&self) -> H256;
pub fn generate_epochs(&self, num_epochs: EpochNumberWithFraction) -> EpochNumberWithFraction;
pub fn notify_transaction(&self, tx: Transaction) -> H256;
pub fn calculate_dao_field(&self, block_template: BlockTemplate) -> JsonBytes;
pub fn generate_block_with_template(&self, block_template: BlockTemplate) -> H256;

// Debug
pub fn jemalloc_profiling_dump(&self) -> String;
pub fn update_main_logger(&self, config: MainLoggerConfig) -> ();
pub fn set_extra_logger(&self, name: String, config_opt: Option<ExtraLoggerConfig>) -> ();

// Experimental
pub fn calculate_dao_maximum_withdraw(&self, out_point: OutPoint, kind: DaoWithdrawingCalculationKind) -> Capacity;
});

fn transform_cycles(cycles: Option<Vec<ckb_jsonrpc_types::Cycle>>) -> Vec<Cycle> {
cycles
.map(|c| c.into_iter().map(Into::into).collect())
.unwrap_or_default()
}

impl From<&CkbRpcClient> for CkbRpcAsyncClient {
fn from(value: &CkbRpcClient) -> Self {
Self {
client: value.client.clone(),
id: 0.into(),
}
}
}

impl CkbRpcClient {
pub fn get_packed_block(&self, hash: H256) -> Result<Option<JsonBytes>, crate::RpcError> {
self.post("get_block", (hash, Some(Uint32::from(0u32))))
Expand Down
16 changes: 16 additions & 0 deletions src/rpc/ckb_indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,19 @@ crate::jsonrpc!(pub struct IndexerRpcClient {
pub fn get_transactions(&self, search_key: SearchKey, order: Order, limit: Uint32, after: Option<JsonBytes>) -> Pagination<Tx>;
pub fn get_cells_capacity(&self, search_key: SearchKey) -> Option<CellsCapacity>;
});

crate::jsonrpc_async!(pub struct IndexerRpcAsyncClient {
pub fn get_indexer_tip(&self) -> Option<Tip>;
pub fn get_cells(&self, search_key: SearchKey, order: Order, limit: Uint32, after: Option<JsonBytes>) -> Pagination<Cell>;
pub fn get_transactions(&self, search_key: SearchKey, order: Order, limit: Uint32, after: Option<JsonBytes>) -> Pagination<Tx>;
pub fn get_cells_capacity(&self, search_key: SearchKey) -> Option<CellsCapacity>;
});

impl From<&IndexerRpcClient> for IndexerRpcAsyncClient {
fn from(value: &IndexerRpcClient) -> Self {
Self {
client: value.client.clone(),
id: 0.into(),
}
}
}
41 changes: 41 additions & 0 deletions src/rpc/ckb_light_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,44 @@ crate::jsonrpc!(pub struct LightClientRpcClient {
pub fn get_peers(&self) -> Vec<RemoteNode>;
pub fn local_node_info(&self) -> LocalNode;
});

crate::jsonrpc_async!(pub struct LightClientRpcAsyncClient {
// BlockFilter
pub fn set_scripts(&self, scripts: Vec<ScriptStatus>, command: Option<SetScriptsCommand>) -> ();
pub fn get_scripts(&self) -> Vec<ScriptStatus>;
pub fn get_cells(&self, search_key: SearchKey, order: Order, limit: Uint32, after: Option<JsonBytes>) -> Pagination<Cell>;
pub fn get_transactions(&self, search_key: SearchKey, order: Order, limit: Uint32, after: Option<JsonBytes>) -> Pagination<Tx>;
pub fn get_cells_capacity(&self, search_key: SearchKey) -> CellsCapacity;

// Transaction
pub fn send_transaction(&self, tx: Transaction) -> H256;

// Chain
pub fn get_tip_header(&self) -> HeaderView;
pub fn get_genesis_block(&self) -> BlockView;
pub fn get_header(&self, block_hash: H256) -> Option<HeaderView>;
pub fn get_transaction(&self, tx_hash: H256) -> Option<TransactionWithStatus>;
pub fn estimate_cycles(&self, tx: Transaction)-> EstimateCycles;
/// Fetch a header from remote node. If return status is `not_found` will re-sent fetching request immediately.
///
/// Returns: FetchStatus<HeaderView>
pub fn fetch_header(&self, block_hash: H256) -> FetchStatus<HeaderView>;

/// Fetch a transaction from remote node. If return status is `not_found` will re-sent fetching request immediately.
///
/// Returns: FetchStatus<TransactionWithHeader>
pub fn fetch_transaction(&self, tx_hash: H256) -> FetchStatus<TransactionWithStatus>;

// Net
pub fn get_peers(&self) -> Vec<RemoteNode>;
pub fn local_node_info(&self) -> LocalNode;
});

impl From<&LightClientRpcClient> for LightClientRpcAsyncClient {
fn from(value: &LightClientRpcClient) -> Self {
Self {
client: value.client.clone(),
id: 0.into(),
}
}
}
Loading
Loading