This repository has been archived by the owner on Oct 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 131
/
private.go
118 lines (103 loc) · 2.65 KB
/
private.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package start
import (
"context"
"fmt"
"log"
"net/http"
"time"
badger "github.com/dgraph-io/badger/v3"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/gin-gonic/gin"
"github.com/stackup-wallet/stackup-bundler/internal/config"
"github.com/stackup-wallet/stackup-bundler/internal/logger"
"github.com/stackup-wallet/stackup-bundler/pkg/bundler"
"github.com/stackup-wallet/stackup-bundler/pkg/client"
"github.com/stackup-wallet/stackup-bundler/pkg/jsonrpc"
"github.com/stackup-wallet/stackup-bundler/pkg/mempool"
"github.com/stackup-wallet/stackup-bundler/pkg/modules/checks"
"github.com/stackup-wallet/stackup-bundler/pkg/modules/paymaster"
"github.com/stackup-wallet/stackup-bundler/pkg/modules/relay"
"github.com/stackup-wallet/stackup-bundler/pkg/signer"
)
func runDBGarbageCollection(db *badger.DB) {
go func(db *badger.DB) {
ticker := time.NewTicker(5 * time.Minute)
defer ticker.Stop()
for range ticker.C {
again:
err := db.RunValueLogGC(0.7)
if err == nil {
goto again
}
}
}(db)
}
func PrivateMode() {
conf := config.GetValues()
logr := logger.NewZeroLogr().
WithName("stackup_bundler").
WithValues("bundler_mode", "private")
eoa, err := signer.New(conf.PrivateKey)
if err != nil {
log.Fatal(err)
}
beneficiary := common.HexToAddress(conf.Beneficiary)
db, err := badger.Open(badger.DefaultOptions(conf.DataDirectory))
if err != nil {
log.Fatal(err)
}
defer db.Close()
runDBGarbageCollection(db)
eth, err := ethclient.Dial(conf.EthClientUrl)
if err != nil {
log.Fatal(err)
}
chain, err := eth.ChainID(context.Background())
if err != nil {
log.Fatal(err)
}
mem, err := mempool.New(db)
if err != nil {
log.Fatal(err)
}
check := checks.New(eth, conf.MaxVerificationGas)
relayer := relay.New(db, eoa, eth, chain, beneficiary, logr)
paymaster := paymaster.New(db)
// Init Client
c := client.New(mem, chain, conf.SupportedEntryPoints)
c.UseLogger(logr)
c.UseModules(
check.ValidateOpValues(),
paymaster.CheckStatus(),
check.SimulateOp(),
paymaster.IncOpsSeen(),
)
// Init Bundler
b := bundler.New(mem, chain, conf.SupportedEntryPoints)
b.UseLogger(logr)
b.UseModules(
check.PaymasterDeposit(),
relayer.SendUserOperation(),
paymaster.IncOpsIncluded(),
)
b.Run()
// Init HTTP server
gin.SetMode(conf.GinMode)
r := gin.New()
r.Use(
logger.WithLogr(logr),
gin.Recovery(),
)
r.SetTrustedProxies(nil)
r.GET("/ping", func(g *gin.Context) {
g.Status(http.StatusOK)
})
r.POST(
"/",
relayer.FilterByClientID(),
jsonrpc.Controller(client.NewRpcAdapter(c)),
relayer.MapRequestIDToClientID(),
)
r.Run(fmt.Sprintf(":%d", conf.Port))
}