Skip to content

Commit

Permalink
refactor: add error checks for missing chain id (#781)
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD authored Jul 6, 2023
1 parent 993d0a6 commit 542eb37
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
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 {
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) {
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

0 comments on commit 542eb37

Please sign in to comment.