Skip to content

Commit

Permalink
Merge pull request #518 from zcash/merge-non-consensus-changes-3
Browse files Browse the repository at this point in the history
Merge non-consensus changes
  • Loading branch information
str4d committed Feb 28, 2022
2 parents efe63ad + 91d9a42 commit c423167
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 215 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: 1.54.0
toolchain: 1.56.1
override: true

- name: Fetch path to Zcash parameters
Expand Down Expand Up @@ -55,7 +55,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: 1.54.0
toolchain: 1.56.1
override: true
- name: Add target
run: rustup target add ${{ matrix.target }}
Expand All @@ -78,7 +78,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: 1.54.0
toolchain: 1.56.1
override: true
# Build benchmarks to prevent bitrot
- name: Build benchmarks
Expand All @@ -88,20 +88,20 @@ jobs:
args: --all --benches

clippy:
name: Clippy (1.54.0)
name: Clippy (1.56.1)
timeout-minutes: 30
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: 1.54.0
toolchain: 1.56.1
components: clippy
override: true
- name: Run clippy
uses: actions-rs/clippy-check@v1
with:
name: Clippy (1.54.0)
name: Clippy (1.56.1)
token: ${{ secrets.GITHUB_TOKEN }}
args: --all-features --all-targets -- -D warnings

Expand Down Expand Up @@ -173,7 +173,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: 1.54.0
toolchain: 1.56.1
override: true
- name: cargo fetch
uses: actions-rs/cargo@v1
Expand All @@ -196,7 +196,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: 1.54.0
toolchain: 1.56.1
override: true

# cargo fmt does not build the code, and running it in a fresh clone of
Expand Down
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ panic = 'abort'
codegen-units = 1

[patch.crates-io]
orchard = { git = "https://github.com/zcash/orchard.git", rev = "f1795f8068c8c83af89af8a061131759f973d656" }
hdwallet = { git = "https://github.com/nuttycom/hdwallet", rev = "576683b9f2865f1118c309017ff36e01f84420c9" }
incrementalmerkletree = { git = "https://github.com/zcash/incrementalmerkletree.git", rev = "dd57b430dee7c0b163f4035fef2280cd1935036c" }
orchard = { git = "https://github.com/zcash/orchard.git", rev = "3ddf6c49f7484ed1295bd5351317bbfe49e14472" }
zcash_encoding = { path = "components/zcash_encoding" }
zcash_note_encryption = { path = "components/zcash_note_encryption" }
hdwallet = { git = "https://github.com/nuttycom/hdwallet", rev = "576683b9f2865f1118c309017ff36e01f84420c9" }
3 changes: 3 additions & 0 deletions components/equihash/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ and this library adheres to Rust's notion of

## [Unreleased]

### Changed
- MSRV is now 1.56.1.

## [0.1.0] - 2020-07-10
Initial release.
3 changes: 3 additions & 0 deletions components/zcash_note_encryption/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ and this library adheres to Rust's notion of

## [Unreleased]

### Changed
- MSRV is now 1.56.1.

## [0.1.0] - 2021-12-17
Initial release.
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.54.0
1.56.1
2 changes: 1 addition & 1 deletion zcash_client_backend/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ and this library adheres to Rust's notion of
- `zcash_client_backend::encoding::encode_transparent_address`

### Changed
- MSRV is now 1.54.0.
- MSRV is now 1.56.1.
- Bumped dependencies to `ff 0.11`, `group 0.11`, `bls12_381 0.6`, `jubjub 0.8`.
- `zcash_client_backend::proto`:
- `compact_formats::CompactSpend` has been renamed to `CompactSaplingSpend`,
Expand Down
2 changes: 1 addition & 1 deletion zcash_client_sqlite/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ and this library adheres to Rust's notion of
rewinds exceed supported bounds.

### Changed
- MSRV is now 1.54.0.
- MSRV is now 1.56.1.
- Bumped dependencies to `ff 0.11`, `group 0.11`, `jubjub 0.8`.
- Renamed the following to use lower-case abbreviations (matching Rust
naming conventions):
Expand Down
1 change: 1 addition & 0 deletions zcash_history/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this library adheres to Rust's notion of
- `zcash_history::Entry::new_leaf`

### Changed
- MSRV is now 1.56.1.
- `zcash_history::{Entry, IndexedNode, Tree}` now have a `Version` parameter.

### Removed
Expand Down
2 changes: 1 addition & 1 deletion zcash_primitives/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ and this library adheres to Rust's notion of
- `impl Sum<&Amount> for Option<Amount>`

### Changed
- MSRV is now 1.54.0.
- MSRV is now 1.56.1.
- Bumped dependencies to `ff 0.11`, `group 0.11`, `bls12_381 0.6`, `jubjub 0.8`.
- The following modules and helpers have been moved into
`zcash_primitives::sapling`:
Expand Down
154 changes: 43 additions & 111 deletions zcash_primitives/src/merkle_tree/incremental.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
//! Implementations of serialization and parsing for Orchard note commitment trees.

use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::convert::TryFrom;
use std::hash::Hash;
use std::convert::{TryFrom, TryInto};
use std::io::{self, Read, Write};

use incrementalmerkletree::{
bridgetree::{
AuthFragment, BridgeTree, Checkpoint, Frontier, Leaf, MerkleBridge, NonEmptyFrontier,
},
bridgetree::{AuthFragment, Frontier, Leaf, MerkleBridge, NonEmptyFrontier},
Hashable, Position,
};
use orchard::tree::MerkleHashOrchard;
Expand All @@ -18,14 +15,6 @@ use super::{CommitmentTree, HashSer};

pub const SER_V1: u8 = 1;

pub fn read_frontier_v0<H: Hashable + super::Hashable, R: Read>(
mut reader: R,
) -> io::Result<Frontier<H, 32>> {
let tree = CommitmentTree::read(&mut reader)?;

Ok(tree.to_frontier())
}

impl HashSer for MerkleHashOrchard {
fn read<R: Read>(mut reader: R) -> io::Result<Self>
where
Expand All @@ -46,6 +35,14 @@ impl HashSer for MerkleHashOrchard {
}
}

pub fn read_frontier_v0<H: Hashable + super::Hashable, R: Read>(
mut reader: R,
) -> io::Result<Frontier<H, 32>> {
let tree = CommitmentTree::read(&mut reader)?;

Ok(tree.to_frontier())
}

pub fn write_nonempty_frontier_v1<H: HashSer, W: Write>(
mut writer: W,
frontier: &NonEmptyFrontier<H>,
Expand Down Expand Up @@ -108,19 +105,32 @@ pub fn read_frontier_v1<H: HashSer + Clone, R: Read>(reader: R) -> io::Result<Fr
}
}

pub fn write_position<W: Write>(mut writer: W, position: Position) -> io::Result<()> {
writer.write_u64::<LittleEndian>(position.try_into().unwrap())
}

pub fn read_position<R: Read>(mut reader: R) -> io::Result<Position> {
let p = reader.read_u64::<LittleEndian>()?;
<usize>::try_from(p).map(Position::from).map_err(|err| {
io::Error::new(
io::ErrorKind::Unsupported,
format!(
"usize could not be decoded to a 64-bit value on this platform: {:?}",
err
),
)
})
}

pub fn write_auth_fragment_v1<H: HashSer, W: Write>(
mut writer: W,
fragment: &AuthFragment<H>,
) -> io::Result<()> {
writer.write_u64::<LittleEndian>(<u64>::from(fragment.position()))?;
writer.write_u64::<LittleEndian>(fragment.altitudes_observed() as u64)?;
write_position(&mut writer, fragment.position())?;
writer.write_u64::<LittleEndian>(fragment.altitudes_observed().try_into().unwrap())?;
Vector::write(&mut writer, fragment.values(), |w, a| a.write(w))
}

pub fn read_position<R: Read>(mut reader: R) -> io::Result<Position> {
Ok(Position::from(reader.read_u64::<LittleEndian>()? as usize))
}

#[allow(clippy::redundant_closure)]
pub fn read_auth_fragment_v1<H: HashSer, R: Read>(mut reader: R) -> io::Result<AuthFragment<H>> {
let position = read_position(&mut reader)?;
Expand All @@ -130,7 +140,7 @@ pub fn read_auth_fragment_v1<H: HashSer, R: Read>(mut reader: R) -> io::Result<A
Ok(AuthFragment::from_parts(position, alts_observed, values))
}

pub fn write_bridge_v1<H: HashSer, W: Write>(
pub fn write_bridge_v1<H: HashSer + Ord, W: Write>(
mut writer: W,
bridge: &MerkleBridge<H>,
) -> io::Result<()> {
Expand All @@ -143,7 +153,7 @@ pub fn write_bridge_v1<H: HashSer, W: Write>(
&mut writer,
&bridge.auth_fragments().iter().collect::<Vec<_>>(),
|w, (i, a)| {
w.write_u64::<LittleEndian>(**i as u64)?;
w.write_u64::<LittleEndian>(u64::from(**i))?;
write_auth_fragment_v1(w, a)
},
)?;
Expand All @@ -152,13 +162,12 @@ pub fn write_bridge_v1<H: HashSer, W: Write>(
Ok(())
}

pub fn read_bridge_v1<H: HashSer + Clone, R: Read>(mut reader: R) -> io::Result<MerkleBridge<H>> {
pub fn read_bridge_v1<H: HashSer + Ord + Clone, R: Read>(
mut reader: R,
) -> io::Result<MerkleBridge<H>> {
let prior_position = Optional::read(&mut reader, read_position)?;
let auth_fragments = Vector::read(&mut reader, |r| {
Ok((
r.read_u64::<LittleEndian>()? as usize,
read_auth_fragment_v1(r)?,
))
let auth_fragments = Vector::read(&mut reader, |mut r| {
Ok((read_position(&mut r)?, read_auth_fragment_v1(r)?))
})?
.into_iter()
.collect();
Expand All @@ -171,99 +180,22 @@ pub fn read_bridge_v1<H: HashSer + Clone, R: Read>(mut reader: R) -> io::Result<
))
}

pub const EMPTY_CHECKPOINT: u8 = 0;
pub const BRIDGE_CHECKPOINT: u8 = 1;

pub fn write_checkpoint_v1<W: Write>(mut writer: W, checkpoint: &Checkpoint) -> io::Result<()> {
match checkpoint {
Checkpoint::Empty => {
writer.write_u8(EMPTY_CHECKPOINT)?;
}
Checkpoint::AtIndex(i) => {
writer.write_u8(BRIDGE_CHECKPOINT)?;
writer.write_u64::<LittleEndian>(*i as u64)?;
}
}

Ok(())
}

pub fn read_checkpoint_v1<R: Read>(mut reader: R) -> io::Result<Checkpoint> {
match reader.read_u8()? {
EMPTY_CHECKPOINT => Ok(Checkpoint::Empty),
BRIDGE_CHECKPOINT => Ok(Checkpoint::AtIndex(
reader.read_u64::<LittleEndian>()? as usize
)),
flag => Err(io::Error::new(
io::ErrorKind::InvalidInput,
format!("Unrecognized checkpoint variant identifier: {:?}", flag),
)),
}
}

pub fn write_tree_v1<H: HashSer + Hash + Eq, W: Write>(
mut writer: W,
tree: &BridgeTree<H, 32>,
) -> io::Result<()> {
Vector::write(&mut writer, tree.bridges(), |w, b| write_bridge_v1(w, b))?;
Vector::write(
&mut writer,
&tree.witnessable_leaves().iter().collect::<Vec<_>>(),
|mut w, (a, i)| {
a.write(&mut w)?;
w.write_u64::<LittleEndian>(**i as u64)?;
Ok(())
},
)?;
Vector::write(&mut writer, tree.checkpoints(), |w, c| {
write_checkpoint_v1(w, c)
})?;
writer.write_u64::<LittleEndian>(tree.max_checkpoints() as u64)?;

Ok(())
}

#[allow(clippy::redundant_closure)]
pub fn read_tree_v1<H: Hashable + HashSer + Hash + Eq + Clone, R: Read>(
mut reader: R,
) -> io::Result<BridgeTree<H, 32>> {
BridgeTree::from_parts(
Vector::read(&mut reader, |r| read_bridge_v1(r))?,
Vector::read(&mut reader, |mut r| {
Ok((H::read(&mut r)?, r.read_u64::<LittleEndian>()? as usize))
})?
.into_iter()
.collect(),
Vector::read(&mut reader, |r| read_checkpoint_v1(r))?,
reader.read_u64::<LittleEndian>()? as usize,
)
.map_err(|err| {
io::Error::new(
io::ErrorKind::InvalidInput,
format!(
"Consistency violation found when attempting to deserialize Merkle tree: {:?}",
err
),
)
})
}

pub fn write_tree<H: HashSer + Hash + Eq, W: Write>(
pub fn write_bridge<H: HashSer + Ord, W: Write>(
mut writer: W,
tree: &BridgeTree<H, 32>,
bridge: &MerkleBridge<H>,
) -> io::Result<()> {
writer.write_u8(SER_V1)?;
write_tree_v1(&mut writer, tree)
write_bridge_v1(writer, bridge)
}

pub fn read_tree<H: Hashable + HashSer + Hash + Eq + Clone, R: Read>(
pub fn read_bridge<H: HashSer + Ord + Clone, R: Read>(
mut reader: R,
) -> io::Result<BridgeTree<H, 32>> {
) -> io::Result<MerkleBridge<H>> {
match reader.read_u8()? {
SER_V1 => read_tree_v1(&mut reader),
SER_V1 => read_bridge_v1(&mut reader),
flag => Err(io::Error::new(
io::ErrorKind::InvalidInput,
format!("Unrecognized tree serialization version: {:?}", flag),
format!("Unrecognized serialization version: {:?}", flag),
)),
}
}
Expand Down

0 comments on commit c423167

Please sign in to comment.