Skip to content

Commit

Permalink
Node/GWRelayer: Should check emitter for ibc transfers (wormhole-foun…
Browse files Browse the repository at this point in the history
  • Loading branch information
bruce-riley authored and andreclaro committed Jul 8, 2024
1 parent b9892ab commit 6eb7873
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
14 changes: 9 additions & 5 deletions node/pkg/gwrelayer/gwrelayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func convertBech32AddressToWormhole(contractAddress string) (vaa.Address, error)
// SubmitVAA checks to see if the VAA should be submitted to the smart contract, and if so, writes it to the channel for publishing.
func (gwr *GatewayRelayer) SubmitVAA(v *vaa.VAA) {
var v2p VaaToPublish
if shouldPub, err := shouldPublishToIbcTranslator(v.Payload, vaa.ChainIDWormchain, gwr.ibcTranslatorPayloadAddress); err != nil {
if shouldPub, err := shouldPublishToIbcTranslator(gwr.tokenBridges, v, vaa.ChainIDWormchain, gwr.ibcTranslatorPayloadAddress); err != nil {
gwr.logger.Error("failed to check if vaa should be published", zap.String("msgId", v.MessageID()), zap.Error(err))
return
} else if shouldPub {
Expand All @@ -246,16 +246,20 @@ func (gwr *GatewayRelayer) SubmitVAA(v *vaa.VAA) {
}

// shouldPublishToIbcTranslator returns true if a message should be forwarded to the contract on wormchain, false if not.
func shouldPublishToIbcTranslator(payload []byte, targetChain vaa.ChainID, targetAddress vaa.Address) (bool, error) {
if len(payload) == 0 {
func shouldPublishToIbcTranslator(tokenBridges tokenBridgeMap, v *vaa.VAA, targetChain vaa.ChainID, targetAddress vaa.Address) (bool, error) {
if _, exists := tokenBridges[tokenBridgeKey{emitterChainId: v.EmitterChain, emitterAddr: v.EmitterAddress}]; !exists {
return false, nil
}

if len(v.Payload) == 0 {
return false, nil
}

if payload[0] != 3 {
if v.Payload[0] != 3 {
return false, nil
}

hdr, err := vaa.DecodeTransferPayloadHdr(payload)
hdr, err := vaa.DecodeTransferPayloadHdr(v.Payload)
if err != nil {
return false, fmt.Errorf("failed to decode payload: %w", err)
}
Expand Down
45 changes: 43 additions & 2 deletions node/pkg/gwrelayer/gwrelayer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,32 @@ func Test_shouldPublishToIbcTranslator(t *testing.T) {
{label: "should publish", payload: decodeBytes("0300000000000000000000000000000000000000000000000000000000000000640000000000000000000000005425890298aed601595a70ab815c96711a31bc650006ade4a5f5803a439835c636395a8d648dee57b2fc90d98dc17fa887159b69638b0C20000000000000000000000000e6990c7e206d418d62b9e50c8e61f59dc360183b7b2262617369635f726563697069656e74223a7b22726563697069656e74223a22633256704d57786c656d3179636d31336348687865575679626e6c344d33706a595768735a4756715958686e4f485a364f484e774d32526f227d7d"), result: true, err: false},
}

logger := zap.NewNop()

solanaEmitter, err := vaa.StringToAddress("ec7372995d5cc8732397fb0ad35c0121e0eaa90d26f828a534cab54391b3a4f5")
require.NoError(t, err)

tokenBridges, _, err := buildTokenBridgeMap(logger, common.MainNet)
require.NoError(t, err)

targetAddress, err := convertBech32AddressToWormhole("wormhole14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9srrg465")
require.NoError(t, err)

for _, tc := range tests {
for seqNum, tc := range tests {
t.Run(string(tc.label), func(t *testing.T) {
result, err := shouldPublishToIbcTranslator(tc.payload, vaa.ChainIDWormchain, targetAddress)
v := &vaa.VAA{
Version: uint8(1),
GuardianSetIndex: uint32(1),
Signatures: []*vaa.Signature{},
Timestamp: time.Unix(0, 0),
Nonce: uint32(1),
Sequence: uint64(seqNum),
ConsistencyLevel: uint8(32),
EmitterChain: vaa.ChainIDSolana,
EmitterAddress: solanaEmitter,
Payload: tc.payload,
}
result, err := shouldPublishToIbcTranslator(tokenBridges, v, vaa.ChainIDWormchain, targetAddress)
assert.Equal(t, tc.err, err != nil)
assert.Equal(t, tc.result, result)
})
Expand Down Expand Up @@ -139,3 +159,24 @@ func Test_verifyDevnetTokenBridgeAddress(t *testing.T) {
require.True(t, exists)
assert.True(t, bytes.Equal(expectedAddress[:], targetAddress[:]))
}

func Test_shouldPublishToIbcTranslatorShouldIgnoreUnknownEmitter(t *testing.T) {
vaaBytes, err := hex.DecodeString("01000000040d008f68689ec1cef3f53d7a20e83b6f42346ddcdcaff1c8f9435905a674949621b73a37e803e65f213564d2b019964ad3b66586c2f8255c5308dde4444759e702ef01024d6b7e7b74d6d56d11ee0fd59468142a7566cf019cc57dd63c010dbdfd32da155d8d4fc3bf6404ff26bfd754b96f7c2426e680acb99a224687ba819e289d92d401040feda4402fdf099b7aa50553aa9bdd991724d25309ae325fbd56ec13136f83bc0fc4129ecee55d08c38257d6bc7465cf21568212e9199ab1e4e28001baeaf2b0000634097288b9f9570af929f1cc52bb966430d1606f13c916180f86ff2e0467b49e2e05d299fa24150f7f478319b39b03815b8a23f52e8e6c994d6bdd6a544607bf0007bb7b6393e60523997c2d2f999a4ce2d57a2958dca25284a966c4abb0c097284324d1329d9eb75b4d1d24601a3ed5b94571ee57b9fb4c873ce5bcc398505f5694000844d5fe1672a004612c436c9fffd2c2c8bbe09e827754cef20b81dd65f9094e9a757888203b0f84747f2a1259e1699a3605b7320202011875b8b497584c4b253500092badc4d98dd77ef6b55d189d74c8191f32a374c450cfaea380951cf266683bd7097f5ada2e3fe5747baa99cc4db933e12f9b3270739230b834fa19c8da962612000a6d08e0b4a6bf5e59ae96c63169f2566a839aa7eee125ff31536f797fa1a4174a158be54f31c583453025fba6b768e2aad6966871ea367d11958f1aa683ec913a000d5a69c9968066e471726fe59633df72ecad6737b931754314ebde14fcf4b723b97ab58774aad432e3aa75896ec4620a07b3041ca531a5b67a1eb40805cd05f222010eb82dc0abf7c87bceb03ded9d83186eb3cd3082d042eaed3b1c05f69f13aeeffa7e93a224100acc12f1425feb63e235b63b38ca6e3507847f099ec41b8aea0453010f55af1b26a5c2b704aa064dad6db5105fa5c75816732da3ecdad1dc285eda6e043aaa37f5f5e1d613c4df5a15a82c347e6badde62382d16f9b1be58e93fb157a30011d63eac9a9c84d01a32fb03f12ae113fd28201682eec627d56db30496a837ed086560b674c2e305235eba76e20260afd566ee4c2125a1e8366aa1ca46415f504401129ed9924a060e5e83ac17076fd01204b5b35c6a0d754dd5ab27f8a4d7d3451b8710708fa090525ecbfeba2e72bdaff0ffecd0e71ab02f10c5bf74bea76e54efeb0166856d4400000001000175233cdd3dab6c9f3134860c2b2443e80072b0880a3405b2bd87234f024711bc0000000000002a1f200301000000000000a62c00000005000000000000000000000000aa446e32b8c1f1bb82e6a548c6baf54654ebe9110000000000000000000000000000348b")
require.NoError(t, err)
v, err := vaa.Unmarshal(vaaBytes)
require.NoError(t, err)

logger := zap.NewNop()

tokenBridges, tokenBridgeAddress, err := buildTokenBridgeMap(logger, common.MainNet)
require.NoError(t, err)
require.NotNil(t, tokenBridges)
require.Equal(t, tokenBridgeAddress, "wormhole1466nf3zuxpya8q9emxukd7vftaf6h4psr0a07srl5zw74zh84yjq4lyjmh")

targetAddress, err := convertBech32AddressToWormhole("wormhole14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9srrg465")
require.NoError(t, err)

shouldPub, err := shouldPublishToIbcTranslator(tokenBridges, v, vaa.ChainIDWormchain, targetAddress)
assert.NoError(t, err)
assert.False(t, shouldPub)
}

0 comments on commit 6eb7873

Please sign in to comment.