Skip to content

Commit 600b2ed

Browse files
committed
Merge remote-tracking branch 'namada/tomas/sorted-prefix-iter' (#458)
- updated `shared/src/types/key/mod.rs` to use `data_encoding::HEXLOWER` instead of `hex` * namada/tomas/sorted-prefix-iter: [ci skip] wasm checksums update changelog: add #458 tests: extend prefix iter tests for reverse order add support for rev_iter_prefix in storage and VP and tx envs test/vm_host_env: check prefix iter order in tx and VP shared/storage/key: add support for int/uint keys that maintain order deps: replace hex with data-encoding
2 parents 74d94b4 + 1d2f1dd commit 600b2ed

File tree

35 files changed

+593
-134
lines changed

35 files changed

+593
-134
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- Fix order of prefix iterator to be sorted by storage
2+
keys and add support for a reverse order prefix iterator.
3+
([#409](https://github.com/anoma/namada/issues/409))

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ clap = {git = "https://github.com/clap-rs/clap/", tag = "v3.0.0-beta.2", default
6262
color-eyre = "0.5.10"
6363
config = "0.11.0"
6464
curl = "0.4.43"
65+
data-encoding = "2.3.2"
6566
derivative = "2.2.0"
6667
directories = "4.0.1"
6768
ed25519-consensus = "1.2.0"
@@ -71,7 +72,6 @@ eyre = "0.6.5"
7172
flate2 = "1.0.22"
7273
file-lock = "2.0.2"
7374
futures = "0.3"
74-
hex = "0.4.3"
7575
itertools = "0.10.1"
7676
jsonpath_lib = "0.3.0"
7777
libc = "0.2.97"

apps/src/lib/client/rpc.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use async_std::fs::{self};
1111
use async_std::path::PathBuf;
1212
use async_std::prelude::*;
1313
use borsh::BorshDeserialize;
14+
use data_encoding::HEXLOWER;
1415
use itertools::Itertools;
1516
use namada::ledger::governance::storage as gov_storage;
1617
use namada::ledger::governance::utils::Votes;
@@ -81,7 +82,7 @@ pub async fn query_raw_bytes(_ctx: Context, args: args::QueryRawBytes) {
8182
.unwrap();
8283
match response.code {
8384
Code::Ok => {
84-
println!("{}", hex::encode(&response.value));
85+
println!("{}", HEXLOWER.encode(&response.value));
8586
}
8687
Code::Err(err) => {
8788
eprintln!(

apps/src/lib/config/genesis.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub mod genesis_config {
2626
use std::path::Path;
2727
use std::str::FromStr;
2828

29-
use hex;
29+
use data_encoding::HEXLOWER;
3030
use namada::ledger::governance::parameters::GovParams;
3131
use namada::ledger::parameters::{EpochDuration, Parameters};
3232
use namada::ledger::pos::types::BasisPoints;
@@ -50,12 +50,12 @@ pub mod genesis_config {
5050

5151
impl HexString {
5252
pub fn to_bytes(&self) -> Result<Vec<u8>, HexKeyError> {
53-
let bytes = hex::decode(&self.0)?;
53+
let bytes = HEXLOWER.decode(self.0.as_ref())?;
5454
Ok(bytes)
5555
}
5656

5757
pub fn to_sha256_bytes(&self) -> Result<[u8; 32], HexKeyError> {
58-
let bytes = hex::decode(&self.0)?;
58+
let bytes = HEXLOWER.decode(self.0.as_ref())?;
5959
let slice = bytes.as_slice();
6060
let array: [u8; 32] = slice.try_into()?;
6161
Ok(array)
@@ -76,15 +76,15 @@ pub mod genesis_config {
7676
#[derive(Error, Debug)]
7777
pub enum HexKeyError {
7878
#[error("Invalid hex string: {0:?}")]
79-
InvalidHexString(hex::FromHexError),
79+
InvalidHexString(data_encoding::DecodeError),
8080
#[error("Invalid sha256 checksum: {0}")]
8181
InvalidSha256(TryFromSliceError),
8282
#[error("Invalid public key: {0}")]
8383
InvalidPublicKey(ParsePublicKeyError),
8484
}
8585

86-
impl From<hex::FromHexError> for HexKeyError {
87-
fn from(err: hex::FromHexError) -> Self {
86+
impl From<data_encoding::DecodeError> for HexKeyError {
87+
fn from(err: data_encoding::DecodeError) -> Self {
8888
Self::InvalidHexString(err)
8989
}
9090
}

apps/src/lib/node/gossip/p2p/identity.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub struct Identity {
2121
// TODO this is needed because libp2p does not export ed255519 serde
2222
// feature maybe a MR for libp2p to export theses functions ?
2323
mod keypair_serde {
24+
use data_encoding::HEXLOWER;
2425
use libp2p::identity::ed25519::Keypair;
2526
use serde::de::Error;
2627
use serde::{Deserialize, Deserializer, Serialize, Serializer};
@@ -33,15 +34,16 @@ mod keypair_serde {
3334
S: Serializer,
3435
{
3536
let bytes = value.encode();
36-
let string = hex::encode(&bytes[..]);
37+
let string = HEXLOWER.encode(&bytes[..]);
3738
string.serialize(serializer)
3839
}
3940
pub fn deserialize<'d, D>(deserializer: D) -> Result<Keypair, D::Error>
4041
where
4142
D: Deserializer<'d>,
4243
{
4344
let string = String::deserialize(deserializer)?;
44-
let mut bytes = hex::decode(&string).map_err(Error::custom)?;
45+
let mut bytes =
46+
HEXLOWER.decode(string.as_ref()).map_err(Error::custom)?;
4547
Keypair::decode(bytes.as_mut()).map_err(Error::custom)
4648
}
4749
}

apps/src/lib/node/ledger/storage/rocksdb.rs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -806,24 +806,36 @@ impl<'iter> DBIter<'iter> for RocksDB {
806806
&'iter self,
807807
prefix: &Key,
808808
) -> PersistentPrefixIterator<'iter> {
809-
let db_prefix = "subspace/".to_owned();
810-
let prefix = format!("{}{}", db_prefix, prefix);
809+
iter_prefix(self, prefix, Direction::Forward)
810+
}
811811

812-
let mut read_opts = ReadOptions::default();
813-
// don't use the prefix bloom filter
814-
read_opts.set_total_order_seek(true);
815-
let mut upper_prefix = prefix.clone().into_bytes();
816-
if let Some(last) = upper_prefix.pop() {
817-
upper_prefix.push(last + 1);
818-
}
819-
read_opts.set_iterate_upper_bound(upper_prefix);
812+
fn rev_iter_prefix(&'iter self, prefix: &Key) -> Self::PrefixIter {
813+
iter_prefix(self, prefix, Direction::Reverse)
814+
}
815+
}
820816

821-
let iter = self.0.iterator_opt(
822-
IteratorMode::From(prefix.as_bytes(), Direction::Forward),
823-
read_opts,
824-
);
825-
PersistentPrefixIterator(PrefixIterator::new(iter, db_prefix))
817+
fn iter_prefix<'iter>(
818+
db: &'iter RocksDB,
819+
prefix: &Key,
820+
direction: Direction,
821+
) -> PersistentPrefixIterator<'iter> {
822+
let db_prefix = "subspace/".to_owned();
823+
let prefix = format!("{}{}", db_prefix, prefix);
824+
825+
let mut read_opts = ReadOptions::default();
826+
// don't use the prefix bloom filter
827+
read_opts.set_total_order_seek(true);
828+
let mut upper_prefix = prefix.clone().into_bytes();
829+
if let Some(last) = upper_prefix.pop() {
830+
upper_prefix.push(last + 1);
826831
}
832+
read_opts.set_iterate_upper_bound(upper_prefix);
833+
834+
let iter = db.0.iterator_opt(
835+
IteratorMode::From(prefix.as_bytes(), direction),
836+
read_opts,
837+
);
838+
PersistentPrefixIterator(PrefixIterator::new(iter, db_prefix))
827839
}
828840

829841
#[derive(Debug)]

apps/src/lib/wallet/keys.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::rc::Rc;
55
use std::str::FromStr;
66

77
use borsh::{BorshDeserialize, BorshSerialize};
8+
use data_encoding::HEXLOWER;
89
use namada::types::key::*;
910
use orion::{aead, kdf};
1011
use serde::{Deserialize, Serialize};
@@ -108,15 +109,15 @@ pub struct EncryptedKeypair(Vec<u8>);
108109

109110
impl Display for EncryptedKeypair {
110111
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
111-
write!(f, "{}", hex::encode(&self.0))
112+
write!(f, "{}", HEXLOWER.encode(self.0.as_ref()))
112113
}
113114
}
114115

115116
impl FromStr for EncryptedKeypair {
116-
type Err = hex::FromHexError;
117+
type Err = data_encoding::DecodeError;
117118

118119
fn from_str(s: &str) -> Result<Self, Self::Err> {
119-
hex::decode(s).map(Self)
120+
HEXLOWER.decode(s.as_ref()).map(Self)
120121
}
121122
}
122123

apps/src/lib/wasm_loader/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use std::collections::HashMap;
44
use std::fs;
55
use std::path::Path;
66

7+
use data_encoding::HEXLOWER;
78
use futures::future::join_all;
8-
use hex;
99
use serde::{Deserialize, Serialize};
1010
use sha2::{Digest, Sha256};
1111
use thiserror::Error;
@@ -144,7 +144,7 @@ pub async fn pre_fetch_wasm(wasm_directory: impl AsRef<Path>) {
144144
Ok(bytes) => {
145145
let mut hasher = Sha256::new();
146146
hasher.update(bytes);
147-
let result = hex::encode(hasher.finalize());
147+
let result = HEXLOWER.encode(&hasher.finalize());
148148
let derived_name = format!(
149149
"{}.{}.wasm",
150150
&name.split('.').collect::<Vec<&str>>()[0],

shared/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ borsh = "0.9.0"
5252
chrono = "0.4.19"
5353
# Using unreleased commit on top of version 0.5.0 that adds Sync to the CLruCache
5454
clru = {git = "https://github.com/marmeladema/clru-rs.git", rev = "71ca566"}
55+
data-encoding = "2.3.2"
5556
derivative = "2.2.0"
5657
ed25519-consensus = "1.2.0"
5758
ferveo = {optional = true, git = "https://github.com/anoma/ferveo"}
5859
ferveo-common = {git = "https://github.com/anoma/ferveo"}
59-
hex = "0.4.3"
6060
tpke = {package = "group-threshold-cryptography", optional = true, git = "https://github.com/anoma/ferveo"}
6161
# TODO using the same version of tendermint-rs as we do here.
6262
ibc = {git = "https://github.com/heliaxdev/ibc-rs", rev = "30b3495ac56c6c37c99bc69ef9f2e84c3309c6cc", default-features = false}

0 commit comments

Comments
 (0)