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: ByteReader and ByteAppender refactored to support generics #199

Closed
wants to merge 10 commits into from
Closed

Feat: ByteReader and ByteAppender refactored to support generics #199

wants to merge 10 commits into from

Conversation

sveamarcus
Copy link
Contributor

@sveamarcus sveamarcus commented Oct 24, 2023

Pull Request type

Please check the type of change your PR introduces:

  • Bugfix
  • Feature
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes, no API changes)
  • Build-related changes
  • Documentation content changes
  • Other (please describe):

What is the current behavior?

Currently .reader() and .append_... only supports ByteArray.

Issue Number: N/A

What is the new behavior?

  • Adds a generic implementation divided into two traits
  • ByteReader - on top of supporting ByteArray it can also read from Array<u8> and Span<u8> making it more versatile
  • ByteAppender - Refactored all .append_... calls into a new trait, which in turn can be used on top of both Array<u8> as well as the currently supported ByteArray
  • Let's illustrate with an example of reading/writing the Bitcoin block header since it is simple enough using both Array<u8> and ByteArray
use alexandria_data_structures::byte_appender::ByteAppender;
use alexandria_data_structures::byte_reader::ByteReader;

// Append
let mut array: Array<u8> = array![];
let version = 1_i32; // strangely a signed int32
let prev_hash = u256{low: 0, high: 0x123};
let merkle_root = u256{low: 0x0, high: 0x456};
let time = 1698179674_u32; 
let n_bits = 0x30c31b18_u32; 
let nonce = fe9f0864_u32;
array.append_i32_le(version); // little endian
array.append_u256(prev_hash); // big endian
array.append_u256(merkle_root); // big endian
array.append_u32_le(time); // little endian
array.append_u32_le(n_bits); // little endian
array.append_u32_le(nonce); // little endian
// this array can now be used to hash the header using sha256

// This is very costly, not sure if it's a good idea to support Into<S, T>? Input welcome. 
// Note, it's not necessary to convert into a ByteArray, .reader() can also be called on
// Array<u8> and Span<u8>. This here is just to show the versatility of the API.
let input: ByteArray = array.into();

// Read a ByteArray containing the header such as above
let mut reader = input.reader();
let version = reader.read_i32_le().unwrap();
let prev_hash = reader.read_u256().unwrap();
let merkle_root = reader.read_u256().unwrap();
let time = reader.read_u32_le().unwrap();
let n_bits = read.read_u32_le().unwrap();
let nonce = reader.read_u32_le().unwrap();

Does this introduce a breaking change?

  • Yes
  • No

Some use imports may need updating. All calls remain largely the same.

Other information

@sveamarcus sveamarcus requested a review from 0xLucqs as a code owner October 24, 2023 20:55
@sveamarcus sveamarcus closed this Nov 9, 2023
@sveamarcus sveamarcus deleted the feat/generic_byte_reader branch November 9, 2023 19:02
@github-actions github-actions bot locked and limited conversation to collaborators Nov 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants