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: add an ignored token list #121

Merged
merged 6 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 6 additions & 2 deletions conf/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
"message_queue": "0xF0B2293F5D834eAe920c6974D50957A1732de763",
"scroll_chain": "0x2D567EcE699Eabe5afCd141eDB7A4f2D0D6ce8a0"
},
"start_messenger_balance": 10000000000000000000
"start_messenger_balance": 10000000000000000000,
"ignored_tokens": [
"0x70fDD35857f4A765C1416ABa83ed5B19e49F27b9"
]
},
"l2_config": {
"l2_url": "<l2 node rpc url>",
Expand All @@ -38,7 +41,8 @@
},
"scroll_messenger": "0xBa50f5340FB9F3Bd074bD638c9BE13eCB36E603d",
"message_queue": "0x5300000000000000000000000000000000000000"
}
},
"ignored_tokens": []
},
"slack_webhook_config": {
"webhook_url": "<slack notify channel>",
Expand Down
14 changes: 8 additions & 6 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ type L1Contracts struct {
type L1Config struct {
L1URL string `json:"l1_url"`
Confirm rpc.BlockNumber
L1Contracts *L1Contracts `json:"l1_contracts"`
StartNumber uint64 `json:"start_number"`
StartMessengerBalance uint64 `json:"start_messenger_balance"`
L1Contracts *L1Contracts `json:"l1_contracts"`
StartNumber uint64 `json:"start_number"`
StartMessengerBalance uint64 `json:"start_messenger_balance"`
IgnoredTokens []common.Address `json:"ignored_tokens"`
}

// L2Contracts l1chain config.
Expand All @@ -53,9 +54,10 @@ type L2Contracts struct {

// L2Config l1 chain config.
type L2Config struct {
L2URL string `json:"l2_url"`
Confirm rpc.BlockNumber
L2Contracts *L2Contracts `json:"l2_contracts"`
L2URL string `json:"l2_url"`
Confirm rpc.BlockNumber
L2Contracts *L2Contracts `json:"l2_contracts"`
IgnoredTokens []common.Address `json:"ignored_tokens"`
}

// SlackWebhookConfig slack webhook config.
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func NewContractController(conf *config.Config, db *gorm.DB, l1Client, l2Client
conf: conf,
eventGatherLogic: events.NewEventGather(),
contractsLogic: contracts.NewContracts(ethclient.NewClient(l1Client), ethclient.NewClient(l2Client)),
messageMatchAssembler: assembler.NewMessageMatchAssembler(db),
messageMatchAssembler: assembler.NewMessageMatchAssembler(conf, db),
messageMatchLogic: messagematch.NewMessageMatchLogic(conf, db),
stopContractChan: make(chan struct{}),
db: db,
Expand Down
5 changes: 3 additions & 2 deletions internal/logic/assembler/assembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/scroll-tech/go-ethereum/rpc"
"gorm.io/gorm"

"github.com/scroll-tech/chain-monitor/internal/config"
"github.com/scroll-tech/chain-monitor/internal/logic/events"
"github.com/scroll-tech/chain-monitor/internal/orm"
"github.com/scroll-tech/chain-monitor/internal/types"
Expand All @@ -27,10 +28,10 @@ type MessageMatchAssembler struct {
}

// NewMessageMatchAssembler returns a new message match instance.
func NewMessageMatchAssembler(db *gorm.DB) *MessageMatchAssembler {
func NewMessageMatchAssembler(conf *config.Config, db *gorm.DB) *MessageMatchAssembler {
return &MessageMatchAssembler{
messengerMessageMatchOrm: orm.NewMessengerMessageMatch(db),
transferMatcher: NewTransferEventMatcher(),
transferMatcher: NewTransferEventMatcher(conf),
}
}

Expand Down
75 changes: 54 additions & 21 deletions internal/logic/assembler/transfer_event_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/scroll-tech/go-ethereum/common"
"github.com/scroll-tech/go-ethereum/log"

"github.com/scroll-tech/chain-monitor/internal/config"
"github.com/scroll-tech/chain-monitor/internal/logic/events"
"github.com/scroll-tech/chain-monitor/internal/logic/slack"
"github.com/scroll-tech/chain-monitor/internal/types"
Expand Down Expand Up @@ -47,11 +48,30 @@ type matcherValue struct {
}

// TransferEventMatcher checks the existence of an event and consistency of the transferred amount.
type TransferEventMatcher struct{}
type TransferEventMatcher struct {
l1IgnoredTokens map[common.Address]struct{}
l2IgnoredTokens map[common.Address]struct{}
}

// NewTransferEventMatcher creates a new instance of TransferEventMatcher.
func NewTransferEventMatcher() *TransferEventMatcher {
return &TransferEventMatcher{}
func NewTransferEventMatcher(conf *config.Config) *TransferEventMatcher {
t := &TransferEventMatcher{
l1IgnoredTokens: make(map[common.Address]struct{}),
l2IgnoredTokens: make(map[common.Address]struct{}),
}

for _, token := range conf.L1Config.IgnoredTokens {
t.l1IgnoredTokens[token] = struct{}{}
}

for _, token := range conf.L2Config.IgnoredTokens {
t.l2IgnoredTokens[token] = struct{}{}
}

// Log the ignored tokens
log.Info("Ignored Tokens", "L1", t.l1IgnoredTokens, "L2", t.l2IgnoredTokens)

return t
}

func (t *TransferEventMatcher) erc20Matcher(transferEvents, gatewayEvents []events.ERC20GatewayEventUnmarshaler) error {
Expand Down Expand Up @@ -135,9 +155,7 @@ func (t *TransferEventMatcher) erc20Matcher(transferEvents, gatewayEvents []even
"gateway balance", info.GatewayBalance.String(),
"err info", info.Error,
)
slack.Notify(slack.MrkDwnGatewayTransferMessage(info))
return fmt.Errorf("balance mismatch for token %s: transfer balance = %s, gateway balance = %s, info = %v",
info.TokenAddress.Hex(), info.TransferBalance.String(), info.GatewayBalance.String(), info)
return t.sendSlackAlert(info)
}
}

Expand Down Expand Up @@ -169,9 +187,7 @@ func (t *TransferEventMatcher) erc20Matcher(transferEvents, gatewayEvents []even
"gateway balance", info.GatewayBalance.String(),
"err info", info.Error,
)
slack.Notify(slack.MrkDwnGatewayTransferMessage(info))
return fmt.Errorf("balance mismatch for token %s: gateway balance = %s, transfer balance = %s, info = %v",
info.TokenAddress.Hex(), info.GatewayBalance.String(), info.TransferBalance.String(), info)
return t.sendSlackAlert(info)
}
}
return nil
Expand Down Expand Up @@ -276,9 +292,7 @@ func (t *TransferEventMatcher) erc721Matcher(transferEvents, gatewayEvents []eve
"gateway balance", info.GatewayBalance.String(),
"err info", info.Error,
)
slack.Notify(slack.MrkDwnGatewayTransferMessage(info))
return fmt.Errorf("erc721 mismatch for tokenAddress %s: transfer amount = %s, gateway amount = %s",
info.TokenAddress.Hex(), info.TransferBalance.String(), info.GatewayBalance.String())
return t.sendSlackAlert(info)
}
}

Expand Down Expand Up @@ -310,9 +324,7 @@ func (t *TransferEventMatcher) erc721Matcher(transferEvents, gatewayEvents []eve
"gateway balance", info.GatewayBalance.String(),
"err info", info.Error,
)
slack.Notify(slack.MrkDwnGatewayTransferMessage(info))
return fmt.Errorf("erc721 mismatch for tokenAddress %s: gateway amount = %s, transfer amount = %s",
georgehao marked this conversation as resolved.
Show resolved Hide resolved
info.TokenAddress.Hex(), info.GatewayBalance.String(), info.TransferBalance.String())
return t.sendSlackAlert(info)
}
}

Expand Down Expand Up @@ -416,9 +428,7 @@ func (t *TransferEventMatcher) erc1155Matcher(transferEvents, gatewayEvents []ev
"gateway balance", info.GatewayBalance.String(),
"err info", info.Error,
)
slack.Notify(slack.MrkDwnGatewayTransferMessage(info))
return fmt.Errorf("erc1155 mismatch for tokenAddress %s: transfer amount = %s, gateway amount = %s",
info.TokenAddress.Hex(), info.TransferBalance.String(), info.GatewayBalance.String())
return t.sendSlackAlert(info)
}
}

Expand Down Expand Up @@ -450,11 +460,34 @@ func (t *TransferEventMatcher) erc1155Matcher(transferEvents, gatewayEvents []ev
"gateway balance", info.GatewayBalance.String(),
"err info", info.Error,
)
slack.Notify(slack.MrkDwnGatewayTransferMessage(info))
return fmt.Errorf("erc1155 mismatch for token %s: gateway amount = %s, transfer amount = %s",
info.TokenAddress.Hex(), info.GatewayBalance.String(), info.TransferBalance.String())
return t.sendSlackAlert(info)
}
}

return nil
}

func (t *TransferEventMatcher) sendSlackAlert(info slack.GatewayTransferInfo) error {
info.TokenIgnored = t.isTokenIgnored(info.Layer, info.TokenAddress)
slack.Notify(slack.MrkDwnGatewayTransferMessage(info))
if info.TokenIgnored {
return nil
}
return fmt.Errorf("balance mismatch for token %s, token type = %s, transfer amount = %s, gateway amount = %s, info = %+v",
info.TokenAddress.Hex(), info.TokenType.String(), info.TransferBalance.String(), info.GatewayBalance.String(), info)
}

func (t *TransferEventMatcher) isTokenIgnored(layer types.LayerType, tokenAddress common.Address) bool {
if layer == types.Layer2 {
if _, ok := t.l2IgnoredTokens[tokenAddress]; ok {
log.Warn("l2 token is ignored", "token address", tokenAddress.Hex())
return true
}
} else {
if _, ok := t.l1IgnoredTokens[tokenAddress]; ok {
log.Warn("l1 token is ignored", "token address", tokenAddress.Hex())
return true
}
}
return false
}
3 changes: 3 additions & 0 deletions internal/logic/slack/alert_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type GatewayTransferInfo struct {
Error string
TransferBalance *big.Int
GatewayBalance *big.Int
TokenIgnored bool
}

// WithdrawRootInfo the alert message of withdraw root info
Expand Down Expand Up @@ -99,6 +100,8 @@ func MrkDwnGatewayTransferMessage(info GatewayTransferInfo) string {
buffer.WriteString(fmt.Sprintf("• msg_hash: %s\n", info.MessageHash.Hex()))
buffer.WriteString(fmt.Sprintf("• transfer balance: %s\n", info.TransferBalance.String()))
buffer.WriteString(fmt.Sprintf("• gateway balance: %s\n", info.GatewayBalance.String()))
buffer.WriteString(fmt.Sprintf("• token address: %s\n", info.TokenAddress.Hex()))
buffer.WriteString(fmt.Sprintf("• token ignored: %t\n", info.TokenIgnored))
buffer.WriteString(fmt.Sprintf("• err info:%s\n", info.Error))
return buffer.String()
}
Expand Down
2 changes: 1 addition & 1 deletion internal/utils/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"runtime/debug"
)

var tag = "v1.1.24"
var tag = "v1.1.25"

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