Skip to content
Closed
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
16 changes: 12 additions & 4 deletions protofsm/msg_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ type MsgEndpoint interface {

// CanHandle returns true if the target message can be routed to this
// endpoint.
CanHandle(msg PeerMsg) bool
CanHandle(msg PeerMsg) (bool, error)

// SendMessage handles the target message, and returns true if the
// message was able being processed.
SendMessage(msg PeerMsg) bool
SendMessage(msg PeerMsg) (bool, error)
}

// MsgRouter is an interface that represents a message router, which is generic
Expand Down Expand Up @@ -274,12 +274,20 @@ func (p *MultiMsgRouter) msgRouter() {
// to those that can handle it the message.
var couldSend bool
for _, endpoint := range endpoints {
if endpoint.CanHandle(msg) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does CanHandle need an error? The implementation should just be doing a type assertion on the message to decide if it can handle it or not.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would say the same for SendMessage above, can you give more detail re the motivating context here?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the interface was changed to support returning an error, as this was required in tapd in order to allow for the server readiness check to fail
https://github.com/lightninglabs/taproot-assets/pull/1126/files#diff-366e46a40f6f60b4f7614eb0976bb51820364bf5ca6ccc4787eb49d7bdbef3e6R827-R845

canHandle, err := endpoint.CanHandle(msg)
if err != nil {
msgQuery.SendError(err)
}

if canHandle {
log.Debugf("MsgRouter: sending msg %T "+
"to endpoint %s", msg.Message,
endpoint.Name())

sent := endpoint.SendMessage(msg)
sent, err := endpoint.SendMessage(msg)
if err != nil {
msgQuery.SendError(err)
}
couldSend = couldSend || sent
}
}
Expand Down
8 changes: 4 additions & 4 deletions protofsm/msg_router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ func (m *mockEndpoint) Name() string {
return args.String(0)
}

func (m *mockEndpoint) CanHandle(msg PeerMsg) bool {
func (m *mockEndpoint) CanHandle(msg PeerMsg) (bool, error) {
args := m.Called(msg)

return args.Bool(0)
return args.Bool(0), nil
}

func (m *mockEndpoint) SendMessage(msg PeerMsg) bool {
func (m *mockEndpoint) SendMessage(msg PeerMsg) (bool, error) {
args := m.Called(msg)

return args.Bool(0)
return args.Bool(0), nil
}

// TestMessageRouterOperation tests the basic operation of the message router:
Expand Down