|
| 1 | +package start |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "net/http" |
| 8 | + |
| 9 | + badger "github.com/dgraph-io/badger/v3" |
| 10 | + "github.com/ethereum/go-ethereum/common" |
| 11 | + "github.com/ethereum/go-ethereum/ethclient" |
| 12 | + "github.com/ethereum/go-ethereum/rpc" |
| 13 | + "github.com/gin-contrib/cors" |
| 14 | + "github.com/gin-gonic/gin" |
| 15 | + "github.com/metachris/flashbotsrpc" |
| 16 | + "github.com/stackup-wallet/stackup-bundler/internal/config" |
| 17 | + "github.com/stackup-wallet/stackup-bundler/internal/logger" |
| 18 | + "github.com/stackup-wallet/stackup-bundler/pkg/bundler" |
| 19 | + "github.com/stackup-wallet/stackup-bundler/pkg/client" |
| 20 | + "github.com/stackup-wallet/stackup-bundler/pkg/jsonrpc" |
| 21 | + "github.com/stackup-wallet/stackup-bundler/pkg/mempool" |
| 22 | + "github.com/stackup-wallet/stackup-bundler/pkg/modules/builder" |
| 23 | + "github.com/stackup-wallet/stackup-bundler/pkg/modules/checks" |
| 24 | + "github.com/stackup-wallet/stackup-bundler/pkg/modules/paymaster" |
| 25 | + "github.com/stackup-wallet/stackup-bundler/pkg/signer" |
| 26 | +) |
| 27 | + |
| 28 | +func SearcherMode() { |
| 29 | + conf := config.GetValues() |
| 30 | + |
| 31 | + logr := logger.NewZeroLogr(). |
| 32 | + WithName("stackup_bundler"). |
| 33 | + WithValues("bundler_mode", "searcher") |
| 34 | + |
| 35 | + eoa, err := signer.New(conf.PrivateKey) |
| 36 | + if err != nil { |
| 37 | + log.Fatal(err) |
| 38 | + } |
| 39 | + beneficiary := common.HexToAddress(conf.Beneficiary) |
| 40 | + |
| 41 | + db, err := badger.Open(badger.DefaultOptions(conf.DataDirectory)) |
| 42 | + if err != nil { |
| 43 | + log.Fatal(err) |
| 44 | + } |
| 45 | + defer db.Close() |
| 46 | + runDBGarbageCollection(db) |
| 47 | + |
| 48 | + rpc, err := rpc.Dial(conf.EthClientUrl) |
| 49 | + if err != nil { |
| 50 | + log.Fatal(err) |
| 51 | + } |
| 52 | + |
| 53 | + eth := ethclient.NewClient(rpc) |
| 54 | + |
| 55 | + fb := flashbotsrpc.NewFlashbotsRPC(conf.EthBuilderUrl) |
| 56 | + |
| 57 | + chain, err := eth.ChainID(context.Background()) |
| 58 | + if err != nil { |
| 59 | + log.Fatal(err) |
| 60 | + } |
| 61 | + if !builder.CompatibleChainIDs.Contains(chain.Uint64()) { |
| 62 | + log.Fatalf( |
| 63 | + "error: network with chainID %d is not compatible with the Block Builder API.", |
| 64 | + chain.Uint64(), |
| 65 | + ) |
| 66 | + } |
| 67 | + |
| 68 | + mem, err := mempool.New(db) |
| 69 | + if err != nil { |
| 70 | + log.Fatal(err) |
| 71 | + } |
| 72 | + |
| 73 | + check := checks.New( |
| 74 | + rpc, |
| 75 | + conf.MaxVerificationGas, |
| 76 | + conf.MaxOpsForUnstakedSender, |
| 77 | + conf.BundlerCollectorTracer, |
| 78 | + ) |
| 79 | + // TODO: Create separate go-routine for tracking transactions sent to the block builder. |
| 80 | + builder := builder.New(eoa, eth, fb, beneficiary, conf.BlocksInTheFuture) |
| 81 | + paymaster := paymaster.New(db) |
| 82 | + |
| 83 | + // Init Client |
| 84 | + c := client.New(mem, chain, conf.SupportedEntryPoints) |
| 85 | + c.SetGetUserOpReceiptFunc(client.GetUserOpReceiptWithEthClient(eth)) |
| 86 | + c.SetGetSimulateValidationFunc(client.GetSimulateValidationWithRpcClient(rpc)) |
| 87 | + c.SetGetCallGasEstimateFunc(client.GetCallGasEstimateWithEthClient(eth)) |
| 88 | + c.SetGetUserOpByHashFunc(client.GetUserOpByHashWithEthClient(eth)) |
| 89 | + c.UseLogger(logr) |
| 90 | + c.UseModules( |
| 91 | + check.ValidateOpValues(), |
| 92 | + paymaster.CheckStatus(), |
| 93 | + check.SimulateOp(), |
| 94 | + // TODO: add p2p propagation module |
| 95 | + paymaster.IncOpsSeen(), |
| 96 | + ) |
| 97 | + |
| 98 | + // Init Bundler |
| 99 | + b := bundler.New(mem, chain, conf.SupportedEntryPoints) |
| 100 | + b.UseLogger(logr) |
| 101 | + b.UseModules( |
| 102 | + check.PaymasterDeposit(), |
| 103 | + builder.SendUserOperation(), |
| 104 | + paymaster.IncOpsIncluded(), |
| 105 | + ) |
| 106 | + if err := b.Run(); err != nil { |
| 107 | + log.Fatal(err) |
| 108 | + } |
| 109 | + |
| 110 | + // init Debug |
| 111 | + var d *client.Debug |
| 112 | + if conf.DebugMode { |
| 113 | + d = client.NewDebug(eoa, eth, mem, b, chain, conf.SupportedEntryPoints[0], beneficiary) |
| 114 | + } |
| 115 | + |
| 116 | + // Init HTTP server |
| 117 | + gin.SetMode(conf.GinMode) |
| 118 | + r := gin.New() |
| 119 | + if err := r.SetTrustedProxies(nil); err != nil { |
| 120 | + log.Fatal(err) |
| 121 | + } |
| 122 | + r.Use( |
| 123 | + cors.Default(), |
| 124 | + logger.WithLogr(logr), |
| 125 | + gin.Recovery(), |
| 126 | + ) |
| 127 | + r.GET("/ping", func(g *gin.Context) { |
| 128 | + g.Status(http.StatusOK) |
| 129 | + }) |
| 130 | + r.POST("/", jsonrpc.Controller(client.NewRpcAdapter(c, d))) |
| 131 | + if err := r.Run(fmt.Sprintf(":%d", conf.Port)); err != nil { |
| 132 | + log.Fatal(err) |
| 133 | + } |
| 134 | +} |
0 commit comments