-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RPC and dev command for Anoma.Protobuf.IntentsService.Verify
- Loading branch information
1 parent
1cb50a6
commit fae3a82
Showing
9 changed files
with
225 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module Commands.Dev.Anoma.Intents where | ||
|
||
import Anoma.Effect.Base | ||
import Anoma.Effect.Intents.Verify | ||
import Commands.Base | ||
import Commands.Dev.Anoma.Base | ||
import Commands.Dev.Anoma.Intents.Options | ||
import Commands.Dev.Anoma.Intents.Verify.Options | ||
import Juvix.Compiler.Nockma.Pretty hiding (Path) | ||
import Juvix.Compiler.Nockma.Translation.FromSource qualified as Nockma | ||
|
||
runCommand :: forall r. (Members (Anoma ': Error SimpleError ': AppEffects) r) => AnomaIntentsCommand -> Sem r () | ||
runCommand = \case | ||
AnomaIntentsVerify opts -> do | ||
intentFile <- fromAppPathFile (opts ^. intentsVerifyFile) | ||
parsedTerm <- runAppError @JuvixError (Nockma.cueJammedFileOrPretty intentFile) | ||
res <- cellOrFail parsedTerm go | ||
if | ||
| res ^. verifyResultValid -> logInfo "Intent is valid" >> exitSuccess | ||
| otherwise -> logInfo "Intent is invalid" >> exitFailure | ||
where | ||
go :: Term Natural -> Sem r VerifyResult | ||
go t = | ||
verify | ||
VerifyInput | ||
{ _verifyIntent = t | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module Commands.Dev.Anoma.Intents.Options where | ||
|
||
import Commands.Dev.Anoma.Intents.Verify.Options | ||
import CommonOptions | ||
|
||
newtype AnomaIntentsCommand | ||
= AnomaIntentsVerify IntentsVerifyOptions | ||
deriving stock (Data) | ||
|
||
parseAnomaIntentsCommand :: Parser AnomaIntentsCommand | ||
parseAnomaIntentsCommand = | ||
hsubparser commandVerify | ||
where | ||
commandVerify :: Mod CommandFields AnomaIntentsCommand | ||
commandVerify = command "verify" runInfo | ||
where | ||
runInfo :: ParserInfo AnomaIntentsCommand | ||
runInfo = | ||
info | ||
(AnomaIntentsVerify <$> parseIntentsVerifyOptions) | ||
(progDesc "Call the Anoma.Protobuf.IntentsService.Verify endpoint") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module Commands.Dev.Anoma.Intents.Verify.Options where | ||
|
||
import CommonOptions | ||
|
||
newtype IntentsVerifyOptions = IntentsVerifyOptions | ||
{_intentsVerifyFile :: AppPath File} | ||
deriving stock (Data) | ||
|
||
makeLenses ''IntentsVerifyOptions | ||
|
||
parseIntentsVerifyOptions :: Parser IntentsVerifyOptions | ||
parseIntentsVerifyOptions = do | ||
_intentsVerifyFile <- parseInputFile FileExtNockma | ||
pure IntentsVerifyOptions {..} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module Anoma.Effect.Intents.Verify where | ||
|
||
import Anoma.Effect.Base | ||
import Anoma.Rpc.Intents.Verify | ||
import Juvix.Compiler.Nockma.Encoding | ||
import Juvix.Compiler.Nockma.Language qualified as Nockma | ||
import Juvix.Compiler.Nockma.Pretty | ||
import Juvix.Prelude | ||
import Juvix.Prelude.Aeson (Value) | ||
import Juvix.Prelude.Aeson qualified as Aeson | ||
|
||
newtype VerifyInput = VerifyInput | ||
{_verifyIntent :: Nockma.Term Natural} | ||
|
||
newtype VerifyResult = VerifyResult | ||
{_verifyResultValid :: Bool} | ||
|
||
makeLenses ''VerifyInput | ||
makeLenses ''VerifyResult | ||
|
||
verify :: | ||
forall r. | ||
(Members '[Anoma, Error SimpleError, Logger] r) => | ||
VerifyInput -> | ||
Sem r VerifyResult | ||
verify i = do | ||
let intent = encodeJam64 (i ^. verifyIntent) | ||
nodeInfo <- getNodeInfo | ||
let msg = Request {_requestNodeInfo = nodeInfo, _requestIntent = Intent {_intentIntent = intent}} | ||
logMessageValue "Request payload" msg | ||
resVal :: Value <- anomaRpc verifyGrpcUrl (Aeson.toJSON msg) >>= fromJSONErr | ||
logMessageValue "Response Payload" resVal | ||
res :: Response <- fromJSONErr resVal | ||
return VerifyResult {_verifyResultValid = res ^. responseValid} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
module Anoma.Rpc.Intents.Verify where | ||
|
||
import Anoma.Rpc.Base | ||
import Juvix.Prelude | ||
import Juvix.Prelude.Aeson as Aeson | ||
|
||
verifyGrpcUrl :: GrpcMethodUrl | ||
verifyGrpcUrl = | ||
mkGrpcMethodUrl $ | ||
"Anoma" :| ["Protobuf", "IntentsService", "Verify"] | ||
|
||
newtype Intent = Intent | ||
{_intentIntent :: Text} | ||
|
||
$( deriveJSON | ||
defaultOptions | ||
{ fieldLabelModifier = \case | ||
"_intentIntent" -> "intent" | ||
_ -> impossibleError "All fields must be covered" | ||
} | ||
''Intent | ||
) | ||
|
||
data Request = Request | ||
{ _requestNodeInfo :: NodeInfo, | ||
_requestIntent :: Intent | ||
} | ||
|
||
$( deriveJSON | ||
defaultOptions | ||
{ fieldLabelModifier = \case | ||
"_requestNodeInfo" -> "node_info" | ||
"_requestIntent" -> "intent" | ||
_ -> impossibleError "All fields must be covered" | ||
} | ||
''Request | ||
) | ||
|
||
newtype Response = Response | ||
{_responseValid :: Bool} | ||
|
||
$( deriveJSON | ||
defaultOptions | ||
{ fieldLabelModifier = \case | ||
"_responseValid" -> "valid" | ||
_ -> impossibleError "All fields must be covered" | ||
} | ||
''Response | ||
) | ||
|
||
makeLenses ''Request | ||
makeLenses ''Response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters