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

feat: check block filter hashes #132

Merged
merged 6 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
341 changes: 173 additions & 168 deletions Cargo.lock

Large diffs are not rendered by default.

39 changes: 20 additions & 19 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,23 @@ homepage = "https://github.com/nervosnetwork/ckb-light-client"
repository = "https://github.com/nervosnetwork/ckb-light-client"

[dependencies]
ckb-app-config = "0.106.0"
ckb-async-runtime = "0.106.0"
ckb-constant = "0.106.0"
ckb-types = "0.106.0"
ckb-network = "0.106.0"
ckb-jsonrpc-types = "0.106.0"
ckb-error = "0.106.0"
ckb-script = "0.106.0"
ckb-chain-spec = "0.106.0"
ckb-traits = "0.106.0"
ckb-resource = "0.106.0"
ckb-verification = "0.106.0"
ckb-app-config = "0.108.0"
ckb-async-runtime = "0.108.0"
ckb-constant = "0.108.0"
ckb-types = "0.108.0"
ckb-network = "0.108.0"
ckb-jsonrpc-types = "0.108.0"
ckb-error = "0.108.0"
ckb-script = "0.108.0"
ckb-chain-spec = "0.108.0"
ckb-traits = "0.108.0"
ckb-resource = "0.108.0"
ckb-verification = "0.108.0"
ckb-systemtime = "0.108.0"
ckb-hash = "0.108.0"
ckb-merkle-mountain-range = "0.5.1"
golomb-coded-set = "0.2.0"
rocksdb = { package = "ckb-rocksdb", version ="=0.18.3", features = ["snappy"], default-features = false }
rocksdb = { package = "ckb-rocksdb", version ="=0.19.0", features = ["snappy"], default-features = false }
numext-fixed-uint = { version = "0.1", features = ["support_rand", "support_heapsize", "support_serde"] }
anyhow = "1.0.56"
thiserror = "1.0.30"
Expand All @@ -37,18 +39,17 @@ path-clean = "0.1.0"
rand = "0.8.5"
dashmap = "5.3"
linked-hash-map = "0.5.6"
faketime = "0.2.1"
jsonrpc-core = "18.0"
jsonrpc-derive = "18.0"
jsonrpc-http-server = "18.0"
jsonrpc-server-utils = "18.0"

[dev-dependencies]
ckb-launcher = "0.106.0"
ckb-shared = "0.106.0"
ckb-chain = "0.106.0"
ckb-tx-pool = "0.106.0"
ckb-store = "0.106.0"
ckb-launcher = "0.108.0"
ckb-shared = "0.108.0"
ckb-chain = "0.108.0"
ckb-tx-pool = "0.108.0"
ckb-store = "0.108.0"
tempfile = "3.0"
rand = "0.6"
serde_json = "1.0"
Expand Down
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ mod types;
mod utils;
mod verify;

// TODO Remove the patches if the code was merged into CKB.
mod patches;

use config::AppConfig;

fn main() -> anyhow::Result<()> {
Expand Down
73 changes: 73 additions & 0 deletions src/patches.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use std::io::{Cursor, Write};

#[cfg(not(test))]
use ckb_hash::blake2b_256;
use golomb_coded_set::{GCSFilterWriter, SipHasher24Builder, M, P};

use ckb_types::{core::TransactionView, packed, prelude::*};

/// Provides data for building block filter data.
pub trait FilterDataProvider {
/// Finds the cell through its out point.
fn cell(&self, out_point: &packed::OutPoint) -> Option<packed::CellOutput>;
}

/// Builds filter data for transactions.
pub fn build_filter_data<P: FilterDataProvider>(
provider: P,
transactions: &[TransactionView],
) -> (Vec<u8>, Vec<packed::OutPoint>) {
let mut filter_writer = Cursor::new(Vec::new());
let mut filter = build_gcs_filter(&mut filter_writer);
let mut missing_out_points = Vec::new();
for tx in transactions {
if !tx.is_cellbase() {
for out_point in tx.input_pts_iter() {
if let Some(input_cell) = provider.cell(&out_point) {
filter.add_element(input_cell.calc_lock_hash().as_slice());
if let Some(type_script) = input_cell.type_().to_opt() {
filter.add_element(type_script.calc_script_hash().as_slice());
}
} else {
missing_out_points.push(out_point);
}
}
}
for output_cell in tx.outputs() {
filter.add_element(output_cell.calc_lock_hash().as_slice());
if let Some(type_script) = output_cell.type_().to_opt() {
filter.add_element(type_script.calc_script_hash().as_slice());
}
}
}
filter
.finish()
.expect("flush to memory writer should be OK");
let filter_data = filter_writer.into_inner();
(filter_data, missing_out_points)
}

/// Calculates a block filter hash.
#[cfg(not(test))]
pub fn calc_filter_hash(
parent_block_filter_hash: &packed::Byte32,
filter_data: &packed::Bytes,
) -> [u8; 32] {
blake2b_256(
[
parent_block_filter_hash.as_slice(),
filter_data.calc_raw_data_hash().as_slice(),
]
.concat(),
)
}

// TODO Use real block filter hashes in unit tests.
#[cfg(test)]
pub fn calc_filter_hash(_: &packed::Byte32, _: &packed::Bytes) -> [u8; 32] {
Default::default()
}

fn build_gcs_filter(out: &mut dyn Write) -> GCSFilterWriter<SipHasher24Builder> {
GCSFilterWriter::new(out, SipHasher24Builder::new(0, 0), M, P)
}
Loading