Skip to content

Commit

Permalink
feat: move axon-tools to axon
Browse files Browse the repository at this point in the history
  • Loading branch information
wenyuanhust committed Oct 26, 2023
1 parent 90bd8c8 commit 03d83e7
Show file tree
Hide file tree
Showing 13 changed files with 1,246 additions and 2 deletions.
36 changes: 35 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ core-consensus = { path = "./core/consensus" }
core-executor = { path = "./core/executor" }
core-run = { path = "./core/run" }
protocol = { path = "./protocol", package = "axon-protocol" }
axon-tools = { path = "./devtools/axon-tools", features = ["impl-serde", "proof"] }

[workspace]
members = [
Expand All @@ -43,6 +44,7 @@ members = [
"core/storage",
"devtools/abi-generator",
"devtools/keypair",
"devtools/axon-tools",
"protocol",
]

Expand Down
2 changes: 1 addition & 1 deletion core/storage/src/tests/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn test_storage_block_insert() {

exec!(storage.insert_block(Context::new(), block));

let block = exec!(storage.get_latest_block(Context::new()));
let block: protocol::types::Block = exec!(storage.get_latest_block(Context::new()));
assert_eq!(height, block.header.number);

let block = exec!(storage.get_block(Context::new(), height));
Expand Down
84 changes: 84 additions & 0 deletions devtools/axon-tools/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
[package]
name = "axon-tools"
version = "0.1.1"
edition = "2021"
authors = ["Axon Dev <axon@axonweb3.io>"]
license = "MIT"
include = ["src/*", "README.md", "LICENSE"]
readme = "README.md"
keywords = ["axon", "tool"]
categories = ["cryptography"]
repository = "https://github.com/axonweb3/axon-tools"
description = """
Some axon related utilities.
"""

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies.bit-vec]
version = "0.6"
default_features = false
optional = true

[dependencies.blst]
version = "0.3"
optional = true

[dependencies.bytes]
version = "1.4"
default-features = false
features = ["serde"]

[dependencies.cita_trie]
version = "4.0"
optional = true

[dependencies.ethereum-types]
version = "0.14"
default-features = false
features = ["serialize"]

[dependencies.faster-hex]
version = "0.8"
optional = true

[dependencies.rlp]
version = "0.5"
default-features = false
optional = true

[dependencies.rlp-derive]
version = "0.1"
optional = true

[dependencies.serde]
version = "1.0"
default_features = false
optional = true
features = ["derive"]

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

[dev-dependencies]
ethereum = "0.14"
rand = "0.8"
overlord = "0.4"

[dependencies]
derive_more = "0.99"
serde_json = "1.0"
log = "0.4.19"

[features]
default = []
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"]
21 changes: 21 additions & 0 deletions devtools/axon-tools/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 AxonWeb3

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Empty file added devtools/axon-tools/README.md
Empty file.
16 changes: 16 additions & 0 deletions devtools/axon-tools/src/consts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use ethereum_types::H160;

pub const METADATA_CONTRACT_ADDRESS: H160 = H160([
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0x01,
]);

pub const CKB_LIGHT_CLIENT_CONTRACT_ADDRESS: H160 = H160([
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0x02,
]);

pub const IMAGE_CELL_CONTRACT_ADDRESS: H160 = H160([
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0x03,
]);
63 changes: 63 additions & 0 deletions devtools/axon-tools/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use std::fmt::{self, Display};

#[allow(dead_code)]
#[derive(Debug)]
pub enum Error {
InvalidProofBlockHash,
NotEnoughSignatures,
VerifyMptProof,
HexPrefix,

#[cfg(feature = "hex")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "hex")))]
Hex(faster_hex::Error),

#[cfg(feature = "proof")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "proof")))]
Bls(blst::BLST_ERROR),

#[cfg(feature = "proof")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "proof")))]
Trie(cita_trie::TrieError),
}

#[cfg(feature = "hex")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "hex")))]
impl From<faster_hex::Error> for Error {
fn from(value: faster_hex::Error) -> Self {
Self::Hex(value)
}
}

#[cfg(feature = "proof")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "proof")))]
impl From<blst::BLST_ERROR> for Error {
fn from(e: blst::BLST_ERROR) -> Self {
Self::Bls(e)
}
}

#[cfg(feature = "proof")]
#[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)
}
}

impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::InvalidProofBlockHash => write!(f, "Invalid proof block hash"),
Error::NotEnoughSignatures => write!(f, "Not enough signatures"),
Error::VerifyMptProof => write!(f, "Verify mpt proof"),
Error::HexPrefix => write!(f, "Hex prefix"),
#[cfg(feature = "hex")]
Error::Hex(e) => write!(f, "Hex error: {:?}", e),
#[cfg(feature = "proof")]
Error::Bls(e) => write!(f, "Bls error: {:?}", e),
#[cfg(feature = "proof")]
Error::Trie(e) => write!(f, "Trie error: {:?}", e),
}
}
}
22 changes: 22 additions & 0 deletions devtools/axon-tools/src/hash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use tiny_keccak::{Hasher, Keccak};

#[cfg(feature = "hash")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "hash")))]
pub fn keccak_256(data: &[u8]) -> [u8; 32] {
let mut ret = [0u8; 32];
let mut hasher = Keccak::v256();
hasher.update(data);
hasher.finalize(&mut ret);
ret
}

#[derive(Default)]
pub(crate) struct InnerKeccak;

impl cita_trie::Hasher for InnerKeccak {
const LENGTH: usize = 32;

fn digest(&self, data: &[u8]) -> Vec<u8> {
keccak_256(data).to_vec()
}
}
23 changes: 23 additions & 0 deletions devtools/axon-tools/src/hex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use crate::Error;

pub fn hex_encode<T: AsRef<[u8]>>(src: T) -> String {
faster_hex::hex_string(src.as_ref())
}

pub fn hex_decode(src: &str) -> Result<Vec<u8>, Error> {
if src.is_empty() {
return Ok(Vec::new());
}

let src = if src.starts_with("0x") {
src.split_at(2).1
} else {
src
};

let src = src.as_bytes();
let mut ret = vec![0u8; src.len() / 2];
faster_hex::hex_decode(src, &mut ret)?;

Ok(ret)
}
25 changes: 25 additions & 0 deletions devtools/axon-tools/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#![cfg_attr(doc_cfg, feature(doc_cfg))]

extern crate alloc;

mod error;
#[cfg(feature = "hash")]
pub mod hash;
#[cfg(feature = "hex")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "hex")))]
pub mod hex;
#[cfg(feature = "proof")]
mod proof;
pub mod types;

pub use error::Error;

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

#[cfg(feature = "hash")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "hash")))]
pub use hash::keccak_256;

pub mod consts;
Loading

0 comments on commit 03d83e7

Please sign in to comment.