Skip to content

Commit 2d4b245

Browse files
committed
add rate limit
1 parent fbade49 commit 2d4b245

37 files changed

+2699
-877
lines changed

.github/workflows/paymaster.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,22 @@ jobs:
4646
if [ -n "$(git status --porcelain)" ]; then
4747
exit 1
4848
fi
49+
tests:
50+
if: github.event.pull_request.draft == false
51+
runs-on: ubuntu-latest
52+
steps:
53+
- name: Install Go
54+
uses: actions/setup-go@v2
55+
with:
56+
go-version: 1.23.0
57+
- name: Checkout code
58+
uses: actions/checkout@v4
59+
- name: Test paymaster packages
60+
run: |
61+
make test
62+
- name: Upload coverage reports to Codecov
63+
uses: codecov/codecov-action@v3
64+
env:
65+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
66+
with:
67+
flags: paymaster

build/.golangci.yml

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,6 @@ linters-settings:
103103
min-len: 3
104104
# minimal occurrences count to trigger, 3 by default
105105
min-occurrences: 3
106-
depguard:
107-
list-type: blacklist
108-
include-go-root: false
109-
packages:
110-
- github.com/davecgh/go-spew/spew
111106
misspell:
112107
# Correct spellings using locale preferences for US or UK.
113108
# Default is to use a neutral variety of English.
@@ -167,7 +162,6 @@ linters:
167162
- govet
168163
- gofmt
169164
- goimports
170-
- varcheck
171165
- misspell
172166
- ineffassign
173167
- gosimple
@@ -179,8 +173,7 @@ linters:
179173
- gosec
180174
- bodyclose
181175
- goprintffuncname
182-
- golint
183-
- depguard
176+
- revive
184177
- gocyclo
185178
- unparam
186179
- exportloopref
@@ -192,10 +185,9 @@ linters:
192185
- unused
193186
enable-all: false
194187
disable:
195-
188+
- depguard
196189
disable-all: false
197190
presets:
198-
199191
fast: false
200192

201193

build/lint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515

1616
const (
1717
// GolangCIVersion to be used for linting.
18-
GolangCIVersion = "github.com/golangci/golangci-lint/cmd/golangci-lint@v1.52.2"
18+
GolangCIVersion = "github.com/golangci/golangci-lint/cmd/golangci-lint@v1.61.0"
1919
)
2020

2121
// GOBIN environment variable.

cmd/main.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package main implements the main entry point for the Scroll paymaster service.
12
package main
23

34
import (
@@ -14,14 +15,16 @@ import (
1415
"strings"
1516
"time"
1617

18+
"github.com/ethereum/go-ethereum/log"
1719
"github.com/gin-gonic/gin"
18-
"github.com/scroll-tech/go-ethereum/log"
1920
"github.com/urfave/cli/v2"
2021

2122
"github.com/scroll-tech/paymaster/internal/config"
2223
"github.com/scroll-tech/paymaster/internal/controller"
24+
"github.com/scroll-tech/paymaster/internal/orm/migrate"
2325
"github.com/scroll-tech/paymaster/internal/route"
2426
"github.com/scroll-tech/paymaster/internal/utils"
27+
"github.com/scroll-tech/paymaster/internal/utils/database"
2528
"github.com/scroll-tech/paymaster/internal/utils/observability"
2629
)
2730

@@ -33,6 +36,25 @@ func action(ctx *cli.Context) error {
3336
log.Crit("failed to load config file", "config file", cfgFile, "error", err)
3437
}
3538

39+
// Create db instance.
40+
db, err := database.InitDB(cfg.DBConfig)
41+
if err != nil {
42+
log.Crit("failed to connect to db", "err", err)
43+
}
44+
45+
// db operation.
46+
if ctx.Bool(utils.DBFlag.Name) {
47+
if ctx.Bool(utils.DBMigrateFlag.Name) {
48+
return migrate.Migrate(db)
49+
}
50+
if ctx.Bool(utils.DBResetFlag.Name) {
51+
return migrate.ResetDB(db)
52+
}
53+
if ctx.IsSet(utils.DBRollBackFlag.Name) {
54+
return migrate.Rollback(db, ctx.Int64(utils.DBRollBackFlag.Name))
55+
}
56+
}
57+
3658
// Perform RPC sanity check
3759
if err = performRPCSanityCheck(cfg); err != nil {
3860
log.Crit("RPC sanity check failed", "error", err)
@@ -41,7 +63,7 @@ func action(ctx *cli.Context) error {
4163
observability.Server(ctx)
4264

4365
router := gin.New()
44-
controller.InitAPI(cfg)
66+
controller.InitAPI(cfg, db)
4567
route.Route(router, cfg)
4668
port := ctx.String(utils.HTTPPortFlag.Name)
4769
srv := &http.Server{
@@ -181,7 +203,8 @@ func main() {
181203
app.Flags = append(app.Flags, utils.CommonFlags...)
182204
app.Commands = []*cli.Command{}
183205
app.Before = func(ctx *cli.Context) error {
184-
return utils.LogSetup(ctx)
206+
utils.LogSetup(ctx)
207+
return nil
185208
}
186209

187210
if err := app.Run(os.Args); err != nil {

conf/config.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,11 @@
99
"https://eth-mainnet.public.blastapi.io",
1010
"https://ethereum.publicnode.com",
1111
"https://eth.drpc.org"
12-
]
12+
],
13+
"db_config": {
14+
"driver_name": "postgres",
15+
"dsn": "postgres://localhost/scroll?sslmode=disable",
16+
"maxOpenNum": 100,
17+
"maxIdleNum": 20
18+
}
1319
}

go.mod

Lines changed: 51 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,75 @@
11
module github.com/scroll-tech/paymaster
22

3-
go 1.20
3+
go 1.23.0
44

55
require (
6-
github.com/bits-and-blooms/bitset v1.13.0
7-
github.com/gin-contrib/cors v1.7.2
8-
github.com/gin-contrib/pprof v1.4.0
9-
github.com/gin-gonic/gin v1.9.1
6+
github.com/bits-and-blooms/bitset v1.20.0
7+
github.com/ethereum/go-ethereum v1.11.6
8+
github.com/gin-contrib/cors v1.7.5
9+
github.com/gin-contrib/pprof v1.5.3
10+
github.com/gin-gonic/gin v1.10.1
1011
github.com/juju/ratelimit v1.0.2
1112
github.com/mattn/go-colorable v0.1.13
1213
github.com/mattn/go-isatty v0.0.20
13-
github.com/prometheus/client_golang v1.18.0
14-
github.com/scroll-tech/go-ethereum v1.10.14-0.20240314095130-4553f5f26935
15-
github.com/urfave/cli/v2 v2.27.1
14+
github.com/pressly/goose/v3 v3.24.3
15+
github.com/prometheus/client_golang v1.14.0
16+
github.com/stretchr/testify v1.10.0
17+
github.com/urfave/cli/v2 v2.27.6
18+
gorm.io/driver/postgres v1.6.0
19+
gorm.io/driver/sqlite v1.6.0
20+
gorm.io/gorm v1.30.0
1621
)
1722

1823
require (
19-
github.com/btcsuite/btcd v0.20.1-beta // indirect
20-
github.com/bytedance/sonic/loader v0.1.1 // indirect
21-
github.com/cloudwego/base64x v0.1.4 // indirect
22-
github.com/cloudwego/iasm v0.2.0 // indirect
23-
github.com/consensys/bavard v0.1.13 // indirect
24-
github.com/crate-crypto/go-kzg-4844 v0.7.0 // indirect
25-
github.com/docker/docker v24.0.6+incompatible // indirect
26-
github.com/ethereum/c-kzg-4844/bindings/go v0.0.0-20230126171313-363c7d7593b4 // indirect
27-
github.com/google/go-cmp v0.6.0 // indirect
28-
github.com/mmcloughlin/addchain v0.4.0 // indirect
29-
github.com/onsi/ginkgo v1.16.5 // indirect
30-
github.com/onsi/gomega v1.18.1 // indirect
31-
github.com/rogpeppe/go-internal v1.11.0 // indirect
32-
github.com/supranational/blst v0.3.11-0.20230124161941-ca03e11a3ff2 // indirect
33-
rsc.io/tmplfunc v0.0.3 // indirect
34-
)
35-
36-
require (
37-
github.com/StackExchange/wmi v1.2.1 // indirect
38-
github.com/VictoriaMetrics/fastcache v1.12.1 // indirect
3924
github.com/beorn7/perks v1.0.1 // indirect
40-
github.com/bytedance/sonic v1.11.6 // indirect
41-
github.com/cespare/xxhash/v2 v2.2.0 // indirect
42-
github.com/consensys/gnark-crypto v0.10.0 // indirect
43-
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
25+
github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect
26+
github.com/bytedance/sonic v1.13.2 // indirect
27+
github.com/bytedance/sonic/loader v0.2.4 // indirect
28+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
29+
github.com/cloudwego/base64x v0.1.5 // indirect
30+
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect
4431
github.com/davecgh/go-spew v1.1.1 // indirect
45-
github.com/deckarep/golang-set v1.8.0 // indirect
46-
github.com/deepmap/oapi-codegen v1.8.2 // indirect
47-
github.com/edsrzf/mmap-go v1.0.0 // indirect
48-
github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5 // indirect
49-
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
50-
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff // indirect
51-
github.com/gin-contrib/sse v0.1.0 // indirect
52-
github.com/go-ole/go-ole v1.2.6 // indirect
32+
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
33+
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
34+
github.com/gin-contrib/sse v1.0.0 // indirect
5335
github.com/go-playground/locales v0.14.1 // indirect
5436
github.com/go-playground/universal-translator v0.18.1 // indirect
55-
github.com/go-playground/validator/v10 v10.20.0 // indirect
37+
github.com/go-playground/validator/v10 v10.26.0 // indirect
5638
github.com/go-stack/stack v1.8.1 // indirect
57-
github.com/goccy/go-json v0.10.2 // indirect
58-
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
59-
github.com/google/uuid v1.6.0 // indirect
60-
github.com/gorilla/websocket v1.4.2 // indirect
61-
github.com/graph-gophers/graphql-go v1.3.0 // indirect
62-
github.com/hashicorp/go-bexpr v0.1.10 // indirect
63-
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d // indirect
64-
github.com/holiman/bloomfilter/v2 v2.0.3 // indirect
65-
github.com/holiman/uint256 v1.2.4 // indirect
66-
github.com/huin/goupnp v1.3.0 // indirect
67-
github.com/iden3/go-iden3-crypto v0.0.15 // indirect
68-
github.com/influxdata/influxdb v1.8.3 // indirect
69-
github.com/influxdata/influxdb-client-go/v2 v2.4.0 // indirect
70-
github.com/influxdata/line-protocol v0.0.0-20210311194329-9aa0e372d097 // indirect
71-
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
39+
github.com/goccy/go-json v0.10.5 // indirect
40+
github.com/golang/protobuf v1.5.4 // indirect
41+
github.com/holiman/uint256 v1.3.2 // indirect
42+
github.com/jackc/pgpassfile v1.0.0 // indirect
43+
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
44+
github.com/jackc/pgx/v5 v5.7.4 // indirect
45+
github.com/jackc/puddle/v2 v2.2.2 // indirect
46+
github.com/jinzhu/inflection v1.0.0 // indirect
47+
github.com/jinzhu/now v1.1.5 // indirect
7248
github.com/json-iterator/go v1.1.12 // indirect
73-
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
49+
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
7450
github.com/leodido/go-urn v1.4.0 // indirect
75-
github.com/mattn/go-runewidth v0.0.13 // indirect
76-
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
77-
github.com/mitchellh/mapstructure v1.5.0 // indirect
78-
github.com/mitchellh/pointerstructure v1.2.0 // indirect
51+
github.com/mattn/go-sqlite3 v1.14.22 // indirect
52+
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
53+
github.com/mfridman/interpolate v0.0.2 // indirect
7954
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
8055
github.com/modern-go/reflect2 v1.0.2 // indirect
81-
github.com/olekukonko/tablewriter v0.0.5 // indirect
82-
github.com/opentracing/opentracing-go v1.1.0 // indirect
83-
github.com/pelletier/go-toml/v2 v2.2.1 // indirect
84-
github.com/peterh/liner v1.1.1-0.20190123174540-a2c9a5303de7 // indirect
85-
github.com/pkg/errors v0.9.1 // indirect
86-
github.com/prometheus/client_model v0.5.0 // indirect
87-
github.com/prometheus/common v0.45.0 // indirect
88-
github.com/prometheus/procfs v0.12.0 // indirect
89-
github.com/prometheus/tsdb v0.7.1 // indirect
90-
github.com/rivo/uniseg v0.2.0 // indirect
91-
github.com/rjeczalik/notify v0.9.1 // indirect
92-
github.com/rs/cors v1.7.0 // indirect
56+
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
57+
github.com/pmezard/go-difflib v1.0.0 // indirect
58+
github.com/prometheus/client_model v0.3.0 // indirect
59+
github.com/prometheus/common v0.39.0 // indirect
60+
github.com/prometheus/procfs v0.16.1 // indirect
9361
github.com/russross/blackfriday/v2 v2.1.0 // indirect
94-
github.com/scroll-tech/zktrie v0.6.0 // indirect
95-
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
96-
github.com/status-im/keycard-go v0.2.0 // indirect
97-
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
98-
github.com/tklauser/go-sysconf v0.3.12 // indirect
99-
github.com/tklauser/numcpus v0.6.1 // indirect
62+
github.com/sethvargo/go-retry v0.3.0 // indirect
10063
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
101-
github.com/tyler-smith/go-bip39 v1.1.0 // indirect
10264
github.com/ugorji/go/codec v1.2.12 // indirect
103-
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
104-
golang.org/x/arch v0.7.0 // indirect
105-
golang.org/x/crypto v0.25.0 // indirect
106-
golang.org/x/net v0.27.0 // indirect
107-
golang.org/x/sync v0.7.0 // indirect
108-
golang.org/x/sys v0.22.0 // indirect
109-
golang.org/x/text v0.16.0 // indirect
110-
golang.org/x/time v0.6.0 // indirect
111-
google.golang.org/protobuf v1.34.0 // indirect
112-
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
113-
gopkg.in/urfave/cli.v1 v1.20.0 // indirect
114-
gopkg.in/yaml.v2 v2.4.0 // indirect
65+
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
66+
go.uber.org/multierr v1.11.0 // indirect
67+
golang.org/x/arch v0.16.0 // indirect
68+
golang.org/x/crypto v0.38.0 // indirect
69+
golang.org/x/net v0.40.0 // indirect
70+
golang.org/x/sync v0.14.0 // indirect
71+
golang.org/x/sys v0.33.0 // indirect
72+
golang.org/x/text v0.25.0 // indirect
73+
google.golang.org/protobuf v1.36.6 // indirect
11574
gopkg.in/yaml.v3 v3.0.1 // indirect
11675
)

0 commit comments

Comments
 (0)