Skip to content

Commit

Permalink
fix: Remove hardcoded multilocation (#146)
Browse files Browse the repository at this point in the history
Signed-off-by: tcar <tcar121293@gmail.com>
Co-authored-by: Matija Petrunić <matija.petrunic@gmail.com>
  • Loading branch information
tcar121293 and mpetrun5 authored Mar 22, 2023
1 parent da98cd2 commit 257058a
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 88 deletions.
39 changes: 1 addition & 38 deletions chains/substrate/executor/message-handler.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package executor

import (
"bytes"
"errors"
"fmt"
"math/big"
"unsafe"

"github.com/ChainSafe/chainbridge-core/relayer/message"
"github.com/ChainSafe/sygma-relayer/chains"
"github.com/centrifuge/go-substrate-rpc-client/v4/scale"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/ethereum/go-ethereum/common"

"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -73,48 +69,15 @@ func FungibleTransferMessageHandler(m *message.Message) (*chains.Proposal, error
if !ok {
return nil, errors.New("wrong payload amount format")
}
reciever, ok := m.Payload[1].([]byte)
recipient, ok := m.Payload[1].([]byte)
if !ok {
return nil, errors.New("wrong payload recipient format")
}
var data []byte
data = append(data, common.LeftPadBytes(amount, 32)...) // amount (uint256)
acc := *(*[]types.U8)(unsafe.Pointer(&reciever))
recipient := constructRecipientData((acc))

recipientLen := big.NewInt(int64(len(recipient))).Bytes()
data = append(data, common.LeftPadBytes(recipientLen, 32)...)
data = append(data, recipient...)
return chains.NewProposal(m.Source, m.Destination, m.DepositNonce, m.ResourceId, data, m.Metadata), nil
}

func constructRecipientData(recipient []types.U8) []byte {
rec := types.MultiLocationV1{
Parents: 0,
Interior: types.JunctionsV1{
IsX1: true,
X1: types.JunctionV1{
IsAccountID32: true,
AccountID32NetworkID: types.NetworkID{
IsAny: true,
},
AccountID: recipient,
},
},
}

encodedRecipient := bytes.NewBuffer([]byte{})
encoder := scale.NewEncoder(encodedRecipient)
_ = rec.Encode(*encoder)

recipientBytes := encodedRecipient.Bytes()
var finalRecipient []byte

// remove accountID size data
// this is a fix because the substrate decoder is not able to parse the data with extra data
// that represents size of the recipient byte array
finalRecipient = append(finalRecipient, recipientBytes[:4]...)
finalRecipient = append(finalRecipient, recipientBytes[5:]...)

return finalRecipient
}
18 changes: 14 additions & 4 deletions chains/substrate/executor/message-handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import (
"encoding/hex"
"errors"
"testing"
"unsafe"

"github.com/ChainSafe/chainbridge-core/relayer/message"
"github.com/ChainSafe/sygma-relayer/chains"
"github.com/ChainSafe/sygma-relayer/chains/substrate/executor"
"github.com/ChainSafe/sygma-relayer/e2e/substrate"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"

"github.com/stretchr/testify/suite"
)

Expand All @@ -24,6 +28,9 @@ func TestRunFungibleTransferHandlerTestSuite(t *testing.T) {
}

func (s *FungibleTransferHandlerTestSuite) TestFungibleTransferHandleMessage() {
recipientAddr := *(*[]types.U8)(unsafe.Pointer(&substrate.SubstratePK.PublicKey))
recipient := substrate.ConstructRecipientData(recipientAddr)

message := &message.Message{
Source: 1,
Destination: 2,
Expand All @@ -32,11 +39,11 @@ func (s *FungibleTransferHandlerTestSuite) TestFungibleTransferHandleMessage() {
Type: message.FungibleTransfer,
Payload: []interface{}{
[]byte{2}, // amount
[]byte{0x8e, 0xaf, 0x4, 0x15, 0x16, 0x87, 0x73, 0x63, 0x26, 0xc9, 0xfe, 0xa1, 0x7e, 0x25, 0xfc, 0x52, 0x87, 0x61, 0x36, 0x93, 0xc9, 0x12, 0x90, 0x9c, 0xb2, 0x26, 0xaa, 0x47, 0x94, 0xf2, 0x6a, 0x48}, // recipientAddress
recipient,
},
Metadata: message.Metadata{},
}
data, _ := hex.DecodeString("00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000024000101008eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48")
data, _ := hex.DecodeString("0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000002400010100d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d")
expectedProp := &chains.Proposal{
OriginDomainID: 1,
Destination: 2,
Expand Down Expand Up @@ -114,6 +121,9 @@ func (s *FungibleTransferHandlerTestSuite) TestFungibleTransferHandleMessageInco
}

func (s *FungibleTransferHandlerTestSuite) TestSuccesfullyRegisterFungibleTransferMessageHandler() {
recipientAddr := *(*[]types.U8)(unsafe.Pointer(&substrate.SubstratePK.PublicKey))
recipient := substrate.ConstructRecipientData(recipientAddr)

messageData := &message.Message{
Source: 1,
Destination: 0,
Expand All @@ -122,7 +132,7 @@ func (s *FungibleTransferHandlerTestSuite) TestSuccesfullyRegisterFungibleTransf
Type: message.FungibleTransfer,
Payload: []interface{}{
[]byte{2}, // amount
[]byte{0x8e, 0xaf, 0x4, 0x15, 0x16, 0x87, 0x73, 0x63, 0x26, 0xc9, 0xfe, 0xa1, 0x7e, 0x25, 0xfc, 0x52, 0x87, 0x61, 0x36, 0x93, 0xc9, 0x12, 0x90, 0x9c, 0xb2, 0x26, 0xaa, 0x47, 0x94, 0xf2, 0x6a, 0x48}, // recipientAddress
recipient,
},
Metadata: message.Metadata{},
}
Expand All @@ -135,7 +145,7 @@ func (s *FungibleTransferHandlerTestSuite) TestSuccesfullyRegisterFungibleTransf
Type: message.NonFungibleTransfer,
Payload: []interface{}{
[]byte{2}, // amount
[]byte{0x8e, 0xaf, 0x4, 0x15, 0x16, 0x87, 0x73, 0x63, 0x26, 0xc9, 0xfe, 0xa1, 0x7e, 0x25, 0xfc, 0x52, 0x87, 0x61, 0x36, 0x93, 0xc9, 0x12, 0x90, 0x9c, 0xb2, 0x26, 0xaa, 0x47, 0x94, 0xf2, 0x6a, 0x48}, // recipientAddress
recipient,
},
Metadata: message.Metadata{},
}
Expand Down
4 changes: 2 additions & 2 deletions chains/substrate/listener/deposit-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ func FungibleTransferHandler(sourceID uint8, destId types.U8, nonce types.U64, r
// amount: first 32 bytes of calldata
amount := calldata[:32]

// 32-64 is recipient address length
// 32-64 is multiLocation length
recipientAddressLength, _ := types.IntBytesToBigInt(calldata[32:64])

// 64 - (64 + recipient address length) is recipient address
// 64 - (64 + multiLocation length) is recipient address
recipientAddress := calldata[64:(64 + recipientAddressLength.Int64())]

// if there is priority data, parse it and use it
Expand Down
44 changes: 14 additions & 30 deletions chains/substrate/listener/deposit-handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ package listener_test

import (
"errors"
"unsafe"

"github.com/ChainSafe/chainbridge-core/relayer/message"
core_types "github.com/ChainSafe/chainbridge-core/types"
"github.com/centrifuge/go-substrate-rpc-client/signature"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"

"math/big"
"testing"

"github.com/ChainSafe/sygma-relayer/chains/substrate/events"
"github.com/ChainSafe/sygma-relayer/chains/substrate/listener"
"github.com/ChainSafe/sygma-relayer/e2e/substrate"
"github.com/stretchr/testify/suite"
)

Expand All @@ -31,22 +32,17 @@ func TestRunErc20HandlerTestSuite(t *testing.T) {
}

func (s *Erc20HandlerTestSuite) TestErc20HandleEvent() {
// Alice
var substratePK = signature.KeyringPair{
URI: "//Alice",
PublicKey: []byte{0xd4, 0x35, 0x93, 0xc7, 0x15, 0xfd, 0xd3, 0x1c, 0x61, 0x14, 0x1a, 0xbd, 0x4, 0xa9, 0x9f, 0xd6, 0x82, 0x2c, 0x85, 0x58, 0x85, 0x4c, 0xcd, 0xe3, 0x9a, 0x56, 0x84, 0xe7, 0xa5, 0x6d, 0xa2, 0x7d},
Address: "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
}
// Bob
recipientByteSlice := []byte{0x8e, 0xaf, 0x4, 0x15, 0x16, 0x87, 0x73, 0x63, 0x26, 0xc9, 0xfe, 0xa1, 0x7e, 0x25, 0xfc, 0x52, 0x87, 0x61, 0x36, 0x93, 0xc9, 0x12, 0x90, 0x9c, 0xb2, 0x26, 0xaa, 0x47, 0x94, 0xf2, 0x6a, 0x48}
recipientAddr := *(*[]types.U8)(unsafe.Pointer(&substrate.SubstratePK.PublicKey))
recipient := substrate.ConstructRecipientData(recipientAddr)

var calldata []byte
amount, _ := types.BigIntToIntBytes(big.NewInt(2), 32)
calldata = append(calldata, amount...)
recipientLen, _ := (types.BigIntToIntBytes(big.NewInt(int64(len(recipientByteSlice))), 32))
recipientLen, _ := (types.BigIntToIntBytes(big.NewInt(int64(len(recipient))), 32))
calldata = append(calldata, recipientLen...)
calldata = append(calldata, types.Bytes(recipientByteSlice)...)
calldata = append(calldata, types.Bytes(recipient)...)

sender, _ := types.NewAccountID(substratePK.PublicKey)
sender, _ := types.NewAccountID(substrate.SubstratePK.PublicKey)
depositLog := &events.Deposit{
Phase: types.Phase{},
DestDomainID: types.NewU8(2),
Expand Down Expand Up @@ -83,16 +79,9 @@ func (s *Erc20HandlerTestSuite) TestErc20HandleEvent() {
}

func (s *Erc20HandlerTestSuite) TestErc20HandleEventIncorrectdeposit_dataLen() {
// Alice
var substratePK = signature.KeyringPair{
URI: "//Alice",
PublicKey: []byte{0xd4, 0x35, 0x93, 0xc7, 0x15, 0xfd, 0xd3, 0x1c, 0x61, 0x14, 0x1a, 0xbd, 0x4, 0xa9, 0x9f, 0xd6, 0x82, 0x2c, 0x85, 0x58, 0x85, 0x4c, 0xcd, 0xe3, 0x9a, 0x56, 0x84, 0xe7, 0xa5, 0x6d, 0xa2, 0x7d},
Address: "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
}

var calldata []byte

sender, _ := types.NewAccountID(substratePK.PublicKey)
sender, _ := types.NewAccountID(substrate.SubstratePK.PublicKey)
depositLog := &events.Deposit{
Phase: types.Phase{},
DestDomainID: types.NewU8(2),
Expand All @@ -113,21 +102,16 @@ func (s *Erc20HandlerTestSuite) TestErc20HandleEventIncorrectdeposit_dataLen() {
}

func (s *Erc20HandlerTestSuite) TestSuccesfullyRegisterFungibleTransferHandler() {
// Alice
var substratePK = signature.KeyringPair{
URI: "//Alice",
PublicKey: []byte{0xd4, 0x35, 0x93, 0xc7, 0x15, 0xfd, 0xd3, 0x1c, 0x61, 0x14, 0x1a, 0xbd, 0x4, 0xa9, 0x9f, 0xd6, 0x82, 0x2c, 0x85, 0x58, 0x85, 0x4c, 0xcd, 0xe3, 0x9a, 0x56, 0x84, 0xe7, 0xa5, 0x6d, 0xa2, 0x7d},
Address: "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
}
recipientByteSlice := []byte{0x8e, 0xaf, 0x4, 0x15, 0x16, 0x87, 0x73, 0x63, 0x26, 0xc9, 0xfe, 0xa1, 0x7e, 0x25, 0xfc, 0x52, 0x87, 0x61, 0x36, 0x93, 0xc9, 0x12, 0x90, 0x9c, 0xb2, 0x26, 0xaa, 0x47, 0x94, 0xf2, 0x6a, 0x48}
recipientAddr := *(*[]types.U8)(unsafe.Pointer(&substrate.SubstratePK.PublicKey))
recipient := substrate.ConstructRecipientData(recipientAddr)
// Create calldata
var calldata []byte
amount, _ := types.BigIntToIntBytes(big.NewInt(2), 32)
calldata = append(calldata, amount...)
recipientLen, _ := (types.BigIntToIntBytes(big.NewInt(int64(len(recipientByteSlice))), 32))
recipientLen, _ := (types.BigIntToIntBytes(big.NewInt(int64(len(recipient))), 32))
calldata = append(calldata, recipientLen...)
calldata = append(calldata, types.Bytes(recipientByteSlice)...)
sender, _ := types.NewAccountID(substratePK.PublicKey)
calldata = append(calldata, types.Bytes(recipient)...)
sender, _ := types.NewAccountID(substrate.SubstratePK.PublicKey)

d1 := &events.Deposit{
Phase: types.Phase{},
Expand Down
23 changes: 9 additions & 14 deletions e2e/substrate/substrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/ChainSafe/chainbridge-core/crypto/secp256k1"
"github.com/ChainSafe/sygma-relayer/chains/substrate/client"
"github.com/ChainSafe/sygma-relayer/chains/substrate/connection"
"github.com/centrifuge/go-substrate-rpc-client/v4/signature"
substrateTypes "github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/ethereum/go-ethereum/core/types"

Expand All @@ -35,12 +34,6 @@ import (
const ETHEndpoint = "ws://localhost:8545"
const SubstrateEndpoint = "ws://localhost:9944"

var substratePK = signature.KeyringPair{
URI: "//Alice",
PublicKey: []byte{0xd4, 0x35, 0x93, 0xc7, 0x15, 0xfd, 0xd3, 0x1c, 0x61, 0x14, 0x1a, 0xbd, 0x4, 0xa9, 0x9f, 0xd6, 0x82, 0x2c, 0x85, 0x58, 0x85, 0x4c, 0xcd, 0xe3, 0x9a, 0x56, 0x84, 0xe7, 0xa5, 0x6d, 0xa2, 0x7d},
Address: "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
}

type TestClient interface {
evm.EVMClient
LatestBlock() (*big.Int, error)
Expand Down Expand Up @@ -82,7 +75,7 @@ func Test_EVMSubstrate(t *testing.T) {
if err != nil {
panic(err)
}
substrateClient := client.NewSubstrateClient(substrateConnection, &substratePK, big.NewInt(5), 0)
substrateClient := client.NewSubstrateClient(substrateConnection, &substrate.SubstratePK, big.NewInt(5), 0)

var assetId uint32 = 2000
assetIdSerialized := make([]byte, 4)
Expand Down Expand Up @@ -158,7 +151,7 @@ func (s *IntegrationTestSuite) SetupSuite() {
func (s *IntegrationTestSuite) Test_Erc20Deposit_Substrate_to_EVM() {
var accountInfoBefore substrate.Account
meta := s.substrateConnection.GetMetadata()
key, _ := substrateTypes.CreateStorageKey(&meta, "Assets", "Account", s.substrateAssetID, substratePK.PublicKey)
key, _ := substrateTypes.CreateStorageKey(&meta, "Assets", "Account", s.substrateAssetID, substrate.SubstratePK.PublicKey)
_, err := s.substrateConnection.RPC.State.GetStorageLatest(key, &accountInfoBefore)
s.Nil(err)

Expand Down Expand Up @@ -227,7 +220,7 @@ func (s *IntegrationTestSuite) Test_Erc20Deposit_Substrate_to_EVM() {

meta = s.substrateConnection.GetMetadata()
var senderBalanceAfter substrate.Account
key, _ = substrateTypes.CreateStorageKey(&meta, "Assets", "Account", s.substrateAssetID, substratePK.PublicKey)
key, _ = substrateTypes.CreateStorageKey(&meta, "Assets", "Account", s.substrateAssetID, substrate.SubstratePK.PublicKey)
_, err = s.substrateConnection.RPC.State.GetStorageLatest(key, &senderBalanceAfter)
s.Nil(err)

Expand All @@ -252,23 +245,25 @@ func (s *IntegrationTestSuite) Test_Erc20Deposit_EVM_to_Substrate() {

meta := s.substrateConnection.GetMetadata()
var destBalanceBefore substrate.Account
key, _ := substrateTypes.CreateStorageKey(&meta, "Assets", "Account", s.substrateAssetID, substratePK.PublicKey)
key, _ := substrateTypes.CreateStorageKey(&meta, "Assets", "Account", s.substrateAssetID, substrate.SubstratePK.PublicKey)
_, err = s.substrateConnection.RPC.State.GetStorageLatest(key, &destBalanceBefore)
s.Nil(err)
pk := []substrateTypes.U8{0xd4, 0x35, 0x93, 0xc7, 0x15, 0xfd, 0xd3, 0x1c, 0x61, 0x14, 0x1a, 0xbd, 0x4, 0xa9, 0x9f, 0xd6, 0x82, 0x2c, 0x85, 0x58, 0x85, 0x4c, 0xcd, 0xe3, 0x9a, 0x56, 0x84, 0xe7, 0xa5, 0x6d, 0xa2, 0x7d}

_, err = bridgeContract1.Erc20Deposit(substratePK.PublicKey, amountToDeposit, s.evmConfig.Erc20LockReleaseResourceID, 3, nil, transactor.TransactOptions{
recipientMultilocation := substrate.ConstructRecipientData(pk)
_, err = bridgeContract1.Erc20Deposit(recipientMultilocation, amountToDeposit, s.evmConfig.Erc20LockReleaseResourceID, 3, nil, transactor.TransactOptions{
Value: s.evmConfig.BasicFee,
})
s.Nil(err)

err = substrate.WaitForProposalExecuted(s.substrateConnection, destBalanceBefore.Balance, substratePK.PublicKey)
err = substrate.WaitForProposalExecuted(s.substrateConnection, destBalanceBefore.Balance, substrate.SubstratePK.PublicKey)
s.Nil(err)
senderBalAfter, err := erc20Contract1.GetBalance(s.evmClient.From())
s.Nil(err)
s.Equal(-1, senderBalAfter.Cmp(senderBalBefore))

var destBalanceAfter substrate.Account
key, _ = substrateTypes.CreateStorageKey(&meta, "Assets", "Account", s.substrateAssetID, substratePK.PublicKey)
key, _ = substrateTypes.CreateStorageKey(&meta, "Assets", "Account", s.substrateAssetID, substrate.SubstratePK.PublicKey)
_, err = s.substrateConnection.RPC.State.GetStorageLatest(key, &destBalanceAfter)
s.Nil(err)

Expand Down
39 changes: 39 additions & 0 deletions e2e/substrate/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
package substrate

import (
"bytes"
"context"
"encoding/binary"
"errors"
"math/big"
"time"

"github.com/centrifuge/go-substrate-rpc-client/v4/scale"
"github.com/centrifuge/go-substrate-rpc-client/v4/signature"
substrateTypes "github.com/centrifuge/go-substrate-rpc-client/v4/types"

"github.com/ChainSafe/sygma-relayer/chains/substrate/connection"
Expand All @@ -27,6 +30,11 @@ var BasicFee = big.NewInt(1000000000000000)
var OracleFee = uint16(500) // 5% - multiplied by 100 to not lose precision on contract side
var GasUsed = uint32(2000000000)
var FeeOracleAddress = common.HexToAddress("0x70B7D7448982b15295150575541D1d3b862f7FE9")
var SubstratePK = signature.KeyringPair{
URI: "//Alice",
PublicKey: []byte{0xd4, 0x35, 0x93, 0xc7, 0x15, 0xfd, 0xd3, 0x1c, 0x61, 0x14, 0x1a, 0xbd, 0x4, 0xa9, 0x9f, 0xd6, 0x82, 0x2c, 0x85, 0x58, 0x85, 0x4c, 0xcd, 0xe3, 0x9a, 0x56, 0x84, 0xe7, 0xa5, 0x6d, 0xa2, 0x7d},
Address: "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
}

type Client interface {
LatestBlock() (*big.Int, error)
Expand Down Expand Up @@ -86,3 +94,34 @@ func checkBalance(beforeBalance substrateTypes.U128, connection *connection.Conn
type Account struct {
Balance substrateTypes.U128
}

func ConstructRecipientData(recipient []substrateTypes.U8) []byte {
rec := substrateTypes.MultiLocationV1{
Parents: 0,
Interior: substrateTypes.JunctionsV1{
IsX1: true,
X1: substrateTypes.JunctionV1{
IsAccountID32: true,
AccountID32NetworkID: substrateTypes.NetworkID{
IsAny: true,
},
AccountID: recipient,
},
},
}

encodedRecipient := bytes.NewBuffer([]byte{})
encoder := scale.NewEncoder(encodedRecipient)
_ = rec.Encode(*encoder)

recipientBytes := encodedRecipient.Bytes()
var finalRecipient []byte

// remove accountID size data
// this is a fix because the substrate decoder is not able to parse the data with extra data
// that represents size of the recipient byte array
finalRecipient = append(finalRecipient, recipientBytes[:4]...)
finalRecipient = append(finalRecipient, recipientBytes[5:]...)

return finalRecipient
}

0 comments on commit 257058a

Please sign in to comment.