Skip to content

Safely cast Optimism subtypes#8516

Merged
emlautarom1 merged 7 commits into
masterfrom
fix/op-eth-rpc-casts
Apr 15, 2025
Merged

Safely cast Optimism subtypes#8516
emlautarom1 merged 7 commits into
masterfrom
fix/op-eth-rpc-casts

Conversation

@emlautarom1

Copy link
Copy Markdown
Contributor

Fixes #8515

Changes

  • Cast only when appropriate avoiding InvalidCastExceptions.

Types of changes

What types of changes does your code introduce?

  • Bugfix (a non-breaking change that fixes an issue)
  • New feature (a non-breaking change that adds functionality)
  • Breaking change (a change that causes existing functionality not to work as expected)
  • Optimization
  • Refactoring
  • Documentation update
  • Build-related changes
  • Other: Description

Testing

Requires testing

  • Yes
  • No

If yes, did you write tests?

  • Yes
  • No

Documentation

Requires documentation update

  • Yes
  • No

Requires explanation in Release Notes

  • Yes
  • No

Remarks

The PR is initially marked as draft due to an issue with subtypes and serialization.

- TODO: Do we need this module now?
Comment thread src/Nethermind/Nethermind.Optimism/Rpc/OptimismEthRpcModule.cs
emlautarom1 and others added 2 commits April 14, 2025 11:27
- Solved by extending the derived class
- Updated tests
@emlautarom1 emlautarom1 marked this pull request as ready for review April 14, 2025 14:38
receipt,
tx.GetGasInfo(spec, block.Header),
receipts.GetBlockLogFirstIndex(receipt.Index)))];
return ResultWrapper<ReceiptForRpc[]?>.Success(result);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

There is a warning regarding covariant conversions but it should be fine considering that the resulting array is intended to be read-only.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yeah, the usual case for it. Having something like IEnumerable<> would be better for the result type (that would address the warning) but that would change a lot of the API surface.

@Scooletz Scooletz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good. One minor question

receipt,
tx.GetGasInfo(spec, block.Header),
receipts.GetBlockLogFirstIndex(receipt.Index)))];
return ResultWrapper<ReceiptForRpc[]?>.Success(result);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yeah, the usual case for it. Having something like IEnumerable<> would be better for the result type (that would address the warning) but that would change a lot of the API surface.

if (transactionModel is DepositTransactionForRpc depositTx)
{
OptimismTxReceipt? receipt = receipts.FirstOrDefault(r => r.TxHash?.Equals(hash) ?? false);
OptimismTxReceipt? receipt = (OptimismTxReceipt?)receipts.FirstOrDefault(r => r.TxHash?.Equals(hash) ?? false);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you provide a comment or spec link why is this always the case?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

According to the yellow paper, the Receipt type is the same as the Transaction type, so a Deposit transaction should be associated with a Deposit receipt.

If you take a look at op-geth's codebase, when working with a Deposit transaction, the associated receipt contains Optimism's specific fields (ex. DepositNonce), which matches our OptimismTxReceipt type.

https://github.com/ethereum-optimism/op-geth/blob/d1b66c4722e7fb5525efbcadda4c1ab3f7dacaa8/internal/ethapi/api.go#L1611-L1616

Unfortunately, a lot of these invariants are not part of the type system and I haven't been able to find them expressed in the specs so I might be wrong about them.

@emlautarom1 emlautarom1 merged commit 635f66c into master Apr 15, 2025
@emlautarom1 emlautarom1 deleted the fix/op-eth-rpc-casts branch April 15, 2025 16:36
kamilchodola pushed a commit that referenced this pull request Apr 22, 2025
* Fix `eth_getBlockReceipts`

- Do not force casts

* Fix `eth_getTransactionReceipt`

* Disable broken test

* Pending changes

* Clean `IOptimismEthRpcModule`

- TODO: Do we need this module now?

* Fix polymorphic array

- Solved by extending the derived class
- Updated tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

eth_getBlockByNumber can trigger InvalidCastException

4 participants