|
| 1 | +use std::fs::read_to_string; |
| 2 | + |
| 3 | +use alloc::format; |
| 4 | +use ckb_jsonrpc_types::TransactionView; |
| 5 | +use ckb_types::packed::WitnessArgs; |
| 6 | +use serde_json::from_str as from_json_str; |
| 7 | + |
| 8 | +use crate::{ |
| 9 | + error::UpdateError, |
| 10 | + molecule::prelude::*, |
| 11 | + tests, |
| 12 | + types::{ |
| 13 | + core, |
| 14 | + packed::{SpvClient, SpvUpdate}, |
| 15 | + prelude::*, |
| 16 | + }, |
| 17 | +}; |
| 18 | + |
| 19 | +// This case shows that: |
| 20 | +// - For the signet network, `header.bits` should be the same as `new_info.1`. |
| 21 | +// To run this test, use the following command: |
| 22 | +// `cargo test --package ckb-bitcoin-spv-verifier --lib -- tests::signet::signet_verify_new_client_error_197568 --exact --show-output` |
| 23 | +#[test] |
| 24 | +fn signet_verify_new_client_error_197568() { |
| 25 | + let ret = verify_new_client_common( |
| 26 | + "tx-0528-error-check-header-target-adjust-info.json", |
| 27 | + 1, // cell_dep_index |
| 28 | + ); |
| 29 | + assert!(ret.is_err()); |
| 30 | +} |
| 31 | + |
| 32 | +// This case shows that: |
| 33 | +// - For the signet network, target max should be Target::MAX_ATTAINABLE_SIGNET. |
| 34 | +// To run this test, use the following command: |
| 35 | +// `cargo test --package ckb-bitcoin-spv-verifier --lib -- tests::signet::signet_verify_new_client_error_header_197567 --exact --show-output` |
| 36 | +#[test] |
| 37 | +fn signet_verify_new_client_error_header_197567() { |
| 38 | + let ret = verify_new_client_common( |
| 39 | + "tx-0xd663a1dfdfbf9a4824950c44c0d5f5e65f6b1ba4710a0308edecadeaed3ac549.json", |
| 40 | + 2, // cell_dep_index |
| 41 | + ); |
| 42 | + assert!(ret.is_err()); |
| 43 | +} |
| 44 | + |
| 45 | +// To run this test, use the following command: |
| 46 | +// `cargo test --package ckb-bitcoin-spv-verifier --lib -- tests::signet::signet_verify_new_client_normal --exact --show-output` |
| 47 | +#[test] |
| 48 | +fn signet_verify_new_client_normal() { |
| 49 | + let ret = verify_new_client_common( |
| 50 | + "tx-0x01d827b049778ffb53532d8263009512a696647bde4acc7f10f39ded14c066ab.json", |
| 51 | + 2, // cell_dep_index |
| 52 | + ); |
| 53 | + assert!(ret.is_ok()); |
| 54 | +} |
| 55 | + |
| 56 | +fn verify_new_client_common(tx_file: &str, cell_dep_index: usize) -> Result<(), UpdateError> { |
| 57 | + tests::setup(); |
| 58 | + |
| 59 | + let path = tests::data::find_bin_file("signet", tx_file); |
| 60 | + let tx = read_to_string(path).unwrap(); |
| 61 | + let tx: TransactionView = from_json_str(&tx).unwrap(); |
| 62 | + |
| 63 | + let witnesses = tx.inner.witnesses; |
| 64 | + let witness_args = WitnessArgs::from_slice(witnesses[0].as_bytes()).unwrap(); |
| 65 | + let spv_update_bin = witness_args.output_type().to_opt().unwrap().raw_data(); |
| 66 | + let spv_update = SpvUpdate::from_slice(&spv_update_bin).unwrap(); |
| 67 | + |
| 68 | + let client_bin = tx.inner.outputs_data[1].clone(); |
| 69 | + let client = SpvClient::from_slice(client_bin.as_bytes()).unwrap(); |
| 70 | + |
| 71 | + let cell_dep = tx.inner.cell_deps[cell_dep_index].out_point.clone(); |
| 72 | + let path = |
| 73 | + tests::data::find_bin_file("signet", format!("tx-0x{}.json", cell_dep.tx_hash).as_str()); |
| 74 | + let previous_tx = read_to_string(path).unwrap(); |
| 75 | + let previous_tx: TransactionView = from_json_str(&previous_tx).unwrap(); |
| 76 | + let cell_dep_data_bin = &previous_tx.inner.outputs_data[cell_dep.index.value() as usize]; |
| 77 | + let cell_dep_client = SpvClient::from_slice(cell_dep_data_bin.as_bytes()).unwrap(); |
| 78 | + |
| 79 | + let mut cell_dep_client: core::SpvClient = cell_dep_client.unpack(); |
| 80 | + cell_dep_client.id = client.id().into(); |
| 81 | + let input_client = cell_dep_client.pack(); |
| 82 | + input_client.verify_new_client(&client, spv_update, 64) |
| 83 | +} |
0 commit comments