Skip to content

Commit

Permalink
Fix e2e group 8 (debug trace) (0xPolygonHermez#2761)
Browse files Browse the repository at this point in the history
* fix problem if L1 block have 2 transactions
  • Loading branch information
joanestebanr authored Nov 10, 2023
1 parent 34d2274 commit 2c08b3c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 54 deletions.
120 changes: 68 additions & 52 deletions test/e2e/debug_calltracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import (
"github.com/stretchr/testify/require"
)

const (
dockersArePreLaunchedForCallTracerTests = false
)

func TestDebugTraceTransactionCallTracer(t *testing.T) {
if testing.Short() {
t.Skip()
Expand All @@ -30,23 +34,27 @@ func TestDebugTraceTransactionCallTracer(t *testing.T) {
const l2ExplorerRPCComponentName = "l2-explorer-json-rpc"

var err error
err = operations.Teardown()
require.NoError(t, err)

defer func() {
require.NoError(t, operations.Teardown())
require.NoError(t, operations.StopComponent(l2ExplorerRPCComponentName))
}()
if !dockersArePreLaunchedForCallTracerTests {
err = operations.Teardown()
require.NoError(t, err)
defer func() {
require.NoError(t, operations.Teardown())
require.NoError(t, operations.StopComponent(l2ExplorerRPCComponentName))
}()
}

ctx := context.Background()
opsCfg := operations.GetDefaultOperationsConfig()
opsMan, err := operations.NewManager(ctx, opsCfg)
require.NoError(t, err)
err = opsMan.Setup()
require.NoError(t, err)

err = operations.StartComponent(l2ExplorerRPCComponentName, func() (bool, error) { return operations.NodeUpCondition(l2NetworkURL) })
require.NoError(t, err)
if !dockersArePreLaunchedForCallTracerTests {
opsMan, err := operations.NewManager(ctx, opsCfg)
require.NoError(t, err)
err = opsMan.Setup()
require.NoError(t, err)
err = operations.StartComponent(l2ExplorerRPCComponentName, func() (bool, error) { return operations.NodeUpCondition(l2NetworkURL) })
require.NoError(t, err)
} else {
log.Info("Using pre-launched dockers: no reset Database")
}

const l1NetworkName, l2NetworkName = "Local L1", "Local L2"

Expand Down Expand Up @@ -277,23 +285,27 @@ func TestDebugTraceBlockCallTracer(t *testing.T) {
const l2ExplorerRPCComponentName = "l2-explorer-json-rpc"

var err error
err = operations.Teardown()
require.NoError(t, err)

defer func() {
require.NoError(t, operations.Teardown())
require.NoError(t, operations.StopComponent(l2ExplorerRPCComponentName))
}()
if !dockersArePreLaunchedForCallTracerTests {
err = operations.Teardown()
require.NoError(t, err)
defer func() {
require.NoError(t, operations.Teardown())
require.NoError(t, operations.StopComponent(l2ExplorerRPCComponentName))
}()
}

ctx := context.Background()
opsCfg := operations.GetDefaultOperationsConfig()
opsMan, err := operations.NewManager(ctx, opsCfg)
require.NoError(t, err)
err = opsMan.Setup()
require.NoError(t, err)

err = operations.StartComponent(l2ExplorerRPCComponentName, func() (bool, error) { return operations.NodeUpCondition(l2NetworkURL) })
require.NoError(t, err)
if !dockersArePreLaunchedForCallTracerTests {
opsMan, err := operations.NewManager(ctx, opsCfg)
require.NoError(t, err)
err = opsMan.Setup()
require.NoError(t, err)
err = operations.StartComponent(l2ExplorerRPCComponentName, func() (bool, error) { return operations.NodeUpCondition(l2NetworkURL) })
require.NoError(t, err)
} else {
log.Info("Using pre-launched dockers: no reset Database")
}

const l1NetworkName, l2NetworkName = "Local L1", "Local L2"

Expand All @@ -317,8 +329,9 @@ func TestDebugTraceBlockCallTracer(t *testing.T) {
PrivateKey: operations.DefaultSequencerPrivateKey,
},
}

results := map[string]json.RawMessage{}
require.Equal(t, len(networks), 2, "only support 2 networks!")
//var results map[string]map[string]interface{}
results := map[string]map[string]interface{}{}

type testCase struct {
name string
Expand Down Expand Up @@ -355,6 +368,7 @@ func TestDebugTraceBlockCallTracer(t *testing.T) {
log.Debug("************************ ", tc.name, " ************************")

for _, network := range networks {
debugID := fmt.Sprintf("[%s/%s]", tc.name, network.Name)
log.Debug("------------------------ ", network.Name, " ------------------------")
ethereumClient := operations.MustGetClient(network.URL)
auth := operations.MustGetAuth(network.PrivateKey, network.ChainID)
Expand All @@ -371,7 +385,7 @@ func TestDebugTraceBlockCallTracer(t *testing.T) {
err = ethereumClient.SendTransaction(ctx, signedTx)
require.NoError(t, err)

log.Debugf("tx sent: %v", signedTx.Hash().String())
log.Debugf("%s tx sent: %v", debugID, signedTx.Hash().String())

err = operations.WaitTxToBeMined(ctx, ethereumClient, signedTx, operations.DefaultTimeoutTxToBeMined)
if err != nil && !strings.HasPrefix(err.Error(), "transaction has failed, reason:") {
Expand All @@ -391,37 +405,39 @@ func TestDebugTraceBlockCallTracer(t *testing.T) {

var response types.Response
if tc.blockNumberOrHash == "number" {
log.Infof("%s debug_traceBlockByNumber %v", debugID, receipt.BlockNumber)
response, err = client.JSONRPCCall(network.URL, "debug_traceBlockByNumber", hex.EncodeBig(receipt.BlockNumber), debugOptions)
} else {
log.Infof("%s debug_traceBlockByHash %v", debugID, receipt.BlockHash.String())
response, err = client.JSONRPCCall(network.URL, "debug_traceBlockByHash", receipt.BlockHash.String(), debugOptions)
}
require.NoError(t, err)
require.Nil(t, response.Error)
require.NotNil(t, response.Result)
log.Debugf("%s response:%s", debugID, string(response.Result))

results[network.Name] = response.Result
txHash := signedTx.Hash().String()
resultForTx := findTxInResponse(t, response.Result, txHash, debugID)
results[network.Name] = resultForTx
}

referenceTransactions := []interface{}{}
err = json.Unmarshal(results[l1NetworkName], &referenceTransactions)
require.NoError(t, err)

for networkName, result := range results {
if networkName == l1NetworkName {
continue
}

resultTransactions := []interface{}{}
err = json.Unmarshal(result, &resultTransactions)
require.NoError(t, err)

for transactionIndex := range referenceTransactions {
referenceTransactionMap := referenceTransactions[transactionIndex].(map[string]interface{})
resultTransactionMap := resultTransactions[transactionIndex].(map[string]interface{})

compareCallFrame(t, referenceTransactionMap, resultTransactionMap, networkName)
}
}
referenceTransactions := results[l1NetworkName]
resultTransactions := results[l2NetworkName]
compareCallFrame(t, referenceTransactions, resultTransactions, l2NetworkName)
})
}
}

func findTxInResponse(t *testing.T, response json.RawMessage, txHash string, debugPrefix string) map[string]interface{} {
valueMap := []interface{}{}
err := json.Unmarshal(response, &valueMap)
require.NoError(t, err)
log.Infof("%s Reponse Length: %d", debugPrefix, len(valueMap))
for transactionIndex := range valueMap {
if valueMap[transactionIndex].(map[string]interface{})["txHash"] == txHash {
return valueMap[transactionIndex].(map[string]interface{})
}
}
log.Infof("%s Transaction not found: %s, returning first index", debugPrefix, txHash)
return valueMap[0].(map[string]interface{})
}
5 changes: 3 additions & 2 deletions test/e2e/debug_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"

"github.com/0xPolygonHermez/zkevm-node/hex"
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/test/contracts/bin/BridgeA"
"github.com/0xPolygonHermez/zkevm-node/test/contracts/bin/BridgeB"
"github.com/0xPolygonHermez/zkevm-node/test/contracts/bin/BridgeC"
Expand Down Expand Up @@ -111,13 +112,13 @@ func createScCallSignedTx(t *testing.T, ctx context.Context, auth *bind.Transact
func prepareERC20Transfer(t *testing.T, ctx context.Context, auth *bind.TransactOpts, client *ethclient.Client) (map[string]interface{}, error) {
_, tx, sc, err := ERC20.DeployERC20(auth, client, "MyToken", "MT")
require.NoError(t, err)

log.Debugf("prepareERC20Transfer DeployERC20 tx: %s", tx.Hash().String())
err = operations.WaitTxToBeMined(ctx, client, tx, operations.DefaultTimeoutTxToBeMined)
require.NoError(t, err)

tx, err = sc.Mint(auth, big.NewInt(1000000000))
require.NoError(t, err)

log.Debugf("prepareERC20Transfer Mint tx: %s", tx.Hash().String())
err = operations.WaitTxToBeMined(ctx, client, tx, operations.DefaultTimeoutTxToBeMined)
require.NoError(t, err)

Expand Down

0 comments on commit 2c08b3c

Please sign in to comment.