Skip to content

Commit 5c052f5

Browse files
committed
Allow for route function to return errors to message source
1 parent 7a3e335 commit 5c052f5

File tree

4 files changed

+77
-3
lines changed

4 files changed

+77
-3
lines changed

relayer/message/message.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ type Message struct {
77
Data interface{} // Data associated with the message
88
ID string // ID is used to track and identify message across networks
99
Type MessageType // Message type
10+
ErrChn chan error // ErrChn is used to share errors that happen on the destination handler
1011
}
1112

12-
func NewMessage(source, destination uint8, data interface{}, id string, msgType MessageType) *Message {
13+
func NewMessage(source, destination uint8, data interface{}, id string, msgType MessageType, errChn chan error) *Message {
1314
return &Message{
1415
Source: source,
1516
Destination: destination,
1617
Data: data,
1718
Type: msgType,
1819
ID: id,
20+
ErrChn: errChn,
1921
}
2022
}

relayer/relayer.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ package relayer
55

66
import (
77
"context"
8+
"fmt"
89

910
"github.com/rs/zerolog/log"
1011
"github.com/sygmaprotocol/sygma-core/relayer/message"
1112
"github.com/sygmaprotocol/sygma-core/relayer/proposal"
13+
"github.com/sygmaprotocol/sygma-core/utils"
1214
)
1315

1416
type RelayedChain interface {
@@ -55,9 +57,11 @@ func (r *Relayer) Start(ctx context.Context, msgChan chan []*message.Message) {
5557

5658
// Route function routes the messages to the destination chain.
5759
func (r *Relayer) route(msgs []*message.Message) {
60+
errChn := msgs[0].ErrChn
5861
destChain, ok := r.relayedChains[msgs[0].Destination]
5962
if !ok {
6063
log.Error().Uint8("domainID", msgs[0].Destination).Msgf("No chain registered for destination domain")
64+
utils.TrySendError(errChn, fmt.Errorf("no chain registered"))
6165
return
6266
}
6367

@@ -69,6 +73,7 @@ func (r *Relayer) route(msgs []*message.Message) {
6973
prop, err := destChain.ReceiveMessage(m)
7074
if err != nil {
7175
log.Err(err).Msgf("Failed receiving message %+v", m)
76+
utils.TrySendError(errChn, err)
7277
continue
7378
}
7479

@@ -79,13 +84,16 @@ func (r *Relayer) route(msgs []*message.Message) {
7984
}
8085
}
8186
if len(props) == 0 {
87+
utils.TrySendError(errChn, nil)
8288
return
8389
}
8490

8591
log.Debug().Msgf("Writing message")
8692
err := destChain.Write(props)
8793
if err != nil {
94+
utils.TrySendError(errChn, err)
8895
log.Err(err).Msgf("Failed writing message")
8996
return
9097
}
98+
utils.TrySendError(errChn, nil)
9199
}

relayer/relayer_test.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,16 @@ func (s *RouteTestSuite) TestWriteFails() {
9595
chains,
9696
)
9797

98+
errChn := make(chan error, 1)
9899
relayer.route([]*message.Message{
99-
{Destination: 1},
100+
{
101+
Destination: 1,
102+
ErrChn: errChn,
103+
},
100104
})
105+
106+
err := <-errChn
107+
s.NotNil(err)
101108
}
102109

103110
func (s *RouteTestSuite) TestWritesToChain() {
@@ -113,8 +120,35 @@ func (s *RouteTestSuite) TestWritesToChain() {
113120
chains,
114121
)
115122

123+
errChn := make(chan error, 1)
116124
relayer.route([]*message.Message{
117-
{Destination: 1},
125+
{
126+
Destination: 1,
127+
ErrChn: errChn,
128+
},
129+
})
130+
131+
err := <-errChn
132+
s.Nil(err)
133+
}
134+
135+
func (s *RouteTestSuite) TestWritesToChain_BlockingErrChn() {
136+
props := make([]*proposal.Proposal, 1)
137+
prop := &proposal.Proposal{}
138+
props[0] = prop
139+
s.mockRelayedChain.EXPECT().ReceiveMessage(gomock.Any()).Return(prop, nil)
140+
s.mockRelayedChain.EXPECT().Write(props).Return(nil)
141+
s.mockRelayedChain.EXPECT().DomainID().Return(uint8(1)).Times(1)
142+
chains := make(map[uint8]RelayedChain)
143+
chains[1] = s.mockRelayedChain
144+
relayer := NewRelayer(
145+
chains,
146+
)
147+
148+
relayer.route([]*message.Message{
149+
{
150+
Destination: 1,
151+
},
118152
})
119153
}
120154

utils/channel_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package utils_test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/suite"
8+
"github.com/sygmaprotocol/sygma-core/utils"
9+
)
10+
11+
type ChannelTestSuite struct {
12+
suite.Suite
13+
}
14+
15+
func TestRunChannelTestSuite(t *testing.T) {
16+
suite.Run(t, new(ChannelTestSuite))
17+
}
18+
19+
func (s *ChannelTestSuite) Test_TrySendError_NonBlocking() {
20+
utils.TrySendError(nil, fmt.Errorf("error"))
21+
22+
errChn := make(chan error)
23+
utils.TrySendError(errChn, fmt.Errorf("error"))
24+
25+
bufErrChn := make(chan error, 1)
26+
utils.TrySendError(bufErrChn, fmt.Errorf("error"))
27+
28+
err := <-bufErrChn
29+
s.NotNil(err)
30+
}

0 commit comments

Comments
 (0)