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

alloy-consensus crate #83

Merged
merged 40 commits into from
Jan 10, 2024
Merged

alloy-consensus crate #83

merged 40 commits into from
Jan 10, 2024

Conversation

prestwich
Copy link
Member

Motivation

Have signature type support the JSON v and yParity formats

Solution

Signature keeps vrs AND an ecdsa::Signature in memory.

PR Checklist

  • Added Tests
  • Added Documentation
  • Breaking changes

@prestwich prestwich added the enhancement New feature or request label Dec 15, 2023
@prestwich prestwich self-assigned this Dec 15, 2023
@prestwich prestwich linked an issue Dec 15, 2023 that may be closed by this pull request
@prestwich prestwich changed the title refactor: signature keeps v r s in mem alloy-consensus crate Dec 16, 2023
Cargo.toml Show resolved Hide resolved
@prestwich
Copy link
Member Author

ty ty i will update here soon

wip: port ser tests

feature: Parity::chain_id

test: normalize 27/28

fix: properly normalize in recovery

fix: serde works now

refactor wip: lots of stuff

refactor: push signature into core primitives

feat: seal

feat: seal

fix: k256 feature in network

feature: rlp for TxLegacy

feature: block

wip test

fix: payload lenght

feat: sealable

feature: Tx types

tests: they pass????

refactor: envelope module

refactor: TxType

refactor: simpler rlp enc/decode for envelope

fix: todo

feature: enable sign_transaction

feat: some froms and some cleanup

lint: clippy

fix: test feature

fix: network receipt bounds

receipt.rs

restore providers
Comment on lines +31 to +43
match self.chain_id() {
Some(tx_chain_id) => {
if tx_chain_id != chain_id {
return Err(crate::Error::TransactionChainIdMismatch {
signer: chain_id,
tx: tx_chain_id,
});
}
}
None => {
self.set_chain_id(chain_id);
}
}
Copy link
Member

Choose a reason for hiding this comment

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

can simplify with

let Some(tx_chain_id)  = self.chain_id()  { else self.set_chain_Id(chain_id); return Ok(()) };

and pull the error check outside

Copy link
Member

@DaniPopes DaniPopes Jan 5, 2024

Choose a reason for hiding this comment

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

don't think it makes it any simpler

Comment on lines +103 to +110
let chain_id = self.chain_id();
if let Some(chain_id) = chain_id {
tx.set_chain_id_checked(chain_id)?;
}
let mut sig = self.sign_hash(tx.signature_hash()).await?;
if let Some(chain_id) = chain_id.or_else(|| tx.chain_id()) {
sig = sig.with_chain_id(chain_id);
}
Copy link
Member

Choose a reason for hiding this comment

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

great!

Comment on lines +271 to +298
tx.chain_id = Some(1);
let sig_1 = sign_tx_test(&mut tx, None).await.unwrap();
let expected = "c9cf86333bcb065d140032ecaab5d9281bde80f21b9687b3e94161de42d51895727a108a0b8d101465414033c3f705a9c7b826e596766046ee1183dbc8aeaa6825".parse().unwrap();
assert_eq!(sig_1, expected);
assert_ne!(sig_1, sig_none);

tx.chain_id = Some(2);
let sig_2 = sign_tx_test(&mut tx, None).await.unwrap();
assert_ne!(sig_2, sig_1);
assert_ne!(sig_2, sig_none);

// Sets chain ID.
tx.chain_id = None;
let sig_none_none = sign_tx_test(&mut tx, None).await.unwrap();
assert_eq!(sig_none_none, sig_none);

tx.chain_id = None;
let sig_none_1 = sign_tx_test(&mut tx, Some(1)).await.unwrap();
assert_eq!(sig_none_1, sig_1);

tx.chain_id = None;
let sig_none_2 = sign_tx_test(&mut tx, Some(2)).await.unwrap();
assert_eq!(sig_none_2, sig_2);

// Errors on mismatch.
tx.chain_id = Some(2);
let error = sign_tx_test(&mut tx, Some(1)).await.unwrap_err();
let expected_error = crate::Error::TransactionChainIdMismatch { signer: 1, tx: 2 };
Copy link
Member

Choose a reason for hiding this comment

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

great!

Comment on lines 104 to 101
#[cfg(TODO)] // TODO: TypedTransaction
#[instrument(err)]
async fn sign_transaction(&self, tx: &TypedTransaction) -> Result<Signature> {
let mut tx_with_chain = tx.clone();
let chain_id = tx_with_chain.chain_id().map(|id| id.as_u64()).unwrap_or(self.chain_id);
tx_with_chain.set_chain_id(chain_id);

let sighash = tx_with_chain.sighash();
self.sign_digest_with_eip155(sighash, chain_id).await
self.sign_digest_inner(hash).await.map_err(alloy_signer::Error::other)
Copy link
Member

Choose a reason for hiding this comment

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

did we lose tx signing on aws signer?

Copy link
Member

@DaniPopes DaniPopes Jan 5, 2024

Choose a reason for hiding this comment

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

uses default impl just like wallet

#[inline]
async fn sign_transaction(&self, tx: &TypedTransaction) -> Result<Signature> {
#[cfg(TODO)]
Copy link
Member

Choose a reason for hiding this comment

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

remove?

Copy link
Member

Choose a reason for hiding this comment

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

work in progress

Copy link
Member

Choose a reason for hiding this comment

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

See #100 as a possible solution

@gakonst
Copy link
Member

gakonst commented Jan 6, 2024

Would love if we can use this to remove a bunch of the Reth storage types. Might mean either manual impls of the Compact trait, or derives on newtypes. Either way would simplify things on Reth side. A fast follow to this PR should remove final Ethers usage from Header etc. types from Anvil.

@DaniPopes DaniPopes marked this pull request as ready for review January 9, 2024 18:25
@DaniPopes DaniPopes requested a review from Evalir as a code owner January 9, 2024 18:25
Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

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

all of this looks reasonable to me.

it's a lot of code, most of it comes from reth directly. imo it's fine (for now) that we have them duplicated, but want to upstream this to reth soonish.

didn't review the trait abstractions too closely, because I think those are still highly unstable, but the approach is fine.

we have a lot of room for follow ups, for example, extracting constants

Copy link
Member

Choose a reason for hiding this comment

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

as discussed with @prestwich we want to move a lot of these into a alloy-eip-constants crate or something similar, so revm can easily import those and we don't have them in two places

pub struct BlockResponse<N: Network> {
#[serde(flatten)]
header: N::HeaderResponse,
transactions: TransactionList<N::TransactionResponse>,
Copy link
Member

Choose a reason for hiding this comment

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

this probably doesn't work for Uncle blocks, so we either need a type for the uncle requests which is just N::HeaderResponse or do some serde magic here, using a dedicated UncleResponse is probably better

Copy link
Member

@gakonst gakonst left a comment

Choose a reason for hiding this comment

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

good w me - let's get this in so we can finish the anvil core transition

Cargo.toml Outdated
k256 = { version = "0.13.2", default-features = false, features = ["ecdsa", "std"] }
sha2 = { version = "0.10.8", default-features = false, features = ["std"] }
spki = { version = "0.7.2", default-features = false, features = ["std"] }
c-kzg = "0.4.0" # for eip-4844
Copy link
Member

Choose a reason for hiding this comment

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

ideally this would be optional but maybe not possible here?

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 the workspace dependencies, but anyway it's unused

tempfile = "3.8"

[patch.crates-io]
alloy-primitives = { git = "https://github.com/alloy-rs/core" }
Copy link
Member

Choose a reason for hiding this comment

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

can remove?

Copy link
Member

Choose a reason for hiding this comment

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

No, this will need a 0.6 release


/// True if the envelope is the legacy variant.
fn is_legacy(&self) -> bool {
self.type_flag().is_none()
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't this also be for Some(0)?

Copy link
Member

Choose a reason for hiding this comment

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

I guess so, added in 1a240d0

crates/eips/src/eip2930.rs Outdated Show resolved Hide resolved
Comment on lines +78 to +88
// TODO: Remove in favor of dyn trait upcasting (1.76+)
#[doc(hidden)]
impl<S: 'static> dyn Transaction<Signature = S> {
pub fn __downcast_ref<T: std::any::Any>(&self) -> Option<&T> {
if std::any::Any::type_id(self) == std::any::TypeId::of::<T>() {
unsafe { Some(&*(self as *const _ as *const T)) }
} else {
None
}
}
}
Copy link
Member

Choose a reason for hiding this comment

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

FYI @prestwich this was needed for the trezor signer

crates/network/src/transaction/signed.rs Show resolved Hide resolved
@DaniPopes DaniPopes merged commit bb469e2 into main Jan 10, 2024
16 of 17 checks passed
@DaniPopes DaniPopes deleted the prestwich/new-signature branch January 10, 2024 10:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] impl RLP traits for Signature
4 participants