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

fix: workaround for WithOtherFields #495

Merged
merged 5 commits into from
Apr 9, 2024

Conversation

klkvr
Copy link
Member

@klkvr klkvr commented Apr 9, 2024

Motivation

Ref serde-rs/serde#1909

ref #493

Currently deserializing WithOtherFields<T> where T is a struct with multiple levels of fields with serde(flatten), the resulted object would have fields of the inner struct duplicated in other. Example of this behavior is a deserialization of AnyTransactionReceipt:

resulted receipt
WithOtherFields {
    inner: TransactionReceipt {
        inner: AnyReceiptEnvelope {
            inner: ReceiptWithBloom {
                receipt: Receipt {
                    status: true,
                    cumulative_gas_used: 1586930,
                    logs: [
                        Log {
                            inner: Log {
                                address: 0x13b82c379e4209acc43005f0b2b661945557f56e,
                                data: LogData {
                                    topics: [
                                        0x8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e0,
                                        0x00000000000000000000000045fa3ec833b6a0bf1ab27c8dd33ae03baacc3aa4,
                                        0x0000000000000000000000006c1e90f219de736167036e9abde81ec8cd7b2daf,
                                    ],
                                    data: 0x,
                                },
                            },
                            block_hash: Some(
                                0xc97a0a94ff0fb5eea6614aa4ea708cb237f1dd0b0e651e7547dc8aa07043f16d,
                            ),
                            block_number: Some(
                                168738165,
                            ),
                            block_timestamp: None,
                            transaction_hash: Some(
                                0xfd0aa3e177ce6cf3eb74387b5284bddf212238ed42872bbab395e95c68d98137,
                            ),
                            transaction_index: Some(
                                3,
                            ),
                            log_index: Some(
                                2,
                            ),
                            removed: false,
                        },
                    ],
                },
                logs_bloom: 0x00000000000000000000000000000000000000000000000000800000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000020000000000200000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000840000000080000000000000400000000000000,
            },
            type: 2,
        },
        transaction_hash: 0xfd0aa3e177ce6cf3eb74387b5284bddf212238ed42872bbab395e95c68d98137,
        transaction_index: 3,
        block_hash: Some(
            0xc97a0a94ff0fb5eea6614aa4ea708cb237f1dd0b0e651e7547dc8aa07043f16d,
        ),
        block_number: Some(
            168738165,
        ),
        gas_used: 458688,
        effective_gas_price: 100000000,
        blob_gas_used: None,
        blob_gas_price: None,
        from: 0x45fa3ec833b6a0bf1ab27c8dd33ae03baacc3aa4,
        to: Some(
            0x13b82c379e4209acc43005f0b2b661945557f56e,
        ),
        contract_address: None,
        state_root: None,
    },
    other: OtherFields {
        inner: {
            "blockHash": String("0xc97a0a94ff0fb5eea6614aa4ea708cb237f1dd0b0e651e7547dc8aa07043f16d"),
            "blockNumber": String("0xa0ebd75"),
            "contractAddress": Null,
            "cumulativeGasUsed": String("0x1836f2"),
            "effectiveGasPrice": String("0x5f5e100"),
            "from": String("0x45fa3ec833b6a0bf1ab27c8dd33ae03baacc3aa4"),
            "gasUsed": String("0x6ffc0"),
            "gasUsedForL1": String("0x68fc3"),
            "l1BlockNumber": String("0x12177f1"),
            "logs": Array [
                Object {
                    "address": String("0x13b82c379e4209acc43005f0b2b661945557f56e"),
                    "blockHash": String("0xc97a0a94ff0fb5eea6614aa4ea708cb237f1dd0b0e651e7547dc8aa07043f16d"),
                    "blockNumber": String("0xa0ebd75"),
                    "data": String("0x"),
                    "logIndex": String("0x2"),
                    "removed": Bool(false),
                    "topics": Array [
                        String("0x8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e0"),
                        String("0x00000000000000000000000045fa3ec833b6a0bf1ab27c8dd33ae03baacc3aa4"),
                        String("0x0000000000000000000000006c1e90f219de736167036e9abde81ec8cd7b2daf"),
                    ],
                    "transactionHash": String("0xfd0aa3e177ce6cf3eb74387b5284bddf212238ed42872bbab395e95c68d98137"),
                    "transactionIndex": String("0x3"),
                },
            ],
            "logsBloom": String("0x00000000000000000000000000000000000000000000000000800000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000020000000000200000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000840000000080000000000000400000000000000"),
            "status": String("0x1"),
            "to": String("0x13b82c379e4209acc43005f0b2b661945557f56e"),
            "transactionHash": String("0xfd0aa3e177ce6cf3eb74387b5284bddf212238ed42872bbab395e95c68d98137"),
            "transactionIndex": String("0x3"),
            "type": String("0x2"),
        },
    },
}

Solution

This PR implements a workaround which serializes inner and removes all duplicating keys after deserialization

PR Checklist

  • Added Tests
  • Added Documentation
  • Breaking changes

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.

smol nit

I consider this a tmp workaround, there's probably a better solution for this problem but for now this is sufficient

Comment on lines 78 to 85
let inner_keys = match &inner_serialzed {
Value::Object(map) => map.keys().collect::<Vec<_>>(),
_ => Vec::new(),
};

for key in inner_keys {
helper.other.remove(key);
}
Copy link
Member

Choose a reason for hiding this comment

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

we can move the for loop into the match arm and avoid the vec alloc I believe

@mattsse mattsse merged commit 987b393 into alloy-rs:main Apr 9, 2024
18 checks passed
klkvr added a commit to klkvr/alloy that referenced this pull request Apr 11, 2024
* fix: avoid duplicating fields in WithOtherFields

* add test

* avoid alloc

* fmt

* chore: add note

---------

Co-authored-by: Matthias Seitz <[email protected]>
ben186 pushed a commit to ben186/alloy that referenced this pull request Jul 27, 2024
* fix: avoid duplicating fields in WithOtherFields

* add test

* avoid alloc

* fmt

* chore: add note

---------

Co-authored-by: Matthias Seitz <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants