-
Notifications
You must be signed in to change notification settings - Fork 256
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
Transaction primitives #39
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
91ff2c7
Helper for serializing CompactSize-prefixed vectors
str4d e490b79
Transaction serialization
str4d 2d8b1fe
ZIP 143
str4d 0c81695
ZIP 243
str4d 1f11c40
Convert Transaction into a wrapping struct with impl Deref
str4d cc183ef
Define MAX_SIZE constant for CompactSize serialization
str4d 2d2e4aa
Pass &[E] into Vector::write() instead of &Vec<E>
str4d 9282c7d
Replace tx_read_write() test vector with one from current testnet chain
str4d 61ce4dd
Enforce range checks when reading Amounts
str4d 9b06205
Reject unexpected binding sig during transaction write
str4d d707ebd
Use Option<[u8; N]> for JoinSplit pubkey and signature in a transaction
str4d 7ff32b0
Document enforcement of consensus rules on transaction components
str4d e25b614
Match error message in Amount::Read_i64() to allow_negative value
str4d c9b23df
Extract single-TxOut hashing from signature_hash_data() for clarity
str4d File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,17 @@ | ||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn it_works() { | ||
assert_eq!(2 + 2, 4); | ||
} | ||
#[macro_use] | ||
extern crate lazy_static; | ||
|
||
extern crate blake2_rfc; | ||
extern crate byteorder; | ||
extern crate pairing; | ||
extern crate rand; | ||
extern crate sapling_crypto; | ||
|
||
use sapling_crypto::jubjub::JubjubBls12; | ||
|
||
mod serialize; | ||
pub mod transaction; | ||
|
||
lazy_static! { | ||
static ref JUBJUB: JubjubBls12 = { JubjubBls12::new() }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; | ||
use std::io::{self, Read, Write}; | ||
|
||
const MAX_SIZE: usize = 0x02000000; | ||
|
||
struct CompactSize; | ||
|
||
impl CompactSize { | ||
fn read<R: Read>(mut reader: R) -> io::Result<usize> { | ||
let flag = reader.read_u8()?; | ||
match if flag < 253 { | ||
Ok(flag as usize) | ||
} else if flag == 253 { | ||
match reader.read_u16::<LittleEndian>()? { | ||
n if n < 253 => Err(io::Error::new( | ||
io::ErrorKind::InvalidInput, | ||
"non-canonical CompactSize", | ||
)), | ||
n => Ok(n as usize), | ||
} | ||
} else if flag == 254 { | ||
match reader.read_u32::<LittleEndian>()? { | ||
n if n < 0x10000 => Err(io::Error::new( | ||
io::ErrorKind::InvalidInput, | ||
"non-canonical CompactSize", | ||
)), | ||
n => Ok(n as usize), | ||
} | ||
} else { | ||
match reader.read_u64::<LittleEndian>()? { | ||
n if n < 0x100000000 => Err(io::Error::new( | ||
io::ErrorKind::InvalidInput, | ||
"non-canonical CompactSize", | ||
)), | ||
n => Ok(n as usize), | ||
} | ||
}? { | ||
s if s > MAX_SIZE => Err(io::Error::new( | ||
io::ErrorKind::InvalidInput, | ||
"CompactSize too large", | ||
)), | ||
s => Ok(s), | ||
} | ||
} | ||
|
||
fn write<W: Write>(mut writer: W, size: usize) -> io::Result<()> { | ||
match size { | ||
s if s < 253 => writer.write_u8(s as u8), | ||
s if s <= 0xFFFF => { | ||
writer.write_u8(253)?; | ||
writer.write_u16::<LittleEndian>(s as u16) | ||
} | ||
s if s <= 0xFFFFFFFF => { | ||
writer.write_u8(254)?; | ||
writer.write_u32::<LittleEndian>(s as u32) | ||
} | ||
s => { | ||
writer.write_u8(255)?; | ||
writer.write_u64::<LittleEndian>(s as u64) | ||
} | ||
} | ||
} | ||
} | ||
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 reviewed that these exactly match the behaviour of the C++ code. |
||
|
||
pub struct Vector; | ||
|
||
impl Vector { | ||
pub fn read<R: Read, E, F>(mut reader: R, func: F) -> io::Result<Vec<E>> | ||
where | ||
F: Fn(&mut R) -> io::Result<E>, | ||
{ | ||
let count = CompactSize::read(&mut reader)?; | ||
(0..count).into_iter().map(|_| func(&mut reader)).collect() | ||
} | ||
|
||
pub fn write<W: Write, E, F>(mut writer: W, vec: &[E], func: F) -> io::Result<()> | ||
daira marked this conversation as resolved.
Show resolved
Hide resolved
|
||
where | ||
F: Fn(&mut W, &E) -> io::Result<()>, | ||
{ | ||
CompactSize::write(&mut writer, vec.len())?; | ||
vec.iter().map(|e| func(&mut writer, e)).collect() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn compact_size() { | ||
macro_rules! eval { | ||
($value:expr, $expected:expr) => { | ||
let mut data = vec![]; | ||
CompactSize::write(&mut data, $value).unwrap(); | ||
assert_eq!(&data[..], &$expected[..]); | ||
match CompactSize::read(&data[..]) { | ||
Ok(n) => assert_eq!(n, $value), | ||
Err(e) => panic!("Unexpected error: {:?}", e), | ||
} | ||
}; | ||
} | ||
|
||
eval!(0, [0]); | ||
eval!(1, [1]); | ||
eval!(252, [252]); | ||
eval!(253, [253, 253, 0]); | ||
eval!(254, [253, 254, 0]); | ||
eval!(255, [253, 255, 0]); | ||
eval!(256, [253, 0, 1]); | ||
eval!(256, [253, 0, 1]); | ||
eval!(65535, [253, 255, 255]); | ||
eval!(65536, [254, 0, 0, 1, 0]); | ||
eval!(65537, [254, 1, 0, 1, 0]); | ||
|
||
eval!(33554432, [254, 0, 0, 0, 2]); | ||
|
||
{ | ||
let value = 33554433; | ||
let encoded = &[254, 1, 0, 0, 2][..]; | ||
let mut data = vec![]; | ||
CompactSize::write(&mut data, value).unwrap(); | ||
assert_eq!(&data[..], encoded); | ||
assert!(CompactSize::read(encoded).is_err()); | ||
} | ||
} | ||
|
||
#[test] | ||
fn vector() { | ||
macro_rules! eval { | ||
($value:expr, $expected:expr) => { | ||
let mut data = vec![]; | ||
Vector::write(&mut data, &$value, |w, e| w.write_u8(*e)).unwrap(); | ||
assert_eq!(&data[..], &$expected[..]); | ||
match Vector::read(&data[..], |r| r.read_u8()) { | ||
Ok(v) => assert_eq!(v, $value), | ||
Err(e) => panic!("Unexpected error: {:?}", e), | ||
} | ||
}; | ||
} | ||
|
||
eval!(vec![], [0]); | ||
eval!(vec![0], [1, 0]); | ||
eval!(vec![1], [1, 1]); | ||
eval!(vec![5; 8], [8, 5, 5, 5, 5, 5, 5, 5, 5]); | ||
|
||
{ | ||
// expected = [253, 4, 1, 7, 7, 7, ...] | ||
let mut expected = vec![7; 263]; | ||
expected[0] = 253; | ||
expected[1] = 4; | ||
expected[2] = 1; | ||
|
||
eval!(vec![7; 260], expected); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should this fail if
s > 0x02000000
?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.
It doesn't in
zcashd
:https://github.com/zcash/zcash/blob/ae49175cd4fa9e751f07e4551dcfc42c4e9d5731/src/serialize.h#L252-L274
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.
Do we know what the rationale was for that?
(I'd be terrified to change it, mind :-) )