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

refactor: break up transformer and rpc package logic into smaller func and add unit tests #1313

Open
wants to merge 51 commits into
base: staging
Choose a base branch
from

Conversation

Monika-Bitfly
Copy link

Changes:

  • Refactored transformer.go functions in the /executionlayer package by breaking them into smaller, more readable functions
  • Simplified erigon.go and geth.go functions in the /rpc package by splitting them into smaller functions and introduced utils.go to share common functionality between them
  • Added unit tests utils_test.go and erigon_test.go in the /rpc package
  • Added unit tests transformer_test.go in the /executionlayer package

Tangui-Bitfly and others added 30 commits January 30, 2025 15:45
@Monika-Bitfly Monika-Bitfly marked this pull request as ready for review February 5, 2025 18:55
Copy link
Contributor

@Tangui-Bitfly Tangui-Bitfly left a comment

Choose a reason for hiding this comment

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

There is a lot of good things in that pr but I'm wondering if some changes go maybe too far (too many little functions, too many tests that test not exported func without direct link to usage, ...) and will be a burden in the long run.

@@ -207,6 +207,16 @@ func (b *BlockchainBackend) DeployContract(t *testing.T, contractData []byte) co
return receipt.ContractAddress
}

func (b *BlockchainBackend) NewERC20Metadata(t *testing.T, address common.Address) (*contracts.IERC20Metadata, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't really see the point of this function, it's just a wrapper around contracts.NewIERC20Metadata but without adding anything.

uncles := getBlockUncles(block.Uncles())
baseFee := getBaseFee(block)
blobGasUsed := getBlobGasUsed(block)
excessBlobGas := getExcessBlobGas(block)
Copy link
Contributor

Choose a reason for hiding this comment

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

those can be moved when we create types.Eth1Block like

&types.Eth1Block{
	...
	BaseFee: getBaseFee(block),
	...
}

to := getReceiver(tx)
maxFeePerBlobGas := getMaxFeePerBlobGas(tx)
blobVersionedHashes := getBlobVersionedHashes(tx)
blobGasPrice := getBlobGasPrice(receipt)
Copy link
Contributor

Choose a reason for hiding this comment

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

same, inline when possible


// TestGetInternalTxs tests the getInternalTxs function
// which returns the internal transactions of a transaction at a given position
func TestGetInternalTxs(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

do we really want those comments on top of each tests ? this doesn't bring anything new IMO


// TestGetMaxFeePerBlobGas tests the getMaxFeePerBlobGas function
// which extracts the max fee per blob gas from a transaction
func TestGetMaxFeePerBlobGas(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really want to test each individual function like that ? Too much tests is like too much codes, harder to maintain, more complex and prone to brake.
I mean what this test is really testing ? Following this logic we could do a test for each field of a block. IMO it's better to create one test where we test each field all together.

if err != nil && tt.expectedErr == nil {
t.Fatalf("unexpected error: %v", err)
}
if err == nil && tt.expectedErr != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

same

if err != nil && tt.expectedErr == nil {
t.Fatalf("unexpected error: %v", err)
}
if err == nil && tt.expectedErr != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

same

return v
}

func updateITxStatus(internalTx []*types.Eth1InternalTransaction, indexedTx *types.Eth1TransactionIndexed) {
Copy link
Contributor

Choose a reason for hiding this comment

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

minor suggestion but the signature would better if reversed and internalTx should be plural

func updateITxStatus(indexedTx *types.Eth1TransactionIndexed, internals []*types.Eth1InternalTransaction)

@@ -320,7 +269,8 @@ func TransformITx(chainID string, block *types.Eth1Block, res *IndexedBlock) err
var transactions []data.InternalWithIndexes
for i, tx := range block.GetTransactions() {
for j, itx := range tx.GetItx() {
if itx.Path == "0" || itx.Path == "[]" || bytes.Equal(itx.Value, []byte{0x0}) { // skip top level and empty calls
// skip top level and empty calls
Copy link
Contributor

Choose a reason for hiding this comment

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

this comment should be inside isValidItx func IMO

if (result == nil && tt.expected != nil) || (result != nil && tt.expected == nil) {
t.Errorf("got %v, want %v", result, tt.expected)
} else if result != nil && tt.expected != nil && result.Error() != tt.expected.Error() {
t.Errorf("got %v, want %v", result, tt.expected)
Copy link
Contributor

Choose a reason for hiding this comment

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

same

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