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

feat: network abstraction and transaction builder #190

Merged
merged 65 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
d030974
refactor: remove `TempProvider`
onbjerg Mar 7, 2024
668d3d0
chore: bump alloy to 0.6.4
onbjerg Mar 7, 2024
a698dee
refactor: add `TypedTransaction` and tx traits
onbjerg Mar 7, 2024
dfbc394
feat: tx builder
onbjerg Mar 7, 2024
6e1b1ee
feat: network signer trait
onbjerg Mar 7, 2024
8c5c484
feat: `Ethereum` network
onbjerg Mar 7, 2024
66cf994
refactor: adjust alloy-contract to new provider
onbjerg Mar 7, 2024
bc5f084
refactor: adjust signers to new signer traits
onbjerg Mar 7, 2024
6e4e7d2
chore: remove unused imports
onbjerg Mar 7, 2024
b809324
feat: EIP-1559 gas methods in builder
onbjerg Mar 7, 2024
98b7c67
feat: EIP-4844 gas methods in builder
onbjerg Mar 7, 2024
13e9a3b
feat: impl ethereum builder
onbjerg Mar 7, 2024
f6b8e00
chore: rm unused dep
onbjerg Mar 7, 2024
16b328e
temp: remove `Hash` derive
onbjerg Mar 7, 2024
b3db0fe
fix: cfg attr for wasm
onbjerg Mar 7, 2024
126a4b7
feat: tx signer trait impls for `Wallet`
onbjerg Mar 7, 2024
92cf293
test: use ethereum network in provider test
onbjerg Mar 7, 2024
5f195ba
docs: adjust provider docs
onbjerg Mar 7, 2024
9d3f6d3
temp: temporarily disable contract tests
onbjerg Mar 7, 2024
b419d25
chore: clippy
onbjerg Mar 7, 2024
105fc61
feat: finish tx signer trait impls
onbjerg Mar 7, 2024
c89e5a4
fix: set tx chain id
onbjerg Mar 7, 2024
f3541d2
test: port old provider tests
onbjerg Mar 7, 2024
07d5aea
test: don't drop `AnvilInstance` too early
onbjerg Mar 7, 2024
d88eba1
test: fix k256 gated tests
onbjerg Mar 7, 2024
5dcd259
chore: fmt
onbjerg Mar 7, 2024
5d0a548
chore: clippy
onbjerg Mar 7, 2024
ac07332
chore: docs lint
onbjerg Mar 7, 2024
264daa1
ci: do not check wasm support for `Network` crate
onbjerg Mar 7, 2024
136cf84
docs: adjust all docs
onbjerg Mar 7, 2024
043cef3
chore: rm unneeded `'static`
onbjerg Mar 7, 2024
34b76ee
test: re-enable contract tests
onbjerg Mar 7, 2024
2404e88
chore: use patched sol-macro
onbjerg Mar 7, 2024
288959f
chore: update sol-macro patch
onbjerg Mar 7, 2024
201df2b
refactor: move anvil methods to extension trait
onbjerg Mar 7, 2024
14d8329
feat: populate gas helpers
onbjerg Mar 7, 2024
5372ae0
fix: remove anvil cfg
onbjerg Mar 7, 2024
abf6e4b
chore: clippy
onbjerg Mar 7, 2024
04158ea
feat: impl `TxSigner` for aws, gcp
onbjerg Mar 8, 2024
4715b3e
refactor: move `Receipt` trait
onbjerg Mar 8, 2024
b04e966
refactor: move tx builder impl to own module
onbjerg Mar 8, 2024
89c8c01
feat: `CallBuilder::map`
onbjerg Mar 8, 2024
d5585ad
test: fix double import
onbjerg Mar 8, 2024
290beab
chore: last nit
onbjerg Mar 8, 2024
d897d2e
feat: signer layer
onbjerg Mar 8, 2024
e228a61
feat: `CallBuilder::send`
onbjerg Mar 8, 2024
ff555ab
chore: nits
DaniPopes Mar 8, 2024
dbf7f7f
chore: ZST cannot be enforced by a trait
DaniPopes Mar 8, 2024
dc18b0d
feat: better private key instantiation discoverability
DaniPopes Mar 8, 2024
8afa99c
chore: rm outdated todo
onbjerg Mar 8, 2024
6f9b180
feat: `EthereumSigner::new`
onbjerg Mar 8, 2024
a7be381
chore: code nit
onbjerg Mar 8, 2024
17eb93a
docs: note about network zst
onbjerg Mar 8, 2024
bf86056
chore: nit
onbjerg Mar 8, 2024
5210141
refactor: rename `sign` to `sign_transaction`
onbjerg Mar 8, 2024
69bb2e1
feat: `ProviderBuilder::signer`
onbjerg Mar 8, 2024
9324861
docs: more network signer docs
onbjerg Mar 11, 2024
2425bc9
refactor: remove unneeded `'static`
onbjerg Mar 11, 2024
95f22db
refactor: `set_chain_id_checked` sort of
onbjerg Mar 11, 2024
5a41fbb
chore: use LocalWallet
DaniPopes Mar 11, 2024
6e03867
fix: docs
DaniPopes Mar 11, 2024
ceb865a
chore: dedup signing code
DaniPopes Mar 11, 2024
ad9c520
chore: more dedup
DaniPopes Mar 11, 2024
81c98a5
ci: run doctests
DaniPopes Mar 11, 2024
d2a349e
test: fix doctest
onbjerg Mar 11, 2024
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
13 changes: 13 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ jobs:
if: ${{ matrix.rust != '1.75' }} # MSRV
run: cargo nextest run --workspace ${{ matrix.flags }}

doctest:
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2
with:
cache-on-failure: true
- run: cargo test --workspace --doc
- run: cargo test --all-features --workspace --doc

wasm:
runs-on: ubuntu-latest
timeout-minutes: 30
Expand All @@ -68,6 +80,7 @@ jobs:
run: |
cargo hack check --workspace --target wasm32-unknown-unknown \
--exclude alloy-contract \
--exclude alloy-network \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

excluded because we now depend on alloy-signer which depends on rand which is not wasm-compatible. the reason alloy-signer depends on rand is for mnemonics, so a better long term solution would be to split Wallet etc. into their own crate. will leave this for a follow up

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup -- let's make alloy-signer wasm compatilbe in follow up, should be easy

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--exclude alloy-node-bindings \
--exclude alloy-providers \
--exclude alloy-signer \
Expand Down
29 changes: 21 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,18 @@ alloy-transport-ipc = { version = "0.1.0", default-features = false, path = "cra
alloy-transport-ws = { version = "0.1.0", default-features = false, path = "crates/transport-ws" }

alloy-core = { version = "0.6.4", default-features = false, features = ["std"] }
alloy-dyn-abi = { version = "0.6.4", default-features = false, features = ["std"] }
alloy-json-abi = { version = "0.6.4", default-features = false, features = ["std"] }
alloy-primitives = { version = "0.6.4", default-features = false, features = ["std"] }
alloy-sol-types = { version = "0.6.4", default-features = false, features = ["std"] }
alloy-dyn-abi = { version = "0.6.4", default-features = false, features = [
"std",
] }
alloy-json-abi = { version = "0.6.4", default-features = false, features = [
"std",
] }
alloy-primitives = { version = "0.6.4", default-features = false, features = [
"std",
] }
alloy-sol-types = { version = "0.6.4", default-features = false, features = [
"std",
] }

alloy-rlp = "0.3"

Expand All @@ -53,8 +61,13 @@ ethereum_ssz_derive = "0.5"
ethereum_ssz = "0.5"

# crypto
elliptic-curve = { version = "0.13", default-features = false, features = ["std"] }
k256 = { version = "0.13", default-features = false, features = ["ecdsa", "std"] }
elliptic-curve = { version = "0.13", default-features = false, features = [
"std",
] }
k256 = { version = "0.13", default-features = false, features = [
"ecdsa",
"std",
] }
sha2 = { version = "0.10", default-features = false, features = ["std"] }
spki = { version = "0.7", default-features = false, features = ["std"] }

Expand Down Expand Up @@ -102,5 +115,5 @@ tempfile = "3.10"

# TODO: Keep around until alloy-contract is stable.
# This should only be used in `alloy-contract` tests.
# [patch.crates-io]
# alloy-sol-macro = { git = "https://github.com/alloy-rs/core", rev = "18b0509950c90d9ec38f25913b692ae4cdd6f227" }
[patch.crates-io]
alloy-sol-macro = { git = "https://github.com/alloy-rs/core", rev = "ab0cab1047a088be6cffa4e7a2fcde7cf77aa460" }
Comment on lines +118 to +119
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now on main so can remove I think? Or still needed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will keep it until release

6 changes: 4 additions & 2 deletions crates/consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ repository.workspace = true
exclude.workspace = true

[dependencies]
alloy-network.workspace = true
alloy-primitives = { workspace = true, features = ["rlp"] }
alloy-rlp.workspace = true
alloy-eips.workspace = true
Expand All @@ -24,10 +23,13 @@ sha2 = { version = "0.10", optional = true }
arbitrary = { workspace = true, features = ["derive"], optional = true }

[dev-dependencies]
alloy-signer.workspace = true
# arbitrary
arbitrary = { workspace = true, features = ["derive"] }
k256.workspace = true
tokio = { workspace = true, features = ["macros"] }

[features]
k256 = ["alloy-primitives/k256", "alloy-network/k256"]
k256 = ["alloy-primitives/k256"]
kzg = ["dep:c-kzg", "dep:sha2", "dep:thiserror"]
arbitrary = ["dep:arbitrary", "alloy-eips/arbitrary"]
5 changes: 3 additions & 2 deletions crates/consensus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ Ethereum consensus interface.

This crate contains constants, types, and functions for implementing Ethereum
EL consensus and communication. This includes headers, blocks, transactions,
eip2718 envelopes, eip2930, eip4844, and more. The types in this crate
implement many of the traits found in [alloy_network].
[EIP-2718] envelopes, [EIP-2930], [EIP-4844], and more.

In general a type belongs in this crate if it is committed to in the EL block
header. This includes:
Expand All @@ -18,6 +17,8 @@ header. This includes:

[alloy-network]: ../network
[EIP-2718]: https://eips.ethereum.org/EIPS/eip-2718
[EIP-2930]: https://eips.ethereum.org/EIPS/eip-2930
[EIP-4844]: https://eips.ethereum.org/EIPS/eip-4844

## Provenance

Expand Down
2 changes: 1 addition & 1 deletion crates/consensus/src/header.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::Sealable;
use alloy_eips::{
eip1559::{calc_next_block_base_fee, BaseFeeParams},
eip4844::{calc_blob_gasprice, calc_excess_blob_gas},
};
use alloy_network::Sealable;
use alloy_primitives::{b256, keccak256, Address, BlockNumber, Bloom, Bytes, B256, B64, U256};
use alloy_rlp::{
length_of_length, Buf, BufMut, Decodable, Encodable, EMPTY_LIST_CODE, EMPTY_STRING_CODE,
Expand Down
12 changes: 8 additions & 4 deletions crates/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,19 @@ mod header;
pub use header::{Header, EMPTY_OMMER_ROOT_HASH, EMPTY_ROOT_HASH};

mod receipt;
pub use receipt::{Receipt, ReceiptEnvelope, ReceiptWithBloom};
pub use receipt::{Receipt, ReceiptEnvelope, ReceiptWithBloom, TxReceipt};

mod transaction;
pub use transaction::{
BlobTransactionSidecar, TxEip1559, TxEip2930, TxEip4844, TxEip4844Variant,
TxEip4844WithSidecar, TxEnvelope, TxLegacy, TxType,
BlobTransactionSidecar, SignableTransaction, Transaction, TxEip1559, TxEip2930, TxEip4844,
TxEip4844Variant, TxEip4844WithSidecar, TxEnvelope, TxLegacy, TxType, TypedTransaction,
};

#[cfg(feature = "kzg")]
pub use transaction::BlobTransactionValidationError;

pub use alloy_network::TxKind;
mod sealed;
pub use sealed::{Sealable, Sealed};

mod signed;
pub use signed::Signed;
26 changes: 25 additions & 1 deletion crates/consensus/src/receipt/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
use alloy_primitives::{Bloom, Log};

mod envelope;
pub use envelope::ReceiptEnvelope;

mod receipts;
pub use receipts::{Receipt, ReceiptWithBloom};

/// Receipt is the result of a transaction execution.
pub trait TxReceipt {
/// Returns true if the transaction was successful.
fn success(&self) -> bool;

/// Returns the bloom filter for the logs in the receipt. This operation
/// may be expensive.
fn bloom(&self) -> Bloom;

/// Returns the bloom filter for the logs in the receipt, if it is cheap to
/// compute.
fn bloom_cheap(&self) -> Option<Bloom> {
None
}

/// Returns the cumulative gas used in the block after this transaction was executed.
fn cumulative_gas_used(&self) -> u64;

/// Returns the logs emitted by this transaction.
fn logs(&self) -> &[Log];
}

#[cfg(test)]
mod tests {
use super::*;
use alloy_eips::eip2718::Encodable2718;
use alloy_primitives::{address, b256, bytes, hex, Bytes, Log, LogData};
use alloy_primitives::{address, b256, bytes, hex, Bytes, LogData};
use alloy_rlp::{Decodable, Encodable};

// Test vector from: https://eips.ethereum.org/EIPS/eip-2481
Expand Down
49 changes: 25 additions & 24 deletions crates/consensus/src/receipt/receipts.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::TxReceipt;
use alloy_primitives::{Bloom, Log};
use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable};

Expand Down Expand Up @@ -28,7 +29,7 @@ impl Receipt {
}
}

impl alloy_network::Receipt for Receipt {
impl TxReceipt for Receipt {
fn success(&self) -> bool {
self.success
}
Expand All @@ -51,7 +52,7 @@ impl alloy_network::Receipt for Receipt {
/// This convenience type allows us to lazily calculate the bloom filter for a
/// receipt, similar to [`Sealed`].
///
/// [`Sealed`]: ::alloy_network::Sealed
/// [`Sealed`]: crate::sealed::Sealed
#[derive(Clone, Debug, PartialEq, Eq, Default)]
pub struct ReceiptWithBloom {
/// The receipt.
Expand All @@ -60,6 +61,28 @@ pub struct ReceiptWithBloom {
pub bloom: Bloom,
}

impl TxReceipt for ReceiptWithBloom {
fn success(&self) -> bool {
self.receipt.success
}

fn bloom(&self) -> Bloom {
self.bloom
}

fn bloom_cheap(&self) -> Option<Bloom> {
Some(self.bloom)
}

fn cumulative_gas_used(&self) -> u64 {
self.receipt.cumulative_gas_used
}

fn logs(&self) -> &[Log] {
&self.receipt.logs
}
}

impl From<Receipt> for ReceiptWithBloom {
fn from(receipt: Receipt) -> Self {
let bloom = receipt.bloom_slow();
Expand Down Expand Up @@ -135,28 +158,6 @@ impl ReceiptWithBloom {
}
}

impl alloy_network::Receipt for ReceiptWithBloom {
fn success(&self) -> bool {
self.receipt.success
}

fn bloom(&self) -> Bloom {
self.bloom
}

fn bloom_cheap(&self) -> Option<Bloom> {
Some(self.bloom)
}

fn cumulative_gas_used(&self) -> u64 {
self.receipt.cumulative_gas_used
}

fn logs(&self) -> &[Log] {
&self.receipt.logs
}
}

impl alloy_rlp::Encodable for ReceiptWithBloom {
fn encode(&self, out: &mut dyn BufMut) {
self.encode_fields(out);
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::Transaction;
use crate::transaction::SignableTransaction;
use alloy_primitives::{Signature, B256};
use alloy_rlp::BufMut;

Expand Down Expand Up @@ -35,9 +35,9 @@ impl<T, Sig> Signed<T, Sig> {
}
}

impl<T: Transaction> Signed<T> {
impl<T: SignableTransaction<Sig>, Sig> Signed<T, Sig> {
/// Instantiate from a transaction and signature. Does not verify the signature.
pub const fn new_unchecked(tx: T, signature: Signature, hash: B256) -> Self {
pub const fn new_unchecked(tx: T, signature: Sig, hash: B256) -> Self {
Self { tx, signature, hash }
}

Expand All @@ -59,22 +59,22 @@ impl<T: Transaction> Signed<T> {
}
}

impl<T: Transaction> alloy_rlp::Encodable for Signed<T> {
impl<T: SignableTransaction<Sig>, Sig> alloy_rlp::Encodable for Signed<T, Sig> {
fn encode(&self, out: &mut dyn BufMut) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i forget why we did Sig as a generic instead of associated type, maybe a follow-up breaking change to the trait could remove the double mention of Sig to make the Signed trait easier to impl

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because transactions can be signed with multiple types of signatures on some networks

self.tx.encode_signed(&self.signature, out)
}

// TODO: impl length
}

impl<T: Transaction> alloy_rlp::Decodable for Signed<T> {
impl<T: SignableTransaction<Sig>, Sig> alloy_rlp::Decodable for Signed<T, Sig> {
fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
T::decode_signed(buf)
<T as SignableTransaction<Sig>>::decode_signed(buf)
}
}

#[cfg(feature = "k256")]
impl<T: Transaction> Signed<T, Signature> {
impl<T: SignableTransaction<Signature>> Signed<T, Signature> {
/// Recover the signer of the transaction
pub fn recover_signer(
&self,
Expand Down
Loading
Loading