Skip to content

Commit

Permalink
feat: add automated integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Alok committed Jul 17, 2024
1 parent 65753f6 commit 7245668
Show file tree
Hide file tree
Showing 8 changed files with 692 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ use (
./oracle
./p2p
./x
./testing
)
202 changes: 202 additions & 0 deletions testing/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
package main

import (
"context"
"fmt"
"net"
"os"
"slices"
"strings"

"github.com/ethereum/go-ethereum/common"
"github.com/primev/mev-commit/testing/pkg/orchestrator"
"github.com/primev/mev-commit/testing/pkg/tests"
"github.com/primev/mev-commit/x/util"
"github.com/urfave/cli/v2"
)

var (
optionSettlementRPCEndpoint = &cli.StringFlag{
Name: "settlement-rpc-endpoint",
Usage: "Settlement RPC endpoint",
Value: "http://localhost:8545",
EnvVars: []string{"MEV_COMMIT_TEST_SETTLEMENT_RPC_ENDPOINT"},
}

optionProviderRegistryAddress = &cli.StringFlag{
Name: "provider-registry-address",
Usage: "Provider registry address",
EnvVars: []string{"MEV_COMMIT_TEST_PROVIDER_REGISTRY_ADDRESS"},
Action: func(c *cli.Context, address string) error {
if !common.IsHexAddress(address) {
return fmt.Errorf("invalid provider registry address")
}
return nil
},
}

optionBootnodeRPCAddresses = &cli.StringSliceFlag{
Name: "bootnode-rpc-addresses",
Usage: "Bootnode RPC addresses",
EnvVars: []string{"MEV_COMMIT_TEST_BOOTNODE_RPC_ADDRESSES"},
Action: func(c *cli.Context, addresses []string) error {
for _, address := range addresses {
if _, _, err := net.SplitHostPort(address); err != nil {
return fmt.Errorf("invalid bootnode RPC address")
}
}
return nil
},
}

optionProviderRPCAddresses = &cli.StringSliceFlag{
Name: "provider-rpc-addresses",
Usage: "Provider RPC addresses",
EnvVars: []string{"MEV_COMMIT_TEST_PROVIDER_RPC_ADDRESSES"},
Action: func(c *cli.Context, addresses []string) error {
for _, address := range addresses {
if _, _, err := net.SplitHostPort(address); err != nil {
return fmt.Errorf("invalid provider RPC address")
}
}
return nil
},
}

optionBidderRPCAddresses = &cli.StringSliceFlag{
Name: "bidder-rpc-addresses",
Usage: "Bidder RPC addresses",
EnvVars: []string{"MEV_COMMIT_TEST_BIDDER_RPC_ADDRESSES"},
Action: func(c *cli.Context, addresses []string) error {
for _, address := range addresses {
if _, _, err := net.SplitHostPort(address); err != nil {
return fmt.Errorf("invalid bidder RPC address")
}
}
return nil
},
}

optionLogFmt = &cli.StringFlag{
Name: "log-fmt",
Usage: "log format to use, options are 'text' or 'json'",
EnvVars: []string{"MEV_COMMIT_TEST_LOG_FMT"},
Value: "text",
Action: func(ctx *cli.Context, s string) error {
if !slices.Contains([]string{"text", "json"}, s) {
return fmt.Errorf("invalid log format")
}
return nil
},
}

optionLogLevel = &cli.StringFlag{
Name: "log-level",
Usage: "log level to use, options are 'debug', 'info', 'warn', 'error'",
EnvVars: []string{"MEV_COMMIT_TEST_LOG_LEVEL"},
Value: "info",
Action: func(ctx *cli.Context, s string) error {
if !slices.Contains([]string{"debug", "info", "warn", "error"}, s) {
return fmt.Errorf("invalid log level")
}
return nil
},
}

optionLogTags = &cli.StringFlag{
Name: "log-tags",
Usage: "log tags is a comma-separated list of <name:value> pairs that will be inserted into each log line",
EnvVars: []string{"MEV_COMMIT_TEST_LOG_TAGS"},
Action: func(ctx *cli.Context, s string) error {
for i, p := range strings.Split(s, ",") {
if len(strings.Split(p, ":")) != 2 {
return fmt.Errorf("invalid log-tags at index %d, expecting <name:value>", i)
}
}
return nil
},
}
)

func main() {
app := &cli.App{
Name: "mev-commit-test",
Usage: "MEV commit test",
Flags: []cli.Flag{
optionSettlementRPCEndpoint,
optionProviderRegistryAddress,
optionBootnodeRPCAddresses,
optionProviderRPCAddresses,
optionBidderRPCAddresses,
optionLogFmt,
optionLogLevel,
optionLogTags,
},
Commands: []*cli.Command{
{
Name: "run",
Usage: "Run MEV commit test",
Action: func(c *cli.Context) error {
return run(c)
},
},
},
}

if err := app.Run(os.Args); err != nil {
fmt.Println(err)
}
}

func run(c *cli.Context) error {
settlementRPCEndpoint := c.String(optionSettlementRPCEndpoint.Name)
providerRegistryAddress := c.String(optionProviderRegistryAddress.Name)
bootnodeRPCAddresses := c.StringSlice(optionBootnodeRPCAddresses.Name)
providerRPCAddresses := c.StringSlice(optionProviderRPCAddresses.Name)
bidderRPCAddresses := c.StringSlice(optionBidderRPCAddresses.Name)

fmt.Println("Settlement RPC endpoint:", settlementRPCEndpoint)
fmt.Println("Provider registry address:", providerRegistryAddress)
fmt.Println("Bootnode RPC addresses:", bootnodeRPCAddresses)
fmt.Println("Provider RPC addresses:", providerRPCAddresses)
fmt.Println("Bidder RPC addresses:", bidderRPCAddresses)

logger, err := util.NewLogger(
c.String(optionLogLevel.Name),
c.String(optionLogFmt.Name),
c.String(optionLogTags.Name),
c.App.Writer,
)
if err != nil {
return fmt.Errorf("failed to create logger: %w", err)
}

o, err := orchestrator.NewOrchestrator(orchestrator.Options{
SettlementRPCEndpoint: settlementRPCEndpoint,
ProviderRegistryAddress: common.HexToAddress(providerRegistryAddress),
BootnodeRPCAddresses: bootnodeRPCAddresses,
ProviderRPCAddresses: providerRPCAddresses,
BidderRPCAddresses: bidderRPCAddresses,
Logger: logger,
})

if err != nil {
return err
}

defer o.Close()

// Run test cases
for name, tc := range tests.TestCases {
logger.Info("running test case", "name", name)
if err := tc(context.Background(), o, nil); err != nil {
logger.Error("test case failed", "name", name, "error", err)
return fmt.Errorf("test case %s failed: %w", name, err)
}
logger.Info("test case passed", "name", name)
}

logger.Info("all test cases passed")

return nil
}
24 changes: 24 additions & 0 deletions testing/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module github.com/primev/mev-commit/testing

go 1.22

require (
github.com/primev/mev-commit/contracts-abi v0.0.1
github.com/primev/mev-commit/p2p v0.0.1
)

require (
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.32.0-20240221180331-f05a6f4403ce.1 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240318140521-94a12d6c2237 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240515191416-fc5f0ca64291 // indirect
google.golang.org/grpc v1.64.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
)

replace github.com/primev/mev-commit/p2p => ../p2p

replace github.com/primev/mev-commit/contracts-abi => ../contracts-abi
25 changes: 25 additions & 0 deletions testing/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.32.0-20240221180331-f05a6f4403ce.1 h1:AmmAwHbvaeOIxDKG2+aTn5C36HjmFIMkrdTp49rp80Q=
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.32.0-20240221180331-f05a6f4403ce.1/go.mod h1:tiTMKD8j6Pd/D2WzREoweufjzaJKHZg35f/VGcZ2v3I=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 h1:/c3QmbOGMGTOumP2iT/rCwB7b0QDGLKzqOmktBjT+Is=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1/go.mod h1:5SN9VR2LTsRFsrEC6FHgRbTWrTHu6tqPeKxEQv15giM=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto/googleapis/api v0.0.0-20240318140521-94a12d6c2237 h1:RFiFrvy37/mpSpdySBDrUdipW/dHwsRwh3J3+A9VgT4=
google.golang.org/genproto/googleapis/api v0.0.0-20240318140521-94a12d6c2237/go.mod h1:Z5Iiy3jtmioajWHDGFk7CeugTyHtPvMHA4UTmUkyalE=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240515191416-fc5f0ca64291 h1:AgADTJarZTBqgjiUzRgfaBchgYB3/WFTC80GPwsMcRI=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240515191416-fc5f0ca64291/go.mod h1:EfXuqaE1J41VCDicxHzUDm+8rk+7ZdXzHV0IhO/I6s0=
google.golang.org/grpc v1.64.0 h1:KH3VH9y/MgNQg1dE7b3XfVK0GsPSIzJwdF617gUSbvY=
google.golang.org/grpc v1.64.0/go.mod h1:oxjF8E3FBnjp+/gVFYdWacaLDx9na1aqy9oovLpxQYg=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
Loading

0 comments on commit 7245668

Please sign in to comment.