From 5ac62a5b4d22ce7fa0d33885477bb359c558bda4 Mon Sep 17 00:00:00 2001 From: mj52951 Date: Thu, 4 Jan 2024 14:26:39 +0100 Subject: [PATCH 1/2] Revert "chore: Converting message and proposal fields to generics (#19)" This reverts commit afa68196ac4be089ab6ae32dba6cfa09c270f9f3. --- chains/evm/chain.go | 8 ++++---- chains/substrate/chain.go | 8 ++++---- mock/message.go | 4 ++-- mock/relayer.go | 6 +++--- relayer/message/handler.go | 4 ++-- relayer/message/handler_test.go | 10 +++++----- relayer/message/message.go | 12 ++++++------ relayer/proposal/proposal.go | 8 ++++---- relayer/relayer.go | 10 +++++----- relayer/relayer_test.go | 20 ++++++++++---------- 10 files changed, 45 insertions(+), 45 deletions(-) diff --git a/chains/evm/chain.go b/chains/evm/chain.go index e0405b89..3ff9e143 100644 --- a/chains/evm/chain.go +++ b/chains/evm/chain.go @@ -18,11 +18,11 @@ type EventListener interface { } type ProposalExecutor interface { - Execute(props []*proposal.Proposal[any]) error + Execute(props []*proposal.Proposal) error } type MessageHandler interface { - HandleMessage(m *message.Message[any]) (*proposal.Proposal[any], error) + HandleMessage(m *message.Message) (*proposal.Proposal, error) } // EVMChain is struct that aggregates all data required for @@ -55,11 +55,11 @@ func (c *EVMChain) PollEvents(ctx context.Context) { go c.listener.ListenToEvents(ctx, c.startBlock) } -func (c *EVMChain) ReceiveMessage(m *message.Message[any]) (*proposal.Proposal[any], error) { +func (c *EVMChain) ReceiveMessage(m *message.Message) (*proposal.Proposal, error) { return c.messageHandler.HandleMessage(m) } -func (c *EVMChain) Write(props []*proposal.Proposal[any]) error { +func (c *EVMChain) Write(props []*proposal.Proposal) error { err := c.executor.Execute(props) if err != nil { c.logger.Err(err).Msgf("error writing proposals %+v on network %d", props, c.DomainID()) diff --git a/chains/substrate/chain.go b/chains/substrate/chain.go index 6e9cfc4a..e4819cc5 100644 --- a/chains/substrate/chain.go +++ b/chains/substrate/chain.go @@ -11,11 +11,11 @@ import ( ) type ProposalExecutor interface { - Execute(props []*proposal.Proposal[any]) error + Execute(props []*proposal.Proposal) error } type MessageHandler interface { - HandleMessage(m *message.Message[any]) (*proposal.Proposal[any], error) + HandleMessage(m *message.Message) (*proposal.Proposal, error) } type EventListener interface { @@ -49,11 +49,11 @@ func (c *SubstrateChain) PollEvents(ctx context.Context) { go c.listener.ListenToEvents(ctx, c.startBlock) } -func (c *SubstrateChain) ReceiveMessage(m *message.Message[any]) (*proposal.Proposal[any], error) { +func (c *SubstrateChain) ReceiveMessage(m *message.Message) (*proposal.Proposal, error) { return c.messageHandler.HandleMessage(m) } -func (c *SubstrateChain) Write(props []*proposal.Proposal[any]) error { +func (c *SubstrateChain) Write(props []*proposal.Proposal) error { err := c.executor.Execute(props) if err != nil { c.logger.Err(err).Msgf("error writing proposals %+v on network %d", props, c.DomainID()) diff --git a/mock/message.go b/mock/message.go index 9e488005..57ff5684 100644 --- a/mock/message.go +++ b/mock/message.go @@ -40,10 +40,10 @@ func (m *MockHandler) EXPECT() *MockHandlerMockRecorder { } // HandleMessage mocks base method. -func (m_2 *MockHandler) HandleMessage(m *message.Message[any]) (*proposal.Proposal[any], error) { +func (m_2 *MockHandler) HandleMessage(m *message.Message) (*proposal.Proposal, error) { m_2.ctrl.T.Helper() ret := m_2.ctrl.Call(m_2, "HandleMessage", m) - ret0, _ := ret[0].(*proposal.Proposal[any]) + ret0, _ := ret[0].(*proposal.Proposal) ret1, _ := ret[1].(error) return ret0, ret1 } diff --git a/mock/relayer.go b/mock/relayer.go index 9624ee18..d222a53a 100644 --- a/mock/relayer.go +++ b/mock/relayer.go @@ -67,10 +67,10 @@ func (mr *MockRelayedChainMockRecorder) PollEvents(ctx any) *gomock.Call { } // ReceiveMessage mocks base method. -func (m_2 *MockRelayedChain) ReceiveMessage(m *message.Message[any]) (*proposal.Proposal[any], error) { +func (m_2 *MockRelayedChain) ReceiveMessage(m *message.Message) (*proposal.Proposal, error) { m_2.ctrl.T.Helper() ret := m_2.ctrl.Call(m_2, "ReceiveMessage", m) - ret0, _ := ret[0].(*proposal.Proposal[any]) + ret0, _ := ret[0].(*proposal.Proposal) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -82,7 +82,7 @@ func (mr *MockRelayedChainMockRecorder) ReceiveMessage(m any) *gomock.Call { } // Write mocks base method. -func (m *MockRelayedChain) Write(proposals []*proposal.Proposal[any]) error { +func (m *MockRelayedChain) Write(proposals []*proposal.Proposal) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Write", proposals) ret0, _ := ret[0].(error) diff --git a/relayer/message/handler.go b/relayer/message/handler.go index bb829d5d..5e8aa41c 100644 --- a/relayer/message/handler.go +++ b/relayer/message/handler.go @@ -7,7 +7,7 @@ import ( ) type Handler interface { - HandleMessage(m *Message[any]) (*proposal.Proposal[any], error) + HandleMessage(m *Message) (*proposal.Proposal, error) } type MessageHandler struct { @@ -21,7 +21,7 @@ func NewMessageHandler() *MessageHandler { } // HandlerMessage calls associated handler for that message type and returns a proposal to be submitted on-chain -func (h *MessageHandler) HandleMessage(m *Message[any]) (*proposal.Proposal[any], error) { +func (h *MessageHandler) HandleMessage(m *Message) (*proposal.Proposal, error) { mh, ok := h.handlers[m.Type] if !ok { return nil, fmt.Errorf("no handler found for type %s", m.Type) diff --git a/relayer/message/handler_test.go b/relayer/message/handler_test.go index 0e6cfe63..16f59724 100644 --- a/relayer/message/handler_test.go +++ b/relayer/message/handler_test.go @@ -29,7 +29,7 @@ func (s *MessageHandlerTestSuite) SetupTest() { func (s *MessageHandlerTestSuite) TestHandleMessageWithoutRegisteredHandler() { mh := message.NewMessageHandler() - _, err := mh.HandleMessage(&message.Message[any]{Type: "invalid"}) + _, err := mh.HandleMessage(&message.Message{Type: "invalid"}) s.NotNil(err) } @@ -38,7 +38,7 @@ func (s *MessageHandlerTestSuite) TestHandleMessageWithInvalidType() { mh := message.NewMessageHandler() mh.RegisterMessageHandler("invalid", s.mockHandler) - _, err := mh.HandleMessage(&message.Message[any]{Type: "valid"}) + _, err := mh.HandleMessage(&message.Message{Type: "valid"}) s.NotNil(err) } @@ -49,13 +49,13 @@ func (s *MessageHandlerTestSuite) TestHandleMessageHandlerReturnsError() { mh := message.NewMessageHandler() mh.RegisterMessageHandler("valid", s.mockHandler) - _, err := mh.HandleMessage(&message.Message[any]{Type: "valid"}) + _, err := mh.HandleMessage(&message.Message{Type: "valid"}) s.NotNil(err) } func (s *MessageHandlerTestSuite) TestHandleMessageWithValidType() { - expectedProp := &proposal.Proposal[any]{ + expectedProp := &proposal.Proposal{ Type: "prop", } s.mockHandler.EXPECT().HandleMessage(gomock.Any()).Return(expectedProp, nil) @@ -63,7 +63,7 @@ func (s *MessageHandlerTestSuite) TestHandleMessageWithValidType() { mh := message.NewMessageHandler() mh.RegisterMessageHandler("valid", s.mockHandler) - msg := message.NewMessage[any](1, 2, nil, "valid") + msg := message.NewMessage(1, 2, nil, "valid") prop, err := mh.HandleMessage(msg) s.Nil(err) diff --git a/relayer/message/message.go b/relayer/message/message.go index bf3963b7..7be00fc0 100644 --- a/relayer/message/message.go +++ b/relayer/message/message.go @@ -1,20 +1,20 @@ package message type MessageType string -type Message[T any] struct { +type Message struct { Source uint8 // Source where message was initiated Destination uint8 // Destination chain of message - Data T // Data associated with the message + Data interface{} // Data associated with the message Type MessageType // Message type } -func NewMessage[T any]( +func NewMessage( source uint8, destination uint8, - data T, + data interface{}, msgType MessageType, -) *Message[T] { - return &Message[T]{ +) *Message { + return &Message{ source, destination, data, diff --git a/relayer/proposal/proposal.go b/relayer/proposal/proposal.go index 9d0813f9..f20cefd7 100644 --- a/relayer/proposal/proposal.go +++ b/relayer/proposal/proposal.go @@ -1,15 +1,15 @@ package proposal type ProposalType string -type Proposal[T any] struct { +type Proposal struct { Source uint8 Destination uint8 - Data T + Data interface{} Type ProposalType } -func NewProposal[T any](source, destination uint8, data T, propType ProposalType) *Proposal[T] { - return &Proposal[T]{ +func NewProposal(source, destination uint8, data []byte, propType ProposalType) *Proposal { + return &Proposal{ Source: source, Destination: destination, Data: data, diff --git a/relayer/relayer.go b/relayer/relayer.go index e6cfbc6a..6f6a10f6 100644 --- a/relayer/relayer.go +++ b/relayer/relayer.go @@ -16,10 +16,10 @@ type RelayedChain interface { PollEvents(ctx context.Context) // ReceiveMessage accepts the message from the source chain and converts it into // a Proposal to be submitted on-chain - ReceiveMessage(m *message.Message[any]) (*proposal.Proposal[any], error) + ReceiveMessage(m *message.Message) (*proposal.Proposal, error) // Write submits proposals on-chain. // If multiple proposals submitted they are expected to be able to be batched. - Write(proposals []*proposal.Proposal[any]) error + Write(proposals []*proposal.Proposal) error DomainID() uint8 } @@ -34,7 +34,7 @@ type Relayer struct { // Start function starts polling events for each chain and listens to cross-chain messages. // If an array of messages is sent to the channel they are expected to be to the same destination and // able to be handled in batches. -func (r *Relayer) Start(ctx context.Context, msgChan chan []*message.Message[any]) { +func (r *Relayer) Start(ctx context.Context, msgChan chan []*message.Message) { log.Info().Msgf("Starting relayer") for _, c := range r.relayedChains { @@ -54,14 +54,14 @@ func (r *Relayer) Start(ctx context.Context, msgChan chan []*message.Message[any } // Route function routes the messages to the destination chain. -func (r *Relayer) route(msgs []*message.Message[any]) { +func (r *Relayer) route(msgs []*message.Message) { destChain, ok := r.relayedChains[msgs[0].Destination] if !ok { log.Error().Uint8("domainID", destChain.DomainID()).Msgf("No chain registered for destination domain") return } - props := make([]*proposal.Proposal[any], 0) + props := make([]*proposal.Proposal, 0) for _, m := range msgs { prop, err := destChain.ReceiveMessage(m) if err != nil { diff --git a/relayer/relayer_test.go b/relayer/relayer_test.go index 4d2f4392..ec8ab3ea 100644 --- a/relayer/relayer_test.go +++ b/relayer/relayer_test.go @@ -46,8 +46,8 @@ func (s *RouteTestSuite) TestStartListensOnChannel() { chains, ) - msgChan := make(chan []*message.Message[any], 1) - msgChan <- []*message.Message[any]{ + msgChan := make(chan []*message.Message, 1) + msgChan <- []*message.Message{ {Destination: 1}, } relayer.Start(ctx, msgChan) @@ -63,7 +63,7 @@ func (s *RouteTestSuite) TestReceiveMessageFails() { chains, ) - relayer.route([]*message.Message[any]{ + relayer.route([]*message.Message{ {Destination: 1}, }) } @@ -76,14 +76,14 @@ func (s *RouteTestSuite) TestAvoidWriteWithoutProposals() { chains, ) - relayer.route([]*message.Message[any]{ + relayer.route([]*message.Message{ {Destination: 1}, }) } func (s *RouteTestSuite) TestWriteFails() { - props := make([]*proposal.Proposal[any], 1) - prop := &proposal.Proposal[any]{} + props := make([]*proposal.Proposal, 1) + prop := &proposal.Proposal{} props[0] = prop s.mockRelayedChain.EXPECT().ReceiveMessage(gomock.Any()).Return(prop, nil) s.mockRelayedChain.EXPECT().Write(props).Return(fmt.Errorf("error")) @@ -94,14 +94,14 @@ func (s *RouteTestSuite) TestWriteFails() { chains, ) - relayer.route([]*message.Message[any]{ + relayer.route([]*message.Message{ {Destination: 1}, }) } func (s *RouteTestSuite) TestWritesToChain() { - props := make([]*proposal.Proposal[any], 1) - prop := &proposal.Proposal[any]{} + props := make([]*proposal.Proposal, 1) + prop := &proposal.Proposal{} props[0] = prop s.mockRelayedChain.EXPECT().ReceiveMessage(gomock.Any()).Return(prop, nil) s.mockRelayedChain.EXPECT().Write(props).Return(nil) @@ -111,7 +111,7 @@ func (s *RouteTestSuite) TestWritesToChain() { chains, ) - relayer.route([]*message.Message[any]{ + relayer.route([]*message.Message{ {Destination: 1}, }) } From a7865e7a9ad6a4ba6c398ef677375d2759ffd79f Mon Sep 17 00:00:00 2001 From: mj52951 Date: Thu, 4 Jan 2024 14:40:09 +0100 Subject: [PATCH 2/2] Removing constructors --- relayer/message/handler_test.go | 7 ++++++- relayer/message/message.go | 14 -------------- relayer/proposal/proposal.go | 9 --------- 3 files changed, 6 insertions(+), 24 deletions(-) diff --git a/relayer/message/handler_test.go b/relayer/message/handler_test.go index 16f59724..d83ea95f 100644 --- a/relayer/message/handler_test.go +++ b/relayer/message/handler_test.go @@ -63,7 +63,12 @@ func (s *MessageHandlerTestSuite) TestHandleMessageWithValidType() { mh := message.NewMessageHandler() mh.RegisterMessageHandler("valid", s.mockHandler) - msg := message.NewMessage(1, 2, nil, "valid") + msg := &message.Message{ + Source: 1, + Destination: 2, + Data: nil, + Type: "valid", + } prop, err := mh.HandleMessage(msg) s.Nil(err) diff --git a/relayer/message/message.go b/relayer/message/message.go index 7be00fc0..32600837 100644 --- a/relayer/message/message.go +++ b/relayer/message/message.go @@ -7,17 +7,3 @@ type Message struct { Data interface{} // Data associated with the message Type MessageType // Message type } - -func NewMessage( - source uint8, - destination uint8, - data interface{}, - msgType MessageType, -) *Message { - return &Message{ - source, - destination, - data, - msgType, - } -} diff --git a/relayer/proposal/proposal.go b/relayer/proposal/proposal.go index f20cefd7..f4f6eebc 100644 --- a/relayer/proposal/proposal.go +++ b/relayer/proposal/proposal.go @@ -7,12 +7,3 @@ type Proposal struct { Data interface{} Type ProposalType } - -func NewProposal(source, destination uint8, data []byte, propType ProposalType) *Proposal { - return &Proposal{ - Source: source, - Destination: destination, - Data: data, - Type: propType, - } -}