Skip to content

Commit

Permalink
NewPayload Tests Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
marioevz committed Sep 11, 2023
1 parent 5365d3d commit 7fc06f5
Show file tree
Hide file tree
Showing 6 changed files with 669 additions and 392 deletions.
2 changes: 1 addition & 1 deletion simulators/ethereum/engine/helper/customizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ func GenerateInvalidPayload(basePayload *typ.ExecutableData, payloadField Invali
customTxData.ChainID = customChainID
}

modifiedTx, err := customizeTransaction(&baseTx, globals.TestAccounts[0], &customTxData)
modifiedTx, err := CustomizeTransaction(&baseTx, globals.TestAccounts[0], &customTxData)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion simulators/ethereum/engine/helper/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type CustomTransactionData struct {
Signature *SignatureValues
}

func customizeTransaction(baseTransaction *types.Transaction, sender SenderAccount, customData *CustomTransactionData) (*types.Transaction, error) {
func CustomizeTransaction(baseTransaction *types.Transaction, sender SenderAccount, customData *CustomTransactionData) (*types.Transaction, error) {
// Create a modified transaction base, from the base transaction and customData mix
var (
modifiedTxData types.TxData
Expand Down
7 changes: 7 additions & 0 deletions simulators/ethereum/engine/suites/cancun/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -1853,6 +1853,13 @@ func init() {
}
}

// Invalid Transaction ChainID Tests
Tests = append(Tests,
suite_engine.InvalidTxChainIDTest{
BaseSpec: onlyBlobTxsSpec,
},
)

Tests = append(Tests, suite_engine.PayloadBuildAfterInvalidPayloadTest{
BaseSpec: onlyBlobTxsSpec,
InvalidField: helper.InvalidParentBeaconBlockRoot,
Expand Down
85 changes: 85 additions & 0 deletions simulators/ethereum/engine/suites/engine/invalid_payload.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package suite_engine

import (
"context"
"encoding/json"
"fmt"
"math/big"
"time"

api "github.com/ethereum/go-ethereum/beacon/engine"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/hive/simulators/ethereum/engine/client/hive_rpc"
"github.com/ethereum/hive/simulators/ethereum/engine/clmock"
"github.com/ethereum/hive/simulators/ethereum/engine/config"
Expand Down Expand Up @@ -363,3 +367,84 @@ func (tc PayloadBuildAfterInvalidPayloadTest) Execute(t *test.Env) {
},
})
}

type InvalidTxChainIDTest struct {
test.BaseSpec
}

func (s InvalidTxChainIDTest) WithMainFork(fork config.Fork) test.Spec {
specCopy := s
specCopy.MainFork = fork
return specCopy
}

func (s InvalidTxChainIDTest) GetName() string {
name := fmt.Sprintf("Build Payload with Invalid ChainID Transaction (%s)", s.TestTransactionType)
return name
}

// Attempt to produce a payload after a transaction with an invalid Chain ID was sent to the client
// using `eth_sendRawTransaction`.
func (spec InvalidTxChainIDTest) Execute(t *test.Env) {
// Wait until TTD is reached by this client
t.CLMock.WaitForTTD()

// Produce blocks before starting the test
t.CLMock.ProduceBlocks(5, clmock.BlockProcessCallbacks{})

// Send a transaction with an incorrect ChainID.
// Transaction must be not be included in payload creation.
var invalidChainIDTx *types.Transaction
t.CLMock.ProduceSingleBlock(clmock.BlockProcessCallbacks{
// Run test after a new payload has been broadcast
OnPayloadAttributesGenerated: func() {
txCreator := helper.BaseTransactionCreator{
Recipient: &globals.PrevRandaoContractAddr,
Amount: big1,
Payload: nil,
TxType: t.TestTransactionType,
GasLimit: 75000,
ForkConfig: t.ForkConfig,
}
sender := globals.TestAccounts[0]

ctx, cancel := context.WithTimeout(t.TestContext, globals.RPCTimeout)
defer cancel()
nonce, err := t.CLMock.NextBlockProducer.NonceAt(ctx, sender.GetAddress(), nil)
if err != nil {
t.Fatalf("FAIL(%s): Unable to get address nonce: %v", t.TestName, err)
}

tx, err := txCreator.MakeTransaction(sender, nonce, t.CLMock.LatestPayloadAttributes.Timestamp)
if err != nil {
t.Fatalf("FAIL(%s): Unable to create transaction: %v", t.TestName, err)
}

txCast, ok := tx.(*types.Transaction)
if !ok {
t.Fatalf("FAIL(%s): Unable to cast transaction to types.Transaction", t.TestName)
}

txCustomizerData := &helper.CustomTransactionData{
ChainID: new(big.Int).Add(globals.ChainID, big1),
}
invalidChainIDTx, err = helper.CustomizeTransaction(txCast, sender, txCustomizerData)
if err != nil {
t.Fatalf("FAIL(%s): Unable to customize transaction: %v", t.TestName, err)
}

ctx, cancel = context.WithTimeout(t.TestContext, globals.RPCTimeout)
defer cancel()
err = t.Engine.SendTransaction(ctx, invalidChainIDTx)
if err != nil {
t.Logf("INFO (%s): Error on sending transaction with incorrect chain ID (Expected): %v", t.TestName, err)
}
},
})

// Verify that the latest payload built does NOT contain the invalid chain Tx
if helper.TransactionInPayload(&t.CLMock.LatestPayloadBuilt, invalidChainIDTx) {
p, _ := json.MarshalIndent(t.CLMock.LatestPayloadBuilt, "", " ")
t.Fatalf("FAIL (%s): Invalid chain ID tx was included in payload: %s", t.TestName, p)
}
}
Loading

0 comments on commit 7fc06f5

Please sign in to comment.