From dfaa7af73c7bf53d7a17ad50ce6931ad59ca2790 Mon Sep 17 00:00:00 2001 From: skosito Date: Tue, 10 Dec 2024 20:34:08 +0100 Subject: [PATCH] fix unit test --- zetaclient/chains/solana/observer/inbound.go | 14 +++++++++ zetaclient/types/event.go | 31 ++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/zetaclient/chains/solana/observer/inbound.go b/zetaclient/chains/solana/observer/inbound.go index f11c40a920..68de065e9c 100644 --- a/zetaclient/chains/solana/observer/inbound.go +++ b/zetaclient/chains/solana/observer/inbound.go @@ -19,6 +19,7 @@ import ( solanarpc "github.com/zeta-chain/node/zetaclient/chains/solana/rpc" "github.com/zeta-chain/node/zetaclient/compliance" zctx "github.com/zeta-chain/node/zetaclient/context" + "github.com/zeta-chain/node/zetaclient/logs" clienttypes "github.com/zeta-chain/node/zetaclient/types" "github.com/zeta-chain/node/zetaclient/zetacore" ) @@ -277,6 +278,19 @@ func (ob *Observer) BuildInboundVoteMsgFromEvent(event *clienttypes.InboundEvent return nil } + // prepare logger fields + lf := map[string]any{ + logs.FieldMethod: "BuildInboundVoteMsgFromEvent", + logs.FieldTx: event.TxHash, + } + + // decode event memo bytes to get the receiver + err := event.DecodeMemo() + if err != nil { + ob.Logger().Inbound.Info().Fields(lf).Msgf("invalid memo bytes: %s", hex.EncodeToString(event.Memo)) + return nil + } + return zetacore.GetInboundVoteMessage( event.Sender, event.SenderChainID, diff --git a/zetaclient/types/event.go b/zetaclient/types/event.go index a0313236e6..6709b063bd 100644 --- a/zetaclient/types/event.go +++ b/zetaclient/types/event.go @@ -1,7 +1,15 @@ package types import ( + "bytes" + "encoding/hex" + + "github.com/pkg/errors" + "github.com/zeta-chain/node/pkg/coin" + "github.com/zeta-chain/node/pkg/constant" + "github.com/zeta-chain/node/pkg/crypto" + "github.com/zeta-chain/node/pkg/memo" ) // InboundEvent represents an inbound event @@ -41,3 +49,26 @@ type InboundEvent struct { // Asset is the asset of the inbound Asset string } + +// DecodeMemo decodes the receiver from the memo bytes +func (event *InboundEvent) DecodeMemo() error { + // skip decoding donation tx as it won't go through zetacore + if bytes.Equal(event.Memo, []byte(constant.DonationMessage)) { + return nil + } + + // decode receiver address from memo + parsedAddress, _, err := memo.DecodeLegacyMemoHex(hex.EncodeToString(event.Memo)) + if err != nil { // unreachable code + return errors.Wrap(err, "invalid memo hex") + } + + // ensure the receiver is valid + if crypto.IsEmptyAddress(parsedAddress) { + return errors.New("got empty receiver address from memo") + } + + event.Receiver = parsedAddress.Hex() + + return nil +}