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(rpc): update eth_tx_from_signed_eth_message to support legacy transactions #4523

Merged
merged 18 commits into from
Jul 17, 2024

Conversation

hanabi1224
Copy link
Contributor

@hanabi1224 hanabi1224 commented Jul 12, 2024

Summary of changes

Changes introduced in this pull request:

  • update eth_tx_from_signed_eth_message to support legacy transaction types namely LEGACY_HOMESTEAD_TX and LEGACY_155_TX
     Running `target/quick/forest-tool api compare /home/me/fr/snapshots/calibnet/forest_snapshot_calibnet_2024-07-15_height_1789837.forest.car.zst --filter EthGetBlock --n-tipsets=100`
| RPC Method                                         | Forest | Lotus |
|----------------------------------------------------|--------|-------|
| Filecoin.EthGetBlockByHash (200)                   | Valid  | Valid |
| Filecoin.EthGetBlockByNumber (200)                 | Valid  | Valid |
| Filecoin.EthGetBlockTransactionCountByHash (100)   | Valid  | Valid |
| Filecoin.EthGetBlockTransactionCountByNumber (100) | Valid  | Valid |

Manually verified the below payload that was failing before

{"jsonrpc":"2.0","id":0,"method":"Filecoin.EthGetBlockByHash","params":
 ["0x3d885d56e4ecd2be098ddbb159ba2dfc6b6756680ffeed2441062fc677efea7a",true]
}

Reference issue to close (if applicable)

Closes #4519 #4518 #4477

Other information and links

Change checklist

  • I have performed a self-review of my own code,
  • I have made corresponding changes to the documentation,
  • I have added tests that prove my fix is effective or that my feature works (if possible),
  • I have made sure the CHANGELOG is up-to-date. All user-facing changes should be reflected in this document.

@hanabi1224 hanabi1224 marked this pull request as ready for review July 15, 2024 05:30
@hanabi1224 hanabi1224 requested a review from a team as a code owner July 15, 2024 05:30
@hanabi1224 hanabi1224 requested review from lemmih and LesnyRumcajs and removed request for a team July 15, 2024 05:30
fn test_rlp_encoding_legacy() {
let tx = EthTx {
r#type: EIP_LEGACY_TX_TYPE.into(),
chain_id: 314159.into(),
Copy link
Member

Choose a reason for hiding this comment

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

Where is this test case coming from? Can we add a link to the filscan/filfox transaction?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

Ok((r.into(), s.into(), v.into()))
}

fn recover_legacy_homestead_sig(sig: &Signature) -> Result<(EthBigInt, EthBigInt, EthBigInt)> {
Copy link
Member

Choose a reason for hiding this comment

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

  1. Let's avoid having hard logic in the rpc module. This should be used for schema cosmetics and re-using existing elements. I know it's not the case for now, but avoiding adding debt here will be helpful for a future refactor.
  2. This looks pretty similar to
    pub(crate) fn with_signature(
    - can we avoid logic duplication?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

Ok(Hash(keccak(self.rlp_signed_message()?)))
}

pub fn rlp_signed_message(&self) -> Result<Vec<u8>> {
Copy link
Member

Choose a reason for hiding this comment

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

Those seems like nitty-gritty details of Ethereum transactions. Should this live in the eth module?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

stream.append(&format_bigint(&self.s)?);

let mut rlp = stream.out().to_vec();
rlp.insert(0, 2);
Copy link
Member

Choose a reason for hiding this comment

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

Let's comment what is this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@@ -61,6 +61,42 @@ impl EthEip1559TxArgs {

Ok(self)
}

pub fn rlp_signed_message(&self) -> anyhow::Result<Vec<u8>> {
let mut stream = rlp::RlpStream::new_list(12);
Copy link
Member

Choose a reason for hiding this comment

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

Is there a less scary API that doesn't require changing this number on a new field?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Switched to the unbounded API

Comment on lines 7 to 15
/// Ethereum Improvement Proposals 1559 transaction type. This EIP changed Ethereum’s fee market mechanism.
/// Transaction type can have 3 distinct values:
/// - 0 for legacy transactions
/// - 1 for transactions introduced in EIP-2930
/// - 2 for transactions introduced in EIP-1559
pub const EIP_LEGACY_TX_TYPE: u64 = 0;
pub const EIP_1559_TX_TYPE: u64 = 2;

pub const ETH_LEGACY_HOMESTEAD_TX_CHAIN_ID: u64 = 0;
Copy link
Member

Choose a reason for hiding this comment

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

Let's have these constants in the eth module.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.


self.r = r;
self.s = s;
self.v = v;

Ok(self)
}

pub fn rlp_signed_message(&self) -> anyhow::Result<Vec<u8>> {
Copy link
Member

Choose a reason for hiding this comment

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

Can we have some tests for this (and other transaction types)? Can be in a different PR. The magic number makes it scary, so I'd like to ensure this doesn't brake if someone adds a new field.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This has been covered by fn test_eth_hash_*

Copy link
Member

Choose a reason for hiding this comment

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

I'd find it safer to have lower-level tests here, even if it means some duplication, but it's not a huge deal.

@hanabi1224 hanabi1224 force-pushed the hm/fix-eth_tx_from_signed_eth_message branch from 4b75183 to 36c03a0 Compare July 16, 2024 23:16

self.r = r;
self.s = s;
self.v = v;

Ok(self)
}

pub fn rlp_signed_message(&self) -> anyhow::Result<Vec<u8>> {
Copy link
Member

Choose a reason for hiding this comment

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

I'd find it safer to have lower-level tests here, even if it means some duplication, but it's not a huge deal.

@hanabi1224 hanabi1224 added this pull request to the merge queue Jul 17, 2024
Merged via the queue into main with commit b60983a Jul 17, 2024
28 checks passed
@hanabi1224 hanabi1224 deleted the hm/fix-eth_tx_from_signed_eth_message branch July 17, 2024 07:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants