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

SC-7142 envelope id and error return #105

Merged
merged 2 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
42 changes: 27 additions & 15 deletions pkg/rvasp/rvasp.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,16 +226,27 @@ func (s *Server) Transfer(ctx context.Context, req *pb.TransferRequest) (reply *
return nil, status.Errorf(codes.FailedPrecondition, "unknown originator policy '%s' for wallet '%s'", policy, account.WalletAddress)
}

if transferError != nil {
log.Debug().Err(transferError).Msg("transfer failed")
xfer.SetState(pb.TransactionState_FAILED)
}

// Return the transfer response
// Build the transfer response
reply = &pb.TransferReply{
Transaction: xfer.Proto(),
}

// Handle rVASP errors and TRISA protocol errors
if transferError != nil {
bbengfort marked this conversation as resolved.
Show resolved Hide resolved
switch err := transferError.(type) {
case *protocol.Error:
log.Warn().Str("message", err.Error()).Msg("TRISA protocol error while performing transfer")
reply.Error = &pb.Error{
Message: err.Error(),
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I tried to put the error code in here instead of using Error() but apparently it wasn't being populated on envelope.Reject()?

Copy link
Collaborator

Choose a reason for hiding this comment

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

It's not? That's not good - let me take a look at the TRISA code.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It looks like Reject is just adding the error to the struct without modification:

https://github.com/trisacrypto/trisa/blob/main/pkg/trisa/envelope/envelope.go#L136-L149

I guess it's up to the client to return an error that has an error code? Is there somewhere in the rVASP code where we're forgetting to add a code?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes there was a spot where the rVASP was not returning the error code back which I fixed in the previous push but I forgot to update the code here.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice!

}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we also include the code here as well?

Suggested change
reply.Error = &pb.Error{
Message: err.Error(),
}
reply.Error = &pb.Error{
Code: err.Code,
Message: err.Message,
}

xfer.SetState(pb.TransactionState_REJECTED)
transferError = nil
default:
log.Warn().Err(err).Msg("error while performing transfer")
xfer.SetState(pb.TransactionState_FAILED)
}
}

// Save the updated transaction
// TODO: Clean up completed transactions in the database
if err = s.db.Save(xfer).Error; err != nil {
Expand Down Expand Up @@ -371,8 +382,7 @@ func (s *Server) sendTransfer(xfer *db.Transaction, beneficiary *db.Wallet, part
reject, isErr := envelope.Check(msg)
if isErr {
if reject != nil {
xfer.SetState(pb.TransactionState_REJECTED)
return nil
return reject
Copy link
Collaborator

Choose a reason for hiding this comment

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

Confirming from above comment that it is returning the rejection error as an error.

}
log.Warn().Err(err).Msg("TRISA protocol error while checking envelope")
return status.Errorf(codes.FailedPrecondition, "TRISA protocol error: %s", err)
Expand Down Expand Up @@ -472,7 +482,7 @@ func (s *Server) sendError(xfer *db.Transaction, beneficiary *db.Wallet) (err er

reject := protocol.Errorf(protocol.ComplianceCheckFail, "rVASP mock compliance check failed")
var msg *protocol.SecureEnvelope
if msg, err = envelope.Reject(reject); err != nil {
if msg, err = envelope.Reject(reject, envelope.WithEnvelopeID(xfer.Envelope)); err != nil {
log.Error().Err(err).Msg("could not create TRISA error envelope")
return status.Errorf(codes.Internal, "could not create TRISA error envelope: %s", err)
}
Expand All @@ -484,13 +494,15 @@ func (s *Server) sendError(xfer *db.Transaction, beneficiary *db.Wallet) (err er
}

// Check for the TRISA rejection error
if state := envelope.Status(msg); state != envelope.Error {
log.Warn().Uint("state", uint(state)).Msg("unexpected TRISA response, expected error envelope")
return fmt.Errorf("expected TRISA rejection error, received envelope in state %d", state)
reject, isErr := envelope.Check(msg)
if !isErr || reject == nil {
state := envelope.Status(msg)
log.Warn().Str("state", state.String()).Msg("unexpected TRISA response, expected reject envelope")
return fmt.Errorf("expected TRISA rejection error, received envelope in state %s", state.String())
}
xfer.SetState(pb.TransactionState_REJECTED)

return nil
return reject
}

// respondAsync responds to a serviced transfer request from the beneficiary by
Expand Down Expand Up @@ -542,10 +554,10 @@ func (s *Server) respondAsync(peer *peers.Peer, payload *protocol.Payload, ident
}

// Create the response envelope
out, reject, err := envelope.Seal(payload, envelope.WithRSAPublicKey(signKey))
out, reject, err := envelope.Seal(payload, envelope.WithRSAPublicKey(signKey), envelope.WithEnvelopeID(xfer.Envelope))
if err != nil {
if reject != nil {
if out, err = envelope.Reject(reject); err != nil {
if out, err = envelope.Reject(reject, envelope.WithEnvelopeID(xfer.Envelope)); err != nil {
return nil, protocol.Errorf(protocol.EnvelopeDecodeFail, "TRISA protocol error: %s", err)
}
xfer.SetState(pb.TransactionState_REJECTED)
Expand Down
4 changes: 2 additions & 2 deletions pkg/rvasp/trisa.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ func (s *TRISA) respondTransfer(in *protocol.SecureEnvelope, peer *peers.Peer, i

s.parent.updates.Broadcast(0, "sealing beneficiary information and returning", pb.MessageCategory_TRISAP2P)

out, reject, err := envelope.Seal(payload, envelope.WithRSAPublicKey(signKey))
out, reject, err := envelope.Seal(payload, envelope.WithRSAPublicKey(signKey), envelope.WithEnvelopeID(in.Id))
if err != nil {
if reject != nil {
if out, err = envelope.Reject(reject, envelope.WithEnvelopeID(in.Id)); err != nil {
Expand Down Expand Up @@ -625,7 +625,7 @@ func (s *TRISA) respondPending(in *protocol.SecureEnvelope, peer *peers.Peer, id
return nil, protocol.Errorf(protocol.InternalError, "request could not be processed")
}

out, reject, err := envelope.Seal(payload, envelope.WithRSAPublicKey(signKey))
out, reject, err := envelope.Seal(payload, envelope.WithRSAPublicKey(signKey), envelope.WithEnvelopeID(in.Id))
if err != nil {
if reject != nil {
if out, err = envelope.Reject(reject, envelope.WithEnvelopeID(in.Id)); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion proto/rvasp/v1/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ message TransferRequest {
// or an error if there are insufficient funds or the account or beneficiary could not
// be looked up. Errors encountered during the TRISA protocol may also be returned.
message TransferReply {
Error error = 1; // Only used in live stream
Error error = 1; // populated with an error encountered during the transfer or from the response envelope
Copy link
Collaborator

Choose a reason for hiding this comment

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

Awesome, thank you!

Transaction transaction = 2;
}

Expand Down