Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style(axon-tools): rename some structs and change code style #1545

Merged
merged 3 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions core/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ use crate::system_contract::{
METADATA_CONTRACT_ADDRESS, METADATA_ROOT_KEY, NATIVE_TOKEN_CONTRACT_ADDRESS,
};

#[cfg(test)]
use {protocol::types::Hasher, std::iter::FromIterator};

lazy_static::lazy_static! {
pub static ref FEE_ALLOCATOR: ArcSwap<Box<dyn FeeAllocate>> = ArcSwap::from_pointee(Box::new(DefaultFeeAllocator));
}
Expand Down Expand Up @@ -431,7 +428,7 @@ impl AxonExecutor {
let txs_len = txs.len();
let block_number = adapter.block_number();
let mut res = Vec::with_capacity(txs_len);
let mut hashes = Vec::with_capacity(txs_len);
let mut encode_receipts = Vec::with_capacity(txs_len);
let (mut gas, mut fee) = (0u64, U256::zero());
let precompiles = build_precompile_set();
let config = Config::london();
Expand All @@ -451,8 +448,8 @@ impl AxonExecutor {

let logs_bloom = logs_bloom(r.logs.iter());
let receipt = tx.encode_receipt(&r, logs_bloom);
encode_receipts.push(receipt);

hashes.push(Hasher::digest(&receipt));
res.push(r);
}

Expand All @@ -475,10 +472,10 @@ impl AxonExecutor {
// commit changes by all txs included in this block only once
let new_state_root = adapter.commit();

let receipt_root = if hashes.is_empty() {
let receipt_root = if encode_receipts.is_empty() {
RLP_NULL
} else {
TrieMerkle::from_iter(hashes.iter().enumerate())
TrieMerkle::from_receipts(&encode_receipts)
.root_hash()
.unwrap_or_else(|err| {
panic!("failed to calculate trie root hash for receipts since {err}")
Expand Down
4 changes: 2 additions & 2 deletions devtools/axon-tools/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rlp::Encodable;

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

const DST: &str = "BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RONUL";
Expand All @@ -26,7 +26,7 @@ pub fn verify_trie_proof(
}

pub fn verify_proof(
block: AxonBlock,
block: Block,
previous_state_root: H256,
validator_list: &mut [ValidatorExtend],
proof: Proof,
Expand Down
4 changes: 2 additions & 2 deletions devtools/axon-tools/src/tests/verify_proof.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(feature = "proof")]
use crate::hash::keccak_256;
#[cfg(feature = "proof")]
use crate::types::{AxonBlock, BlockVersion, Metadata, Proof, Proposal, ValidatorExtend};
use crate::types::{Block, BlockVersion, Metadata, Proof, Proposal, ValidatorExtend};
#[cfg(feature = "proof")]
use bytes::Bytes;
#[cfg(feature = "proof")]
Expand Down Expand Up @@ -56,7 +56,7 @@ fn test_proposal() {
#[test]
#[cfg(feature = "proof")]
fn test_verify_proof() {
let block: AxonBlock = read_json("src/tests/block.json");
let block: Block = read_json("src/tests/block.json");
let proof: Proof = read_json("src/tests/proof.json");
let metadata: Metadata = read_json("src/tests/metadata.json");
let mut validators = metadata
Expand Down
93 changes: 2 additions & 91 deletions devtools/axon-tools/src/tests/verify_trie_proof.rs
Original file line number Diff line number Diff line change
@@ -1,63 +1,15 @@
#[cfg(feature = "proof")]
use crate::verify_trie_proof;
#[cfg(feature = "proof")]
use eth_light_client_in_ckb_prover::{encode_receipt, Receipts};
use eth_light_client_in_ckb_prover::Receipts;
#[cfg(feature = "proof")]
use ethereum_types::U256;
use ethereum_types::{Bloom, H256};
use ethers_core::{types::Log, utils::keccak256};
use ethereum_types::{H256, U256};
#[cfg(feature = "proof")]
use ethers_core::{
types::{TransactionReceipt, U64},
utils::rlp,
};

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

{
let logs = vec![Log::default()];
let receipt = TransactionReceipt {
transaction_hash: H256::from([0u8; 32]),
transaction_index: 0.into(),
cumulative_gas_used: U256::from(10),
transaction_type: Some(U64::from(2)),
status: Some(U64::from(1)),
logs_bloom: logs_bloom(logs.iter()),
logs,
..Default::default()
};

let receipt_encode = encode_receipt(&receipt);
let reference_encode: Vec<u8> = [
2u8, 249, 1, 30, 1, 10, 185, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 216, 215, 148, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 192, 128,
]
.to_vec();

assert_eq!(receipt_encode, reference_encode);
tx_receipts.push(receipt);
}

let receipts: Receipts = tx_receipts.into();
let ref_root = [
197u8, 180, 204, 76, 181, 157, 142, 152, 246, 237, 148, 126, 24, 207, 94, 119, 119, 205,
11, 16, 193, 17, 102, 157, 61, 7, 166, 133, 173, 208, 124, 6,
];
assert_eq!(receipts.root().0, ref_root);
}

#[test]
#[cfg(all(feature = "proof", feature = "std"))]
fn test_verify_trie_proof() {
Expand Down Expand Up @@ -143,44 +95,3 @@ fn test_verify_trie_proof() {
}
}
}

pub fn logs_bloom<'a, I>(logs: I) -> Bloom
where
I: Iterator<Item = &'a Log>,
{
let mut bloom = Bloom::zero();

for log in logs {
m3_2048(&mut bloom, log.address.as_bytes());
for topic in log.topics.iter() {
m3_2048(&mut bloom, topic.as_bytes());
}
}
bloom
}

pub struct Hasher;

impl Hasher {
pub fn digest<B: AsRef<[u8]>>(bytes: B) -> H256 {
if bytes.as_ref().is_empty() {
return NIL_DATA;
}

H256(keccak256(bytes))
}
}

pub const NIL_DATA: H256 = H256([
0xc5, 0xd2, 0x46, 0x01, 0x86, 0xf7, 0x23, 0x3c, 0x92, 0x7e, 0x7d, 0xb2, 0xdc, 0xc7, 0x03, 0xc0,
0xe5, 0x00, 0xb6, 0x53, 0xca, 0x82, 0x27, 0x3b, 0x7b, 0xfa, 0xd8, 0x04, 0x5d, 0x85, 0xa4, 0x70,
]);
const BLOOM_BYTE_LENGTH: usize = 256;

fn m3_2048(bloom: &mut Bloom, x: &[u8]) {
let hash = Hasher::digest(x).0;
for i in [0, 2, 4] {
let bit = (hash[i + 1] as usize + ((hash[i] as usize) << 8)) & 0x7FF;
bloom.0[BLOOM_BYTE_LENGTH - 1 - bit / 8] |= 1 << (bit % 8);
}
}
18 changes: 14 additions & 4 deletions devtools/axon-tools/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
// The structures (such as Block, Header, etc.) defined in this file have
// corresponding definitions in the 'axon-protocol' package. It's crucial to
// maintain consistency between these definitions for the correct functioning of
// the system. Therefore, any changes in the structure definitions in the
// 'axon-protocol' package must be mirrored here. Regular synchronization checks
// are recommended to ensure the definitions in this file align with those in
// the 'axon-protocol' package.
use crate::error::TypesError;
use alloc::vec;
use alloc::vec::Vec;
Expand Down Expand Up @@ -138,7 +145,6 @@ impl From<BlockVersion> for u8 {
}
}

// #[cfg(feature = "std")]
impl TryFrom<u8> for BlockVersion {
type Error = TypesError;

Expand Down Expand Up @@ -171,13 +177,14 @@ pub struct ExtraData {
pub inner: Bytes,
}

// A copy of axon_protocol::types::block::Header, must be updated simultaneously
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
feature = "impl-rlp",
derive(rlp_derive::RlpEncodable, rlp_derive::RlpDecodable)
)]
#[cfg_attr(feature = "impl-serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AxonHeader {
pub struct Header {
pub version: BlockVersion,
pub prev_hash: Hash,
pub proposer: H160,
Expand Down Expand Up @@ -228,17 +235,20 @@ pub struct AxonHeader {
pub chain_id: u64,
}

// A copy of axon_protocol::types::block::Block, must be updated simultaneously
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
feature = "impl-rlp",
derive(rlp_derive::RlpEncodable, rlp_derive::RlpDecodable)
)]
#[cfg_attr(feature = "impl-serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AxonBlock {
pub header: AxonHeader,
pub struct Block {
pub header: Header,
pub tx_hashes: Vec<H256>,
}

// A copy of axon_protocol::types::block::Proposal, must be updated
// simultaneously
#[cfg(feature = "proof")]
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(doc_cfg, doc(cfg(feature = "proof")))]
Expand Down
Loading