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

refactor: add error checks for missing chain id #781

Merged
merged 6 commits into from
Jul 6, 2023
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
9 changes: 8 additions & 1 deletion zetaclient/evm_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,8 +603,15 @@ func (ob *EVMChainClient) observeInTX() error {
event := logs.Event
ob.logger.ExternalChainWatcher.Info().Msgf("TxBlockNumber %d Transaction Hash: %s Message : %s", event.Raw.BlockNumber, event.Raw.TxHash, event.Message)
destChain := common.GetChainFromChainID(event.DestinationChainId.Int64())
if destChain == nil {
brewmaster012 marked this conversation as resolved.
Show resolved Hide resolved
ob.logger.ExternalChainWatcher.Warn().Msgf("chain id not supported %d", event.DestinationChainId.Int64())
continue
}
destAddr := clienttypes.BytesToEthHex(event.DestinationAddress)

if ob.cfg.EVMChainConfigs[destChain.ChainId] == nil {
ob.logger.ExternalChainWatcher.Warn().Msgf("chain id not present in EVMChainConfigs %d", event.DestinationChainId.Int64())
continue
}
if strings.EqualFold(destAddr, ob.cfg.EVMChainConfigs[destChain.ChainId].CoreParams.ZETATokenContractAddress) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's be defensive and check whether ob.cfg.EVMChainConfigs[destChain.ChainId] is nil or not.

ob.logger.ExternalChainWatcher.Warn().Msgf("potential attack attempt: %s destination address is ZETA token contract address %s", destChain, destAddr)
continue
Expand Down
8 changes: 8 additions & 0 deletions zetaclient/evm_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,18 @@ func (signer *EVMSigner) TryProcessOutTx(send *types.CrossChainTx, outTxMan *Out
if send.CctxStatus.Status == types.CctxStatus_PendingRevert {
to = ethcommon.HexToAddress(send.InboundTxParams.Sender)
toChain = common.GetChainFromChainID(send.InboundTxParams.SenderChainId)
if toChain == nil {
logger.Error().Msgf("Unknown chain: %d", send.InboundTxParams.SenderChainId)
return
}
logger.Info().Msgf("Abort: reverting inbound")
} else if send.CctxStatus.Status == types.CctxStatus_PendingOutbound {
to = ethcommon.HexToAddress(send.GetCurrentOutTxParam().Receiver)
toChain = common.GetChainFromChainID(send.GetCurrentOutTxParam().ReceiverChainId)
if toChain == nil {
logger.Error().Msgf("Unknown chain: %d", send.GetCurrentOutTxParam().ReceiverChainId)
return
}
}
if err != nil {
logger.Error().Err(err).Msg("ParseChain fail; skip")
Expand Down
2 changes: 1 addition & 1 deletion zetaclient/zetabridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (b *ZetaCoreBridge) UpdateConfigFromCore(cfg *config.Config) error {
}
_, found := cfg.EVMChainConfigs[params.ChainId]
if !found {
panic(fmt.Sprintf("EvmConfig %s is nil for this client ", common.GetChainFromChainID(params.ChainId).String()))
panic(fmt.Sprintf("EvmConfig %d is nil for this client ", params.ChainId))
}
if cfg.EVMChainConfigs[params.ChainId].CoreParams == nil {
cfg.EVMChainConfigs[params.ChainId].CoreParams = config.NewCoreParams()
Expand Down
27 changes: 20 additions & 7 deletions zetaclient/zetacore_observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,11 @@ func (co *CoreObserver) startSendScheduler() {
co.logger.ZetaChainWatcher.Info().Msgf("send outTx already included; do not schedule")
continue
}
chain := GetTargetChain(send)
chain, err := GetTargetChain(send)
if err != nil {
co.logger.ZetaChainWatcher.Error().Err(err).Msgf("GetTargetChain fail , Chain ID : %s", c.ChainName)
continue
}
nonce := send.GetCurrentOutTxParam().OutboundTxTssNonce
outTxID := fmt.Sprintf("%s-%d-%d", send.Index, send.GetCurrentOutTxParam().ReceiverChainId, nonce) // should be the outTxID?

Expand Down Expand Up @@ -210,8 +214,8 @@ func trimSends(sends []*types.CrossChainTx) int {
func SplitAndSortSendListByChain(sendList []*types.CrossChainTx) map[string][]*types.CrossChainTx {
sendMap := make(map[string][]*types.CrossChainTx)
for _, send := range sendList {
targetChain := GetTargetChain(send)
if targetChain == "" {
targetChain, err := GetTargetChain(send)
if targetChain == "" || err != nil {
continue
}
if _, found := sendMap[targetChain]; !found {
Expand All @@ -230,16 +234,25 @@ func SplitAndSortSendListByChain(sendList []*types.CrossChainTx) map[string][]*t
return sendMap
}

func GetTargetChain(send *types.CrossChainTx) string {
func GetTargetChain(send *types.CrossChainTx) (string, error) {
chainID := send.GetCurrentOutTxParam().ReceiverChainId
return common.GetChainFromChainID(chainID).GetChainName().String()
chain := common.GetChainFromChainID(chainID)
if chain == nil {
return "", fmt.Errorf("chain %d not found", chainID)
}
return chain.GetChainName().String(), nil
}

func (co *CoreObserver) getTargetChainOb(send *types.CrossChainTx) (ChainClient, error) {
chainStr := GetTargetChain(send)
chainStr, err := GetTargetChain(send)
if err != nil {
return nil, fmt.Errorf("chain %d not found", send.GetCurrentOutTxParam().ReceiverChainId)
}
chainName := common.ParseChainName(chainStr)
c := common.GetChainFromChainName(chainName)

if c == nil {
return nil, fmt.Errorf("chain %s not found", chainName)
}
chainOb, found := co.clientMap[*c]
if !found {
return nil, fmt.Errorf("chain %s not found", c)
Expand Down
Loading