From f9531ca9315c034ad8e83097acd004e69d2d7744 Mon Sep 17 00:00:00 2001 From: mpetrun5 Date: Thu, 5 Oct 2023 16:22:19 +0200 Subject: [PATCH] Remove extra substrate message handler --- chains/substrate/executor/message-handler.go | 61 -------- .../executor/message-handler_test.go | 130 ------------------ 2 files changed, 191 deletions(-) delete mode 100644 chains/substrate/executor/message-handler.go delete mode 100644 chains/substrate/executor/message-handler_test.go diff --git a/chains/substrate/executor/message-handler.go b/chains/substrate/executor/message-handler.go deleted file mode 100644 index ea8ef225..00000000 --- a/chains/substrate/executor/message-handler.go +++ /dev/null @@ -1,61 +0,0 @@ -// The Licensed Work is (c) 2022 Sygma -// SPDX-License-Identifier: LGPL-3.0-only - -package executor - -import ( - "fmt" - - "github.com/ChainSafe/sygma-core/types" - - "github.com/rs/zerolog/log" -) - -type Handlers map[types.TransferType]MessageHandlerFunc -type MessageHandlerFunc func(m *types.Message) (*types.Proposal, error) - -type SubstrateMessageHandler struct { - handlers Handlers -} - -// NewSubstrateMessageHandler creates an instance of SubstrateMessageHandler that contains -// message handler functions for converting deposit message into a chain specific -// proposal -func NewSubstrateMessageHandler() *SubstrateMessageHandler { - return &SubstrateMessageHandler{ - handlers: make(map[types.TransferType]MessageHandlerFunc), - } -} - -func (mh *SubstrateMessageHandler) HandleMessage(m *types.Message) (*types.Proposal, error) { - // Based on handler that was registered on BridgeContract - handleMessage, err := mh.matchTransferTypeHandlerFunc(m.Type) - if err != nil { - return nil, err - } - log.Info().Str("type", string(m.Type)).Uint8("src", m.Source).Uint8("dst", m.Destination).Uint64("nonce", m.DepositNonce).Str("resourceID", fmt.Sprintf("%x", m.ResourceId)).Msg("Handling new message") - prop, err := handleMessage(m) - if err != nil { - return nil, err - } - return prop, nil -} - -func (mh *SubstrateMessageHandler) matchTransferTypeHandlerFunc(transferType types.TransferType) (MessageHandlerFunc, error) { - h, ok := mh.handlers[transferType] - if !ok { - return nil, fmt.Errorf("no corresponding message handler for this transfer type %s exists", transferType) - } - return h, nil -} - -// RegisterEventHandler registers an message handler by associating a handler function to a specified transfer type -func (mh *SubstrateMessageHandler) RegisterMessageHandler(transferType types.TransferType, handler MessageHandlerFunc) { - if transferType == "" { - return - } - - log.Info().Msgf("Registered message handler for transfer type %s", transferType) - - mh.handlers[transferType] = handler -} diff --git a/chains/substrate/executor/message-handler_test.go b/chains/substrate/executor/message-handler_test.go deleted file mode 100644 index 8bed70a3..00000000 --- a/chains/substrate/executor/message-handler_test.go +++ /dev/null @@ -1,130 +0,0 @@ -// The Licensed Work is (c) 2022 Sygma -// SPDX-License-Identifier: LGPL-3.0-only - -package executor_test - -import ( - "bytes" - "errors" - "math/big" - "testing" - "unsafe" - - "github.com/ChainSafe/sygma-core/chains/substrate/executor" - "github.com/ChainSafe/sygma-core/types" - "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/ethereum/go-ethereum/common" - - "github.com/stretchr/testify/suite" -) - -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 MessageHandlerTestSuite struct { - suite.Suite -} - -func TestRunFungibleTransferHandlerTestSuite(t *testing.T) { - suite.Run(t, new(MessageHandlerTestSuite)) -} - -func (s *MessageHandlerTestSuite) TestSuccesfullyRegisterFungibleTransferMessageHandler() { - recipientAddr := *(*[]substrateTypes.U8)(unsafe.Pointer(&SubstratePK.PublicKey)) - recipient := ConstructRecipientData(recipientAddr) - - messageData := &types.Message{ - Source: 1, - Destination: 0, - DepositNonce: 1, - ResourceId: [32]byte{0}, - Type: "fungible", - Payload: []interface{}{ - []byte{2}, // amount - recipient, - }, - Metadata: types.Metadata{}, - } - - invalidMessageData := &types.Message{ - Source: 1, - Destination: 0, - DepositNonce: 1, - ResourceId: [32]byte{0}, - Type: "nonFungible", - Payload: []interface{}{ - []byte{2}, // amount - recipient, - }, - Metadata: types.Metadata{}, - } - - depositMessageHandler := executor.NewSubstrateMessageHandler() - // Register FungibleTransferMessageHandler function - depositMessageHandler.RegisterMessageHandler("fungible", FungibleMessageHandler) - prop1, err1 := depositMessageHandler.HandleMessage(messageData) - s.Nil(err1) - s.NotNil(prop1) - - // Use unregistered transfer type - prop2, err2 := depositMessageHandler.HandleMessage(invalidMessageData) - s.Nil(prop2) - s.NotNil(err2) -} - -func FungibleMessageHandler(m *types.Message) (*types.Proposal, error) { - if len(m.Payload) != 2 { - return nil, errors.New("malformed payload. Len of payload should be 2") - } - amount, ok := m.Payload[0].([]byte) - if !ok { - return nil, errors.New("wrong payload amount format") - } - 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) - - recipientLen := big.NewInt(int64(len(recipient))).Bytes() - data = append(data, common.LeftPadBytes(recipientLen, 32)...) - data = append(data, recipient...) - return types.NewProposal(m.Source, m.Destination, m.DepositNonce, m.ResourceId, data, m.Metadata), nil -} - -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 -}