Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Reverting generics and minor fixes #20

Merged
merged 2 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions chains/evm/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand Down
8 changes: 4 additions & 4 deletions chains/substrate/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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())
Expand Down
4 changes: 2 additions & 2 deletions mock/message.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions mock/relayer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions relayer/message/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
15 changes: 10 additions & 5 deletions relayer/message/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -49,21 +49,26 @@ 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)

mh := message.NewMessageHandler()
mh.RegisterMessageHandler("valid", s.mockHandler)

msg := message.NewMessage[any](1, 2, nil, "valid")
msg := &message.Message{
Source: 1,
Destination: 2,
Data: nil,
Type: "valid",
}
prop, err := mh.HandleMessage(msg)

s.Nil(err)
Expand Down
18 changes: 2 additions & 16 deletions relayer/message/message.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
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](
source uint8,
destination uint8,
data T,
msgType MessageType,
) *Message[T] {
return &Message[T]{
source,
destination,
data,
msgType,
}
}
13 changes: 2 additions & 11 deletions relayer/proposal/proposal.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
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]{
Source: source,
Destination: destination,
Data: data,
Type: propType,
}
}
10 changes: 5 additions & 5 deletions relayer/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
20 changes: 10 additions & 10 deletions relayer/relayer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -63,7 +63,7 @@ func (s *RouteTestSuite) TestReceiveMessageFails() {
chains,
)

relayer.route([]*message.Message[any]{
relayer.route([]*message.Message{
{Destination: 1},
})
}
Expand All @@ -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"))
Expand All @@ -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)
Expand All @@ -111,7 +111,7 @@ func (s *RouteTestSuite) TestWritesToChain() {
chains,
)

relayer.route([]*message.Message[any]{
relayer.route([]*message.Message{
{Destination: 1},
})
}
Loading