Skip to content

Commit

Permalink
feat: add no-std version of axon-tools (#1532)
Browse files Browse the repository at this point in the history
* refactor: impl-serde & proof shouldn't be default features

* feat: add no-std version of axon-tools
  • Loading branch information
wenyuanhust authored and KaoImin committed Nov 17, 2023
1 parent 9a815e0 commit f67e07c
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 134 deletions.
44 changes: 27 additions & 17 deletions devtools/axon-tools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,21 @@ eth_light_client_in_ckb-prover = { version = "0.3.0-alpha", git = "https://githu
ethereum = "0.14"
ethers-core = "2.0.10"
hex = "0.4"
overlord = "0.4"
rand = "0.8"

[dependencies]
derive_more = "0.99"
log = "0.4.19"
overlord = "0.4"
serde_json = "1.0"
[features]
default = []
proof = ["blst", "bit-vec", "hash", "impl-rlp"]
hash = ["tiny-keccak"]
hex = ["faster-hex"]
impl-rlp = ["rlp", "rlp-derive", "ethereum-types/rlp"]
impl-serde = ["serde", "ethereum-types/serialize"]
std = ["cita_trie", "hex", "log", "derive_more", "serde_json"]

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "doc_cfg"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies.bit-vec]
Expand All @@ -45,6 +53,10 @@ features = ["serde"]
version = "4.0"
optional = true

[dependencies.derive_more]
version = "0.99"
optional = true

[dependencies.ethereum-types]
version = "0.14"
default-features = false
Expand All @@ -54,6 +66,11 @@ features = ["serialize"]
version = "0.8"
optional = true

[dependencies.log]
version = "0.4.19"
default_features = false
optional = true

[dependencies.rlp]
version = "0.5"
default-features = false
Expand All @@ -69,19 +86,12 @@ default_features = false
optional = true
features = ["derive"]

[dependencies.serde_json]
version = "1.0"
default_features = false
optional = true

[dependencies.tiny-keccak]
version = "2.0"
optional = true
features = ["keccak"]

[features]
default = ["impl-serde", "proof"]
proof = ["blst", "bit-vec", "cita_trie", "hash", "impl-rlp"]
hash = ["tiny-keccak"]
hex = ["faster-hex"]
impl-rlp = ["rlp", "rlp-derive", "ethereum-types/rlp"]
impl-serde = ["serde", "ethereum-types/serialize", "hex"]

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "doc_cfg"]
61 changes: 5 additions & 56 deletions devtools/axon-tools/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use derive_more::{Display, From};
use ethereum_types::H256;
#[cfg(feature = "std")]
use std::fmt::{self, Display};

#[allow(dead_code)]
Expand All @@ -18,7 +17,7 @@ pub enum Error {
#[cfg_attr(doc_cfg, doc(cfg(feature = "proof")))]
Bls(blst::BLST_ERROR),

#[cfg(feature = "proof")]
#[cfg(all(feature = "proof", feature = "std"))]
#[cfg_attr(doc_cfg, doc(cfg(feature = "proof")))]
Trie(cita_trie::TrieError),
}
Expand All @@ -39,14 +38,15 @@ impl From<blst::BLST_ERROR> for Error {
}
}

#[cfg(feature = "proof")]
#[cfg(all(feature = "proof", feature = "std"))]
#[cfg_attr(doc_cfg, doc(cfg(feature = "proof")))]
impl From<cita_trie::TrieError> for Error {
fn from(e: cita_trie::TrieError) -> Self {
Self::Trie(e)
}
}

#[cfg(feature = "std")]
impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Expand All @@ -64,58 +64,7 @@ impl Display for Error {
}
}

#[derive(Debug, Display, From)]
#[derive(Debug)]
pub enum TypesError {
#[display(fmt = "Expect {:?}, get {:?}.", expect, real)]
LengthMismatch { expect: usize, real: usize },

#[display(
fmt = "Eip1559Transaction hash mismatch origin {:?}, computed {:?}",
origin,
calc
)]
TxHashMismatch { origin: H256, calc: H256 },

#[display(fmt = "{:?}", _0)]
FromHex(faster_hex::Error),

#[display(fmt = "{:?} is an invalid address", _0)]
InvalidAddress(String),

#[display(fmt = "Hex should start with 0x")]
HexPrefix,

#[display(fmt = "Invalid public key")]
InvalidPublicKey,

#[display(fmt = "Invalid check sum")]
InvalidCheckSum,

#[display(fmt = "Unsigned")]
Unsigned,

// #[display(fmt = "Crypto error {:?}", _0)]
// Crypto(CryptoError),
#[display(fmt = "Missing signature")]
MissingSignature,

#[display(fmt = "Invalid crosschain direction")]
InvalidDirection,

#[display(fmt = "Signature R is empty")]
SignatureRIsEmpty,

#[display(fmt = "Invalid signature R type")]
InvalidSignatureRType,

#[display(fmt = "Invalid address source type")]
InvalidAddressSourceType,

#[display(fmt = "Missing interoperation sender")]
MissingInteroperationSender,

#[display(fmt = "InvalidBlockVersion {:?}", _0)]
InvalidBlockVersion(u8),
}

impl std::error::Error for TypesError {}
1 change: 1 addition & 0 deletions devtools/axon-tools/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub fn keccak_256(data: &[u8]) -> [u8; 32] {
#[derive(Default)]
pub(crate) struct InnerKeccak;

#[cfg(all(feature = "proof", feature = "std"))]
impl cita_trie::Hasher for InnerKeccak {
const LENGTH: usize = 32;

Expand Down
7 changes: 6 additions & 1 deletion devtools/axon-tools/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg_attr(not(feature = "std"), no_std)]
// #![no_std]
#![cfg_attr(doc_cfg, feature(doc_cfg))]

extern crate alloc;
Expand All @@ -17,7 +19,10 @@ pub use error::Error;

#[cfg(feature = "proof")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "proof")))]
pub use proof::{verify_proof, verify_trie_proof};
pub use proof::verify_proof;

#[cfg(all(feature = "proof", feature = "std"))]
pub use proof::verify_trie_proof;

#[cfg(feature = "hash")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "hash")))]
Expand Down
10 changes: 4 additions & 6 deletions devtools/axon-tools/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ use bytes::Bytes;
use ethereum_types::H256;
use rlp::Encodable;

#[cfg(all(feature = "proof", feature = "std"))]
use crate::hash::InnerKeccak;
use crate::types::{AxonBlock, Proof, Proposal, ValidatorExtend, Vote};
use crate::{error::Error, hash::InnerKeccak, keccak_256};
use crate::{error::Error, keccak_256};

const DST: &str = "BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RONUL";

#[cfg(all(feature = "proof", feature = "std"))]
pub fn verify_trie_proof(
root: H256,
key: &[u8],
Expand Down Expand Up @@ -91,11 +94,6 @@ fn extract_pks(
count += 1;
}

log::debug!(
"extract_pks count: {}, validator len: {}",
count,
validator_list.len()
);
if count * 3 <= validator_list.len() * 2 {
return Err(Error::NotEnoughSignatures);
}
Expand Down
5 changes: 4 additions & 1 deletion devtools/axon-tools/src/rlp_codec.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::types::{BlockVersion, Proposal, Vote};
#[cfg(feature = "impl-rlp")]
use crate::types::BlockVersion;
#[cfg(feature = "proof")]
use crate::types::{Proposal, Vote};
#[cfg(feature = "impl-rlp")]
use rlp::{Decodable, DecoderError, Encodable, Rlp, RlpStream};

Expand Down
9 changes: 9 additions & 0 deletions devtools/axon-tools/src/tests/verify_proof.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
#[cfg(feature = "proof")]
use crate::hash::keccak_256;
#[cfg(feature = "proof")]
use crate::types::{AxonBlock, BlockVersion, Metadata, Proof, Proposal, ValidatorExtend};
#[cfg(feature = "proof")]
use bytes::Bytes;
#[cfg(feature = "proof")]
use ethereum_types::{H160, H256, U256};
#[cfg(feature = "proof")]
use rlp::Encodable;
#[cfg(feature = "proof")]
use serde::de::DeserializeOwned;

#[cfg(feature = "proof")]
fn read_json<T: DeserializeOwned>(path: &str) -> T {
let json = std::fs::read_to_string(path).unwrap();
serde_json::from_str(&json).unwrap()
}

#[test]
#[cfg(feature = "proof")]
fn test_proposal() {
let proposal = Proposal {
version: BlockVersion::V0,
Expand Down Expand Up @@ -46,6 +54,7 @@ fn test_proposal() {
}

#[test]
#[cfg(feature = "proof")]
fn test_verify_proof() {
let block: AxonBlock = read_json("src/tests/block.json");
let proof: Proof = read_json("src/tests/proof.json");
Expand Down
14 changes: 11 additions & 3 deletions devtools/axon-tools/src/tests/verify_trie_proof.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
#[cfg(feature = "proof")]
use crate::verify_trie_proof;
#[cfg(feature = "proof")]
use eth_light_client_in_ckb_prover::{encode_receipt, Receipts};
use ethereum_types::{Bloom, H256, U256};
#[cfg(feature = "proof")]
use ethereum_types::U256;
use ethereum_types::{Bloom, H256};
use ethers_core::{types::Log, utils::keccak256};
#[cfg(feature = "proof")]
use ethers_core::{
types::{Log, TransactionReceipt, U64},
utils::{keccak256, rlp},
types::{TransactionReceipt, U64},
utils::rlp,
};

#[test]
#[cfg(feature = "std")]
fn test_receipt() {
let mut tx_receipts = Vec::<TransactionReceipt>::new();

Expand Down Expand Up @@ -52,6 +59,7 @@ fn test_receipt() {
}

#[test]
#[cfg(all(feature = "proof", feature = "std"))]
fn test_verify_trie_proof() {
let mut tx_receipts = Vec::<TransactionReceipt>::new();

Expand Down
Loading

0 comments on commit f67e07c

Please sign in to comment.