Skip to content

Commit

Permalink
feat: intro preview chain
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangsoledad committed Oct 9, 2024
1 parent 54d5edb commit 843e29b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 22 deletions.
10 changes: 8 additions & 2 deletions verification/src/block_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
use ckb_chain_spec::consensus::Consensus;
use ckb_error::Error;
use ckb_types::{
core::BlockView,
core::{BlockView, ScriptHashType},
packed::{CellInput, CellbaseWitness},
prelude::*,
};
Expand Down Expand Up @@ -105,7 +105,13 @@ impl CellbaseVerifier {
if cellbase_transaction
.witnesses()
.get(0)
.and_then(|witness| CellbaseWitness::from_slice(&witness.raw_data()).ok())
.and_then(|witness| {
CellbaseWitness::from_slice(&witness.raw_data())
.ok()
.and_then(|cellbase_witness| {
ScriptHashType::try_from(cellbase_witness.lock().hash_type()).ok()
})
})
.is_none()
{
return Err((CellbaseError::InvalidWitness).into());
Expand Down
74 changes: 54 additions & 20 deletions verification/src/tests/block_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use ckb_types::{

use super::BuilderBaseOnBlockNumber;

const MOCK_BLOCK_NUMBER: BlockNumber = 2;

fn create_cellbase_transaction_with_block_number(number: BlockNumber) -> TransactionView {
TransactionBuilder::default()
.input(CellInput::new_cellbase_input(number))
Expand All @@ -32,7 +34,7 @@ fn create_cellbase_transaction_with_block_number(number: BlockNumber) -> Transac

fn create_cellbase_transaction_with_capacity(capacity: Capacity) -> TransactionView {
TransactionBuilder::default()
.input(CellInput::new_cellbase_input(0))
.input(CellInput::new_cellbase_input(MOCK_BLOCK_NUMBER))
.output(
CellOutputBuilder::default()
.capacity(capacity.pack())
Expand All @@ -45,7 +47,7 @@ fn create_cellbase_transaction_with_capacity(capacity: Capacity) -> TransactionV

fn create_cellbase_transaction_with_non_empty_output_data() -> TransactionView {
TransactionBuilder::default()
.input(CellInput::new_cellbase_input(0))
.input(CellInput::new_cellbase_input(MOCK_BLOCK_NUMBER))
.output(
CellOutputBuilder::default()
.capacity(capacity_bytes!(100).pack())
Expand All @@ -58,7 +60,7 @@ fn create_cellbase_transaction_with_non_empty_output_data() -> TransactionView {

fn create_cellbase_transaction_with_two_output() -> TransactionView {
TransactionBuilder::default()
.input(CellInput::new_cellbase_input(0))
.input(CellInput::new_cellbase_input(MOCK_BLOCK_NUMBER))
.output(
CellOutputBuilder::default()
.capacity(capacity_bytes!(100).pack())
Expand All @@ -76,7 +78,7 @@ fn create_cellbase_transaction_with_two_output() -> TransactionView {

fn create_cellbase_transaction_with_two_output_data() -> TransactionView {
TransactionBuilder::default()
.input(CellInput::new_cellbase_input(0))
.input(CellInput::new_cellbase_input(MOCK_BLOCK_NUMBER))
.output(
CellOutputBuilder::default()
.capacity(capacity_bytes!(100).pack())
Expand All @@ -92,6 +94,25 @@ fn create_cellbase_transaction() -> TransactionView {
create_cellbase_transaction_with_capacity(capacity_bytes!(100))
}

fn create_cellbase_transaction_with_unknown_hash_type() -> TransactionView {
TransactionBuilder::default()
.input(CellInput::new_cellbase_input(MOCK_BLOCK_NUMBER))
.output(
CellOutputBuilder::default()
.capacity(capacity_bytes!(100).pack())
.build(),
)
.output_data(Bytes::new().pack())
.witness(
Script::default()
.as_builder()
.hash_type(3.into())
.build()
.into_witness(),
)
.build()
}

fn create_normal_transaction() -> TransactionView {
TransactionBuilder::default()
.input(CellInput::new(OutPoint::new(h256!("0x1").pack(), 0), 0))
Expand Down Expand Up @@ -154,7 +175,7 @@ pub fn test_block_with_incorrect_cellbase_number() {

#[test]
pub fn test_block_with_one_cellbase_at_last() {
let block = BlockBuilder::new_with_number(2)
let block = BlockBuilder::new_with_number(MOCK_BLOCK_NUMBER)
.transaction(create_normal_transaction())
.transaction(create_cellbase_transaction())
.build();
Expand All @@ -166,9 +187,22 @@ pub fn test_block_with_one_cellbase_at_last() {
);
}

#[test]
pub fn test_block_with_unknown_hash_type_cellbase() {
let block = BlockBuilder::new_with_number(MOCK_BLOCK_NUMBER)
.transaction(create_cellbase_transaction_with_unknown_hash_type())
.build();

let verifier = CellbaseVerifier::new();
assert_error_eq!(
verifier.verify(&block).unwrap_err(),
CellbaseError::InvalidWitness,
);
}

#[test]
pub fn test_cellbase_with_non_empty_output_data() {
let block = BlockBuilder::new_with_number(2)
let block = BlockBuilder::new_with_number(MOCK_BLOCK_NUMBER)
.transaction(create_cellbase_transaction_with_non_empty_output_data())
.build();
let verifier = CellbaseVerifier::new();
Expand All @@ -182,38 +216,38 @@ pub fn test_cellbase_with_non_empty_output_data() {
pub fn test_cellbase_without_output() {
// without_output
let cellbase_without_output = TransactionBuilder::default()
.input(CellInput::new_cellbase_input(2u64))
.input(CellInput::new_cellbase_input(MOCK_BLOCK_NUMBER))
.witness(Script::default().into_witness())
.build();
let block = BlockBuilder::new_with_number(2)
let block = BlockBuilder::new_with_number(MOCK_BLOCK_NUMBER)
.transaction(cellbase_without_output)
.build();
let result = CellbaseVerifier::new().verify(&block);
assert!(result.is_ok(), "Unexpected error {result:?}");

// only output_data
let cellbase_without_output = TransactionBuilder::default()
.input(CellInput::new_cellbase_input(2u64))
.input(CellInput::new_cellbase_input(MOCK_BLOCK_NUMBER))
.witness(Script::default().into_witness())
.output_data(Bytes::new().pack())
.build();
let block = BlockBuilder::new_with_number(2)
let block = BlockBuilder::new_with_number(MOCK_BLOCK_NUMBER)
.transaction(cellbase_without_output)
.build();
let result = CellbaseVerifier::new().verify(&block);
assert_error_eq!(result.unwrap_err(), CellbaseError::InvalidOutputQuantity);

// only output
let cellbase_without_output = TransactionBuilder::default()
.input(CellInput::new_cellbase_input(2u64))
.input(CellInput::new_cellbase_input(MOCK_BLOCK_NUMBER))
.witness(Script::default().into_witness())
.output(
CellOutputBuilder::default()
.capacity(capacity_bytes!(100).pack())
.build(),
)
.build();
let block = BlockBuilder::new_with_number(2)
let block = BlockBuilder::new_with_number(MOCK_BLOCK_NUMBER)
.transaction(cellbase_without_output)
.build();
let result = CellbaseVerifier::new().verify(&block);
Expand All @@ -222,7 +256,7 @@ pub fn test_cellbase_without_output() {

#[test]
pub fn test_cellbase_with_two_output() {
let block = BlockBuilder::new_with_number(2)
let block = BlockBuilder::new_with_number(MOCK_BLOCK_NUMBER)
.transaction(create_cellbase_transaction_with_two_output())
.build();
let verifier = CellbaseVerifier::new();
Expand All @@ -234,7 +268,7 @@ pub fn test_cellbase_with_two_output() {

#[test]
pub fn test_cellbase_with_two_output_data() {
let block = BlockBuilder::new_with_number(2)
let block = BlockBuilder::new_with_number(MOCK_BLOCK_NUMBER)
.transaction(create_cellbase_transaction_with_two_output_data())
.build();
let verifier = CellbaseVerifier::new();
Expand All @@ -247,7 +281,7 @@ pub fn test_cellbase_with_two_output_data() {
#[test]
pub fn test_block_with_duplicated_txs() {
let tx = create_normal_transaction();
let block = BlockBuilder::new_with_number(2)
let block = BlockBuilder::new_with_number(MOCK_BLOCK_NUMBER)
.transaction(tx.clone())
.transaction(tx)
.build();
Expand All @@ -261,7 +295,7 @@ pub fn test_block_with_duplicated_txs() {

#[test]
pub fn test_block_with_duplicated_proposals() {
let block = BlockBuilder::new_with_number(2)
let block = BlockBuilder::new_with_number(MOCK_BLOCK_NUMBER)
.proposal(ProposalShortId::zero())
.proposal(ProposalShortId::zero())
.build();
Expand All @@ -275,7 +309,7 @@ pub fn test_block_with_duplicated_proposals() {

#[test]
pub fn test_transaction_root() {
let header = HeaderBuilder::new_with_number(2)
let header = HeaderBuilder::new_with_number(MOCK_BLOCK_NUMBER)
.transactions_root(Byte32::zero())
.build();
let block = BlockBuilder::default()
Expand All @@ -292,7 +326,7 @@ pub fn test_transaction_root() {

#[test]
pub fn test_proposals_root() {
let header = HeaderBuilder::new_with_number(2)
let header = HeaderBuilder::new_with_number(MOCK_BLOCK_NUMBER)
.proposals_hash(h256!("0x1").pack())
.build();
let block = BlockBuilder::default()
Expand All @@ -309,7 +343,7 @@ pub fn test_proposals_root() {

#[test]
pub fn test_block_with_two_cellbases() {
let block = BlockBuilder::new_with_number(2)
let block = BlockBuilder::new_with_number(MOCK_BLOCK_NUMBER)
.transaction(create_cellbase_transaction())
.transaction(create_cellbase_transaction())
.build();
Expand Down Expand Up @@ -370,7 +404,7 @@ pub fn test_max_block_bytes_verifier_skip_genesis() {

#[test]
pub fn test_max_block_bytes_verifier() {
let block = BlockBuilder::new_with_number(2).build();
let block = BlockBuilder::new_with_number(MOCK_BLOCK_NUMBER).build();

{
let verifier =
Expand Down

0 comments on commit 843e29b

Please sign in to comment.