Skip to content

Commit

Permalink
fix: MissingProtocolExecuter (#140)
Browse files Browse the repository at this point in the history
For ATL-5840

Signed-off-by: Fabio <Pinheiro>
  • Loading branch information
FabioPinheiro authored and Fabio committed Apr 30, 2024
1 parent 6514117 commit fb177b7
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 30 deletions.
32 changes: 16 additions & 16 deletions Mediator-Error_Handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ https://identity.foundation/didcomm-messaging/spec/#problem-reports

This table defines the expected behavior of the mediator in different scenarios not covered by the specifications.

| Mediators | Atala Mediator | Roadmap Atala Mediator | RootsId | Blocktrust |
|-------------|----------------|------------------------|---------|------------|
| Scenario G1 | G1C | - | ? | ? |
| Scenario G2 | G2A | [TODO ATL-5840] G2B | ? | ? |
| Scenario G3 | G3A | [TODO (next)] G3B | ? | ? |
| Scenario G4 | G4B | - | ? | ? |
| Scenario G5 | Fallback G4B | [TODO] G5B | ? | ? |
| Scenario G6 | Fallback G4B | [WIP] G6B | ? | ? |
| Scenario G7 | Fallback G4B | [TODO] G7B | ? | ? |
| Scenario G8 | G8C | - | ? | ? |
| | | | | |
| Scenario M1 | M1B | - | ? | ? |
| Scenario M2 | M2B | - | ? | ? |
| Scenario M3 | Fallback G4 | M3B | ? | ? |
| Scenario M4 | M4B | - | ? | ? |
| Scenario M5 | M5A | [TODO] M5B | ? | ? |
| Mediators | Atala Mediator | Roadmap Atala Mediator | | RootsId | Blocktrust |
|-------------|----------------|------------------------|---|---------|------------|
| Scenario G1 | G1C | - | | ? | ? |
| Scenario G2 | G2B [ATL-5840] | - || | ? |
| Scenario G3 | Fallback G2B | [TODO] G3B | | | ? |
| Scenario G4 | G4B | - | | | ? |
| Scenario G5 | Fallback G4B | [TODO] G5B | | | ? |
| Scenario G6 | Fallback G4B | [WIP] G6B | | | ? |
| Scenario G7 | Fallback G4B | [TODO] G7B | | | ? |
| Scenario G8 | G8C | - | | | ? |
| | | | | | |
| Scenario M1 | M1B | - | | | ? |
| Scenario M2 | M2B | - | | | ? |
| Scenario M3 | Fallback G4 | M3B | | | ? |
| Scenario M4 | M4B | - | | | ? |
| Scenario M5 | M5A | [TODO] M5B | | | ? |

### Scenarios Description

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import fmgp.did.comm.protocol.trustping2.*
import io.iohk.atala.mediator.*
import io.iohk.atala.mediator.comm.*
import io.iohk.atala.mediator.db.*
import io.iohk.atala.mediator.protocols.NullProtocolExecuter
import zio.*
import zio.json.*
import io.iohk.atala.mediator.protocols.MissingProtocolExecuter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,29 @@ import fmgp.did.comm.protocol.trustping2.*
import io.iohk.atala.mediator.*
import io.iohk.atala.mediator.comm.*
import io.iohk.atala.mediator.db.*
import io.iohk.atala.mediator.protocols.NullProtocolExecuter
import io.iohk.atala.mediator.protocols.MissingProtocolExecuter
import zio.*
import zio.json.*
import io.iohk.atala.mediator.protocols.MissingProtocolExecuter
//TODO pick a better name // maybe "Protocol" only

trait ProtocolExecuter[-R, +E] { // <: MediatorError | StorageError] {

def supportedPIURI: Seq[PIURI]

/** @return can return a Sync Reply Msg */
/** @return
* can return a Sync Reply Msg
*
* MUST be override
* {{{
* override def execute[R1 <: R](
* plaintextMessage: PlaintextMessage
* ): ZIO[R1, E, Option[SignedMessage | EncryptedMessage]] =
* program(plaintextMessage) *> ZIO.none
* }}}
*/
def execute[R1 <: R](
plaintextMessage: PlaintextMessage
): ZIO[R1, E, Option[SignedMessage | EncryptedMessage]] =
program(plaintextMessage) *> ZIO.none
): ZIO[R1, E, Option[SignedMessage | EncryptedMessage]]

def program[R1 <: R](plaintextMessage: PlaintextMessage): ZIO[R1, E, Action]
}
Expand All @@ -33,28 +41,29 @@ object ProtocolExecuter {
type Services = Resolver & Agent & Operations & MessageDispatcher & OutboxMessageRepo
type Erros = MediatorError | StorageError
}
case class ProtocolExecuterCollection[-R <: Agent, +E](
case class ProtocolExecuterCollection[-R, +E](
executers: ProtocolExecuter[R, E]*
) extends ProtocolExecuter[R, E] {
)(fallback: ProtocolExecuter[R, E])
extends ProtocolExecuter[R, E] {

override def supportedPIURI: Seq[PIURI] = executers.flatMap(_.supportedPIURI)

def selectExecutersFor(piuri: PIURI) = executers.find(_.supportedPIURI.contains(piuri))

override def execute[R1 <: R](
plaintextMessage: PlaintextMessage,
): ZIO[R1, E, Option[SignedMessage | EncryptedMessage]] =
): ZIO[R, E, Option[SignedMessage | EncryptedMessage]] =
selectExecutersFor(plaintextMessage.`type`) match
// case None => NullProtocolExecuter.execute(plaintextMessage)
case None => MissingProtocolExecuter.execute(plaintextMessage)
case None => fallback.execute(plaintextMessage)
case Some(px) => px.execute(plaintextMessage)

override def program[R1 <: R](
plaintextMessage: PlaintextMessage,
): ZIO[R1, E, Action] =
selectExecutersFor(plaintextMessage.`type`) match
// case None => NullProtocolExecuter.program(plaintextMessage)
case None => MissingProtocolExecuter.program(plaintextMessage)
case None => fallback.program(plaintextMessage)
case Some(px) => px.program(plaintextMessage)
}

Expand All @@ -70,6 +79,7 @@ trait ProtocolExecuterWithServices[
program(plaintextMessage)
.tap(v => ZIO.logDebug(v.toString)) // DEBUG
.flatMap(action => ActionUtils.packResponse(Some(plaintextMessage), action))
.debug

override def program[R1 <: R](
plaintextMessage: PlaintextMessage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ case class MediatorAgent(
MediatorCoordinationExecuter,
ForwardMessageExecuter,
PickupExecuter,
)
)(fallback = MissingProtocolExecuter())
)

val messageDispatcherLayer: ZLayer[Client, MediatorThrowable, MessageDispatcher] =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.iohk.atala.mediator.protocols

import fmgp.crypto.error.FailToParse
import fmgp.did.comm.{PIURI, PlaintextMessage}
import fmgp.did.comm.{PIURI, PlaintextMessage, SignedMessage, EncryptedMessage}
import fmgp.did.comm.protocol.basicmessage2.BasicMessage
import io.iohk.atala.mediator.{MediatorError, MediatorDidError, MediatorThrowable}
import io.iohk.atala.mediator.actions.{NoReply, ProtocolExecuter}
Expand All @@ -10,6 +10,12 @@ import zio.{Console, ZIO}
object BasicMessageExecuter extends ProtocolExecuter[Any, MediatorError] {

override def supportedPIURI: Seq[PIURI] = Seq(BasicMessage.piuri)

override def execute[R](
plaintextMessage: PlaintextMessage
): ZIO[R, MediatorError, Option[SignedMessage | EncryptedMessage]] =
program(plaintextMessage).debug *> ZIO.none

override def program[R1 <: Any](plaintextMessage: PlaintextMessage) = for {
job <- BasicMessage.fromPlaintextMessage(plaintextMessage) match
case Left(error) => ZIO.fail(MediatorDidError(FailToParse(error)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import fmgp.did.comm.PlaintextMessage
import io.iohk.atala.mediator.MissingProtocolError
import io.iohk.atala.mediator.actions.ProtocolExecuter
import io.iohk.atala.mediator.actions.Reply
import io.iohk.atala.mediator.actions.ProtocolExecuterWithServices
import io.iohk.atala.mediator.MediatorError

object MissingProtocolExecuter extends ProtocolExecuter[Agent, Nothing] {
case class MissingProtocolExecuter() extends ProtocolExecuterWithServices[ProtocolExecuter.Services, MediatorError] {

override def supportedPIURI = Seq()
override def program[R1 <: Agent](plaintextMessage: PlaintextMessage) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@ import fmgp.did.comm.PlaintextMessage
import io.iohk.atala.mediator.MissingProtocolError
import io.iohk.atala.mediator.actions.ProtocolExecuter
import zio.ZIO
import fmgp.did.comm.SignedMessage
import fmgp.did.comm.EncryptedMessage

object NullProtocolExecuter extends ProtocolExecuter[Any, MissingProtocolError] {

override def supportedPIURI = Seq()

override def execute[Any](
plaintextMessage: PlaintextMessage
): ZIO[Any, MissingProtocolError, Option[SignedMessage | EncryptedMessage]] =
program(plaintextMessage).debug *> ZIO.none

override def program[R1 <: Any](plaintextMessage: PlaintextMessage) =
ZIO.fail(MissingProtocolError(plaintextMessage.`type`))
}

0 comments on commit fb177b7

Please sign in to comment.