Skip to content

Commit

Permalink
chore: apply review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
bendanzhentan committed Dec 8, 2023
1 parent bdf1614 commit 558e51b
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 27 deletions.
6 changes: 3 additions & 3 deletions core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ type L2StandardBridgeBotConfig struct {
// process only whitelists the tokens in this list and ignore tokens not in this list.
//
// **IMPORTANT: If this list is empty (by default), all L2 tokens are whitelisted.**
WhitelistL2TokenList []string `toml:"whitelist-l2-token-list"`
WhitelistL2TokenList *[]string `toml:"whitelist-l2-token-list"`

// UpperMinGasLimit is the upper limit of the minimum gas limit of the L2StandardBridgeBot contract.
//
// **IMPORTANT: If this value is 0 (by default), the L2StandardBridgeBot contract doesn't limit the minimum gas limit.**
UpperMinGasLimit uint32 `toml:"upper-min-gas-limit"`
UpperMinGasLimit *uint32 `toml:"upper-min-gas-limit"`

// UpperExtraDataSize is the upper limit of the extra data size of the L2StandardBridgeBot contract.
//
// **IMPORTANT: If this value is 0 (by default), the L2StandardBridgeBot contract doesn't limit the extra data size.**
UpperExtraDataSize uint32 `toml:"upper-extra-data-size"`
UpperExtraDataSize *uint32 `toml:"upper-extra-data-size"`
}

// LoadConfig loads the `bot.toml` config file from a given path
Expand Down
67 changes: 44 additions & 23 deletions core/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type Processor struct {

cfg Config
L2Contracts config.L2Contracts

whitelistL2TokenMap map[common.Address]struct{}
}

func NewProcessor(
Expand All @@ -33,10 +35,16 @@ func NewProcessor(
l2Client *ClientExt,
cfg Config,
) *Processor {
log = log.New("processor", "Processor")

l2Contracts := config.L2ContractsFromPredeploys()
return &Processor{log, l1Client, l2Client, cfg, l2Contracts}

var whitelistL2TokenMap map[common.Address]struct{} = nil
if cfg.L2StandardBridgeBot.WhitelistL2TokenList != nil {
for _, l2Token := range *cfg.L2StandardBridgeBot.WhitelistL2TokenList {
whitelistL2TokenMap[common.HexToAddress(l2Token)] = struct{}{}
}
}

return &Processor{log, l1Client, l2Client, cfg, l2Contracts, whitelistL2TokenMap}
}

func (b *Processor) toWithdrawal(botDelegatedWithdrawToEvent *L2ContractEvent, receipt *types.Receipt) (*bindings.TypesWithdrawalTransaction, error) {
Expand Down Expand Up @@ -538,30 +546,43 @@ func (b *Processor) CheckByFilterOptions(botDelegatedWithdrawToEvent *L2Contract
return fmt.Errorf("parse non-indexed event arguments from log.data of L2StandardBridgeBotWithdrawTo event, err: %v", err)
}

if len(b.cfg.L2StandardBridgeBot.WhitelistL2TokenList) > 0 {
isWhitelisted := false
for _, tokenAddress := range b.cfg.L2StandardBridgeBot.WhitelistL2TokenList {
if withdrawToEvent.L2Token.Cmp(common.HexToAddress(tokenAddress)) == 0 {
isWhitelisted = true
break
}
}
if !isWhitelisted {
return fmt.Errorf("filtered: token is not whitelisted, l2-token: %s", withdrawToEvent.L2Token)
}
if !IsL2TokenWhitelisted(b.whitelistL2TokenMap, &withdrawToEvent.L2Token) {
return fmt.Errorf("filtered: token is not whitelisted, l2-token: %s", withdrawToEvent.L2Token)
}
if !IsMinGasLimitValid(b.cfg.L2StandardBridgeBot.UpperMinGasLimit, withdrawToEvent.MinGasLimit) {
return fmt.Errorf("filtered: minGasLimit is too large, minGasLimit: %d", withdrawToEvent.MinGasLimit)
}
if !IsExtraDataValid(b.cfg.L2StandardBridgeBot.UpperMinGasLimit, &withdrawToEvent.ExtraData) {
return fmt.Errorf("filtered: extraData is too large, extraDataSize: %d", len(withdrawToEvent.ExtraData))
}

if b.cfg.L2StandardBridgeBot.UpperMinGasLimit > 0 {
if withdrawToEvent.MinGasLimit > b.cfg.L2StandardBridgeBot.UpperMinGasLimit {
return fmt.Errorf("filtered: minGasLimit is too large, minGasLimit: %d", withdrawToEvent.MinGasLimit)
}
return nil
}

func IsL2TokenWhitelisted(whitelistL2TokenMap map[common.Address]struct{}, l2Token *common.Address) bool {
// nil means all L2 tokens are whitelisted
if whitelistL2TokenMap == nil {
return true
}

if b.cfg.L2StandardBridgeBot.UpperExtraDataSize > 0 {
if len(withdrawToEvent.ExtraData) > int(b.cfg.L2StandardBridgeBot.UpperExtraDataSize) {
return fmt.Errorf("filtered: extraData is too large, extraDataSize: %d", len(withdrawToEvent.ExtraData))
}
_, exists := whitelistL2TokenMap[*l2Token]
return exists
}

func IsMinGasLimitValid(upperMinGasLimit *uint32, minGasLimit uint32) bool {
// nil means no limit
if upperMinGasLimit == nil {
return true
}

return nil
return minGasLimit <= *upperMinGasLimit
}

func IsExtraDataValid(upperExtraDataSize *uint32, extraData *[]byte) bool {
// nil means no limit
if upperExtraDataSize == nil {
return true
}

return len(*extraData) <= int(*upperExtraDataSize)
}
52 changes: 52 additions & 0 deletions core/processor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package core_test

import (
"bnbchain/opbnb-bridge-bot/core"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/assert"
)

func TestIsL2TokenWhitelisted(t *testing.T) {
// Test case 1: whitelistL2TokenMap is nil
var whitelistL2TokenMap map[common.Address]struct{} = nil
l2Token := common.HexToAddress("0x1234abcdef")
assert.True(t, core.IsL2TokenWhitelisted(whitelistL2TokenMap, &l2Token))

// Test case 2: l2Token is whitelisted
whitelistL2TokenMap = make(map[common.Address]struct{})
whitelistL2TokenMap[l2Token] = struct{}{}
assert.True(t, core.IsL2TokenWhitelisted(whitelistL2TokenMap, &l2Token))

// Test case 3: l2Token is not whitelisted
whitelistL2TokenMap = make(map[common.Address]struct{})
anotherL2Token := &common.Address{}
assert.False(t, core.IsL2TokenWhitelisted(whitelistL2TokenMap, anotherL2Token))

// Test case 4: checksum formated l2Token is whitelisted
whitelistL2TokenMap = make(map[common.Address]struct{})
whitelistL2TokenMap[l2Token] = struct{}{}
checksumL2Token := common.HexToAddress("0x1234ABCDEF")
assert.True(t, core.IsL2TokenWhitelisted(whitelistL2TokenMap, &checksumL2Token))
}
func TestIsMinGasLimitValid(t *testing.T) {
// Test case 1: upperMinGasLimit is nil
var upperMinGasLimit *uint32 = nil
minGasLimit := uint32(100)
assert.True(t, core.IsMinGasLimitValid(upperMinGasLimit, minGasLimit))

// Test case 2: minGasLimit is less than or equal to upperMinGasLimit
upperMinGasLimit = uint32Ptr(200)
minGasLimit = uint32(200)
assert.True(t, core.IsMinGasLimitValid(upperMinGasLimit, minGasLimit))

// Test case 3: minGasLimit is greater than upperMinGasLimit
upperMinGasLimit = uint32Ptr(200)
minGasLimit = uint32(250)
assert.False(t, core.IsMinGasLimitValid(upperMinGasLimit, minGasLimit))
}

func uint32Ptr(n uint32) *uint32 {
return &n
}
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ require (
github.com/BurntSushi/toml v1.3.2
github.com/ethereum-optimism/optimism v1.2.0
github.com/ethereum/go-ethereum v1.13.4
github.com/stretchr/testify v1.8.4
github.com/urfave/cli/v2 v2.25.7
golang.org/x/crypto v0.14.0
gorm.io/driver/postgres v1.5.4
gorm.io/gorm v1.25.5
)
Expand All @@ -28,6 +30,7 @@ require (
github.com/consensys/gnark-crypto v0.12.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/crate-crypto/go-kzg-4844 v0.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set/v2 v2.1.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/ethereum/c-kzg-4844 v0.3.1 // indirect
Expand Down Expand Up @@ -55,6 +58,7 @@ require (
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.17.0 // indirect
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 // indirect
github.com/prometheus/common v0.44.0 // indirect
Expand All @@ -69,7 +73,6 @@ require (
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/sync v0.3.0 // indirect
Expand All @@ -78,5 +81,6 @@ require (
golang.org/x/text v0.13.0 // indirect
golang.org/x/tools v0.13.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
rsc.io/tmplfunc v0.0.3 // indirect
)

0 comments on commit 558e51b

Please sign in to comment.