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

[Merge] Add serde impls for Transactions type #2649

Merged
merged 3 commits into from
Sep 30, 2021

Conversation

paulhauner
Copy link
Member

Issue Addressed

NA

Proposed Changes

Add serde impls and tests for the execution_payload.transactions field.

Additional Info

NA

@paulhauner paulhauner added the ready-for-review The code is ready for review label Sep 29, 2021
Comment on lines +410 to +428
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: serde::de::SeqAccess<'a>,
{
let mut outer = VariableList::default();

while let Some(val) = seq.next_element::<String>()? {
let inner_vec = hex::decode(&val).map_err(de::Error::custom)?;
let opaque_transaction = VariableList::new(inner_vec).map_err(|e| {
serde::de::Error::custom(format!("transaction too large: {:?}", e))
})?;
let transaction = Transaction::OpaqueTransaction(opaque_transaction);
outer.push(transaction).map_err(|e| {
serde::de::Error::custom(format!("too many transactions: {:?}", e))
})?;
}

Ok(outer)
}
Copy link
Member

Choose a reason for hiding this comment

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

I think we can avoid a handwritten visitor implementation by defining a JsonTransaction type that derives (De)Serialize, and then uses serde attributes to tweak the behaviour. This has the advantage of being declarative rather than imperative.

AFAICT you want the serialisation to match the types::Transaction enum, except without the {"selector": 0, "value": value} wrapping, which could be achieved by using the untagged enum representation serde(untagged): https://serde.rs/enum-representations.html#untagged

Everything else could be reused from Transaction:

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Encode, Decode, TreeHash)]
#[ssz(enum_behaviour = "union")]
#[tree_hash(enum_behaviour = "union")]
#[serde(tag = "selector", content = "value")]
#[serde(bound = "T: EthSpec")]
pub enum Transaction<T: EthSpec> {
// FIXME(merge): renaming this enum variant to 0 is a bit of a hack...
#[serde(rename = "0")]
OpaqueTransaction(
#[serde(with = "ssz_types::serde_utils::hex_var_list")]
VariableList<u8, T::MaxBytesPerOpaqueTransaction>,
),
}

(and you don't need the rename = "0" hack because of the untagged repr)

Comment on lines +440 to +442
// It's important to match on the inner values of the transaction. Serializing the
// entire `Transaction` will result in appending the SSZ union prefix byte. The
// execution node does not want that.
Copy link
Member

Choose a reason for hiding this comment

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

I think this is exactly serde(untagged)

@paulhauner
Copy link
Member Author

Very good points about the serde impls, thanks. I'm tempted to stay with what have for the time being because:

  1. This serialization is marked for change: Transaction Type representation in the Merge ethereum/consensus-specs#2608
  2. I'm concerned that we're going to have issues communicating with the execution nodes and this handwritten impl will be easier to debug.
  3. It's easier to leave it as is, and we're a bit short on time.

Copy link
Member

@michaelsproul michaelsproul left a comment

Choose a reason for hiding this comment

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

Yeah ok cool. I thought it seemed strange that the exec engines weren't using the selectors. Merge at will

@paulhauner paulhauner mentioned this pull request Sep 29, 2021
7 tasks
@paulhauner paulhauner merged commit 075286e into sigp:merge-f2f Sep 30, 2021
paulhauner added a commit that referenced this pull request Oct 1, 2021
* Start implemented serde for transactions

* Revise serde impl

* Add tests for transaction decoding
paulhauner added a commit that referenced this pull request Oct 12, 2021
* Start implemented serde for transactions

* Revise serde impl

* Add tests for transaction decoding
paulhauner added a commit that referenced this pull request Oct 27, 2021
* Start implemented serde for transactions

* Revise serde impl

* Add tests for transaction decoding
paulhauner added a commit to paulhauner/lighthouse that referenced this pull request Nov 3, 2021
* Start implemented serde for transactions

* Revise serde impl

* Add tests for transaction decoding
paulhauner added a commit that referenced this pull request Nov 11, 2021
* Start implemented serde for transactions

* Revise serde impl

* Add tests for transaction decoding
paulhauner added a commit that referenced this pull request Nov 28, 2021
* Start implemented serde for transactions

* Revise serde impl

* Add tests for transaction decoding
paulhauner added a commit that referenced this pull request Nov 28, 2021
* Start implemented serde for transactions

* Revise serde impl

* Add tests for transaction decoding
paulhauner added a commit that referenced this pull request Dec 2, 2021
* Start implemented serde for transactions

* Revise serde impl

* Add tests for transaction decoding
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready-for-review The code is ready for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants