Skip to content

Commit

Permalink
Merge pull request #80 from eval-exec/exec/data2
Browse files Browse the repository at this point in the history
Upgrade ckb dependencies to `v0.111.0`
  • Loading branch information
quake authored Sep 14, 2023
2 parents 8812f5b + f312324 commit e0d516f
Show file tree
Hide file tree
Showing 12 changed files with 181 additions and 79 deletions.
22 changes: 11 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ckb-sdk"
version = "2.5.0"
version = "3.0.0"
authors = ["Linfeng Qian <[email protected]>", "Nervos Core Dev <[email protected]>"]
edition = "2018"
license = "MIT"
Expand Down Expand Up @@ -29,22 +29,22 @@ lru = "0.7.1"
dashmap = "5.4"
dyn-clone = "1.0"

ckb-types = "=0.108.0"
ckb-dao-utils = "=0.108.0"
ckb-traits = "=0.108.0"
ckb-jsonrpc-types = "=0.108.0"
ckb-hash = "=0.108.0"
ckb-resource = "=0.108.0"
ckb-crypto = { version = "=0.108.0", features = ["secp"] }
ckb-script = { version = "=0.108.0"}
ckb-types = "0.111.0"
ckb-dao-utils = "0.111.0"
ckb-traits = "0.111.0"
ckb-jsonrpc-types = "0.111.0"
ckb-hash = "0.111.0"
ckb-resource = "0.111.0"
ckb-crypto = { version = "=0.111.0", features = ["secp"] }
ckb-script = "0.111.0"
bitflags = "1.3.2"
sha3 = "0.10.1"
enum-repr-derive = "0.2.0"

# for feature test
rand = { version = "0.7.3", optional = true }
ckb-mock-tx-types = { version = "0.108.0", optional = true }
ckb-chain-spec = { version = "0.108.0", optional = true }
ckb-mock-tx-types = "0.111.0"
ckb-chain-spec = "0.111.0"

sparse-merkle-tree = "0.6.1"
lazy_static = "1.3.0"
Expand Down
14 changes: 8 additions & 6 deletions src/rpc/ckb.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use ckb_jsonrpc_types::{
Alert, BannedAddr, Block, BlockEconomicState, BlockNumber, BlockResponse, BlockTemplate,
BlockView, CellWithStatus, ChainInfo, Consensus, EpochNumber, EpochView, EstimateCycles,
ExtraLoggerConfig, FeeRateStatics, HeaderView, JsonBytes, LocalNode, MainLoggerConfig,
OutPoint, OutputsValidator, RawTxPool, RemoteNode, Script, SyncState, Timestamp, Transaction,
TransactionAndWitnessProof, TransactionProof, TransactionWithStatusResponse, TxPoolInfo,
Uint32, Uint64, Version,
BlockView, CellWithStatus, ChainInfo, Consensus, DeploymentsInfo, EpochNumber, EpochView,
EstimateCycles, ExtraLoggerConfig, FeeRateStatistics, HeaderView, JsonBytes, LocalNode,
MainLoggerConfig, OutPoint, OutputsValidator, RawTxPool, RemoteNode, Script, SyncState,
Timestamp, Transaction, TransactionAndWitnessProof, TransactionProof,
TransactionWithStatusResponse, TxPoolInfo, Uint32, Uint64, Version,
};
use ckb_types::{core::Cycle, H256};

Expand Down Expand Up @@ -36,10 +36,12 @@ crate::jsonrpc!(pub struct CkbRpcClient {
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>)->FeeRateStatics;
pub fn get_fee_rate_statics(&self, tartet:Option<Uint64>)->FeeRateStatistics;
pub fn get_fee_rate_statistics(&self, tartet:Option<Uint64>)->FeeRateStatistics;

// Indexer
pub fn get_indexer_tip(&self) -> Option<Tip>;
Expand Down
34 changes: 30 additions & 4 deletions src/test_util.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use ckb_chain_spec::consensus::ConsensusBuilder;
use std::cmp::Ordering;
use std::collections::{HashMap, HashSet};
use std::convert::TryFrom;
use std::sync::Arc;

use ckb_jsonrpc_types::Serialize;
use ckb_types::core::TransactionBuilder;
use ckb_types::core::{HeaderBuilder, TransactionBuilder};
use rand::{thread_rng, Rng};
use thiserror::Error;

Expand All @@ -24,7 +26,8 @@ use ckb_hash::blake2b_256;
use ckb_mock_tx_types::{
MockCellDep, MockInfo, MockInput, MockResourceLoader, MockTransaction, Resource,
};
use ckb_script::TransactionScriptsVerifier;
use ckb_script::{TransactionScriptsVerifier, TxVerifyEnv};
use ckb_types::core::hardfork::{HardForks, CKB2021, CKB2023};
use ckb_types::{
bytes::Bytes,
core::{
Expand Down Expand Up @@ -321,6 +324,7 @@ impl Context {
inputs: self.inputs.clone(),
cell_deps: self.cell_deps.clone(),
header_deps: self.header_deps.clone(),
extensions: vec![],
};
MockTransaction { mock_info, tx }
}
Expand Down Expand Up @@ -354,8 +358,21 @@ impl Context {
let resource = Resource::from_both(&mock_tx, DummyLoader).map_err(Error::VerifyScript)?;
let rtx = resolve_transaction(tx, &mut HashSet::new(), &resource, &resource)
.map_err(|err| Error::VerifyScript(format!("Resolve transaction error: {:?}", err)))?;

let mut verifier = TransactionScriptsVerifier::new(&rtx, &resource);
let consensus = ConsensusBuilder::default()
.hardfork_switch(HardForks {
ckb2021: CKB2021::new_dev_default(),
ckb2023: CKB2023::new_dev_default(),
})
.build();
let tip = HeaderBuilder::default().number(0.pack()).build();
let tx_verify_env = TxVerifyEnv::new_submit(&tip);

let mut verifier = TransactionScriptsVerifier::new(
Arc::new(rtx),
resource,
Arc::new(consensus),
Arc::new(tx_verify_env),
);
verifier.set_debug_printer(|script_hash, message| {
println!("script: {:x}, debug: {}", script_hash, message);
});
Expand Down Expand Up @@ -412,6 +429,15 @@ impl TransactionDependencyProvider for Context {
"header not found".to_string(),
))
}

fn get_block_extension(
&self,
_block_hash: &Byte32,
) -> Result<Option<ckb_types::packed::Bytes>, TransactionDependencyError> {
Err(TransactionDependencyError::NotFound(
"block extension not found".to_string(),
))
}
}

impl HeaderDepResolver for Context {
Expand Down
2 changes: 1 addition & 1 deletion src/tests/ckb_rpc.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::rpc::{CkbRpcClient, ResponseFormatGetter};
use ckb_types::{core, h256, prelude::Entity, H256};
use ckb_types::{core, h256, prelude::*, H256};
// use serde_json;

const TEST_CKB_RPC_URL: &str = "https://testnet.ckb.dev";
Expand Down
65 changes: 30 additions & 35 deletions src/tests/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use ckb_types::{
H256,
};

use crate::test_util::Context;
use crate::{
constants::ONE_CKB,
tests::{build_sighash_script, init_context, ACCOUNT2_ARG, FEE_RATE},
Expand Down Expand Up @@ -98,10 +99,10 @@ fn test_change_enough(loops: u64) {
let sender = build_script(loops);
let receiver = build_sighash_script(ACCOUNT2_ARG);

let ctx = init_context(
let ctx: &'static Context = Box::leak(Box::new(init_context(
vec![(CYCLE_BIN, true)],
vec![(sender.clone(), Some(200 * ONE_CKB))],
);
)));

let output = CellOutput::new_builder()
.capacity((140 * ONE_CKB).pack())
Expand All @@ -114,7 +115,7 @@ fn test_change_enough(loops: u64) {
let mut cell_collector = ctx.to_live_cells_context();
let unlockers = build_cycle_unlockers(loops);
let (tx, new_locked_groups) = builder
.build_balance_unlocked(&mut cell_collector, &ctx, &ctx, &ctx, &balancer, &unlockers)
.build_balance_unlocked(&mut cell_collector, ctx, ctx, ctx, &balancer, &unlockers)
.unwrap();

assert!(new_locked_groups.is_empty());
Expand Down Expand Up @@ -144,10 +145,10 @@ fn vsize_big_and_fee_enough() {
let sender = build_script(loops);
let receiver = build_sighash_script(ACCOUNT2_ARG);

let ctx = init_context(
let ctx: &'static Context = Box::leak(Box::new(init_context(
vec![(CYCLE_BIN, true)],
vec![(sender.clone(), Some(200 * ONE_CKB + 123_456))],
);
)));

let output = CellOutput::new_builder()
.capacity((200 * ONE_CKB).pack())
Expand All @@ -161,7 +162,7 @@ fn vsize_big_and_fee_enough() {
let mut cell_collector = ctx.to_live_cells_context();
let unlockers = build_cycle_unlockers(loops);
let (tx, new_locked_groups) = builder
.build_balance_unlocked(&mut cell_collector, &ctx, &ctx, &ctx, &balancer, &unlockers)
.build_balance_unlocked(&mut cell_collector, ctx, ctx, ctx, &balancer, &unlockers)
.unwrap();

assert!(new_locked_groups.is_empty());
Expand Down Expand Up @@ -190,10 +191,10 @@ fn vsize_big_and_fee_not_enough() {
let sender = build_script(loops);
let receiver = build_sighash_script(ACCOUNT2_ARG);

let ctx = init_context(
let ctx: &'static Context = Box::leak(Box::new(init_context(
vec![(CYCLE_BIN, true)],
vec![(sender.clone(), Some(200 * ONE_CKB + 456))],
);
)));

let output = CellOutput::new_builder()
.capacity((200 * ONE_CKB).pack())
Expand All @@ -206,14 +207,8 @@ fn vsize_big_and_fee_not_enough() {

let mut cell_collector = ctx.to_live_cells_context();
let unlockers = build_cycle_unlockers(loops);
let result = builder.build_balance_unlocked(
&mut cell_collector,
&ctx,
&ctx,
&ctx,
&balancer,
&unlockers,
);
let result =
builder.build_balance_unlocked(&mut cell_collector, ctx, ctx, ctx, &balancer, &unlockers);

if let Err(TxBuilderError::BalanceCapacity(BalanceTxCapacityError::CapacityNotEnough(_msg))) =
result
Expand All @@ -229,13 +224,13 @@ fn vsize_big_and_can_find_more_capacity() {
let sender = build_script(loops);
let receiver = build_sighash_script(ACCOUNT2_ARG);

let ctx = init_context(
let ctx: &'static Context = Box::leak(Box::new(init_context(
vec![(CYCLE_BIN, true)],
vec![
(sender.clone(), Some(200 * ONE_CKB + 286)), // 286 is fee calculated from tx_size
(sender.clone(), Some(70 * ONE_CKB)),
],
);
)));

let output = CellOutput::new_builder()
.capacity((200 * ONE_CKB).pack())
Expand All @@ -254,38 +249,38 @@ fn vsize_big_and_can_find_more_capacity() {
// .unwrap();
let (tx, new_locked_groups) = {
let base_tx = builder
.build_base(&mut cell_collector, &ctx, &ctx, &ctx)
.build_base(&mut cell_collector, ctx, ctx, ctx)
.unwrap();
let (tx_filled_witnesses, _) =
fill_placeholder_witnesses(base_tx, &ctx, &unlockers).unwrap();
fill_placeholder_witnesses(base_tx, ctx, &unlockers).unwrap();
let (balanced_tx, mut change_idx) = balancer
.rebalance_tx_capacity(
&tx_filled_witnesses,
&mut cell_collector,
&ctx,
&ctx,
&ctx,
ctx,
ctx,
ctx,
0,
None,
)
.unwrap();

assert_eq!(balanced_tx.inputs().len(), 1);
assert_eq!(balanced_tx.outputs().len(), 1);
let (mut tx, unlocked_group) = unlock_tx(balanced_tx, &ctx, &unlockers).unwrap();
let (mut tx, unlocked_group) = unlock_tx(balanced_tx, ctx, &unlockers).unwrap();
assert!(unlocked_group.is_empty());
let mut ready = false;
let mut loop_times = 0;
while !ready {
loop_times += 1;
let (new_tx, new_change_idx, ok) = balancer
.check_cycle_fee(tx, &mut cell_collector, &ctx, &ctx, &ctx, change_idx)
.check_cycle_fee(tx, &mut cell_collector, ctx, ctx, ctx, change_idx)
.unwrap();
tx = new_tx;
ready = ok;
change_idx = new_change_idx;
if !ready {
let (new_tx, _) = unlock_tx(tx, &ctx, &unlockers).unwrap();
let (new_tx, _) = unlock_tx(tx, ctx, &unlockers).unwrap();
tx = new_tx
}
}
Expand Down Expand Up @@ -319,13 +314,13 @@ fn vsize_big_and_cannot_find_more_capacity() {
let sender = build_script(loops);
let receiver = build_sighash_script(ACCOUNT2_ARG);

let ctx = init_context(
let ctx: &'static Context = Box::leak(Box::new(init_context(
vec![(CYCLE_BIN, true)],
vec![
(sender.clone(), Some(200 * ONE_CKB + 286)), // 286 is fee calculated from tx_size
(sender.clone(), Some(49 * ONE_CKB)),
],
);
)));

let output = CellOutput::new_builder()
.capacity((200 * ONE_CKB).pack())
Expand All @@ -343,26 +338,26 @@ fn vsize_big_and_cannot_find_more_capacity() {
// .build_balance_unlocked(&mut cell_collector, &ctx, &ctx, &ctx, &balancer, &unlockers)
// .unwrap();
let base_tx = builder
.build_base(&mut cell_collector, &ctx, &ctx, &ctx)
.build_base(&mut cell_collector, ctx, ctx, ctx)
.unwrap();
let (tx_filled_witnesses, _) = fill_placeholder_witnesses(base_tx, &ctx, &unlockers).unwrap();
let (tx_filled_witnesses, _) = fill_placeholder_witnesses(base_tx, ctx, &unlockers).unwrap();
let (balanced_tx, change_idx) = balancer
.rebalance_tx_capacity(
&tx_filled_witnesses,
&mut cell_collector,
&ctx,
&ctx,
&ctx,
ctx,
ctx,
ctx,
0,
None,
)
.unwrap();

assert_eq!(balanced_tx.inputs().len(), 1);
assert_eq!(balanced_tx.outputs().len(), 1);
let (tx, unlocked_group) = unlock_tx(balanced_tx, &ctx, &unlockers).unwrap();
let (tx, unlocked_group) = unlock_tx(balanced_tx, ctx, &unlockers).unwrap();
assert!(unlocked_group.is_empty());
let result = balancer.check_cycle_fee(tx, &mut cell_collector, &ctx, &ctx, &ctx, change_idx);
let result = balancer.check_cycle_fee(tx, &mut cell_collector, ctx, ctx, ctx, change_idx);
if let Err(BalanceTxCapacityError::ForceSmallChangeAsFeeFailed(_msg)) = result {
} else {
panic!("not expected result: {:?}", result);
Expand Down
16 changes: 16 additions & 0 deletions src/traits/default_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,22 @@ impl TransactionDependencyProvider for DefaultTransactionDependencyProvider {
inner.header_cache.put(block_hash.clone(), header.clone());
Ok(header)
}

fn get_block_extension(
&self,
block_hash: &Byte32,
) -> Result<Option<ckb_types::packed::Bytes>, TransactionDependencyError> {
let inner = self.inner.lock();

let block = inner
.rpc_client
.get_block(block_hash.unpack())
.map_err(|err| TransactionDependencyError::Other(err.into()))?;
match block {
Some(block) => Ok(block.extension.map(ckb_types::packed::Bytes::from)),
None => Ok(None),
}
}
}

/// A signer use secp256k1 raw key, the id is `blake160(pubkey)`.
Expand Down
8 changes: 8 additions & 0 deletions src/traits/dummy_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,12 @@ impl TransactionDependencyProvider for DummyTransactionDependencyProvider {
"dummy get_header"
)))
}
fn get_block_extension(
&self,
_block_hash: &Byte32,
) -> Result<Option<ckb_types::packed::Bytes>, TransactionDependencyError> {
Err(TransactionDependencyError::Other(anyhow!(
"dummy get_block_extension"
)))
}
}
9 changes: 9 additions & 0 deletions src/traits/light_client_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,15 @@ impl TransactionDependencyProvider for LightClientTransactionDependencyProvider
}
}
}

fn get_block_extension(
&self,
_block_hash: &Byte32,
) -> Result<Option<ckb_types::packed::Bytes>, TransactionDependencyError> {
Err(TransactionDependencyError::NotFound(
"get_block_extension not supported".to_string(),
))
}
}

#[derive(Clone)]
Expand Down
Loading

0 comments on commit e0d516f

Please sign in to comment.