Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rollup_relayer): add chain_monitor client #885

Merged
merged 44 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
60cca24
Add batch status check logic
mask-pp Aug 28, 2023
34fc3ba
chore: auto version bump [bot]
mask-pp Aug 28, 2023
fc06952
fix comments
mask-pp Aug 28, 2023
a3abfd7
Merge branch 'develop' into feat/add_chain_monitor
mask-pp Aug 28, 2023
471af43
chore: auto version bump [bot]
mask-pp Aug 28, 2023
cde6981
Merge branch 'develop' into feat/add_chain_monitor
mask-pp Aug 28, 2023
8165c3d
chore: auto version bump [bot]
mask-pp Aug 28, 2023
020317e
fix comments
mask-pp Aug 29, 2023
d7b6331
chore: auto version bump [bot]
mask-pp Aug 29, 2023
057e498
Merge branch 'develop' into feat/add_chain_monitor
mask-pp Aug 29, 2023
69e1a5c
fix comments
mask-pp Aug 29, 2023
d85b422
fix comments
mask-pp Aug 29, 2023
1d59c6f
fix comments and add test case
mask-pp Aug 29, 2023
11a3810
fix ci
mask-pp Aug 29, 2023
9e8e944
fix ci
mask-pp Aug 29, 2023
4090cf0
fix ci
mask-pp Aug 29, 2023
ca2991e
fix ci
mask-pp Aug 29, 2023
f7bc5c4
fix bug in integration test
mask-pp Aug 30, 2023
c8b6b37
Feat/upgrade cmd (#889)
mask-pp Aug 30, 2023
c7a6949
Merge branch 'develop' into feat/add_chain_monitor
mask-pp Aug 30, 2023
af39650
chore: auto version bump [bot]
mask-pp Aug 30, 2023
aaaa741
fix bug
mask-pp Aug 30, 2023
fb255aa
fix bug
mask-pp Aug 30, 2023
1ff0e21
fix bug
mask-pp Aug 30, 2023
cc68797
fix bug
mask-pp Aug 30, 2023
90f8a28
fix bug
mask-pp Aug 30, 2023
26ba4d9
fix bug
mask-pp Aug 30, 2023
cdb658b
fix bug
mask-pp Aug 30, 2023
f02242a
fix bug
mask-pp Aug 30, 2023
5650704
fix bug
mask-pp Aug 30, 2023
4c14fae
fix bug
mask-pp Aug 30, 2023
08cb2d8
fix bug
mask-pp Aug 30, 2023
1869abc
fix ci
mask-pp Aug 30, 2023
70c700b
fix ci
mask-pp Aug 30, 2023
80bc054
fix bug
mask-pp Aug 30, 2023
bb10509
Merge branch 'develop' into feat/add_chain_monitor
mask-pp Aug 30, 2023
8756858
chore: auto version bump [bot]
mask-pp Aug 30, 2023
c822f58
revert change
mask-pp Aug 30, 2023
af2e477
temporary close the test case
mask-pp Aug 30, 2023
d236f7a
fix bug
mask-pp Aug 30, 2023
253b9a9
Update common/utils/utils.go
mask-pp Aug 30, 2023
0e24477
Update bridge/internal/config/relayer.go
mask-pp Aug 31, 2023
ec542d0
Merge branch 'develop' into feat/add_chain_monitor
mask-pp Aug 31, 2023
1274adc
chore: auto version bump [bot]
mask-pp Aug 31, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bridge/conf/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
"gas_price_diff": 50000
},
"finalize_batch_interval_sec": 0,
"chain_monitor": {
"timeout": 3,
"try_times": 5,
"base_url": "http://localhost:8750"
},
"message_sender_private_key": "1212121212121212121212121212121212121212121212121212121212121212",
"gas_oracle_sender_private_key": "1313131313131313131313131313131313131313131313131313131313131313"
}
Expand Down
2 changes: 2 additions & 0 deletions bridge/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set v1.8.0 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-resty/resty/v2 v2.7.0 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
Expand Down Expand Up @@ -58,6 +59,7 @@ require (
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/time v0.3.0 // indirect
Expand Down
10 changes: 10 additions & 0 deletions bridge/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff h1:tY80oXqGNY4FhTFhk+o9oFHGINQ/+vhlm8HFzi6znCI=
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/go-resty/resty/v2 v2.7.0 h1:me+K9p3uhSmXtrBZ4k9jcEAfJmuC8IivWHwaLZwPrFY=
github.com/go-resty/resty/v2 v2.7.0/go.mod h1:9PWDzw47qPphMRFfhsyk0NnSgvluHcljSMVIq3w7q0I=
github.com/go-stack/stack v1.8.1 h1:ntEHSVwIt7PNXNpgPmVfMrNhLtgjlmnZha2kOpuRiDw=
github.com/go-stack/stack v1.8.1/go.mod h1:dcoOX6HbPZSZptuspn9bctJ+N/CnF5gGygcUP3XYfe4=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down Expand Up @@ -154,6 +156,9 @@ golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98y
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20211029224645-99673261e6eb/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -163,15 +168,20 @@ golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
Expand Down
9 changes: 9 additions & 0 deletions bridge/internal/config/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ type SenderConfig struct {
PendingLimit int `json:"pending_limit"`
}

// ChainMonitor this config is used to get batch status from chain_monitor API.
type ChainMonitor struct {
TimeOut int `json:"timeout"`
TryTimes int `json:"try_times"`
BaseURL string `json:"base_url"`
}

// RelayerConfig loads relayer configuration items.
// What we need to pay attention to is that
type RelayerConfig struct {
Expand All @@ -54,6 +61,8 @@ type RelayerConfig struct {
FinalizeBatchIntervalSec uint64 `json:"finalize_batch_interval_sec"`
// MessageRelayMinGasLimit to avoid OutOfGas error
MessageRelayMinGasLimit uint64 `json:"message_relay_min_gas_limit,omitempty"`
// chain-monitor endpoint
mask-pp marked this conversation as resolved.
Show resolved Hide resolved
ChainMonitor *ChainMonitor `json:"chain_monitor"`
// The private key of the relayer
MessageSenderPrivateKey *ecdsa.PrivateKey `json:"-"`
GasOracleSenderPrivateKey *ecdsa.PrivateKey `json:"-"`
Expand Down
46 changes: 46 additions & 0 deletions bridge/internal/controller/relayer/l2_relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sync"
"time"

"github.com/go-resty/resty/v2"
"github.com/prometheus/client_golang/prometheus"
"github.com/scroll-tech/go-ethereum/accounts/abi"
"github.com/scroll-tech/go-ethereum/common"
Expand Down Expand Up @@ -59,6 +60,9 @@ type Layer2Relayer struct {
minGasPrice uint64
gasPriceDiff uint64

// Used to get batch status from chain_monitor api.
chainMonitorClient *resty.Client

// A list of processing message.
// key(string): confirmation ID, value(string): layer2 hash.
processingMessage sync.Map
Expand Down Expand Up @@ -114,6 +118,11 @@ func NewLayer2Relayer(ctx context.Context, l2Client *ethclient.Client, db *gorm.
minGasLimitForMessageRelay = cfg.MessageRelayMinGasLimit
}

// chain_monitor client
chainMonitorClient := resty.New()
chainMonitorClient.SetRetryCount(cfg.ChainMonitor.TryTimes)
chainMonitorClient.SetTimeout(time.Duration(cfg.ChainMonitor.TimeOut) * time.Second)

layer2Relayer := &Layer2Relayer{
ctx: ctx,
db: db,
Expand Down Expand Up @@ -143,6 +152,7 @@ func NewLayer2Relayer(ctx context.Context, l2Client *ethclient.Client, db *gorm.
processingMessage: sync.Map{},
processingCommitment: sync.Map{},
processingFinalization: sync.Map{},
chainMonitorClient: chainMonitorClient,
}

// Initialize genesis before we do anything else
Expand Down Expand Up @@ -434,6 +444,19 @@ func (r *Layer2Relayer) ProcessCommittedBatches() {
log.Info("Start to roll up zk proof", "hash", hash)
r.metrics.bridgeL2RelayerProcessCommittedBatchesFinalizedTotal.Inc()

// Check batch status before send `finalizeBatchWithProof` tx.
batchStatus, err := r.getBatchStatusByIndex(batch.Index)
if err != nil {
r.metrics.bridgeL2ChainMonitorLatestFailedCall.Set(float64(batch.Index))
mask-pp marked this conversation as resolved.
Show resolved Hide resolved
log.Warn("failed to get batch status, please check chain_monitor api server", "batch_index", batch.Index, "err", err)
mask-pp marked this conversation as resolved.
Show resolved Hide resolved
georgehao marked this conversation as resolved.
Show resolved Hide resolved
return
}
if !batchStatus {
r.metrics.bridgeL2ChainMonitorLatestFailedBatchStatus.Set(float64(batch.Index))
mask-pp marked this conversation as resolved.
Show resolved Hide resolved
log.Error("the batch status is not right, stop finalize batch and check the reason", "batch_index", batch.Index)
return
}

var parentBatchStateRoot string
if batch.Index > 0 {
var parentBatch *orm.Batch
Expand Down Expand Up @@ -527,6 +550,29 @@ func (r *Layer2Relayer) ProcessCommittedBatches() {
}
}

// batchStatusResponse the response schema
type batchStatusResponse struct {
ErrCode int `json:"errcode"`
ErrMsg string `json:"errmsg"`
Data bool `json:"data"`
}

func (r *Layer2Relayer) getBatchStatusByIndex(batchIndex uint64) (bool, error) {
mask-pp marked this conversation as resolved.
Show resolved Hide resolved
var response batchStatusResponse
resp, err := r.chainMonitorClient.R().SetResult(&response).Get(fmt.Sprintf("%s/v1/batch_status?batch_index=%d", r.cfg.ChainMonitor.BaseURL, batchIndex))
if err != nil {
return false, err
}
if resp.IsError() {
return false, resp.Error().(error)
}
if response.ErrCode != 0 {
return false, fmt.Errorf("failed to get batch status, errCode: %d, errMsg: %s", response.ErrCode, response.ErrMsg)
}

return response.Data, nil
mask-pp marked this conversation as resolved.
Show resolved Hide resolved
}

func (r *Layer2Relayer) handleConfirmation(confirmation *sender.Confirmation) {
transactionType := "Unknown"
// check whether it is CommitBatches transaction
Expand Down
10 changes: 10 additions & 0 deletions bridge/internal/controller/relayer/l2_relayer_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type l2RelayerMetrics struct {
bridgeL2BatchesCommittedConfirmedTotal prometheus.Counter
bridgeL2BatchesFinalizedConfirmedTotal prometheus.Counter
bridgeL2BatchesGasOraclerConfirmedTotal prometheus.Counter
bridgeL2ChainMonitorLatestFailedCall prometheus.Gauge
bridgeL2ChainMonitorLatestFailedBatchStatus prometheus.Gauge
}

var (
Expand Down Expand Up @@ -68,6 +70,14 @@ func initL2RelayerMetrics(reg prometheus.Registerer) *l2RelayerMetrics {
Name: "bridge_layer2_process_gras_oracler_confirmed_total",
Help: "The total number of layer2 process finalized batches confirmed total",
}),
bridgeL2ChainMonitorLatestFailedCall: promauto.With(reg).NewGauge(prometheus.GaugeOpts{
Name: "bridge_layer2_chain_monitor_latest_failed_batch_call",
Help: "",
mask-pp marked this conversation as resolved.
Show resolved Hide resolved
}),
bridgeL2ChainMonitorLatestFailedBatchStatus: promauto.With(reg).NewGauge(prometheus.GaugeOpts{
Name: "bridge_layer2_chain_monitor_latest_failed_batch_status",
Help: "The latest failed batch index before sending finalize batch tx",
}),
}
})
return l2RelayerMetric
Expand Down
2 changes: 1 addition & 1 deletion common/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"
)

var tag = "v4.2.5"
var tag = "v4.2.6"

var commit = func() string {
if info, ok := debug.ReadBuildInfo(); ok {
Expand Down
Loading