module arch.node.engines.decryption_behaviour; import prelude open; import arch.node.types.messages open; import arch.system.identity.identity open; import arch.node.types.engine open; import arch.node.engines.decryption_config open; import arch.node.engines.decryption_environment open; import arch.node.engines.decryption_messages open; import arch.node.types.identities open; import arch.node.types.anoma as Anoma open; type ReplyTo := mkReplyTo@{ whoAsked : Option EngineID; mailbox : Option MailboxID; }; type DecryptionActionArgument := | DecryptionActionArgumentReplyTo ReplyTo; DecryptionActionArguments : Type := List DecryptionActionArgument; DecryptionAction : Type := Action DecryptionCfg DecryptionLocalState DecryptionMailboxState DecryptionTimerHandle DecryptionActionArguments Anoma.Msg Anoma.Cfg Anoma.Env; DecryptionActionInput : Type := ActionInput DecryptionCfg DecryptionLocalState DecryptionMailboxState DecryptionTimerHandle DecryptionActionArguments Anoma.Msg; DecryptionActionEffect : Type := ActionEffect DecryptionLocalState DecryptionMailboxState DecryptionTimerHandle Anoma.Msg Anoma.Cfg Anoma.Env; DecryptionActionExec : Type := ActionExec DecryptionCfg DecryptionLocalState DecryptionMailboxState DecryptionTimerHandle DecryptionActionArguments Anoma.Msg Anoma.Cfg Anoma.Env; decryptAction (input : DecryptionActionInput) : Option DecryptionActionEffect := let env := ActionInput.env input; cfg := ActionInput.cfg input; tt := ActionInput.trigger input; in case getEngineMsgFromTimestampedTrigger tt of | some emsg := case EngineMsg.msg emsg of { | Anoma.PreMsg.MsgDecryption (DecryptionMsg.Request request) := let decryptedData := Decryptor.decrypt (DecryptionCfg.decryptor (EngineCfg.cfg cfg)) (DecryptionCfg.backend (EngineCfg.cfg cfg)) (RequestDecryption.data request); responseMsg := case decryptedData of | none := ReplyDecryption.mkReplyDecryption@{ data := emptyByteString; err := some "Decryption Failed"; } | some plaintext := ReplyDecryption.mkReplyDecryption@{ data := plaintext; err := none; }; in some ActionEffect.mk@{ env := env; msgs := [ EngineMsg.mk@{ sender := getEngineIDFromEngineCfg cfg; target := EngineMsg.sender emsg; mailbox := some 0; msg := Anoma.PreMsg.MsgDecryption (DecryptionMsg.Reply responseMsg); }; ]; timers := []; engines := []; } | _ := none } | _ := none; decryptActionLabel : DecryptionActionExec := ActionExec.Seq [decryptAction]; DecryptionGuard : Type := Guard DecryptionCfg DecryptionLocalState DecryptionMailboxState DecryptionTimerHandle DecryptionActionArguments Anoma.Msg Anoma.Cfg Anoma.Env; DecryptionGuardOutput : Type := GuardOutput DecryptionCfg DecryptionLocalState DecryptionMailboxState DecryptionTimerHandle DecryptionActionArguments Anoma.Msg Anoma.Cfg Anoma.Env; DecryptionGuardEval : Type := GuardEval DecryptionCfg DecryptionLocalState DecryptionMailboxState DecryptionTimerHandle DecryptionActionArguments Anoma.Msg Anoma.Cfg Anoma.Env; decryptGuard (tt : TimestampedTrigger DecryptionTimerHandle Anoma.Msg) (cfg : EngineCfg DecryptionCfg) (env : DecryptionEnv) : Option DecryptionGuardOutput := case getEngineMsgFromTimestampedTrigger tt of | some EngineMsg.mk@{ msg := Anoma.PreMsg.MsgDecryption (DecryptionMsg.Request _); } := some GuardOutput.mk@{ action := decryptActionLabel; args := []; } | _ := none; DecryptionBehaviour : Type := EngineBehaviour DecryptionCfg DecryptionLocalState DecryptionMailboxState DecryptionTimerHandle DecryptionActionArguments Anoma.Msg Anoma.Cfg Anoma.Env; decryptionBehaviour : DecryptionBehaviour := EngineBehaviour.mk@{ guards := GuardEval.First [decryptGuard]; };