Skip to content

Commit

Permalink
Add RPC call and dev command for listUnrevealedCommits
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcadman committed Dec 6, 2024
1 parent 4eb7509 commit 8637618
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/Commands/Dev/Anoma.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Commands.Base
import Commands.Dev.Anoma.AddTransaction.Options
import Commands.Dev.Anoma.Base
import Commands.Dev.Anoma.Client
import Commands.Dev.Anoma.Indexer qualified as Indexer
import Commands.Dev.Anoma.Options
import Commands.Dev.Anoma.Prove qualified as Prove
import Commands.Dev.Anoma.Start qualified as Start
Expand All @@ -29,6 +30,7 @@ runCommand g =
AnomaCommandAddTransaction opts ->
runAnomaWithHostConfig
(addTransaction (opts ^. addTransactionFile))
AnomaCommandIndexer opts -> runAnomaWithHostConfig (Indexer.runCommand opts)
where
runAnomaWithHostConfig :: (Members (Error SimpleError ': AppEffects) x) => Sem (Anoma ': x) () -> Sem x ()
runAnomaWithHostConfig eff = do
Expand Down
21 changes: 21 additions & 0 deletions app/Commands/Dev/Anoma/Indexer.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Commands.Dev.Anoma.Indexer where

import Anoma.Effect.Base
import Anoma.Effect.Indexer.ListUnrevealedCommits
import Commands.Base
import Commands.Dev.Anoma.Indexer.ListUnrevealedCommits.Options
import Commands.Dev.Anoma.Indexer.Options
import Data.Text qualified as T
import Juvix.Compiler.Nockma.Pretty hiding (Path)

runCommand :: forall r. (Members (Anoma ': Error SimpleError ': AppEffects) r) => AnomaIndexerCommand -> Sem r ()
runCommand = \case
AnomaIndexerListUnrevealedCommits opts -> do
res <- listUnrevealedCommits
case opts ^. indexerListUnrevealedCommitsOutputFile of
Just out -> do
f <- fromAppFile out
let cs = T.unlines (ppPrint <$> res ^. listUnrevealedCommitsResultCommits)
writeFileEnsureLn' f cs
Nothing -> do
forM_ (res ^. listUnrevealedCommitsResultCommits) (renderStdOutLn . ppOutDefault)
14 changes: 14 additions & 0 deletions app/Commands/Dev/Anoma/Indexer/ListUnrevealedCommits/Options.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Commands.Dev.Anoma.Indexer.ListUnrevealedCommits.Options where

import CommonOptions

newtype IndexerListUnrevealedCommitsOptions = IndexerListUnrevealedCommitsOptions
{_indexerListUnrevealedCommitsOutputFile :: Maybe (AppPath File)}
deriving stock (Data)

parseUnrevealedCommitsOptions :: Parser IndexerListUnrevealedCommitsOptions
parseUnrevealedCommitsOptions = do
_indexerListUnrevealedCommitsOutputFile <- optional parseGenericOutputFile
pure IndexerListUnrevealedCommitsOptions {..}

makeLenses ''IndexerListUnrevealedCommitsOptions
21 changes: 21 additions & 0 deletions app/Commands/Dev/Anoma/Indexer/Options.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Commands.Dev.Anoma.Indexer.Options where

import Commands.Dev.Anoma.Indexer.ListUnrevealedCommits.Options
import CommonOptions

newtype AnomaIndexerCommand
= AnomaIndexerListUnrevealedCommits IndexerListUnrevealedCommitsOptions
deriving stock (Data)

parseAnomaIndexerCommand :: Parser AnomaIndexerCommand
parseAnomaIndexerCommand =
hsubparser commandListUnrevealedCommits
where
commandListUnrevealedCommits :: Mod CommandFields AnomaIndexerCommand
commandListUnrevealedCommits = command "list-unrevealed-commits" runInfo
where
runInfo :: ParserInfo AnomaIndexerCommand
runInfo =
info
(AnomaIndexerListUnrevealedCommits <$> parseUnrevealedCommitsOptions)
(progDesc "Call the Anoma.Protobuf.IndexerService.ListUnrevealedCommits endpoint")
12 changes: 11 additions & 1 deletion app/Commands/Dev/Anoma/Options.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Commands.Dev.Anoma.Options where

import Commands.Dev.Anoma.AddTransaction.Options
import Commands.Dev.Anoma.Indexer.Options
import Commands.Dev.Anoma.Prove.Options
import Commands.Dev.Anoma.Start.Options
import CommonOptions
Expand All @@ -11,6 +12,7 @@ data AnomaCommand
| AnomaCommandStop
| AnomaCommandProve ProveOptions
| AnomaCommandAddTransaction AddTransactionOptions
| AnomaCommandIndexer AnomaIndexerCommand
deriving stock (Data)

data AnomaCommandGlobal = AnomaCommandGlobal
Expand All @@ -31,7 +33,8 @@ parseAnomaCommand =
commandStatus,
commandStop,
commandProve,
commandAddTransaction
commandAddTransaction,
commandIndexer
]
)
where
Expand Down Expand Up @@ -93,3 +96,10 @@ parseAnomaCommand =
info
(AnomaCommandAddTransaction <$> parseAddTransactionOptions)
(progDesc "Submit a Nockma transaction candidate to Anoma.Protobuf.Mempool.AddTransaction")

commandIndexer :: Mod CommandFields AnomaCommand
commandIndexer =
command "indexer" $
info
(AnomaCommandIndexer <$> parseAnomaIndexerCommand)
(progDesc "Subcommands related to the Anoma indexer")
39 changes: 39 additions & 0 deletions src/Anoma/Effect/Indexer/ListUnrevealedCommits.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module Anoma.Effect.Indexer.ListUnrevealedCommits where

import Anoma.Effect.Base
import Anoma.Rpc.Indexer.ListUnrevealedCommits
import Data.ByteString.Base64 qualified as Base64
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 ListUnrevealedCommitsResult = ListUnrevealedCommitsResult
{_listUnrevealedCommitsResultCommits :: [Nockma.Term Natural]}

listUnrevealedCommits ::
forall r.
(Members '[Anoma, Error SimpleError, Logger] r) =>
Sem r ListUnrevealedCommitsResult
listUnrevealedCommits = do
nodeInfo <- getNodeInfo
let msg = Request {_requestNodeInfo = nodeInfo}
logMessageValue "Request payload" msg
resVal :: Value <- anomaRpc listUnrevealedCommitsGrpcUrl (Aeson.toJSON msg) >>= fromJSONErr
logMessageValue "Response Payload" resVal
res :: Response <- fromJSONErr resVal
commitBs :: [ByteString] <- mapM decodeCommit (res ^. responseCommits)
commits :: [Atom Natural] <-
mapError @NockNaturalNaturalError
(SimpleError . mkAnsiText @Text . show)
(mapM byteStringToAtom commitBs)
return ListUnrevealedCommitsResult {_listUnrevealedCommitsResultCommits = TermAtom <$> commits}
where
decodeCommit :: Text -> Sem r ByteString
decodeCommit t = case (Base64.decode (encodeUtf8 t)) of
Left e -> throw (SimpleError (mkAnsiText ("Failed to decode commitment: " <> pack e)))
Right bs -> return bs

makeLenses ''ListUnrevealedCommitsResult
39 changes: 39 additions & 0 deletions src/Anoma/Rpc/Indexer/ListUnrevealedCommits.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module Anoma.Rpc.Indexer.ListUnrevealedCommits where

import Anoma.Rpc.Base
import Anoma.Rpc.Indexer.ListUnrevealedCommits.JsonOptions
import Juvix.Prelude
import Juvix.Prelude.Aeson as Aeson

listUnrevealedCommitsGrpcUrl :: GrpcMethodUrl
listUnrevealedCommitsGrpcUrl =
mkGrpcMethodUrl $
"Anoma" :| ["Protobuf", "IndexerService", "ListUnrevealedCommits"]

newtype Request = Request
{_requestNodeInfo :: NodeInfo}

$( deriveJSON
defaultOptions
{ fieldLabelModifier = \case
"_requestNodeInfo" -> "node_info"
_ -> impossibleError "All fields must be covered"
}
''Request
)

newtype Response = Response
{_responseCommits :: [Text]}

$(deriveToJSON responseOptions ''Response)

instance FromJSON Response where
parseJSON =
$(mkParseJSON responseOptions ''Response)
. addDefaultValues' defaultValues
where
defaultValues :: HashMap Key Value
defaultValues = hashMap [("commits", Aeson.Array mempty)]

makeLenses ''Request
makeLenses ''Response
14 changes: 14 additions & 0 deletions src/Anoma/Rpc/Indexer/ListUnrevealedCommits/JsonOptions.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- | Options needed to derive JSON instances need to be put in a separate file due to
-- Template Haskell stage restriction
module Anoma.Rpc.Indexer.ListUnrevealedCommits.JsonOptions where

import Juvix.Prelude
import Juvix.Prelude.Aeson as Aeson

responseOptions :: Aeson.Options
responseOptions =
defaultOptions
{ fieldLabelModifier = \case
"_responseCommits" -> "commits"
_ -> impossibleError "All fields must be covered"
}

0 comments on commit 8637618

Please sign in to comment.