Skip to content

Commit

Permalink
fix unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
skosito committed Dec 10, 2024
1 parent 0f85e3d commit dfaa7af
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
14 changes: 14 additions & 0 deletions zetaclient/chains/solana/observer/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}

Check warning on line 292 in zetaclient/chains/solana/observer/inbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/solana/observer/inbound.go#L290-L292

Added lines #L290 - L292 were not covered by tests

return zetacore.GetInboundVoteMessage(
event.Sender,
event.SenderChainID,
Expand Down
31 changes: 31 additions & 0 deletions zetaclient/types/event.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
}

Check warning on line 58 in zetaclient/types/event.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/types/event.go#L57-L58

Added lines #L57 - L58 were not covered by tests

// 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")
}

Check warning on line 64 in zetaclient/types/event.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/types/event.go#L63-L64

Added lines #L63 - L64 were not covered by tests

// ensure the receiver is valid
if crypto.IsEmptyAddress(parsedAddress) {
return errors.New("got empty receiver address from memo")
}

Check warning on line 69 in zetaclient/types/event.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/types/event.go#L68-L69

Added lines #L68 - L69 were not covered by tests

event.Receiver = parsedAddress.Hex()

return nil
}

0 comments on commit dfaa7af

Please sign in to comment.