Commit 96ac4c23 authored by Adrian Sutton's avatar Adrian Sutton

op-e2e: Extract out helper methods for connecting peers.

parent c3dd839f
package geth
import (
"context"
"testing"
"time"
"github.com/ethereum-optimism/optimism/op-e2e/e2eutils/wait"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/eth/ethconfig"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p"
"github.com/stretchr/testify/require"
)
// ConnectP2P creates a p2p peer connection between node1 and node2.
func ConnectP2P(t *testing.T, node1 *ethclient.Client, node2 *ethclient.Client) {
var targetInfo p2p.NodeInfo
require.NoError(t, node2.Client().Call(&targetInfo, "admin_nodeInfo"), "get node info")
var peerAdded bool
require.NoError(t, node1.Client().Call(&peerAdded, "admin_addPeer", targetInfo.Enode), "add peer")
require.True(t, peerAdded, "should have added peer successfully")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
err := wait.For(ctx, time.Second, func() (bool, error) {
var peerCount hexutil.Uint64
if err := node1.Client().Call(&peerCount, "net_peerCount"); err != nil {
return false, err
}
t.Logf("Peer count %v", uint64(peerCount))
return peerCount >= hexutil.Uint64(1), nil
})
require.NoError(t, err, "wait for a peer to be connected")
}
func WithP2P() func(ethCfg *ethconfig.Config, nodeCfg *node.Config) error {
return func(ethCfg *ethconfig.Config, nodeCfg *node.Config) error {
ethCfg.RollupDisableTxPoolGossip = false
nodeCfg.P2P = p2p.Config{
NoDiscovery: true,
ListenAddr: "127.0.0.1:0",
MaxPeers: 10,
}
return nil
}
}
package op_e2e package op_e2e
import ( import (
"context"
"testing" "testing"
"time"
"github.com/ethereum-optimism/optimism/op-e2e/e2eutils/wait" "github.com/ethereum-optimism/optimism/op-e2e/e2eutils/geth"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/eth/ethconfig"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
...@@ -18,15 +12,7 @@ func TestTxGossip(t *testing.T) { ...@@ -18,15 +12,7 @@ func TestTxGossip(t *testing.T) {
InitParallel(t) InitParallel(t)
cfg := DefaultSystemConfig(t) cfg := DefaultSystemConfig(t)
gethOpts := []GethOption{ gethOpts := []GethOption{
func(ethCfg *ethconfig.Config, nodeCfg *node.Config) error { geth.WithP2P(),
ethCfg.RollupDisableTxPoolGossip = false
nodeCfg.P2P = p2p.Config{
NoDiscovery: true,
ListenAddr: "127.0.0.1:0",
MaxPeers: 10,
}
return nil
},
} }
cfg.GethOptions["sequencer"] = gethOpts cfg.GethOptions["sequencer"] = gethOpts
cfg.GethOptions["verifier"] = gethOpts cfg.GethOptions["verifier"] = gethOpts
...@@ -35,26 +21,7 @@ func TestTxGossip(t *testing.T) { ...@@ -35,26 +21,7 @@ func TestTxGossip(t *testing.T) {
seqClient := sys.Clients["sequencer"] seqClient := sys.Clients["sequencer"]
verifClient := sys.Clients["verifier"] verifClient := sys.Clients["verifier"]
var seqInfo p2p.NodeInfo geth.ConnectP2P(t, seqClient, verifClient)
require.NoError(t, seqClient.Client().Call(&seqInfo, "admin_nodeInfo"), "get sequencer node info")
var verifInfo p2p.NodeInfo
require.NoError(t, verifClient.Client().Call(&verifInfo, "admin_nodeInfo"), "get verifier node info")
var peerAdded bool
require.NoError(t, verifClient.Client().Call(&peerAdded, "admin_addPeer", seqInfo.Enode), "add peer to verifier")
require.True(t, peerAdded, "should have added peer to verifier successfully")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
err = wait.For(ctx, time.Second, func() (bool, error) {
var peerCount hexutil.Uint64
if err := verifClient.Client().Call(&peerCount, "net_peerCount"); err != nil {
return false, err
}
t.Logf("Peer count %v", uint64(peerCount))
return peerCount >= hexutil.Uint64(1), nil
})
require.NoError(t, err, "wait for a peer to be connected")
// Send a transaction to the verifier and it should be gossiped to the sequencer and included in a block. // Send a transaction to the verifier and it should be gossiped to the sequencer and included in a block.
SendL2Tx(t, cfg, verifClient, cfg.Secrets.Alice, func(opts *TxOpts) { SendL2Tx(t, cfg, verifClient, cfg.Secrets.Alice, func(opts *TxOpts) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment