Skip to content

Commit

Permalink
feat: add no-std version of axon-tools
Browse files Browse the repository at this point in the history
  • Loading branch information
wenyuanhust committed Nov 7, 2023
1 parent 18f68c1 commit e10aabb
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 103 deletions.
26 changes: 21 additions & 5 deletions devtools/axon-tools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ 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"
# cita_trie = "4.0"

[dependencies]
derive_more = "0.99"
log = "0.4.19"
overlord = "0.4"
serde_json = "1.0"
# derive_more = "0.99"
# log = "0.4.19"
# overlord = "0.4"
# serde_json = "1.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies.bit-vec]
Expand All @@ -45,6 +47,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 +60,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,6 +80,11 @@ 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
Expand All @@ -81,7 +97,7 @@ hash = ["tiny-keccak"]
hex = ["faster-hex"]
impl-rlp = ["rlp", "rlp-derive", "ethereum-types/rlp"]
impl-serde = ["serde", "ethereum-types/serialize"]
std = ["cita_trie", "hex"]
std = ["cita_trie", "hex", "log", "derive_more", "serde_json"]

[package.metadata.docs.rs]
all-features = true
Expand Down
62 changes: 5 additions & 57 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,59 +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)]
#[cfg(feature = "hex")]
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
15 changes: 9 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,11 @@ fn extract_pks(
count += 1;
}

log::debug!(
"extract_pks count: {}, validator len: {}",
count,
validator_list.len()
);
// log::debug!(
// "extract_pks count: {}, validator len: {}",
// count,
// validator_list.len()
// );
if count * 3 <= validator_list.len() * 2 {
return Err(Error::NotEnoughSignatures);
}
Expand Down
1 change: 1 addition & 0 deletions devtools/axon-tools/src/tests/verify_trie_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use ethers_core::{
};

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

Expand Down
Loading

0 comments on commit e10aabb

Please sign in to comment.