Page MenuHomePhabricator

[Chronik] Add `read_bytes` and `read_array`
ClosedPublic

Authored by tobias_ruck on Apr 26 2023, 10:44.

Details

Reviewers
Fabien
Group Reviewers
Restricted Project
Commits
rABC03cac5b3187e: [Chronik] Add `read_bytes` and `read_array`
Summary

Simple helpers that allows parsing out bytes and returning an error otherwise.

Test Plan

ninja check-crates

Diff Detail

Repository
rABC Bitcoin ABC
Branch
chronik-read-bytes
Lint
Lint Passed
Unit
No Test Coverage
Build Status
Buildable 23469
Build 46557: Build Diffbuild-chronik
Build 46556: arc lint + arc unit

Event Timeline

chronik/bitcoinsuite-core/src/bytes.rs
35

Even if well documented, this is rather unusual to have read function mutating the input to be read. Is there a real benefit in doing so vs returning a shorten copy of the input bytes ?

chronik/bitcoinsuite-core/src/bytes.rs
35

The benefit is that you can write really neat code parsing structured data:

fn parse_slpv2(data: &mut Bytes) -> Result<...> {
  let lokad_id: [u8; 4] = read_array(data)?;
  let token_type = read_array::<1>(data)?[0];
  let tx_type_len= read_array::<1>(data)?[0];
  let tx_type = read_bytes(data, tx_type_len as usize)?;
  // ...
}
This revision is now accepted and ready to land.Apr 26 2023, 13:30