Skip to content

Commit a4d5752

Browse files
authored
Merge pull request #145 from cowlicks/update-encodable
Update to new version of compact encodable and use it Clean up encoding implementations
2 parents 32999f7 + 860eeb5 commit a4d5752

File tree

10 files changed

+676
-703
lines changed

10 files changed

+676
-703
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ on:
88
env:
99
RUST_BACKTRACE: 1
1010
CARGO_TERM_COLOR: always
11+
RUSTFLAGS: "-Dwarnings"
12+
RUSTDOCFLAGS: "-Dwarnings"
1113

1214
jobs:
1315
ci-pass:
@@ -145,4 +147,6 @@ jobs:
145147
token: ${{ secrets.GITHUB_TOKEN }}
146148
- name: Format check
147149
run: |
150+
cargo doc
148151
cargo fmt -- --check
152+
cargo clippy --all-targets

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ ed25519-dalek = { version = "2", features = ["rand_core"] }
2727
getrandom = { version = "0.2", features = ["js"] }
2828
thiserror = "1"
2929
tracing = "0.1"
30-
compact-encoding = "1"
30+
compact-encoding = "2"
3131
flat-tree = "6"
3232
merkle-tree-stream = "0.12"
3333
pretty-hash = "0.4"
@@ -47,8 +47,8 @@ random-access-disk = { version = "3", default-features = false }
4747

4848
[dev-dependencies]
4949
anyhow = "1.0.70"
50-
proptest = "1.1.0"
51-
proptest-derive = "0.2.0"
50+
proptest = "1.6.0"
51+
proptest-derive = "0.5.1"
5252
data-encoding = "2.2.0"
5353
remove_dir_all = "0.7.0"
5454
tempfile = "3.1.0"

src/common/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub use self::store::Store;
1616
pub(crate) use self::store::{StoreInfo, StoreInfoInstruction, StoreInfoType};
1717

1818
#[derive(Debug, Clone, PartialEq, Eq)]
19-
pub struct BitfieldUpdate {
19+
pub(crate) struct BitfieldUpdate {
2020
pub(crate) drop: bool,
2121
pub(crate) start: u64,
2222
pub(crate) length: u64,

src/common/node.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ pub struct Node {
2323
/// This node's index in the Merkle tree
2424
pub(crate) index: u64,
2525
/// Hash of the data in this node
26+
// TODO make this [u8; 32] like:
27+
// https://github.com/holepunchto/hypercore/blob/d21ebdeca1b27eb4c2232f8af17d5ae939ee97f2/lib/messages.js#L246
2628
pub(crate) hash: Vec<u8>,
2729
/// Number of bytes in this [`Node::data`]
2830
pub(crate) length: u64,

src/crypto/hash.rs

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use blake2::{
33
Blake2b, Blake2bMac, Digest,
44
};
55
use byteorder::{BigEndian, WriteBytesExt};
6-
use compact_encoding::State;
6+
use compact_encoding::{as_array, to_encoded_bytes, EncodingError, FixedWidthEncoding};
77
use ed25519_dalek::VerifyingKey;
88
use merkle_tree_stream::Node as NodeTrait;
99
use std::convert::AsRef;
@@ -124,10 +124,9 @@ impl Hash {
124124

125125
/// Hash data
126126
pub(crate) fn data(data: &[u8]) -> Self {
127-
let (mut state, mut size) = State::new_with_size(8);
128-
state
129-
.encode_u64(data.len() as u64, &mut size)
130-
.expect("Encoding u64 should not fail");
127+
let size =
128+
(|| Ok::<_, EncodingError>(to_encoded_bytes!((data.len() as u64).as_fixed_width())))()
129+
.expect("Encoding u64 should not fail");
131130

132131
let mut hasher = Blake2b256::new();
133132
hasher.update(LEAF_TYPE);
@@ -147,10 +146,10 @@ impl Hash {
147146
(right, left)
148147
};
149148

150-
let (mut state, mut size) = State::new_with_size(8);
151-
state
152-
.encode_u64(node1.length + node2.length, &mut size)
153-
.expect("Encoding u64 should not fail");
149+
let len = node1.length + node2.length;
150+
let size: Box<[u8]> =
151+
(|| Ok::<_, EncodingError>(to_encoded_bytes!(len.as_fixed_width())))()
152+
.expect("Encoding u64 should not fail");
154153

155154
let mut hasher = Blake2b256::new();
156155
hasher.update(PARENT_TYPE);
@@ -170,13 +169,13 @@ impl Hash {
170169

171170
for node in roots {
172171
let node = node.as_ref();
173-
let (mut state, mut buffer) = State::new_with_size(16);
174-
state
175-
.encode_u64(node.index(), &mut buffer)
176-
.expect("Encoding u64 should not fail");
177-
state
178-
.encode_u64(node.len(), &mut buffer)
179-
.expect("Encoding u64 should not fail");
172+
let buffer = (|| {
173+
Ok::<_, EncodingError>(to_encoded_bytes!(
174+
node.index().as_fixed_width(),
175+
node.len().as_fixed_width()
176+
))
177+
})()
178+
.expect("Encoding u64 should not fail");
180179

181180
hasher.update(node.hash());
182181
hasher.update(&buffer[..8]);
@@ -212,20 +211,15 @@ impl DerefMut for Hash {
212211
/// Create a signable buffer for tree. This is treeSignable in Javascript.
213212
/// See https://github.com/hypercore-protocol/hypercore/blob/70b271643c4e4b1e5ecae5bb579966dfe6361ff3/lib/caps.js#L17
214213
pub(crate) fn signable_tree(hash: &[u8], length: u64, fork: u64) -> Box<[u8]> {
215-
let (mut state, mut buffer) = State::new_with_size(80);
216-
state
217-
.encode_fixed_32(&TREE, &mut buffer)
218-
.expect("Should be able ");
219-
state
220-
.encode_fixed_32(hash, &mut buffer)
221-
.expect("Encoding fixed 32 bytes should not fail");
222-
state
223-
.encode_u64(length, &mut buffer)
224-
.expect("Encoding u64 should not fail");
225-
state
226-
.encode_u64(fork, &mut buffer)
227-
.expect("Encoding u64 should not fail");
228-
buffer
214+
(|| {
215+
Ok::<_, EncodingError>(to_encoded_bytes!(
216+
&TREE,
217+
as_array::<32>(hash)?,
218+
length.as_fixed_width(),
219+
fork.as_fixed_width()
220+
))
221+
})()
222+
.expect("Encoding should not fail")
229223
}
230224

231225
#[cfg(test)]

0 commit comments

Comments
 (0)