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

[ckb_chain]: Calculate tx fees when switch is Switch::DISABLE_ALL #3905

Draft
wants to merge 7 commits into
base: develop
Choose a base branch
from
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ ckb-launcher = { path = "../util/launcher", version = "= 0.109.0-pre" }
lazy_static = "1.4"
tempfile.workspace = true
ckb-systemtime = { path = "../util/systemtime", version = "= 0.109.0-pre" ,features = ["enable_faketime"]}
rand = "0.7"

[features]
default = []
Expand Down
119 changes: 66 additions & 53 deletions chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use ckb_proposal_table::ProposalTable;
use ckb_rust_unstable_port::IsSorted;
use ckb_shared::shared::Shared;
use ckb_stop_handler::{SignalSender, StopHandler};
use ckb_store::data_loader_wrapper::AsDataLoader;
use ckb_store::{attach_block_cell, detach_block_cell, ChainStore, StoreTransaction};
use ckb_systemtime::unix_time_as_millis;
use ckb_types::{
Expand Down Expand Up @@ -733,20 +734,20 @@ impl ChainService {
.map_err(|e| InternalErrorKind::MMR.other(e))?;
}

let verify_context = VerifyContext::new(Arc::clone(&txn), consensus);
let verify_context = VerifyContext::new(Arc::clone(&txn), Arc::clone(&consensus));

let mut found_error = None;
for (ext, b) in fork
.dirty_exts
.iter()
.zip(fork.attached_blocks.iter().skip(verified_len))
{
if !switch.disable_all() {
if found_error.is_none() {
let resolved = self.resolve_block_transactions(&txn, b, &verify_context);
match resolved {
Ok(resolved) => {
let verified = {
if found_error.is_none() {
let resolved = self.resolve_block_transactions(&txn, b, &verify_context);
match resolved {
Ok(resolved) => {
let cycles_and_cache_entries: Result<(Cycle, Vec<Completed>), Error> = {
if !switch.disable_all() {
let contextual_block_verifier = ContextualBlockVerifier::new(
verify_context.clone(),
async_handle,
Expand All @@ -755,58 +756,70 @@ impl ChainService {
&mmr,
);
contextual_block_verifier.verify(&resolved, b)
};
match verified {
Ok((cycles, cache_entries)) => {
let txs_sizes = resolved
.iter()
.map(|rtx| {
rtx.transaction.data().serialized_size_in_block() as u64
})
.collect();
txn.attach_block(b)?;
attach_block_cell(&txn, b)?;
mmr.push(b.digest())
.map_err(|e| InternalErrorKind::MMR.other(e))?;

self.insert_ok_ext(
&txn,
&b.header().hash(),
ext.clone(),
Some(&cache_entries),
Some(txs_sizes),
)?;

if !switch.disable_script() && b.transactions().len() > 1 {
self.monitor_block_txs_verified(
b,
&resolved,
&cache_entries,
cycles,
);
}
}
Err(err) => {
self.print_error(b, &err);
found_error = Some(err);
self.insert_failure_ext(&txn, &b.header().hash(), ext.clone())?;
} else {
// when switch is Switch::DISABLE_ALL, calculate tx_fees and return zero cycles
resolved
.iter()
.skip(1)
.map(|rtx| {
ckb_verification::FeeCalculator::new(
Arc::clone(rtx),
&consensus,
txn.as_data_loader(),
)
.transaction_fee()
.map(|fee| Completed { cycles: 0, fee })
.map_err(Error::from)
})
.collect::<Result<Vec<Completed>, Error>>()
.map(|cache_entries| (0 as Cycle, cache_entries))
}
};

match cycles_and_cache_entries {
Ok((cycles, cache_entries)) => {
let txs_sizes = resolved
.iter()
.map(|rtx| {
rtx.transaction.data().serialized_size_in_block() as u64
})
.collect();
txn.attach_block(b)?;
attach_block_cell(&txn, b)?;
mmr.push(b.digest())
.map_err(|e| InternalErrorKind::MMR.other(e))?;

self.insert_ok_ext(
&txn,
&b.header().hash(),
ext.clone(),
Some(&cache_entries),
Some(txs_sizes),
)?;

if !switch.disable_script() && b.transactions().len() > 1 {
self.monitor_block_txs_verified(
b,
&resolved,
&cache_entries,
cycles,
);
}
}
}
Err(err) => {
found_error = Some(err);
self.insert_failure_ext(&txn, &b.header().hash(), ext.clone())?;
Err(err) => {
self.print_error(b, &err);
found_error = Some(err);
self.insert_failure_ext(&txn, &b.header().hash(), ext.clone())?;
}
}
}
} else {
self.insert_failure_ext(&txn, &b.header().hash(), ext.clone())?;
Err(err) => {
found_error = Some(err);
self.insert_failure_ext(&txn, &b.header().hash(), ext.clone())?;
}
}
} else {
txn.attach_block(b)?;
attach_block_cell(&txn, b)?;
mmr.push(b.digest())
.map_err(|e| InternalErrorKind::MMR.other(e))?;
self.insert_ok_ext(&txn, &b.header().hash(), ext.clone(), None, None)?;
self.insert_failure_ext(&txn, &b.header().hash(), ext.clone())?;
}
}

Expand Down