Skip to content

Commit

Permalink
refactor: remove the verify by ckb vm verification
Browse files Browse the repository at this point in the history
  • Loading branch information
Eason committed Nov 14, 2023
1 parent 04a7ae3 commit befcbb9
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 778 deletions.
1 change: 0 additions & 1 deletion core/executor/src/precompiles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ mod sha256;

#[cfg(test)]
mod tests;
mod verify_by_ckb_vm;

use std::collections::BTreeMap;

Expand Down
140 changes: 0 additions & 140 deletions core/executor/src/precompiles/verify_by_ckb_vm.rs

This file was deleted.

51 changes: 4 additions & 47 deletions core/interoperation/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
pub mod utils;
use std::error::Error;

use std::{error::Error, sync::Arc};

use ckb_chain_spec::consensus::Consensus;
use ckb_script::{TransactionScriptsVerifier, TxVerifyEnv};
use ckb_traits::CellDataProvider;
use ckb_types::core::{Cycle, HeaderBuilder, TransactionView};
use ckb_types::{packed, prelude::Pack};
use ckb_types::packed;
use ckb_vm::machine::{asm::AsmCoreMachine, DefaultMachineBuilder, SupportMachine, VERSION1};
use ckb_vm::{Error as VMError, ISA_B, ISA_IMC, ISA_MOP};

use protocol::traits::{CkbDataProvider, Context, Interoperation};
use protocol::types::{Bytes, CellDep, CellWithData, OutPoint, VMResp};
use protocol::traits::{Context, Interoperation};
use protocol::types::{Bytes, CellDep, OutPoint, VMResp};
use protocol::{Display, ProtocolError, ProtocolErrorKind, ProtocolResult};

use crate::utils::resolve_transaction;

const ISA: u8 = ISA_IMC | ISA_B | ISA_MOP;
const GAS_TO_CYCLE_COEF: u64 = 6_000;

// The following information is from CKB block [10976708](https://explorer.nervos.org/block/10976708)
// which is CKB2023 disabled.
const CKB2023_DISABLED_NUMBER: u64 = 10_976_708;
const CKB2023_DISABLED_EPOCH: u64 = 0x53c007f0020c8;

pub const fn gas_to_cycle(gas: u64) -> u64 {
gas * GAS_TO_CYCLE_COEF
}
Expand Down Expand Up @@ -76,37 +64,6 @@ impl Interoperation for InteroperationImpl {
cycles: vm.machine.cycles(),
})
}

/// Todo: After CKB2023 is enabled, a hardfork is needed to support the new
/// VM version and syscalls.
fn verify_by_ckb_vm<DL: CkbDataProvider + Sync + Send + 'static>(
_ctx: Context,
data_loader: DL,
mocked_tx: &TransactionView,
dummy_input: Option<CellWithData>,
max_cycles: u64,
) -> ProtocolResult<Cycle> {
let rtx = Arc::new(resolve_transaction(&data_loader, mocked_tx, dummy_input)?);
log::debug!("[mempool]: Verify by ckb vm tx {:?}", rtx);

// The consensus and tx_env arguments are used for judge if the VM version2 and
// syscalls3 are enabled. Due to only the hardfork field in consensus and the
// epoch field in tx_env is used, the provided arguments only need to fill these
// fields correctly.
let (ckb_spec, ckb2023_disabled_env) = (
Arc::new(Consensus::default()),
Arc::new(TxVerifyEnv::new_commit(
&HeaderBuilder::default()
.number(CKB2023_DISABLED_NUMBER.pack())
.epoch(CKB2023_DISABLED_EPOCH.pack())
.build(),
)),
);

TransactionScriptsVerifier::new(rtx, data_loader, ckb_spec, ckb2023_disabled_env)
.verify(max_cycles)
.map_err(|e| InteroperationError::Ckb(e).into())
}
}

#[derive(Debug, Display)]
Expand Down
82 changes: 0 additions & 82 deletions core/interoperation/src/utils.rs

This file was deleted.

55 changes: 11 additions & 44 deletions core/mempool/src/adapter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use protocol::traits::{
};
use protocol::types::{
recover_intact_pub_key, Backend, BatchSignedTxs, CellDepWithPubKey, Hash, MerkleRoot,
SignatureR, SignatureS, SignedTransaction, H160, U256,
SignedTransaction, H160, U256,
};
use protocol::{
async_trait, codec::ProtocolCodec, tokio, trie, Display, ProtocolError, ProtocolErrorKind,
Expand All @@ -35,8 +35,6 @@ use crate::adapter::message::{MsgPullTxs, END_GOSSIP_NEW_TXS, RPC_PULL_TXS};
use crate::context::TxContext;
use crate::MemPoolError;

const MAX_VERIFY_CKB_VM_CYCLES: u64 = 50_000_000;

struct IntervalTxsBroadcaster;

impl IntervalTxsBroadcaster {
Expand Down Expand Up @@ -289,48 +287,17 @@ where

let root = self.executor_backend(ctx).await?.get_image_cell_root();

// Verify interoperation signature
match signature.r[0] {
0u8 => {
// Call CKB-VM mode
let r = rlp::decode::<CellDepWithPubKey>(&signature.r[1..])
.map_err(AdapterError::Rlp)?;

InteroperationImpl::call_ckb_vm(
Default::default(),
&DataProvider::new(root),
r.cell_dep,
&[r.pub_key, signature.s],
u64::MAX,
)
.map_err(|e| AdapterError::VerifySignature(e.to_string()))?;
}
_ => {
// Verify by mock transaction mode
let r = SignatureR::decode(&signature.r)?;
let s = SignatureS::decode(&signature.s)?;

if r.inputs_len() != s.witnesses.len() {
return Err(AdapterError::VerifySignature(
"signature item mismatch".to_string(),
)
.into());
}
// Verify interoperation signature call CKB-VM mode
let r = rlp::decode::<CellDepWithPubKey>(&signature.r[1..]).map_err(AdapterError::Rlp)?;
InteroperationImpl::call_ckb_vm(
Default::default(),
&DataProvider::new(root),
r.cell_dep,
&[r.pub_key, signature.s],
u64::MAX,
)
.map_err(|e| AdapterError::VerifySignature(e.to_string()))?;

InteroperationImpl::verify_by_ckb_vm(
Default::default(),
DataProvider::new(root),
&InteroperationImpl::dummy_transaction(
r.clone(),
s,
Some(stx.transaction.signature_hash(true).0),
),
r.dummy_input(),
MAX_VERIFY_CKB_VM_CYCLES,
)
.map_err(|e| AdapterError::VerifySignature(e.to_string()))?;
}
}
Ok(())
}

Expand Down
Loading

0 comments on commit befcbb9

Please sign in to comment.