Skip to content

Commit

Permalink
Remove dependency on sp-std and sp-core
Browse files Browse the repository at this point in the history
  • Loading branch information
romac committed Aug 8, 2022
1 parent 9788227 commit f5eb3b1
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 20 deletions.
5 changes: 0 additions & 5 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,14 @@ prost = { version = "0.10", default-features = false, features = ["prost-derive"
bytes = { version = "1.0.1", default-features = false }
hex = { version = "0.4.3", default-features = false, features = [ "alloc" ] }
anyhow = { version = "1.0.40", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.22", default-features = false }
sha2 = { version = "0.9.3", optional = true }
sha3 = { version = "0.9.1", optional = true }
ripemd160 = { version = "0.9.1", optional = true }
sp-core = { version = "6.0.0", optional = true }

[dev-dependencies]
sha2 = { version = "0.9.3" }
sha3 = { version = "0.9.1" }
ripemd160 = { version = "0.9.1" }
sp-core = { version = "6.0.0" }
serde = { version = "1.0.125", features = ["derive"] }
serde_json = "1.0.64"

Expand All @@ -38,9 +35,7 @@ std = [
"bytes/std",
"hex/std",
"anyhow/std",
"sp-std/std",
"sha2",
"sha3",
"ripemd160",
"sp-core",
]
6 changes: 3 additions & 3 deletions rust/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use sp_std::collections::btree_map::BTreeMap;
use sp_std::vec;
use alloc::collections::btree_map::BTreeMap;
use alloc::vec;

use crate::compress::{decompress, is_compressed};
use crate::host_functions::HostFunctionsProvider;
Expand Down Expand Up @@ -233,10 +233,10 @@ mod tests {
use super::*;

use alloc::string::String;
use alloc::vec::Vec;
use anyhow::{bail, ensure};
use prost::Message;
use serde::Deserialize;
use sp_std::vec::Vec;
#[cfg(feature = "std")]
use std::fs::File;
#[cfg(feature = "std")]
Expand Down
6 changes: 3 additions & 3 deletions rust/src/compress.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use alloc::borrow::ToOwned;
use alloc::collections::btree_map::BTreeMap as HashMap;
use alloc::vec::Vec;
use prost::Message;
use sp_std::borrow::ToOwned;
use sp_std::collections::btree_map::BTreeMap as HashMap;
use sp_std::vec::Vec;

use crate::helpers::Result;
use crate::ics23;
Expand Down
2 changes: 1 addition & 1 deletion rust/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub type Result<T> = anyhow::Result<T>;
pub type Hash = sp_std::vec::Vec<u8>;
pub type Hash = alloc::vec::Vec<u8>;
7 changes: 5 additions & 2 deletions rust/src/host_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ pub trait HostFunctionsProvider {
pub mod host_functions_impl {
use crate::host_functions::HostFunctionsProvider;
use ripemd160::Ripemd160;
use sha2::{Digest, Sha512, Sha512Trunc256};
use sha2::{Digest, Sha256, Sha512, Sha512Trunc256};
use sha3::Sha3_512;

pub struct HostFunctionsManager;
impl HostFunctionsProvider for HostFunctionsManager {
fn sha2_256(message: &[u8]) -> [u8; 32] {
sp_core::hashing::sha2_256(message)
let digest = Sha256::digest(message);
let mut buf = [0u8; 32];
buf.copy_from_slice(&digest);
buf
}

fn sha2_512(message: &[u8]) -> [u8; 64] {
Expand Down
7 changes: 5 additions & 2 deletions rust/src/ops.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::{bail, ensure};
use sp_std::convert::TryInto;
use core::convert::TryInto;

use crate::helpers::{Hash, Result};
use crate::host_functions::HostFunctionsProvider;
Expand Down Expand Up @@ -80,8 +80,11 @@ fn proto_len(length: usize) -> Result<Hash> {
#[cfg(test)]
mod tests {
use super::*;

use crate::host_functions::host_functions_impl::HostFunctionsManager;
use sp_std::vec::Vec;

use alloc::vec;
use alloc::vec::Vec;

fn decode(input: &str) -> Vec<u8> {
hex::decode(input).unwrap()
Expand Down
9 changes: 5 additions & 4 deletions rust/src/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::helpers::Result;
use crate::host_functions::HostFunctionsProvider;
use crate::ics23;
use crate::ops::{apply_inner, apply_leaf};
use sp_std::vec::Vec;
use alloc::vec::Vec;

pub type CommitmentRoot = Vec<u8>;

Expand Down Expand Up @@ -317,12 +317,13 @@ fn right_branches_are_empty(spec: &ics23::InnerSpec, op: &ics23::InnerOp) -> Res
#[cfg(test)]
mod tests {
use super::*;

use crate::api;
use crate::host_functions::host_functions_impl::HostFunctionsManager;
use crate::ics23::{ExistenceProof, HashOp, InnerOp, InnerSpec, LeafOp, LengthOp, ProofSpec};
use sp_std::collections::btree_map::BTreeMap as HashMap;
#[cfg(not(feature = "std"))]
use sp_std::prelude::*;

use alloc::collections::btree_map::BTreeMap as HashMap;
use alloc::vec;

#[test]
fn calculate_root_from_leaf() {
Expand Down

0 comments on commit f5eb3b1

Please sign in to comment.