-
Notifications
You must be signed in to change notification settings - Fork 243
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
Block header parsing and transaction ID computation #66
Changes from 3 commits
e21be37
20d5cdc
a1664c6
670bb27
4289843
b856d23
663f9d6
3501365
6c99d71
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use hex; | ||
use std::fmt; | ||
use std::ops::Deref; | ||
|
||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | ||
pub struct BlockHash(pub [u8; 32]); | ||
|
||
impl fmt::Display for BlockHash { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aside from the name, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not convinced that the extra indirection to the underlying bytes is worth it yet. If we find the reverse-hex format is bleeding in too much from |
||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | ||
let mut data = self.0.to_vec(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead, you could initialize a local clone of the (Not blocking.) |
||
data.reverse(); | ||
formatter.write_str(&hex::encode(data)) | ||
} | ||
} | ||
|
||
/// A Zcash block header. | ||
pub struct BlockHeader(BlockHeaderData); | ||
|
||
impl Deref for BlockHeader { | ||
type Target = BlockHeaderData; | ||
|
||
fn deref(&self) -> &BlockHeaderData { | ||
&self.0 | ||
} | ||
} | ||
|
||
pub struct BlockHeaderData { | ||
pub version: i32, | ||
pub prev_block: BlockHash, | ||
pub merkle_root: [u8; 32], | ||
pub final_sapling_root: [u8; 32], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if this is out of scope for the PR, but the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They are all hash outputs, but of at least two different hash functions. In I use separate types for block hashes and transaction IDs because those are the only two that external code is likely to actually interact with, whereas other block header fields (thus far) will only be used internally. Once the refactor is further along and we get to the stage of cleaning up the crate APIs, we should revisit this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Opened #73. |
||
pub time: u32, | ||
pub bits: u32, | ||
pub nonce: [u8; 32], | ||
pub solution: Vec<u8>, | ||
} | ||
|
||
impl BlockHeaderData { | ||
pub fn freeze(self) -> BlockHeader { | ||
BlockHeader(self) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; | ||
use hex; | ||
use sapling_crypto::redjubjub::Signature; | ||
use std::fmt; | ||
use std::io::{self, Read, Write}; | ||
use std::ops::Deref; | ||
|
||
|
@@ -20,6 +22,17 @@ const OVERWINTER_TX_VERSION: u32 = 3; | |
const SAPLING_VERSION_GROUP_ID: u32 = 0x892F2085; | ||
const SAPLING_TX_VERSION: u32 = 4; | ||
|
||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | ||
pub struct TxId(pub [u8; 32]); | ||
|
||
impl fmt::Display for TxId { | ||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | ||
let mut data = self.0.to_vec(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as the other review comment about something similar to this. |
||
data.reverse(); | ||
formatter.write_str(&hex::encode(data)) | ||
} | ||
} | ||
|
||
/// A Zcash transaction. | ||
#[derive(Debug)] | ||
pub struct Transaction(TransactionData); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sigh! Rust should have added a hex literal.