Skip to content

Commit

Permalink
Use lazy reader(local version)
Browse files Browse the repository at this point in the history
  • Loading branch information
XuJiandong committed Mar 19, 2024
1 parent fce1b3c commit 67e1ec6
Show file tree
Hide file tree
Showing 20 changed files with 2,816 additions and 77 deletions.
9 changes: 7 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ opt-level = 1
debug = false
panic = 'abort'
debug-assertions = true

[patch.crates-io]
molecule = { path = "../molecule/bindings/rust" }
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ mol:
moleculec --language rust --schema-file schemas/top_level.mol > ckb-transaction-cobuild/src/schemas/top_level.rs
cargo fmt

mol2:
moleculec --language rust-lazy-reader --schema-file schemas/basic.mol > ckb-transaction-cobuild/src/schemas2/basic.rs
moleculec --language rust-lazy-reader --schema-file schemas/top_level.mol > ckb-transaction-cobuild/src/schemas2/top_level.rs
moleculec --language rust-lazy-reader --schema-file schemas/blockchain.mol > ckb-transaction-cobuild/src/schemas2/blockchain.rs
cargo fmt


install:
rustup target add riscv64imac-unknown-none-elf
cargo install cross --git https://github.com/cross-rs/cross
Expand Down
2 changes: 1 addition & 1 deletion ckb-transaction-cobuild/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ log = []
[dependencies]
blake2b-ref = "0.3.1"
ckb-std = { version = "0.14.3", default-features = false, features = ["ckb-types"] }
molecule = { version = "0.7.5", default-features = false }
molecule = { version = "0.7.5", default-features = false, features = ["bytes_vec"] }
ckb-gen-types = { version = "0.111.0", default-features = false }
13 changes: 13 additions & 0 deletions ckb-transaction-cobuild/src/blake2b.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
pub use blake2b_ref::{Blake2b, Blake2bBuilder};
pub use molecule::lazy_reader::Cursor;

pub const PERSONALIZATION_SIGHASH_ALL: &[u8] = b"ckb-tcob-sighash";
pub const PERSONALIZATION_SIGHASH_ALL_ONLY: &[u8] = b"ckb-tcob-sgohash";
pub const PERSONALIZATION_OTX: &[u8] = b"ckb-tcob-otxhash";

const BATCH_SIZE: usize = 2048;

/// return a blake2b instance with personalization for SighashAll
pub fn new_sighash_all_blake2b() -> Blake2bStatistics {
Blake2bStatistics::new(
Expand Down Expand Up @@ -45,6 +48,16 @@ impl Blake2bStatistics {
self.blake2b.update(data);
self.count += data.len();
}
pub fn update_cursor(&mut self, mut cursor: Cursor) {
let mut buf = [0u8; BATCH_SIZE];
while cursor.size > 0 {
let read_len = cursor.read_at(&mut buf).unwrap();
if read_len > 0 {
self.blake2b.update(&buf[0..read_len]);
cursor = cursor.slice_by_start(read_len).unwrap();
}
}
}

pub fn finalize(self, dst: &mut [u8]) {
self.blake2b.finalize(dst)
Expand Down
36 changes: 36 additions & 0 deletions ckb-transaction-cobuild/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use ckb_std::error::SysError;
use molecule::error::VerificationError;
use molecule::lazy_reader::Error as LazyReaderError;

#[derive(Debug)]
pub enum Error {
Sys(SysError),
LazyReader(LazyReaderError),
MoleculeEncoding,
WrongSighashAll,
WrongWitnessLayout,
WrongOtxStart,
WrongOtx,
NoSealFound,
AuthError,
ScriptHashAbsent,
WrongCount,
}

impl From<SysError> for Error {
fn from(e: SysError) -> Self {
Error::Sys(e)
}
}

impl From<VerificationError> for Error {
fn from(_: VerificationError) -> Self {
Error::MoleculeEncoding
}
}

impl From<LazyReaderError> for Error {
fn from(e: LazyReaderError) -> Self {
Error::LazyReader(e)
}
}
56 changes: 56 additions & 0 deletions ckb-transaction-cobuild/src/lazy_reader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use core::cmp::min;

use alloc::boxed::Box;
use ckb_std::{error::SysError, syscalls};
use molecule::lazy_reader::{Cursor, Error, Read};

use crate::log;

pub struct TransactionReader {
pub total_length: usize,
}

impl TransactionReader {
pub fn new() -> Self {
let mut buf = [0u8; 4];
let result = syscalls::load_transaction(&mut buf, 0);
let total_length = match result {
Ok(l) => l,
Err(e) => match e {
SysError::LengthNotEnough(l) => l,
_ => panic!("error on load_transaction"),
},
};
Self { total_length }
}
}

impl Read for TransactionReader {
fn read(&self, buf: &mut [u8], offset: usize) -> Result<usize, Error> {
log!(
"try to read {} bytes into buffer with offset {}",
buf.len(),
offset
);
if offset >= self.total_length {
return Err(Error::OutOfBound(offset, self.total_length));
}

let remaining_len = self.total_length - offset;
let min_len = min(remaining_len, buf.len());

if (offset + min_len) > self.total_length {
return Err(Error::OutOfBound(offset + min_len, self.total_length));
}
let actual_len = syscalls::load_transaction(buf, offset).map_err(|_| Error::Common)?;
let read_len = min(buf.len(), actual_len);
log!("totally read {} bytes", read_len);
Ok(read_len)
}
}

impl From<TransactionReader> for Cursor {
fn from(data: TransactionReader) -> Self {
Cursor::new(data.total_length, Box::new(data))
}
}
Loading

0 comments on commit 67e1ec6

Please sign in to comment.