Skip to content

Commit

Permalink
msg handler: extract deciderRejectedMsg() helper
Browse files Browse the repository at this point in the history
addresses review comment #344 (comment)
  • Loading branch information
feuGeneA committed Jul 19, 2024
1 parent 5c6422e commit 584556e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 38 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ require (
github.com/aws/aws-sdk-go-v2/config v1.27.9
github.com/aws/aws-sdk-go-v2/service/kms v1.32.1
github.com/ethereum/go-ethereum v1.12.0
github.com/golang/protobuf v1.5.4
github.com/onsi/ginkgo/v2 v2.19.0
github.com/onsi/gomega v1.33.1
github.com/pkg/errors v0.9.1
Expand Down
2 changes: 0 additions & 2 deletions main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials/insecure"
)

Expand Down Expand Up @@ -247,7 +246,6 @@ func main() {
panic(err)
}
runtime.SetFinalizer(grpcClient, func(c *grpc.ClientConn) { c.Close() })
grpcClient.WaitForStateChange(ctx, connectivity.Ready)
}

// Create listeners for each of the subnets configured as a source
Expand Down
85 changes: 50 additions & 35 deletions messages/teleporter/message_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,47 +183,62 @@ func (m *messageHandler) ShouldSendMessage(destinationClient vms.DestinationClie
return false, nil
}

var decision bool = true
deciderRejectedMsg, err := m.deciderRejectedMessage()
if err != nil {
m.logger.Warn(
"Error delegating to decider",
zap.String("warpMessageID", m.unsignedMessage.ID().String()),
zap.String("teleporterMessageID", teleporterMessageID.String()),
)
}
if deciderRejectedMsg {
m.logger.Info(
"Decider rejected message",
zap.String("warpMessageID", m.unsignedMessage.ID().String()),
zap.String("teleporterMessageID", teleporterMessageID.String()),
zap.String("destinationBlockchainID", destinationBlockchainID.String()),
)
}

return true, nil
}

func (m *messageHandler) deciderRejectedMessage() (bool, error) {
deciderClientValue := reflect.ValueOf(m.deciderClient)
if deciderClientValue.IsValid() && !deciderClientValue.IsNil() {
warpMsgIDStr := m.unsignedMessage.ID().Hex()

warpMsgID, err := hex.DecodeString(warpMsgIDStr)
if err != nil {
m.logger.Error(
"Error decoding message ID",
zap.String("warpMsgIDStr", warpMsgIDStr),
zap.Error(err),
)
return decision, err
}

// TODO: add a timeout to the context
response, err := m.deciderClient.ShouldSendMessage(
context.Background(),
&pbDecider.ShouldSendMessageRequest{
NetworkId: m.unsignedMessage.NetworkID,
SourceChainId: m.unsignedMessage.SourceChainID[:],
Payload: m.unsignedMessage.Payload,
BytesRepresentation: m.unsignedMessage.Bytes(),
Id: warpMsgID,
},
if !deciderClientValue.IsValid() || deciderClientValue.IsNil() {
return false, nil
}

warpMsgIDStr := m.unsignedMessage.ID().Hex()

warpMsgID, err := hex.DecodeString(warpMsgIDStr)
if err != nil {
m.logger.Error(
"Error decoding message ID",
zap.String("warpMsgIDStr", warpMsgIDStr),
zap.Error(err),
)
if err != nil {
m.logger.Error(
"Error response from decider.",
zap.String("destinationBlockchainID", destinationBlockchainID.String()),
zap.String("teleporterMessageID", teleporterMessageID.String()),
zap.Any("warpMessageID", warpMsgIDStr),
zap.Error(err),
)
return decision, err
}
return false, err
}

decision = response.ShouldSendMessage
// TODO: add a timeout to the context
response, err := m.deciderClient.ShouldSendMessage(
context.Background(),
&pbDecider.ShouldSendMessageRequest{
NetworkId: m.unsignedMessage.NetworkID,
SourceChainId: m.unsignedMessage.SourceChainID[:],
Payload: m.unsignedMessage.Payload,
BytesRepresentation: m.unsignedMessage.Bytes(),
Id: warpMsgID,
},
)
if err != nil {
m.logger.Error("Error response from decider.", zap.Error(err))
return false, err
}

return decision, nil
return !response.ShouldSendMessage, nil
}

// SendMessage extracts the gasLimit and packs the call data to call the receiveCrossChainMessage
Expand Down

0 comments on commit 584556e

Please sign in to comment.