Merge branch 'staging' into maskpp/sender_upgrade_2

This commit is contained in:
colin
2023-02-13 17:26:05 +08:00
committed by GitHub
22 changed files with 374 additions and 221 deletions

View File

@@ -1,6 +1,6 @@
{
"l1_config": {
"confirmations": "number=6",
"confirmations": "0x6",
"endpoint": "https://goerli.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161",
"l1_messenger_address": "0x0000000000000000000000000000000000000000",
"rollup_contract_address": "0x0000000000000000000000000000000000000000",
@@ -11,7 +11,7 @@
"endpoint": "/var/lib/jenkins/workspace/SequencerPipeline/MyPrivateNetwork/geth.ipc",
"check_pending_time": 3,
"escalate_blocks": 100,
"confirmations": "number=1",
"confirmations": "0x1",
"escalate_multiple_num": 11,
"escalate_multiple_den": 10,
"max_gas_price": 10000000000,
@@ -24,7 +24,7 @@
}
},
"l2_config": {
"confirmations": "number=1",
"confirmations": "0x1",
"endpoint": "/var/lib/jenkins/workspace/SequencerPipeline/MyPrivateNetwork/geth.ipc",
"l2_messenger_address": "0x0000000000000000000000000000000000000000",
"relayer_config": {
@@ -34,7 +34,7 @@
"endpoint": "https://goerli.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161",
"check_pending_time": 10,
"escalate_blocks": 100,
"confirmations": "number=6",
"confirmations": "0x6",
"escalate_multiple_num": 11,
"escalate_multiple_den": 10,
"max_gas_price": 10000000000,

View File

@@ -1,15 +1,14 @@
package config
import (
"scroll-tech/bridge/utils"
"github.com/scroll-tech/go-ethereum/common"
"github.com/scroll-tech/go-ethereum/rpc"
)
// L1Config loads l1eth configuration items.
type L1Config struct {
// Confirmations block height confirmations number.
Confirmations utils.ConfirmationParams `json:"confirmations"`
Confirmations rpc.BlockNumber `json:"confirmations"`
// l1 eth node url.
Endpoint string `json:"endpoint"`
// The start height to sync event from layer 1

View File

@@ -3,7 +3,7 @@ package config
import (
"encoding/json"
"scroll-tech/bridge/utils"
"github.com/scroll-tech/go-ethereum/rpc"
"github.com/scroll-tech/go-ethereum/common"
)
@@ -11,7 +11,7 @@ import (
// L2Config loads l2geth configuration items.
type L2Config struct {
// Confirmations block height confirmations number.
Confirmations utils.ConfirmationParams `json:"confirmations"`
Confirmations rpc.BlockNumber `json:"confirmations"`
// l2geth node url.
Endpoint string `json:"endpoint"`
// The messenger contract address deployed on layer 2 chain.

View File

@@ -6,10 +6,9 @@ import (
"fmt"
"math/big"
"scroll-tech/bridge/utils"
"github.com/scroll-tech/go-ethereum/common"
"github.com/scroll-tech/go-ethereum/crypto"
"github.com/scroll-tech/go-ethereum/rpc"
)
// SenderConfig The config for transaction sender
@@ -21,7 +20,7 @@ type SenderConfig struct {
// The number of blocks to wait to escalate increase gas price of the transaction.
EscalateBlocks uint64 `json:"escalate_blocks"`
// The gap number between a block be confirmed and the latest block.
Confirmations utils.ConfirmationParams `json:"confirmations"`
Confirmations rpc.BlockNumber `json:"confirmations"`
// The numerator of gas price escalate multiple.
EscalateMultipleNum uint64 `json:"escalate_multiple_num"`
// The denominator of gas price escalate multiple.

View File

@@ -5,7 +5,7 @@ go 1.18
require (
github.com/iden3/go-iden3-crypto v0.0.13
github.com/orcaman/concurrent-map v1.0.0
github.com/scroll-tech/go-ethereum v1.10.14-0.20230127005331-08ba436d8bb3
github.com/scroll-tech/go-ethereum v1.10.14-0.20230210091139-8224bc8b9ca5
github.com/stretchr/testify v1.8.0
github.com/urfave/cli/v2 v2.10.2
golang.org/x/sync v0.1.0
@@ -32,7 +32,7 @@ require (
github.com/rjeczalik/notify v0.9.1 // indirect
github.com/rogpeppe/go-internal v1.8.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/scroll-tech/zktrie v0.4.1 // indirect
github.com/scroll-tech/zktrie v0.4.3 // indirect
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
github.com/tklauser/go-sysconf v0.3.10 // indirect
github.com/tklauser/numcpus v0.4.0 // indirect

View File

@@ -350,11 +350,10 @@ github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/scroll-tech/go-ethereum v1.10.14-0.20230127005331-08ba436d8bb3 h1:kYPsjs9hr579hMFuHXrOy0zveCLHD/kC+PGv9wnadvM=
github.com/scroll-tech/go-ethereum v1.10.14-0.20230127005331-08ba436d8bb3/go.mod h1:jurIpDQ0hqtp9//xxeWzr8X9KMP/+TYn+vz3K1wZrv0=
github.com/scroll-tech/zktrie v0.3.0/go.mod h1:CuJFlG1/soTJJBAySxCZgTF7oPvd5qF6utHOEciC43Q=
github.com/scroll-tech/zktrie v0.4.1 h1:+enbK4g6/Pj76Do6Pz+ncaqJuYczua+yhP3Phs0pD3E=
github.com/scroll-tech/zktrie v0.4.1/go.mod h1:XvNo7vAk8yxNyTjBDj5WIiFzYW4bx/gJ78+NK6Zn6Uk=
github.com/scroll-tech/go-ethereum v1.10.14-0.20230210091139-8224bc8b9ca5 h1:CvkJOFK7Zapepx6Dwyc+86dZWWypXhSP4RFJmv6EJDk=
github.com/scroll-tech/go-ethereum v1.10.14-0.20230210091139-8224bc8b9ca5/go.mod h1:OH4ZTAz6RM1IL0xcQ1zM6+Iy9s2vtcYqqwcEQdfHV7g=
github.com/scroll-tech/zktrie v0.4.3 h1:RyhusIu8F8u5ITmzqZjkAwlL6jdC9TK9i6tfuJoZcpk=
github.com/scroll-tech/zktrie v0.4.3/go.mod h1:XvNo7vAk8yxNyTjBDj5WIiFzYW4bx/gJ78+NK6Zn6Uk=
github.com/segmentio/kafka-go v0.1.0/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfPOCvTvk+EJo=
github.com/segmentio/kafka-go v0.2.0/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfPOCvTvk+EJo=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=

View File

@@ -12,6 +12,7 @@ import (
"github.com/scroll-tech/go-ethereum/ethclient"
"github.com/scroll-tech/go-ethereum/log"
"github.com/scroll-tech/go-ethereum/metrics"
"github.com/scroll-tech/go-ethereum/rpc"
"scroll-tech/database"
"scroll-tech/database/orm"
@@ -43,7 +44,7 @@ type Watcher struct {
db database.OrmFactory
// The number of new blocks to wait for a block to be confirmed
confirmations utils.ConfirmationParams
confirmations rpc.BlockNumber
messengerAddress common.Address
messengerABI *abi.ABI
@@ -58,7 +59,7 @@ type Watcher struct {
// NewWatcher returns a new instance of Watcher. The instance will be not fully prepared,
// and still needs to be finalized and ran by calling `watcher.Start`.
func NewWatcher(ctx context.Context, client *ethclient.Client, startHeight uint64, confirmations utils.ConfirmationParams, messengerAddress common.Address, rollupAddress common.Address, db database.OrmFactory) *Watcher {
func NewWatcher(ctx context.Context, client *ethclient.Client, startHeight uint64, confirmations rpc.BlockNumber, messengerAddress common.Address, rollupAddress common.Address, db database.OrmFactory) *Watcher {
savedHeight, err := db.GetLayer1LatestWatchedHeight()
if err != nil {
log.Warn("Failed to fetch height from db", "err", err)

View File

@@ -15,6 +15,7 @@ import (
"github.com/scroll-tech/go-ethereum/event"
"github.com/scroll-tech/go-ethereum/log"
"github.com/scroll-tech/go-ethereum/metrics"
"github.com/scroll-tech/go-ethereum/rpc"
bridge_abi "scroll-tech/bridge/abi"
"scroll-tech/bridge/utils"
@@ -45,7 +46,7 @@ type WatcherClient struct {
orm database.OrmFactory
confirmations utils.ConfirmationParams
confirmations rpc.BlockNumber
messengerAddress common.Address
messengerABI *abi.ABI
@@ -59,7 +60,7 @@ type WatcherClient struct {
}
// NewL2WatcherClient take a l2geth instance to generate a l2watcherclient instance
func NewL2WatcherClient(ctx context.Context, client *ethclient.Client, confirmations utils.ConfirmationParams, bpCfg *config.BatchProposerConfig, messengerAddress common.Address, orm database.OrmFactory) *WatcherClient {
func NewL2WatcherClient(ctx context.Context, client *ethclient.Client, confirmations rpc.BlockNumber, bpCfg *config.BatchProposerConfig, messengerAddress common.Address, orm database.OrmFactory) *WatcherClient {
savedHeight, err := orm.GetLayer2LatestWatchedHeight()
if err != nil {
log.Warn("fetch height from db failed", "err", err)

View File

@@ -12,10 +12,9 @@ import (
"github.com/scroll-tech/go-ethereum/common"
"github.com/scroll-tech/go-ethereum/core/types"
"github.com/scroll-tech/go-ethereum/ethclient"
"github.com/scroll-tech/go-ethereum/rpc"
"github.com/stretchr/testify/assert"
"scroll-tech/bridge/utils"
"scroll-tech/bridge/config"
"scroll-tech/bridge/mock_bridge"
"scroll-tech/bridge/sender"
@@ -38,7 +37,7 @@ func testCreateNewWatcherAndStop(t *testing.T) {
defer rc.Stop()
l1cfg := cfg.L1Config
l1cfg.RelayerConfig.SenderConfig.Confirmations = utils.ConfirmationParams{Type: utils.BlockNumberConfirmation, Number: 0}
l1cfg.RelayerConfig.SenderConfig.Confirmations = rpc.LatestBlockNumber
newSender, err := sender.NewSender(context.Background(), l1cfg.RelayerConfig.SenderConfig, l1cfg.RelayerConfig.MessageSenderPrivateKeys)
assert.NoError(t, err)
@@ -192,7 +191,7 @@ func testFetchMultipleSentMessageInOneBlock(t *testing.T) {
}
func prepareRelayerClient(l2Cli *ethclient.Client, bpCfg *config.BatchProposerConfig, db database.OrmFactory, contractAddr common.Address) *WatcherClient {
confirmations := utils.ConfirmationParams{Type: utils.BlockNumberConfirmation, Number: 0}
confirmations := rpc.LatestBlockNumber
return NewL2WatcherClient(context.Background(), l2Cli, confirmations, bpCfg, contractAddr, db)
}

View File

@@ -14,12 +14,11 @@ import (
cmap "github.com/orcaman/concurrent-map"
"github.com/scroll-tech/go-ethereum/common"
"github.com/scroll-tech/go-ethereum/crypto"
"github.com/scroll-tech/go-ethereum/rpc"
"github.com/stretchr/testify/assert"
"scroll-tech/common/docker"
"scroll-tech/bridge/utils"
"scroll-tech/bridge/config"
"scroll-tech/bridge/sender"
)
@@ -92,7 +91,7 @@ func testBatchSender(t *testing.T, batchSize int) {
}
senderCfg := cfg.L1Config.RelayerConfig.SenderConfig
senderCfg.Confirmations = utils.ConfirmationParams{Type: utils.BlockNumberConfirmation, Number: 0}
senderCfg.Confirmations = rpc.LatestBlockNumber
newSender, err := sender.NewSender(context.Background(), senderCfg, privateKeys)
if err != nil {
t.Fatal(err)

View File

@@ -6,19 +6,18 @@ import (
"math/big"
"testing"
"scroll-tech/common/docker"
"scroll-tech/bridge/utils"
"scroll-tech/bridge/config"
"scroll-tech/bridge/mock_bridge"
"github.com/scroll-tech/go-ethereum/accounts/abi/bind"
"github.com/scroll-tech/go-ethereum/common"
"github.com/scroll-tech/go-ethereum/core/types"
"github.com/scroll-tech/go-ethereum/crypto"
"github.com/scroll-tech/go-ethereum/ethclient"
"github.com/scroll-tech/go-ethereum/rpc"
"github.com/stretchr/testify/assert"
"scroll-tech/bridge/config"
"scroll-tech/bridge/mock_bridge"
"scroll-tech/common/docker"
)
var (
@@ -66,10 +65,10 @@ func setupEnv(t *testing.T) {
// Load config.
cfg, err = config.NewConfig("../config.json")
assert.NoError(t, err)
cfg.L1Config.Confirmations = utils.ConfirmationParams{Type: utils.BlockNumberConfirmation, Number: 0}
cfg.L1Config.Confirmations = rpc.LatestBlockNumber
cfg.L1Config.RelayerConfig.MessageSenderPrivateKeys = []*ecdsa.PrivateKey{messagePrivateKey}
cfg.L1Config.RelayerConfig.RollupSenderPrivateKeys = []*ecdsa.PrivateKey{rollupPrivateKey}
cfg.L2Config.Confirmations = utils.ConfirmationParams{Type: utils.BlockNumberConfirmation, Number: 0}
cfg.L2Config.Confirmations = rpc.LatestBlockNumber
cfg.L2Config.RelayerConfig.MessageSenderPrivateKeys = []*ecdsa.PrivateKey{messagePrivateKey}
cfg.L2Config.RelayerConfig.RollupSenderPrivateKeys = []*ecdsa.PrivateKey{rollupPrivateKey}

View File

@@ -3,20 +3,20 @@ package tests
import (
"context"
"math/big"
"scroll-tech/database"
"scroll-tech/database/migrate"
"scroll-tech/database/orm"
"testing"
"scroll-tech/bridge/utils"
"scroll-tech/bridge/l1"
"scroll-tech/bridge/l2"
"github.com/scroll-tech/go-ethereum/accounts/abi/bind"
"github.com/scroll-tech/go-ethereum/common"
"github.com/scroll-tech/go-ethereum/core/types"
"github.com/scroll-tech/go-ethereum/rpc"
"github.com/stretchr/testify/assert"
"scroll-tech/database"
"scroll-tech/database/migrate"
"scroll-tech/database/orm"
"scroll-tech/bridge/l1"
"scroll-tech/bridge/l2"
)
func testRelayL2MessageSucceed(t *testing.T) {
@@ -35,7 +35,7 @@ func testRelayL2MessageSucceed(t *testing.T) {
defer l2Relayer.Stop()
// Create L2Watcher
confirmations := utils.ConfirmationParams{Type: utils.BlockNumberConfirmation, Number: 0}
confirmations := rpc.LatestBlockNumber
l2Watcher := l2.NewL2WatcherClient(context.Background(), l2Client, confirmations, l2Cfg.BatchProposerConfig, l2Cfg.L2MessengerAddress, db)
// Create L1Watcher

View File

@@ -2,111 +2,24 @@ package utils
import (
"context"
"encoding/json"
"fmt"
"math/big"
"regexp"
"strconv"
"github.com/scroll-tech/go-ethereum/core/types"
"github.com/scroll-tech/go-ethereum/rpc"
)
var pattern = regexp.MustCompile(`^number=(\d{1,3})$`)
// ConfirmationType defines the type of confirmation logic used by the watcher or the relayer.
type ConfirmationType int
const (
// FinalizedTagConfirmation means that we consider a block confirmed based on the "finalized" Ethereum tag.
FinalizedTagConfirmation ConfirmationType = iota
// SafeTagConfirmation means that we consider a block confirmed based on the "safe" Ethereum tag.
SafeTagConfirmation
// BlockNumberConfirmation means that we consider a block confirmed after waiting for a certain number of blocks.
BlockNumberConfirmation
)
// ConfirmationParams defines the confirmation configuration parameters used by the watcher or the relayer.
type ConfirmationParams struct {
// Type shows whether we confirm by specific block tags or by block number.
Type ConfirmationType
// Number specifies the number of blocks after which a block is considered confirmed.
// This field can only be used when Type is set to Number.
Number uint64
}
// UnmarshalJSON implements custom JSON decoding from JSON string to ConfirmationParams.
func (c *ConfirmationParams) UnmarshalJSON(input []byte) error {
var raw string
if err := json.Unmarshal(input, &raw); err != nil {
return err
}
if raw == "finalized" {
c.Type = FinalizedTagConfirmation
return nil
}
if raw == "safe" {
c.Type = SafeTagConfirmation
return nil
}
matches := pattern.FindStringSubmatch(raw)
if len(matches) != 2 {
return fmt.Errorf("invalid configuration value for confirmations: %v", raw)
}
number, err := strconv.Atoi(matches[1])
if err != nil {
return fmt.Errorf("invalid configuration value for confirmations: %v", raw)
}
c.Type = BlockNumberConfirmation
c.Number = uint64(number)
return nil
}
// MarshalJSON implements custom JSON encoding from ConfirmationParams to JSON string.
func (c *ConfirmationParams) MarshalJSON() ([]byte, error) {
var raw string
switch c.Type {
case FinalizedTagConfirmation:
raw = "finalized"
case SafeTagConfirmation:
raw = "safe"
case BlockNumberConfirmation:
raw = fmt.Sprintf("number=%d", c.Number)
default:
return nil, fmt.Errorf("unable to marshal unknown confirmation type: %v", c.Type)
}
return json.Marshal(&raw)
}
type ethClient interface {
BlockNumber(ctx context.Context) (uint64, error)
HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error)
}
// GetLatestConfirmedBlockNumber queries the RPC provider and returns the latest
// confirmed block number according to the provided confirmation parameters.
func GetLatestConfirmedBlockNumber(ctx context.Context, client ethClient, confirmations ConfirmationParams) (uint64, error) {
switch confirmations.Type {
// use eth_getBlockByNumber and a tag
case FinalizedTagConfirmation:
case SafeTagConfirmation:
// GetLatestConfirmedBlockNumber get confirmed block number by rpc.BlockNumber type.
func GetLatestConfirmedBlockNumber(ctx context.Context, client ethClient, confirm rpc.BlockNumber) (uint64, error) {
switch true {
case confirm == rpc.SafeBlockNumber || confirm == rpc.FinalizedBlockNumber:
var tag *big.Int
if confirmations.Type == FinalizedTagConfirmation {
if confirm == rpc.FinalizedBlockNumber {
tag = big.NewInt(int64(rpc.FinalizedBlockNumber))
} else {
tag = big.NewInt(int64(rpc.SafeBlockNumber))
@@ -116,29 +29,28 @@ func GetLatestConfirmedBlockNumber(ctx context.Context, client ethClient, confir
if err != nil {
return 0, err
}
if !header.Number.IsUint64() {
return 0, fmt.Errorf("received invalid block number: %v", header.Number)
if !header.Number.IsInt64() {
return 0, fmt.Errorf("received invalid block confirm: %v", header.Number)
}
return header.Number.Uint64(), nil
// use eth_blockNumber
case BlockNumberConfirmation:
case confirm == rpc.LatestBlockNumber:
number, err := client.BlockNumber(ctx)
if err != nil {
return 0, err
}
if number >= confirmations.Number {
return number - confirmations.Number, nil
return number, nil
case confirm.Int64() >= 0: // If it's positive integer, consider it as a certain confirm value.
number, err := client.BlockNumber(ctx)
if err != nil {
return 0, err
}
cfmNum := uint64(confirm.Int64())
if number >= cfmNum {
return number - cfmNum, nil
}
return 0, nil
default:
return 0, fmt.Errorf("unknown confirmation type: %v", confirmations.Type)
return 0, fmt.Errorf("unknown confirmation type: %v", confirm)
}
return 0, nil
}

View File

@@ -4,72 +4,79 @@ import (
"context"
"encoding/json"
"math/big"
"strings"
"testing"
"scroll-tech/bridge/utils"
"github.com/scroll-tech/go-ethereum/core/types"
"github.com/stretchr/testify/assert"
"github.com/scroll-tech/go-ethereum/common/math"
"github.com/scroll-tech/go-ethereum/core/types"
"github.com/scroll-tech/go-ethereum/rpc"
"scroll-tech/bridge/utils"
)
var (
tests = []struct {
input string
mustFail bool
expected rpc.BlockNumber
}{
{`"0x"`, true, rpc.BlockNumber(0)},
{`"0x0"`, false, rpc.BlockNumber(0)},
{`"0X1"`, false, rpc.BlockNumber(1)},
{`"0x00"`, true, rpc.BlockNumber(0)},
{`"0x01"`, true, rpc.BlockNumber(0)},
{`"0x1"`, false, rpc.BlockNumber(1)},
{`"0x12"`, false, rpc.BlockNumber(18)},
{`"0x7fffffffffffffff"`, false, rpc.BlockNumber(math.MaxInt64)},
{`"0x8000000000000000"`, true, rpc.BlockNumber(0)},
{"0", true, rpc.BlockNumber(0)},
{`"ff"`, true, rpc.BlockNumber(0)},
{`"safe"`, false, rpc.SafeBlockNumber},
{`"finalized"`, false, rpc.FinalizedBlockNumber},
{`"pending"`, false, rpc.PendingBlockNumber},
{`"latest"`, false, rpc.LatestBlockNumber},
{`"earliest"`, false, rpc.EarliestBlockNumber},
{`someString`, true, rpc.BlockNumber(0)},
{`""`, true, rpc.BlockNumber(0)},
{``, true, rpc.BlockNumber(0)},
}
)
func TestUnmarshalJSON(t *testing.T) {
var params utils.ConfirmationParams
decoder := json.NewDecoder(strings.NewReader(`"finalized"`))
decoder.DisallowUnknownFields()
err := decoder.Decode(&params)
assert.Nil(t, err)
assert.Equal(t, utils.FinalizedTagConfirmation, params.Type)
decoder = json.NewDecoder(strings.NewReader(`"safe"`))
decoder.DisallowUnknownFields()
err = decoder.Decode(&params)
assert.Nil(t, err)
assert.Equal(t, utils.SafeTagConfirmation, params.Type)
decoder = json.NewDecoder(strings.NewReader(`"number=6"`))
decoder.DisallowUnknownFields()
err = decoder.Decode(&params)
assert.Nil(t, err)
assert.Equal(t, utils.BlockNumberConfirmation, params.Type)
assert.Equal(t, uint64(6), params.Number)
decoder = json.NewDecoder(strings.NewReader(`"number=999"`))
decoder.DisallowUnknownFields()
err = decoder.Decode(&params)
assert.Nil(t, err)
assert.Equal(t, utils.BlockNumberConfirmation, params.Type)
assert.Equal(t, uint64(999), params.Number)
decoder = json.NewDecoder(strings.NewReader(`"number=1000"`))
decoder.DisallowUnknownFields()
err = decoder.Decode(&params)
assert.NotNil(t, err)
decoder = json.NewDecoder(strings.NewReader(`"number=6x"`))
decoder.DisallowUnknownFields()
err = decoder.Decode(&params)
assert.NotNil(t, err)
decoder = json.NewDecoder(strings.NewReader(`"latest"`))
decoder.DisallowUnknownFields()
err = decoder.Decode(&params)
assert.NotNil(t, err)
for i, test := range tests {
var num rpc.BlockNumber
err := json.Unmarshal([]byte(test.input), &num)
if test.mustFail && err == nil {
t.Errorf("Test %d should fail", i)
continue
}
if !test.mustFail && err != nil {
t.Errorf("Test %d should pass but got err: %v", i, err)
continue
}
if num != test.expected {
t.Errorf("Test %d got unexpected value, want %d, got %d", i, test.expected, num)
}
}
}
func TestMarshalJSON(t *testing.T) {
bytes, err := json.Marshal(&utils.ConfirmationParams{Type: utils.FinalizedTagConfirmation, Number: 6})
assert.Nil(t, err)
assert.Equal(t, `"finalized"`, string(bytes))
for i, test := range tests {
var num rpc.BlockNumber
want, err := json.Marshal(test.expected)
assert.Nil(t, err)
if !test.mustFail {
err = json.Unmarshal([]byte(test.input), &num)
assert.Nil(t, err)
got, err := json.Marshal(&num)
assert.Nil(t, err)
if string(want) != string(got) {
t.Errorf("Test %d got unexpected value, want %d, got %d", i, test.expected, num)
bytes, err = json.Marshal(&utils.ConfirmationParams{Type: utils.SafeTagConfirmation, Number: 6})
assert.Nil(t, err)
assert.Equal(t, `"safe"`, string(bytes))
bytes, err = json.Marshal(&utils.ConfirmationParams{Type: utils.BlockNumberConfirmation, Number: 6})
assert.Nil(t, err)
assert.Equal(t, `"number=6"`, string(bytes))
}
}
}
}
type MockEthClient struct {
@@ -89,12 +96,12 @@ func TestGetLatestConfirmedBlockNumber(t *testing.T) {
client := MockEthClient{}
client.val = 5
confirmed, err := utils.GetLatestConfirmedBlockNumber(ctx, &client, utils.ConfirmationParams{Type: utils.BlockNumberConfirmation, Number: 6})
confirmed, err := utils.GetLatestConfirmedBlockNumber(ctx, &client, 6)
assert.Nil(t, err)
assert.Equal(t, uint64(0), confirmed)
client.val = 7
confirmed, err = utils.GetLatestConfirmedBlockNumber(ctx, &client, utils.ConfirmationParams{Type: utils.BlockNumberConfirmation, Number: 6})
confirmed, err = utils.GetLatestConfirmedBlockNumber(ctx, &client, 6)
assert.Nil(t, err)
assert.Equal(t, uint64(1), confirmed)
}

View File

@@ -5,7 +5,7 @@ import (
"runtime/debug"
)
var tag = "prealpha-v12.1"
var tag = "prealpha-v13.1"
var commit = func() string {
if info, ok := debug.ReadBuildInfo(); ok {

View File

@@ -13,18 +13,34 @@ import { L2ERC721Gateway } from "../../src/L2/gateways/L2ERC721Gateway.sol";
import { L2GatewayRouter } from "../../src/L2/gateways/L2GatewayRouter.sol";
import { L2ScrollMessenger } from "../../src/L2/L2ScrollMessenger.sol";
import { L2StandardERC20Gateway } from "../../src/L2/gateways/L2StandardERC20Gateway.sol";
import { L2TxFeeVault } from "../../src/L2/predeploys/L2TxFeeVault.sol";
import { Whitelist } from "../../src/L2/predeploys/Whitelist.sol";
import { ScrollStandardERC20 } from "../../src/libraries/token/ScrollStandardERC20.sol";
import { ScrollStandardERC20Factory } from "../../src/libraries/token/ScrollStandardERC20Factory.sol";
contract DeployL2BridgeContracts is Script {
uint256 L2_DEPLOYER_PRIVATE_KEY = vm.envUint("L2_DEPLOYER_PRIVATE_KEY");
address L1_TX_FEE_RECIPIENT_ADDR = vm.envAddress("L1_TX_FEE_RECIPIENT_ADDR");
L2ScrollMessenger messenger;
ProxyAdmin proxyAdmin;
address L2_SCROLL_MESSENGER_PREDEPLOY_ADDR = vm.envOr("L2_SCROLL_MESSENGER_PREDEPLOY_ADDR", address(0));
address L2_TX_FEE_VAULT_PREDEPLOY_ADDR = vm.envOr("L2_TX_FEE_VAULT_PREDEPLOY_ADDR", address(0));
address L2_PROXY_ADMIN_PREDEPLOY_ADDR = vm.envOr("L2_PROXY_ADMIN_PREDEPLOY_ADDR", address(0));
address L2_STANDARD_ERC20_GATEWAY_PROXY_PREDEPLOY_ADDR = vm.envOr("L2_STANDARD_ERC20_GATEWAY_PROXY_PREDEPLOY_ADDR", address(0));
address L2_GATEWAY_ROUTER_PROXY_PREDEPLOY_ADDR = vm.envOr("L2_GATEWAY_ROUTER_PROXY_PREDEPLOY_ADDR", address(0));
address L2_SCROLL_STANDARD_ERC20_FACTORY_PREDEPLOY_ADDR = vm.envOr("L2_SCROLL_STANDARD_ERC20_FACTORY_PREDEPLOY_ADDR", address(0));
address L2_CUSTOM_ERC20_GATEWAY_PROXY_PREDEPLOY_ADDR = vm.envOr("L2_CUSTOM_ERC20_GATEWAY_PROXY_PREDEPLOY_ADDR", address(0));
address L2_ERC721_GATEWAY_PROXY_PREDEPLOY_ADDR = vm.envOr("L2_ERC721_GATEWAY_PROXY_PREDEPLOY_ADDR", address(0));
address L2_ERC1155_GATEWAY_PROXY_PREDEPLOY_ADDR = vm.envOr("L2_ERC1155_GATEWAY_PROXY_PREDEPLOY_ADDR", address(0));
address L2_WHITELIST_PREDEPLOY_ADDR = vm.envOr("L2_WHITELIST_PREDEPLOY_ADDR", address(0));
function run() external {
vm.startBroadcast(L2_DEPLOYER_PRIVATE_KEY);
deployL2ScrollMessenger();
deployTxFeeVault();
deployProxyAdmin();
deployL2StandardERC20Gateway();
deployL2GatewayRouter();
@@ -38,19 +54,45 @@ contract DeployL2BridgeContracts is Script {
}
function deployL2ScrollMessenger() internal {
address owner = vm.addr(L2_DEPLOYER_PRIVATE_KEY);
L2ScrollMessenger l2ScrollMessenger = new L2ScrollMessenger(owner);
if (L2_SCROLL_MESSENGER_PREDEPLOY_ADDR != address(0)) {
logAddress("L2_SCROLL_MESSENGER_ADDR", address(L2_SCROLL_MESSENGER_PREDEPLOY_ADDR));
return;
}
logAddress("L2_SCROLL_MESSENGER_ADDR", address(l2ScrollMessenger));
address owner = vm.addr(L2_DEPLOYER_PRIVATE_KEY);
messenger = new L2ScrollMessenger(owner);
logAddress("L2_SCROLL_MESSENGER_ADDR", address(messenger));
}
function deployTxFeeVault() internal {
if (L2_TX_FEE_VAULT_PREDEPLOY_ADDR != address(0)) {
logAddress("L2_TX_FEE_VAULT_ADDR", address(L2_TX_FEE_VAULT_PREDEPLOY_ADDR));
return;
}
L2TxFeeVault feeVault = new L2TxFeeVault(address(messenger), L1_TX_FEE_RECIPIENT_ADDR);
logAddress("L2_TX_FEE_VAULT_ADDR", address(feeVault));
}
function deployProxyAdmin() internal {
if (L2_PROXY_ADMIN_PREDEPLOY_ADDR != address(0)) {
logAddress("L2_PROXY_ADMIN_ADDR", address(L2_PROXY_ADMIN_PREDEPLOY_ADDR));
return;
}
proxyAdmin = new ProxyAdmin();
logAddress("L2_PROXY_ADMIN_ADDR", address(proxyAdmin));
}
function deployL2StandardERC20Gateway() internal {
if (L2_STANDARD_ERC20_GATEWAY_PROXY_PREDEPLOY_ADDR != address(0)) {
logAddress("L2_STANDARD_ERC20_GATEWAY_PROXY_ADDR", address(L2_STANDARD_ERC20_GATEWAY_PROXY_PREDEPLOY_ADDR));
return;
}
L2StandardERC20Gateway impl = new L2StandardERC20Gateway();
TransparentUpgradeableProxy proxy = new TransparentUpgradeableProxy(address(impl), address(proxyAdmin), new bytes(0));
@@ -59,6 +101,11 @@ contract DeployL2BridgeContracts is Script {
}
function deployL2GatewayRouter() internal {
if (L2_GATEWAY_ROUTER_PROXY_PREDEPLOY_ADDR != address(0)) {
logAddress("L2_GATEWAY_ROUTER_PROXY_ADDR", address(L2_GATEWAY_ROUTER_PROXY_PREDEPLOY_ADDR));
return;
}
L2GatewayRouter impl = new L2GatewayRouter();
TransparentUpgradeableProxy proxy = new TransparentUpgradeableProxy(address(impl), address(proxyAdmin), new bytes(0));
@@ -67,6 +114,11 @@ contract DeployL2BridgeContracts is Script {
}
function deployScrollStandardERC20Factory() internal {
if (L2_SCROLL_STANDARD_ERC20_FACTORY_PREDEPLOY_ADDR != address(0)) {
logAddress("L2_SCROLL_STANDARD_ERC20_FACTORY_ADDR", address(L2_SCROLL_STANDARD_ERC20_FACTORY_PREDEPLOY_ADDR));
return;
}
ScrollStandardERC20 tokenImpl = new ScrollStandardERC20();
ScrollStandardERC20Factory scrollStandardERC20Factory = new ScrollStandardERC20Factory(address(tokenImpl));
@@ -75,6 +127,11 @@ contract DeployL2BridgeContracts is Script {
}
function deployL2CustomERC20Gateway() internal {
if (L2_CUSTOM_ERC20_GATEWAY_PROXY_PREDEPLOY_ADDR != address(0)) {
logAddress("L2_CUSTOM_ERC20_GATEWAY_PROXY_ADDR", address(L2_CUSTOM_ERC20_GATEWAY_PROXY_PREDEPLOY_ADDR));
return;
}
L2CustomERC20Gateway impl = new L2CustomERC20Gateway();
TransparentUpgradeableProxy proxy = new TransparentUpgradeableProxy(address(impl), address(proxyAdmin), new bytes(0));
@@ -83,6 +140,11 @@ contract DeployL2BridgeContracts is Script {
}
function deployL2ERC721Gateway() internal {
if (L2_ERC721_GATEWAY_PROXY_PREDEPLOY_ADDR != address(0)) {
logAddress("L2_ERC721_GATEWAY_PROXY_ADDR", address(L2_ERC721_GATEWAY_PROXY_PREDEPLOY_ADDR));
return;
}
L2ERC721Gateway impl = new L2ERC721Gateway();
TransparentUpgradeableProxy proxy = new TransparentUpgradeableProxy(address(impl), address(proxyAdmin), new bytes(0));
@@ -91,6 +153,11 @@ contract DeployL2BridgeContracts is Script {
}
function deployL2ERC1155Gateway() internal {
if (L2_ERC1155_GATEWAY_PROXY_PREDEPLOY_ADDR != address(0)) {
logAddress("L2_ERC1155_GATEWAY_PROXY_ADDR", address(L2_ERC1155_GATEWAY_PROXY_PREDEPLOY_ADDR));
return;
}
L2ERC1155Gateway impl = new L2ERC1155Gateway();
TransparentUpgradeableProxy proxy = new TransparentUpgradeableProxy(address(impl), address(proxyAdmin), new bytes(0));
@@ -99,6 +166,11 @@ contract DeployL2BridgeContracts is Script {
}
function deployL2Whitelist() internal {
if (L2_WHITELIST_PREDEPLOY_ADDR != address(0)) {
logAddress("L2_WHITELIST_ADDR", address(L2_WHITELIST_PREDEPLOY_ADDR));
return;
}
address owner = vm.addr(L2_DEPLOYER_PRIVATE_KEY);
Whitelist whitelist = new Whitelist(owner);

View File

@@ -22,6 +22,7 @@ contract InitializeL2BridgeContracts is Script {
address L1_ERC1155_GATEWAY_PROXY_ADDR = vm.envAddress("L1_ERC1155_GATEWAY_PROXY_ADDR");
address L2_SCROLL_MESSENGER_ADDR = vm.envAddress("L2_SCROLL_MESSENGER_ADDR");
address L2_TX_FEE_VAULT_ADDR = vm.envAddress("L2_TX_FEE_VAULT_ADDR");
address L2_STANDARD_ERC20_GATEWAY_PROXY_ADDR = vm.envAddress("L2_STANDARD_ERC20_GATEWAY_PROXY_ADDR");
address L2_GATEWAY_ROUTER_PROXY_ADDR = vm.envAddress("L2_GATEWAY_ROUTER_PROXY_ADDR");
address L2_SCROLL_STANDARD_ERC20_FACTORY_ADDR = vm.envAddress("L2_SCROLL_STANDARD_ERC20_FACTORY_ADDR");
@@ -74,12 +75,13 @@ contract InitializeL2BridgeContracts is Script {
// whitelist contracts which can call sendMessage
{
address[] memory gateways = new address[](5);
address[] memory gateways = new address[](6);
gateways[0] = L2_STANDARD_ERC20_GATEWAY_PROXY_ADDR;
gateways[1] = L2_GATEWAY_ROUTER_PROXY_ADDR;
gateways[2] = L2_CUSTOM_ERC20_GATEWAY_PROXY_ADDR;
gateways[3] = L2_ERC1155_GATEWAY_PROXY_ADDR;
gateways[4] = L2_ERC721_GATEWAY_PROXY_ADDR;
gateways[5] = L2_TX_FEE_VAULT_ADDR;
Whitelist(L2_WHITELIST_ADDR).updateWhitelistStatus(gateways, true);
}

View File

@@ -0,0 +1,14 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import { FeeVault } from "../../libraries/FeeVault.sol";
/// @title L2TxFeeVault
/// @notice The `L2TxFeeVault` contract collects all L2 transaction fees and allows withdrawing these fees to a predefined L1 address.
/// The minimum withdrawal amount is 10 ether.
contract L2TxFeeVault is FeeVault {
/// @param _messenger The address of L2ScrollMessenger.
/// @param _recipient The fee recipient address on L1.
constructor(address _messenger, address _recipient) FeeVault(_messenger, _recipient, 10 ether) {}
}

View File

@@ -0,0 +1,108 @@
// SPDX-License-Identifier: MIT
// MIT License
// Copyright (c) 2022 Optimism
// Copyright (c) 2022 Scroll
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
pragma solidity ^0.8.0;
import { IL2ScrollMessenger } from "../L2/IL2ScrollMessenger.sol";
/**
* @title FeeVault
* @notice The FeeVault contract contains the basic logic for the various different vault contracts
* used to hold fee revenue generated by the L2 system.
*/
abstract contract FeeVault {
/**
* @notice Emits each time that a withdrawal occurs.
*
* @param value Amount that was withdrawn (in wei).
* @param to Address that the funds were sent to.
* @param from Address that triggered the withdrawal.
*/
event Withdrawal(uint256 value, address to, address from);
/**
* @notice Minimum balance before a withdrawal can be triggered.
*/
uint256 public MIN_WITHDRAWAL_AMOUNT;
/**
* @notice Scroll L2 messenger address.
*/
address public MESSENGER;
/**
* @notice Wallet that will receive the fees on L1.
*/
address public RECIPIENT;
/**
* @notice Total amount of wei processed by the contract.
*/
uint256 public totalProcessed;
/**
* @param _recipient Wallet that will receive the fees on L1.
* @param _minWithdrawalAmount Minimum balance before a withdrawal can be triggered.
*/
constructor(
address _messenger,
address _recipient,
uint256 _minWithdrawalAmount
) {
MIN_WITHDRAWAL_AMOUNT = _minWithdrawalAmount;
MESSENGER = _messenger;
RECIPIENT = _recipient;
}
/**
* @notice Allow the contract to receive ETH.
*/
receive() external payable {}
/**
* @notice Triggers a withdrawal of funds to the L1 fee wallet.
*/
function withdraw() external {
uint256 value = address(this).balance;
require(
value >= MIN_WITHDRAWAL_AMOUNT,
"FeeVault: withdrawal amount must be greater than minimum withdrawal amount"
);
unchecked {
totalProcessed += value;
}
emit Withdrawal(value, RECIPIENT, msg.sender);
IL2ScrollMessenger(MESSENGER).sendMessage{ value: value }(
RECIPIENT,
0, // no fee provided
bytes(""), // no message (simple eth transfer)
0 // _gasLimit is not used for eth transfers
);
}
}

View File

@@ -0,0 +1,43 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import { DSTestPlus } from "solmate/test/utils/DSTestPlus.sol";
import { MockScrollMessenger } from "./mocks/MockScrollMessenger.sol";
import { L2TxFeeVault } from "../L2/predeploys/L2TxFeeVault.sol";
contract L2TxFeeVaultTest is DSTestPlus {
MockScrollMessenger private messenger;
L2TxFeeVault private vault;
function setUp() public {
messenger = new MockScrollMessenger();
vault = new L2TxFeeVault(address(messenger), address(1));
}
function testCantWithdrawBelowMinimum() public {
hevm.deal(address(vault), 9 ether);
hevm.expectRevert("FeeVault: withdrawal amount must be greater than minimum withdrawal amount");
vault.withdraw();
}
function testWithdrawOnce() public {
hevm.deal(address(vault), 11 ether);
vault.withdraw();
assertEq(address(messenger).balance, 11 ether);
assertEq(vault.totalProcessed(), 11 ether);
}
function testWithdrawTwice() public {
hevm.deal(address(vault), 11 ether);
vault.withdraw();
assertEq(address(messenger).balance, 11 ether);
assertEq(vault.totalProcessed(), 11 ether);
hevm.deal(address(vault), 22 ether);
vault.withdraw();
assertEq(address(messenger).balance, 33 ether);
assertEq(vault.totalProcessed(), 33 ether);
}
}

View File

@@ -13,6 +13,7 @@ import (
"time"
"github.com/scroll-tech/go-ethereum/crypto"
"github.com/scroll-tech/go-ethereum/rpc"
"github.com/stretchr/testify/assert"
"scroll-tech/database"
@@ -28,8 +29,6 @@ import (
"scroll-tech/common/cmd"
"scroll-tech/common/docker"
"scroll-tech/bridge/utils"
_ "scroll-tech/coordinator/cmd/app"
coordinatorConfig "scroll-tech/coordinator/config"
)
@@ -121,7 +120,7 @@ func runSender(t *testing.T, endpoint string) *sender.Sender {
Endpoint: endpoint,
CheckPendingTime: 3,
EscalateBlocks: 100,
Confirmations: utils.ConfirmationParams{Type: utils.BlockNumberConfirmation, Number: 0},
Confirmations: rpc.LatestBlockNumber,
EscalateMultipleNum: 11,
EscalateMultipleDen: 10,
TxType: "LegacyTx",