Commit 1be5226f authored by cby3149's avatar cby3149

Support external client in tests

parent b245c0ac
...@@ -46,13 +46,14 @@ func (eec *ExternalEthClient) WSAuthEndpoint() string { ...@@ -46,13 +46,14 @@ func (eec *ExternalEthClient) WSAuthEndpoint() string {
return eec.Endpoints.WSAuthEndpoint return eec.Endpoints.WSAuthEndpoint
} }
func (eec *ExternalEthClient) Close() { func (eec *ExternalEthClient) Close() error {
eec.Session.Terminate() eec.Session.Terminate()
select { select {
case <-time.After(5 * time.Second): case <-time.After(5 * time.Second):
eec.Session.Kill() eec.Session.Kill()
case <-eec.Session.Exited: case <-eec.Session.Exited:
} }
return nil
} }
func (er *ExternalRunner) Run(t *testing.T) *ExternalEthClient { func (er *ExternalRunner) Run(t *testing.T) *ExternalEthClient {
......
...@@ -39,7 +39,7 @@ func TestShim(t *testing.T) { ...@@ -39,7 +39,7 @@ func TestShim(t *testing.T) {
Name: "TestShim", Name: "TestShim",
BinPath: shimPath, BinPath: shimPath,
}).Run(t) }).Run(t)
t.Cleanup(ec.Close) t.Cleanup(func() { _ = ec.Close })
for _, endpoint := range []string{ for _, endpoint := range []string{
ec.HTTPEndpoint(), ec.HTTPEndpoint(),
......
...@@ -38,9 +38,10 @@ var ( ...@@ -38,9 +38,10 @@ var (
// OpGeth is an actor that functions as a l2 op-geth node // OpGeth is an actor that functions as a l2 op-geth node
// It provides useful functions for advancing and querying the chain // It provides useful functions for advancing and querying the chain
type OpGeth struct { type OpGeth struct {
node *gn.Node node EthInstance
l2Engine *sources.EngineClient l2Engine *sources.EngineClient
L2Client *ethclient.Client L2Client *ethclient.Client
L2RpcClient *rpc.Client
SystemConfig eth.SystemConfig SystemConfig eth.SystemConfig
L1ChainConfig *params.ChainConfig L1ChainConfig *params.ChainConfig
L2ChainConfig *params.ChainConfig L2ChainConfig *params.ChainConfig
...@@ -73,9 +74,21 @@ func NewOpGeth(t *testing.T, ctx context.Context, cfg *SystemConfig) (*OpGeth, e ...@@ -73,9 +74,21 @@ func NewOpGeth(t *testing.T, ctx context.Context, cfg *SystemConfig) (*OpGeth, e
SystemConfig: e2eutils.SystemConfigFromDeployConfig(cfg.DeployConfig), SystemConfig: e2eutils.SystemConfigFromDeployConfig(cfg.DeployConfig),
} }
node, _, err := geth.InitL2("l2", big.NewInt(int64(cfg.DeployConfig.L2ChainID)), l2Genesis, cfg.JWTFilePath) var node EthInstance
require.Nil(t, err) if cfg.ExternalL2Shim == "" {
require.Nil(t, node.Start()) gethNode, _, err := geth.InitL2("l2", big.NewInt(int64(cfg.DeployConfig.L2ChainID)), l2Genesis, cfg.JWTFilePath)
require.Nil(t, err)
require.Nil(t, gethNode.Start())
node = gethNode
} else {
externalNode := (&ExternalRunner{
Name: "Sequencer",
BinPath: cfg.ExternalL2Shim,
Genesis: l2Genesis,
JWTPath: cfg.JWTFilePath,
}).Run(t)
node = externalNode
}
auth := rpc.WithHTTPAuth(gn.NewJWTAuth(cfg.JWTSecret)) auth := rpc.WithHTTPAuth(gn.NewJWTAuth(cfg.JWTSecret))
l2Node, err := client.NewRPC(ctx, logger, node.WSAuthEndpoint(), client.WithGethRPCOptions(auth)) l2Node, err := client.NewRPC(ctx, logger, node.WSAuthEndpoint(), client.WithGethRPCOptions(auth))
...@@ -93,12 +106,16 @@ func NewOpGeth(t *testing.T, ctx context.Context, cfg *SystemConfig) (*OpGeth, e ...@@ -93,12 +106,16 @@ func NewOpGeth(t *testing.T, ctx context.Context, cfg *SystemConfig) (*OpGeth, e
l2Client, err := ethclient.Dial(node.HTTPEndpoint()) l2Client, err := ethclient.Dial(node.HTTPEndpoint())
require.Nil(t, err) require.Nil(t, err)
l2RpcClient, err := rpc.Dial(node.HTTPEndpoint())
require.Nil(t, err)
genesisPayload, err := eth.BlockAsPayload(l2GenesisBlock) genesisPayload, err := eth.BlockAsPayload(l2GenesisBlock)
require.Nil(t, err) require.Nil(t, err)
return &OpGeth{ return &OpGeth{
node: node, node: node,
L2Client: l2Client, L2Client: l2Client,
L2RpcClient: l2RpcClient,
l2Engine: l2Engine, l2Engine: l2Engine,
SystemConfig: rollupGenesis.SystemConfig, SystemConfig: rollupGenesis.SystemConfig,
L1ChainConfig: l1Genesis.Config, L1ChainConfig: l1Genesis.Config,
...@@ -112,6 +129,7 @@ func (d *OpGeth) Close() { ...@@ -112,6 +129,7 @@ func (d *OpGeth) Close() {
_ = d.node.Close() _ = d.node.Close()
d.l2Engine.Close() d.l2Engine.Close()
d.L2Client.Close() d.L2Client.Close()
d.L2RpcClient.Close()
} }
// AddL2Block Appends a new L2 block to the current chain including the specified transactions // AddL2Block Appends a new L2 block to the current chain including the specified transactions
......
...@@ -38,6 +38,7 @@ func TestMissingGasLimit(t *testing.T) { ...@@ -38,6 +38,7 @@ func TestMissingGasLimit(t *testing.T) {
attrs.GasLimit = nil attrs.GasLimit = nil
res, err := opGeth.StartBlockBuilding(ctx, attrs) res, err := opGeth.StartBlockBuilding(ctx, attrs)
require.Error(t, err)
require.ErrorIs(t, err, eth.InputError{}) require.ErrorIs(t, err, eth.InputError{})
require.Equal(t, eth.InvalidPayloadAttributes, err.(eth.InputError).Code) require.Equal(t, eth.InvalidPayloadAttributes, err.(eth.InputError).Code)
require.Nil(t, res) require.Nil(t, res)
...@@ -157,16 +158,12 @@ func TestGethOnlyPendingBlockIsLatest(t *testing.T) { ...@@ -157,16 +158,12 @@ func TestGethOnlyPendingBlockIsLatest(t *testing.T) {
require.NoError(t, opGeth.L2Client.SendTransaction(ctx, tx), "send tx to make pending work different") require.NoError(t, opGeth.L2Client.SendTransaction(ctx, tx), "send tx to make pending work different")
checkPending("prepared", 0) checkPending("prepared", 0)
rpcClient, err := opGeth.node.Attach()
require.NoError(t, err)
defer rpcClient.Close()
// Wait for tx to be in tx-pool, for it to be picked up in block building // Wait for tx to be in tx-pool, for it to be picked up in block building
var txPoolStatus struct { var txPoolStatus struct {
Pending hexutil.Uint64 `json:"pending"` Pending hexutil.Uint64 `json:"pending"`
} }
for i := 0; i < 5; i++ { for i := 0; i < 5; i++ {
require.NoError(t, rpcClient.CallContext(ctx, &txPoolStatus, "txpool_status")) require.NoError(t, opGeth.L2RpcClient.CallContext(ctx, &txPoolStatus, "txpool_status"))
if txPoolStatus.Pending == 0 { if txPoolStatus.Pending == 0 {
time.Sleep(time.Second) time.Sleep(time.Second)
} else { } else {
......
...@@ -224,8 +224,8 @@ func (gi *GethInstance) HTTPAuthEndpoint() string { ...@@ -224,8 +224,8 @@ func (gi *GethInstance) HTTPAuthEndpoint() string {
return gi.Node.HTTPAuthEndpoint() return gi.Node.HTTPAuthEndpoint()
} }
func (gi *GethInstance) Close() { func (gi *GethInstance) Close() error {
gi.Node.Close() return gi.Node.Close()
} }
// EthInstance is either an in process Geth or external process exposing its // EthInstance is either an in process Geth or external process exposing its
...@@ -235,7 +235,7 @@ type EthInstance interface { ...@@ -235,7 +235,7 @@ type EthInstance interface {
WSEndpoint() string WSEndpoint() string
HTTPAuthEndpoint() string HTTPAuthEndpoint() string
WSAuthEndpoint() string WSAuthEndpoint() string
Close() Close() error
} }
type System struct { type System struct {
......
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