Skip to content

Commit

Permalink
Merge pull request #1 from akjong/feat/init
Browse files Browse the repository at this point in the history
Init
  • Loading branch information
Akagi201 authored Nov 26, 2024
2 parents fc5b5ef + 4b79e78 commit 8bcaed3
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 1 deletion.
53 changes: 53 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Rust Check & Build

on:
push:
tags:
- v*
branches:
- master
- main
paths-ignore:
- '**.md'
pull_request:
paths-ignore:
- '**.md'
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

env:
# Not needed in CI, should make things a bit faster
CARGO_INCREMENTAL: 0
CARGO_TERM_COLOR: always
# Build smaller artifacts to avoid running out of space in CI
RUSTFLAGS: -C strip=debuginfo

jobs:
check_and_build:
name: Check and Build
runs-on: ubuntu-latest

steps:
- name: Cancel Previous Runs
uses: styfle/cancel-workflow-action@main
with:
access_token: ${{ github.token }}
- name: Checkout
uses: actions/checkout@v4
- name: Setup Rust toolchain
uses: dtolnay/rust-toolchain@nightly
with:
components: clippy,rustfmt,miri
- uses: taiki-e/install-action@v2
with:
tool: cargo-sort,cargo-machete
- name: Setup Rust cache
uses: Swatinem/rust-cache@v2
with:
cache-on-failure: true
- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly
- name: Install Protoc
uses: arduino/setup-protoc@v3
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ Cargo.lock
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
#.idea/

# Added by cargo

/target
9 changes: 9 additions & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
unstable_features = true
newline_style = "Unix"
imports_granularity = "Crate"
group_imports = "StdExternalCrate"
reorder_imports = true
normalize_comments = true
normalize_doc_attributes = true
format_code_in_doc_comments = true
use_field_init_shorthand = true
12 changes: 12 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "alloy-compact"
version = "0.1.0"
edition = "2021"

[dependencies]
alloy = { version = "0.6.4", features = ["full", "node-bindings", "rlp", "rpc-types-engine"] }
ethereum-consensus = { git = "https://github.com/ralexstokes/ethereum-consensus", rev = "8fbd8a53dca0170bedeca40a92ee70fd48c4615b" }
reth-primitives = { git = "https://github.com/paradigmxyz/reth", tag = "v1.1.2" }

[dev-dependencies]
tokio = "1.41.1"
61 changes: 61 additions & 0 deletions src/block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use alloy::{
eips::eip4895::Withdrawal,
primitives::{Address, B256, U256},
rpc::types::engine::{
ExecutionPayload as AlloyExecutionPayload, ExecutionPayloadV1, ExecutionPayloadV2,
ExecutionPayloadV3,
},
};
use reth_primitives::SealedBlock;

pub fn to_alloy_withdrawal(value: ethereum_consensus::deneb::Withdrawal) -> Withdrawal {
Withdrawal {
index: value.index as u64,
validator_index: value.validator_index as u64,
address: Address::from_slice(value.address.as_ref()),
amount: value.amount,
}
}

pub fn to_alloy_execution_payload(block: &SealedBlock, block_hash: B256) -> AlloyExecutionPayload {
let alloy_withdrawals = block
.body
.withdrawals
.as_ref()
.map(|withdrawals| {
withdrawals
.iter()
.map(|w| Withdrawal {
index: w.index,
validator_index: w.validator_index,
address: w.address,
amount: w.amount,
})
.collect::<Vec<_>>()
})
.unwrap_or_default();

AlloyExecutionPayload::V3(ExecutionPayloadV3 {
blob_gas_used: block.blob_gas_used(),
excess_blob_gas: block.excess_blob_gas.unwrap_or_default(),
payload_inner: ExecutionPayloadV2 {
payload_inner: ExecutionPayloadV1 {
base_fee_per_gas: U256::from(block.base_fee_per_gas.unwrap_or_default()),
block_hash,
block_number: block.number,
extra_data: block.extra_data.clone(),
transactions: block.raw_transactions(),
fee_recipient: block.header.beneficiary,
gas_limit: block.gas_limit,
gas_used: block.gas_used,
logs_bloom: block.logs_bloom,
parent_hash: block.parent_hash,
prev_randao: block.mix_hash,
receipts_root: block.receipts_root,
state_root: block.state_root,
timestamp: block.timestamp,
},
withdrawals: alloy_withdrawals,
},
})
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod block;
pub mod time;
pub mod transaction;
17 changes: 17 additions & 0 deletions src/time.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use std::time::{SystemTime, SystemTimeError, UNIX_EPOCH};

pub fn get_nanos_timestamp() -> Result<u64, SystemTimeError> {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_nanos() as u64)
}

#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_nanos_timestamp() {
let timestamp = get_nanos_timestamp().unwrap();
assert!(timestamp > 0);
}
}
23 changes: 23 additions & 0 deletions src/transaction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use alloy::{
consensus::TxEnvelope,
eips::eip2718::Encodable2718,
network::{Ethereum, EthereumWallet, TransactionBuilder, TransactionBuilderError},
primitives::{Bytes, ChainId},
rpc::types::TransactionRequest,
};

pub fn envelope_to_raw_tx(tx: &TxEnvelope) -> Bytes {
let mut encoded = Vec::new();
tx.encode_2718(&mut encoded);
encoded.into()
}

pub async fn tx_req_to_envelope(
tx_req: TransactionRequest,
chain_id: ChainId,
wallet: EthereumWallet,
) -> Result<TxEnvelope, TransactionBuilderError<Ethereum>> {
<TransactionRequest as TransactionBuilder<Ethereum>>::with_chain_id(tx_req, chain_id)
.build(&wallet)
.await
}

0 comments on commit 8bcaed3

Please sign in to comment.