Skip to content

Commit

Permalink
Merge branch 'develop' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
wukongcheng authored Jun 19, 2023
2 parents 73a7ce6 + f401e20 commit 365bb12
Show file tree
Hide file tree
Showing 71 changed files with 4,947 additions and 343 deletions.
41 changes: 21 additions & 20 deletions batch-submitter/batch_submitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ package batchsubmitter

import (
"context"
"os"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/getsentry/sentry-go"
"github.com/urfave/cli"

"github.com/mantlenetworkio/mantle/batch-submitter/drivers/proposer"
"github.com/mantlenetworkio/mantle/batch-submitter/drivers/sequencer"
tss "github.com/mantlenetworkio/mantle/batch-submitter/tss-client"
bsscore "github.com/mantlenetworkio/mantle/bss-core"
"github.com/mantlenetworkio/mantle/bss-core/dial"
"github.com/mantlenetworkio/mantle/bss-core/metrics"
"github.com/mantlenetworkio/mantle/bss-core/txmgr"
"github.com/urfave/cli"
"os"
"time"
)

// Main is the entrypoint into the batch submitter service. This method returns
Expand Down Expand Up @@ -159,23 +161,22 @@ func Main(gitVersion string) func(ctx *cli.Context) error {

if cfg.RunStateBatchSubmitter {
batchStateDriver, err := proposer.NewDriver(proposer.Config{
Name: "Proposer",
L1Client: l1Client,
L2Client: l2Client,
TssClient: tssClient,
BlockOffset: cfg.BlockOffset,
MinStateRootElements: cfg.MinStateRootElements,
MaxStateRootElements: cfg.MaxStateRootElements,
SCCAddr: sccAddress,
CTCAddr: ctcAddress,
FPRollupAddr: common.HexToAddress(cfg.FPRollupAddress),
ChainID: chainID,
PrivKey: proposerPrivKey,
EnableProposerHsm: cfg.EnableProposerHsm,
ProposerHsmAddress: cfg.ProposerHsmAddress,
ProposerHsmCreden: cfg.ProposerHsmCreden,
ProposerHsmAPIName: cfg.ProposerHsmAPIName,
SccRollback: cfg.EnableSccRollback,
Name: "Proposer",
L1Client: l1Client,
L2Client: l2Client,
TssClient: tssClient,
BlockOffset: cfg.BlockOffset,
MinStateRootElements: cfg.MinStateRootElements,
MaxStateRootElements: cfg.MaxStateRootElements,
SCCAddr: sccAddress,
CTCAddr: ctcAddress,
FPRollupAddr: common.HexToAddress(cfg.FPRollupAddress),
ChainID: chainID,
PrivKey: proposerPrivKey,
SccRollback: cfg.EnableSccRollback,
MaxBatchSubmissionTime: cfg.MaxBatchSubmissionTime,
PollInterval: cfg.PollInterval,
FinalityConfirmations: cfg.FinalityConfirmations,
})
if err != nil {
return err
Expand Down
3 changes: 3 additions & 0 deletions batch-submitter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ type Config struct {
SequencerHsmAPIName string

SequencerHsmCreden string

RollupClientHttp string
}

// NewConfig parses the Config from the provided flags or environment variables.
Expand Down Expand Up @@ -282,6 +284,7 @@ func NewConfig(ctx *cli.Context) (Config, error) {
ProposerHsmAddress: ctx.GlobalString(flags.ProposerHsmAddressFlag.Name),
ProposerHsmAPIName: ctx.GlobalString(flags.ProposerHsmAPIName.Name),
ProposerHsmCreden: ctx.GlobalString(flags.ProposerHsmCreden.Name),
RollupClientHttp: ctx.GlobalString(flags.RollupClientHttpFlag.Name),
}

err := ValidateConfig(&cfg)
Expand Down
96 changes: 62 additions & 34 deletions batch-submitter/drivers/proposer/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package proposer

import (
"bytes"
kms "cloud.google.com/go/kms/apiv1"
"context"
"crypto/ecdsa"
"encoding/hex"
Expand All @@ -12,15 +11,18 @@ import (
"math/big"
"strings"
"sync"
"time"

"github.com/ethereum/go-ethereum/log"

kms "cloud.google.com/go/kms/apiv1"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log"
"google.golang.org/api/option"

"github.com/mantlenetworkio/mantle/batch-submitter/bindings/ctc"
"github.com/mantlenetworkio/mantle/batch-submitter/bindings/scc"
tssClient "github.com/mantlenetworkio/mantle/batch-submitter/tss-client"
Expand All @@ -33,32 +35,37 @@ import (
l2types "github.com/mantlenetworkio/mantle/l2geth/core/types"
l2ethclient "github.com/mantlenetworkio/mantle/l2geth/ethclient"
tss_types "github.com/mantlenetworkio/mantle/tss/common"
"google.golang.org/api/option"
)

// stateRootSize is the size in bytes of a state root.
const stateRootSize = 32

// block number buffer for dtl to sync data
const blockBuffer = 2

var bigOne = new(big.Int).SetUint64(1) //nolint:unused

type Config struct {
Name string
L1Client *ethclient.Client
L2Client *l2ethclient.Client
TssClient *tssClient.Client
BlockOffset uint64
MaxStateRootElements uint64
MinStateRootElements uint64
SCCAddr common.Address
CTCAddr common.Address
FPRollupAddr common.Address
ChainID *big.Int
PrivKey *ecdsa.PrivateKey
EnableProposerHsm bool
ProposerHsmAddress string
ProposerHsmAPIName string
ProposerHsmCreden string
SccRollback bool
Name string
L1Client *ethclient.Client
L2Client *l2ethclient.Client
TssClient *tssClient.Client
BlockOffset uint64
MaxStateRootElements uint64
MinStateRootElements uint64
SCCAddr common.Address
CTCAddr common.Address
FPRollupAddr common.Address
ChainID *big.Int
PrivKey *ecdsa.PrivateKey
SccRollback bool
MaxBatchSubmissionTime time.Duration
PollInterval time.Duration
FinalityConfirmations uint64
EnableProposerHsm bool
ProposerHsmCreden string
ProposerHsmAddress string
ProposerHsmAPIName string
}

type Driver struct {
Expand All @@ -73,6 +80,8 @@ type Driver struct {
rollbackEndBlock *big.Int
rollbackEndStateRoot [stateRootSize]byte
once sync.Once
lastCommitTime time.Time
lastStart *big.Int
metrics *metrics.Base
}

Expand Down Expand Up @@ -160,6 +169,7 @@ func NewDriver(cfg Config) (*Driver, error) {
rollbackEndBlock: big.NewInt(0),
rollbackEndStateRoot: [stateRootSize]byte{},
once: sync.Once{},
lastStart: big.NewInt(0),
metrics: metrics.NewBase("batch_submitter", cfg.Name),
}, nil
}
Expand Down Expand Up @@ -211,13 +221,20 @@ func (d *Driver) GetBatchBlockRange(
}
start.Add(start, blockOffset)

end, err := d.ctcContract.GetTotalElements(&bind.CallOpts{
Pending: false,
Context: ctx,
})
currentHeader, err := d.cfg.L1Client.HeaderByNumber(ctx, nil)
if err != nil {
return nil, nil, err
}
finality := new(big.Int).SetUint64(d.cfg.FinalityConfirmations)
finality.Add(finality, new(big.Int).SetInt64(blockBuffer)) // add 2 block number buffer to dtl sync data
currentNumber := currentHeader.Number
currentNumber.Sub(currentNumber, finality)

end, err := d.ctcContract.GetTotalElements(&bind.CallOpts{
Pending: false,
Context: ctx,
BlockNumber: currentNumber,
})
end.Add(end, blockOffset)

if start.Cmp(end) > 0 {
Expand All @@ -242,6 +259,25 @@ func (d *Driver) CraftBatchTx(

log.Info(name+" crafting batch tx", "start", start, "end", end, "nonce", nonce)

if start.Cmp(d.lastStart) > 0 {
d.lastStart = start
d.lastCommitTime = time.Now().Add(-d.cfg.PollInterval)
}

//If the waiting time has not been reached, then check whether the minimum stateroot number
//is met. if not, return nil
if time.Now().Add(-d.cfg.MaxBatchSubmissionTime).Before(d.lastCommitTime) {
// Abort if we don't have enough state roots to meet our minimum
// requirement.
rangeLen := end.Uint64() - start.Uint64()
if rangeLen < d.cfg.MinStateRootElements {
log.Info(name+" number of state roots below minimum",
"num_state_roots", rangeLen,
"min_state_roots", d.cfg.MinStateRootElements)
return nil, nil
}
}

var blocks []*l2types.Block
var stateRoots [][stateRootSize]byte
for i := new(big.Int).Set(start); i.Cmp(end) < 0; i.Add(i, bigOne) {
Expand All @@ -261,15 +297,6 @@ func (d *Driver) CraftBatchTx(
stateRoots = append(stateRoots, block.Root())
}

// Abort if we don't have enough state roots to meet our minimum
// requirement.
if uint64(len(stateRoots)) < d.cfg.MinStateRootElements {
log.Info(name+" number of state roots below minimum",
"num_state_roots", len(stateRoots),
"min_state_roots", d.cfg.MinStateRootElements)
return nil, nil
}

d.metrics.NumElementsPerBatch().Observe(float64(len(stateRoots)))

log.Info(name+" batch constructed", "num_state_roots", len(stateRoots))
Expand Down Expand Up @@ -553,6 +580,7 @@ func (d *Driver) SendTransaction(
ctx context.Context,
tx *types.Transaction,
) error {

return d.cfg.L1Client.SendTransaction(ctx, tx)
}

Expand Down
8 changes: 4 additions & 4 deletions batch-submitter/drivers/sequencer/driver.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
package sequencer

import (
kms "cloud.google.com/go/kms/apiv1"
"context"
"crypto/ecdsa"
"encoding/hex"
"fmt"
bsscore "github.com/mantlenetworkio/mantle/bss-core"
"google.golang.org/api/option"
"math/big"
"strings"

ethereum "github.com/ethereum/go-ethereum"
kms "cloud.google.com/go/kms/apiv1"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log"
"google.golang.org/api/option"

"github.com/mantlenetworkio/mantle/batch-submitter/bindings/ctc"
"github.com/mantlenetworkio/mantle/batch-submitter/bindings/da"
bsscore "github.com/mantlenetworkio/mantle/bss-core"
"github.com/mantlenetworkio/mantle/bss-core/drivers"
"github.com/mantlenetworkio/mantle/bss-core/metrics"
"github.com/mantlenetworkio/mantle/bss-core/txmgr"
Expand Down
6 changes: 6 additions & 0 deletions batch-submitter/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,12 @@ var (
Value: "",
EnvVar: prefixEnvVar("PROPOSER_HSM_CREDEN"),
}
RollupClientHttpFlag = cli.StringFlag{
Name: "rollup.clienthttp",
Usage: "HTTP endpoint for the rollup client",
Value: "http://localhost:7878",
EnvVar: "ROLLUP_CLIENT_HTTP",
}
)

var requiredFlags = []cli.Flag{
Expand Down
2 changes: 0 additions & 2 deletions fraud-proof/bindings/gen.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// Copyright 2022, Specular contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down
4 changes: 4 additions & 0 deletions fraud-proof/proof/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ type chainContext struct {
ctx context.Context
}

func (context *chainContext) ChainDb() ethdb.Database {
panic("not implemented")
}

func (context *chainContext) Engine() consensus.Engine {
return context.backend.Engine()
}
Expand Down
7 changes: 7 additions & 0 deletions gas-oracle/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,11 @@ binding: abi
--type GasPriceOracle \
--bin $(temp)

cat abis/uniswapv3_quoter.json \
| jq .abi \
| abigen --pkg bindings \
--abi - \
--out bindings/uniswapv3_quoter.go \
--type Uniswapv3Quoter $(temp)

rm $(temp)
Loading

0 comments on commit 365bb12

Please sign in to comment.