Skip to content

Validate transactions by consensus rules#323

Merged
m-Peter merged 6 commits into
mainfrom
consensus-rules-tx-validation
Jul 3, 2024
Merged

Validate transactions by consensus rules#323
m-Peter merged 6 commits into
mainfrom
consensus-rules-tx-validation

Conversation

@m-Peter
Copy link
Copy Markdown
Collaborator

@m-Peter m-Peter commented Jun 27, 2024

Closes: #117

Description

Performs the same validation rules as the Geth TxPool


For contributor use:

  • Targeted PR against master branch
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work.
  • Code follows the standards mentioned here.
  • Updated relevant documentation
  • Re-reviewed Files changed in the Github PR explorer
  • Added appropriate labels

Summary by CodeRabbit

  • New Features

    • Enhanced transaction validation logic with additional parameters and consensus rule checks.
  • Tests

    • Added new test functions for validating consensus rules and transaction scenarios.
    • Updated integration tests to use the latest network ID.
  • Chores

    • Introduced constants for transaction size constraints to improve consistency and maintainability.

@m-Peter m-Peter self-assigned this Jun 27, 2024
@coderabbitai

This comment was marked as outdated.

@m-Peter m-Peter force-pushed the consensus-rules-tx-validation branch 2 times, most recently from e4d094d to ffc4131 Compare June 27, 2024 15:34
@m-Peter m-Peter changed the title WIP Validate transactions by consensus rules Jun 27, 2024
@m-Peter m-Peter added this to the Flow-EVM-M2 milestone Jun 27, 2024
@m-Peter m-Peter force-pushed the consensus-rules-tx-validation branch from ffc4131 to 0e712e1 Compare June 28, 2024 11:13
@m-Peter m-Peter marked this pull request as ready for review June 28, 2024 11:17
Comment thread services/requester/requester.go Outdated
Comment thread tests/integration_test.go
Comment thread tests/integration_test.go
@m-Peter m-Peter requested a review from ramtinms June 28, 2024 11:24
coderabbitai[bot]

This comment was marked as outdated.

Comment thread services/requester/requester.go Outdated
Comment on lines +169 to +190
head := &types.Header{
Number: big.NewInt(20_182_324),
Time: uint64(time.Now().Unix()),
GasLimit: 30_000_000,
}
chainConfig := emulator.DefaultChainConfig
chainConfig.ChainID = e.config.EVMNetworkID
signer := types.MakeSigner(
chainConfig,
head.Number,
head.Time,
)
opts := &txpool.ValidationOptions{
Config: chainConfig,
Accept: 0 |
1<<types.LegacyTxType |
1<<types.AccessListTxType |
1<<types.DynamicFeeTxType |
1<<types.BlobTxType,
MaxSize: models.TxMaxSize,
MinTip: new(big.Int),
}
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.

can we move this inside the ValidateConsensusRules

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Ideally, this is something that we should have cached somewhere, maybe in our implementation of TxPool.
So that we don't create all these objects for every request to eth_sendRawTransaction.

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.

Let's just have these defined as variables in the models where validate transaction is, not sure why would we need to have them defined in a function.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I have updated this in 4ad00ea .
We can't define them as variable in the top-level scope, as we need access to the Gateway config, for fetching the current EVM Networkd ID (chain ID).

Comment thread services/requester/requester.go Outdated
MinTip: new(big.Int),
}

if err := models.ValidateConsensusRules(tx, head, signer, opts); err != nil {
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.

can we call this inside the ValidateTransaction

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

We could add this logic to the existing ValidateTransaction method. I just introduced a different method, to be able to test it in isolation.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Updated in 4ad00ea

Comment thread models/transaction.go Outdated
return nil
}

func ValidateConsensusRules(
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.

why is defining this here needed? can't this just be called in the existing ValidateTransaction

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I can merge the two functions. I just wanted to keep them apart, so that they can be composed in other parts of the code-base. But maybe that was a premature thinking 👍

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Function removed in cb82dc6


}

func TestValidateConsensusRules(t *testing.T) {
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.

this feels to me like it's testing txpool.ValidateTransaction which is test in go-ethereum so I don't feel like we should add tests here

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Unfortunately the test cases on go-ethereum were very little. And because that method is a rather black-box for us, I wanted to make sure that we validate transaction properly, without false positives / false negatives.

Copy link
Copy Markdown
Contributor

@devbugging devbugging Jul 1, 2024

Choose a reason for hiding this comment

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

Sorry, I don't exactly understand, aren't all this test existing in go-ethereum already? is there any bellow you added yourself? If not, I don't understand why would we want to have them here.

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.

I feel the same, I think this test for Ethereum, we don't need to test this logic.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@sideninja @ramtinms These tests do not exist in the go-ethereum repository.
The rationale behind them is not to test the logic of txpool.ValidateTransaction(tx, head, signer, opts) .
I have added these tests mainly as a safeguard, against future upgrades of the go-ethereum package.
And the reason is that this method accepts several parameters.
Suppose that the method is updated and makes use of extra fields from the head or opts arguments.
Without the tests, any such changes will go unnoticed, and might result in crashes on production.

head := &types.Header{
	Number:   big.NewInt(20_182_324),
	Time:     uint64(time.Now().Unix()),
	GasLimit: 30_000_000,
}

For example, right now we only populate the above fields for the head argument.

@m-Peter m-Peter force-pushed the consensus-rules-tx-validation branch from 0e712e1 to afa0980 Compare July 1, 2024 12:31
coderabbitai[bot]

This comment was marked as outdated.

Comment thread services/requester/requester.go Outdated
Comment on lines +174 to +175
chainConfig := emulator.DefaultChainConfig
chainConfig.ChainID = e.config.EVMNetworkID
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.

maybe just rewrite this to this way
emulator.NewConfig(WithChainID(chainID))

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Good idea 👌
Updated in 73261f7

@m-Peter m-Peter force-pushed the consensus-rules-tx-validation branch from afa0980 to 592a7d9 Compare July 2, 2024 06:18
coderabbitai[bot]

This comment was marked as outdated.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 592a7d9 and 73261f7.

Files selected for processing (1)
  • services/requester/requester.go (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • services/requester/requester.go

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 73261f7 and 4ad00ea.

Files selected for processing (3)
  • models/transaction.go (3 hunks)
  • models/transaction_test.go (3 hunks)
  • services/requester/requester.go (4 hunks)
Files not reviewed due to errors (1)
  • models/transaction_test.go (no review received)
Files skipped from review as they are similar to previous changes (1)
  • models/transaction.go
Additional comments not posted (1)
services/requester/requester.go (1)

194-194: Ensure error handling for validation.

The validation logic is a good addition. Ensure that the error messages provide sufficient context for debugging.

Comment on lines +135 to +155
head := &types.Header{
Number: big.NewInt(20_182_324),
Time: uint64(time.Now().Unix()),
GasLimit: 30_000_000,
}
emulatorConfig := emulator.NewConfig(
emulator.WithChainID(config.EVMNetworkID),
emulator.WithBlockNumber(head.Number),
emulator.WithBlockTime(head.Time),
)
evmSigner := emulator.GetSigner(emulatorConfig)
validationOptions := &txpool.ValidationOptions{
Config: emulatorConfig.ChainConfig,
Accept: 0 |
1<<types.LegacyTxType |
1<<types.AccessListTxType |
1<<types.DynamicFeeTxType |
1<<types.BlobTxType,
MaxSize: models.TxMaxSize,
MinTip: new(big.Int),
}
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.

Ensure head values are configurable.

The head values such as Number, Time, and GasLimit are hard-coded. Consider making these configurable to enhance flexibility and testing.

head := &types.Header{
    Number:   big.NewInt(config.BlockNumber),
    Time:     uint64(time.Now().Unix()),
    GasLimit: config.GasLimit,
}

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 4ad00ea and cb82dc6.

Files selected for processing (2)
  • models/transaction.go (3 hunks)
  • models/transaction_test.go (3 hunks)
Files skipped from review as they are similar to previous changes (2)
  • models/transaction.go
  • models/transaction_test.go

@m-Peter m-Peter requested a review from devbugging July 2, 2024 08:46
@m-Peter m-Peter requested a review from ramtinms July 2, 2024 08:46
Comment on lines +135 to +155
head := &types.Header{
Number: big.NewInt(20_182_324),
Time: uint64(time.Now().Unix()),
GasLimit: 30_000_000,
}
emulatorConfig := emulator.NewConfig(
emulator.WithChainID(config.EVMNetworkID),
emulator.WithBlockNumber(head.Number),
emulator.WithBlockTime(head.Time),
)
evmSigner := emulator.GetSigner(emulatorConfig)
validationOptions := &txpool.ValidationOptions{
Config: emulatorConfig.ChainConfig,
Accept: 0 |
1<<types.LegacyTxType |
1<<types.AccessListTxType |
1<<types.DynamicFeeTxType |
1<<types.BlobTxType,
MaxSize: models.TxMaxSize,
MinTip: new(big.Int),
}
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.

why do we want to have these config here in the requester? I think it would be better if we just place it in the models/transaction with the ValidateTransaction function on the top as a variable

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Because we need access to config.EVMNetworkID. This means that even if we pass the config as a parameter, we will have to construct all these objects every time we call the ValidateTransaction function.

)
evmSigner := emulator.GetSigner(emulatorConfig)
validationOptions := &txpool.ValidationOptions{
Config: emulatorConfig.ChainConfig,
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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It was suggested by Ramtin (#323 (comment)), to construct a new config, with the desired options applied. This also facilitates fetching the signer, based on the created config. Note that we do not need the default config for all networks, as the default config is hardcoded to PreviewNet.

@m-Peter m-Peter force-pushed the consensus-rules-tx-validation branch from cb82dc6 to d5906af Compare July 2, 2024 16:59
coderabbitai[bot]

This comment was marked as outdated.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between d5906af and cf8bc7d.

Files selected for processing (2)
  • models/transaction.go (3 hunks)
  • models/transaction_test.go (3 hunks)
Files skipped from review as they are similar to previous changes (2)
  • models/transaction.go
  • models/transaction_test.go

@m-Peter m-Peter merged commit 5127995 into main Jul 3, 2024
@m-Peter m-Peter deleted the consensus-rules-tx-validation branch July 3, 2024 15:24
@coderabbitai coderabbitai Bot mentioned this pull request Dec 18, 2024
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

Validation of transactions submitted

3 participants