Skip to content

Commit

Permalink
fix: declared_cycles check
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangsoledad committed Oct 9, 2021
1 parent 3d0fd29 commit 1a95cbc
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 24 deletions.
1 change: 1 addition & 0 deletions rpc/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ impl RPCError {
Reject::Full(_, _) => RPCError::PoolIsFull,
Reject::Duplicated(_) => RPCError::PoolRejectedDuplicatedTransaction,
Reject::Malformed(_) => RPCError::PoolRejectedMalformedTransaction,
Reject::DeclaredWrongCycles(..) => RPCError::PoolRejectedMalformedTransaction,
Reject::Resolve(_) => RPCError::TransactionFailedToResolve,
Reject::Verification(_) => RPCError::TransactionFailedToVerify,
};
Expand Down
12 changes: 12 additions & 0 deletions tx-pool/src/chunk_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,18 @@ impl TxChunkProcess {
}
};

if let Some((declared_cycle, _peer)) = remote {
if declared_cycle != completed.cycles {
return (
Err(Reject::DeclaredWrongCycles(
declared_cycle,
completed.cycles,
)),
snapshot,
);
}
}

let entry = TxEntry::new(rtx.clone(), completed.cycles, fee, tx_size);
let (ret, submit_snapshot) = self.handle.block_on(
self.service
Expand Down
46 changes: 22 additions & 24 deletions tx-pool/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use ckb_app_config::BlockAssemblerConfig;
use ckb_dao::DaoCalculator;
use ckb_error::{AnyError, InternalErrorKind};
use ckb_jsonrpc_types::BlockTemplate;
use ckb_logger::{debug, error, info, warn};
use ckb_logger::{debug, error, info};
use ckb_network::PeerIndex;
use ckb_snapshot::Snapshot;
use ckb_store::ChainStore;
Expand Down Expand Up @@ -697,29 +697,16 @@ impl TxPoolService {

match remote {
Some((declared_cycle, peer)) => match ret {
Ok(verified) => {
if declared_cycle == verified.cycles {
self.broadcast_tx(Some(peer), tx_hash, with_vm_2021);
self.process_orphan_tx(&tx).await;
} else {
warn!(
"peer {} declared cycles {} mismatch actual {} tx_hash: {}",
peer, declared_cycle, verified.cycles, tx_hash
);
self.ban_malformed(
peer,
format!(
"peer {} declared cycles {} mismatch actual {} tx_hash: {}",
peer, declared_cycle, verified.cycles, tx_hash
),
);
}
Ok(_) => {
self.broadcast_tx(Some(peer), tx_hash, with_vm_2021);
self.process_orphan_tx(&tx).await;
}
Err(reject) => {
if is_missing_input(&reject) && all_inputs_is_unknown(snapshot, &tx) {
self.add_orphan(tx, peer, declared_cycle).await;
} else if reject.is_malformed_tx() {
debug!("after_process {} reject: {} ", tx_hash, reject);
if reject.is_malformed_tx() {
self.ban_malformed(peer, format!("reject {}", reject));
} else if is_missing_input(&reject) && all_inputs_is_unknown(snapshot, &tx) {
self.add_orphan(tx, peer, declared_cycle).await;
}
}
},
Expand Down Expand Up @@ -782,7 +769,9 @@ impl TxPoolService {
.await
.add_remote_tx(orphan.tx, (orphan.cycle, orphan.peer));
} else {
let (ret, snapshot) = self._process_tx(orphan.tx.clone(), None).await;
let (ret, snapshot) = self
._process_tx(orphan.tx.clone(), Some(orphan.cycle))
.await;
let with_vm_2021 = {
let epoch = snapshot.tip_header().epoch().number();
self.consensus
Expand Down Expand Up @@ -959,7 +948,7 @@ impl TxPoolService {
pub(crate) async fn _process_tx(
&self,
tx: TransactionView,
max_cycles: Option<Cycle>,
declared_cycles: Option<Cycle>,
) -> (Result<Completed, Reject>, Arc<Snapshot>) {
let tx_hash = tx.hash();

Expand All @@ -968,13 +957,22 @@ impl TxPoolService {
let (tip_hash, rtx, status, fee, tx_size) = try_or_return_with_snapshot!(ret, snapshot);

let verify_cache = self.fetch_tx_verify_cache(&tx_hash).await;
let max_cycles = max_cycles.unwrap_or_else(|| self.consensus.max_block_cycles());
let max_cycles = declared_cycles.unwrap_or_else(|| self.consensus.max_block_cycles());
let tip_header = snapshot.tip_header();
let tx_env = status.with_env(tip_header);
let verified_ret = verify_rtx(&snapshot, &rtx, &tx_env, &verify_cache, max_cycles);

let verified = try_or_return_with_snapshot!(verified_ret, snapshot);

if let Some(declared) = declared_cycles {
if declared != verified.cycles {
return (
Err(Reject::DeclaredWrongCycles(declared, verified.cycles)),
snapshot,
);
}
}

let entry = TxEntry::new(rtx, verified.cycles, fee, tx_size);

let (ret, submit_snapshot) = self.submit_entry(verified, tip_hash, entry, status).await;
Expand Down
4 changes: 4 additions & 0 deletions util/jsonrpc-types/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ pub enum PoolTransactionReject {
/// Malformed transaction
Malformed(String),

/// Declared wrong cycles
DeclaredWrongCycles(String),

/// Resolve failed
Resolve(String),

Expand All @@ -210,6 +213,7 @@ impl From<Reject> for PoolTransactionReject {
Reject::Full(..) => Self::Full(format!("{}", reject)),
Reject::Duplicated(_) => Self::Duplicated(format!("{}", reject)),
Reject::Malformed(_) => Self::Malformed(format!("{}", reject)),
Reject::DeclaredWrongCycles(..) => Self::DeclaredWrongCycles(format!("{}", reject)),
Reject::Resolve(_) => Self::Resolve(format!("{}", reject)),
Reject::Verification(_) => Self::Verification(format!("{}", reject)),
}
Expand Down
5 changes: 5 additions & 0 deletions util/types/src/core/tx_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ pub enum Reject {
#[error("Malformed {0} transaction")]
Malformed(String),

/// Declared wrong cycles
#[error("Declared wrong cycles {0}, actual {1}")]
DeclaredWrongCycles(Cycle, Cycle),

/// Resolve failed
#[error("Resolve failed {0}")]
Resolve(OutPointError),
Expand Down Expand Up @@ -64,6 +68,7 @@ impl Reject {
pub fn is_malformed_tx(&self) -> bool {
match self {
Reject::Malformed(_) => true,
Reject::DeclaredWrongCycles(..) => true,
Reject::Verification(err) => is_malformed_from_verification(err),
Reject::Resolve(OutPointError::OverMaxDepExpansionLimit { ban }) => *ban,
_ => false,
Expand Down

0 comments on commit 1a95cbc

Please sign in to comment.