From e24c766eef916e38bd9f99d562bd4d2843f53710 Mon Sep 17 00:00:00 2001 From: Jan Mas Rovira Date: Fri, 31 Jan 2025 16:18:53 +0100 Subject: [PATCH] support unjammed arguments --- app/Commands/Dev/Anoma/Base.hs | 31 +++++++----- .../Dev/Anoma/Prove/Options/ProveArg.hs | 47 +++++++++++++++++-- .../Dev/Anoma/Prove/Options/ProveArgTag.hs | 14 +++++- 3 files changed, 75 insertions(+), 17 deletions(-) diff --git a/app/Commands/Dev/Anoma/Base.hs b/app/Commands/Dev/Anoma/Base.hs index 8a9c907cc7..6c42c04517 100644 --- a/app/Commands/Dev/Anoma/Base.hs +++ b/app/Commands/Dev/Anoma/Base.hs @@ -45,18 +45,27 @@ runNock programAppPath pargs = do prepareArg :: forall r. (Members '[Error SimpleError, Files, App] r) => ProveArg -> Sem r Anoma.RunNockmaArg prepareArg = \case ProveArgNat n -> return (Anoma.RunNockmaArgTerm (toNock n)) - ProveArgBytes n -> do - bs <- readAppFile n - Anoma.RunNockmaArgJammed <$> fromBytes bs - ProveArgBase64 n -> do - bs <- readAppFile n - Anoma.RunNockmaArgJammed <$> fromBytes (Base64.decodeLenient bs) - where - fromBytes :: ByteString -> Sem r (Atom Natural) - fromBytes b = asSimpleErrorShow @NockNaturalNaturalError (byteStringToAtom @Natural b) + ProveArgFile ArgFileSpec {..} -> + readAppFile _argFileSpecFile >>= fmap toArg . fromBytes + where + toArg :: Atom Natural -> Anoma.RunNockmaArg + toArg + | _argFileSpecEncoding ^. encodingJammed = Anoma.RunNockmaArgJammed + | otherwise = Anoma.RunNockmaArgTerm . toNock - readAppFile :: AppPath File -> Sem r ByteString - readAppFile f = fromAppPathFile f >>= readFileBS' + fromBytes :: ByteString -> Sem r (Atom Natural) + fromBytes b = + asSimpleErrorShow @NockNaturalNaturalError + . byteStringToAtom @Natural + . decode + $ b + where + decode = case _argFileSpecEncoding ^. encodingLayout of + EncodingBytes -> id + EncodingBase64 -> Base64.decodeLenient + + readAppFile :: AppPath File -> Sem r ByteString + readAppFile f = fromAppPathFile f >>= readFileBS' -- | Calls Anoma.Protobuf.Mempool.AddTransaction addTransaction :: diff --git a/app/Commands/Dev/Anoma/Prove/Options/ProveArg.hs b/app/Commands/Dev/Anoma/Prove/Options/ProveArg.hs index e2273396e7..4fa392db2c 100644 --- a/app/Commands/Dev/Anoma/Prove/Options/ProveArg.hs +++ b/app/Commands/Dev/Anoma/Prove/Options/ProveArg.hs @@ -1,5 +1,10 @@ module Commands.Dev.Anoma.Prove.Options.ProveArg ( ProveArg (..), + ArgFileSpec (..), + EncodingLayout (..), + Encoding (..), + encodingLayout, + encodingJammed, parseProveArg, ) where @@ -16,10 +21,28 @@ newtype ProveArg' = ProveArg' { _proveArg :: Sigma ProveArgTag ProveArgTypeSym0 } +data Encoding = Encoding + { _encodingLayout :: EncodingLayout, + _encodingJammed :: Bool + } + deriving stock (Data) + +data EncodingLayout + = EncodingBytes + | EncodingBase64 + deriving stock (Data) + +makeLenses ''Encoding + +data ArgFileSpec = ArgFileSpec + { _argFileSpecEncoding :: Encoding, + _argFileSpecFile :: AppPath File + } + deriving stock (Data) + data ProveArg = ProveArgNat Natural - | ProveArgBase64 (AppPath File) - | ProveArgBytes (AppPath File) + | ProveArgFile ArgFileSpec deriving stock (Data) parseProveArg :: Parser ProveArg @@ -28,8 +51,22 @@ parseProveArg = fromProveArg' <$> parseProveArg' fromProveArg' :: ProveArg' -> ProveArg fromProveArg' (ProveArg' (ty :&: a)) = case ty of SProveArgTagNat -> ProveArgNat a - SProveArgTagBase64 -> ProveArgBase64 a - SProveArgTagBytes -> ProveArgBytes a + SProveArgTagBase64 -> fileHelper a EncodingBase64 True + SProveArgTagBytes -> fileHelper a EncodingBytes True + SProveArgTagBase64UnJammed -> fileHelper a EncodingBase64 False + SProveArgTagBytesUnJammed -> fileHelper a EncodingBytes False + where + fileHelper :: AppPath File -> EncodingLayout -> Bool -> ProveArg + fileHelper f l jammed = + ProveArgFile + ArgFileSpec + { _argFileSpecEncoding = + Encoding + { _encodingLayout = l, + _encodingJammed = jammed + }, + _argFileSpecFile = f + } parseProveArg' :: Parser ProveArg' parseProveArg' = @@ -70,6 +107,8 @@ parseProveArg' = ret <- case p of SProveArgTagBytes -> pAppPath SProveArgTagBase64 -> pAppPath + SProveArgTagBase64UnJammed -> pAppPath + SProveArgTagBytesUnJammed -> pAppPath SProveArgTagNat -> do off <- getOffset i <- (^. withLocParam . integerWithBaseValue) <$> integerWithBase' diff --git a/app/Commands/Dev/Anoma/Prove/Options/ProveArgTag.hs b/app/Commands/Dev/Anoma/Prove/Options/ProveArgTag.hs index 299dfee521..781ffdf044 100644 --- a/app/Commands/Dev/Anoma/Prove/Options/ProveArgTag.hs +++ b/app/Commands/Dev/Anoma/Prove/Options/ProveArgTag.hs @@ -9,6 +9,8 @@ data ProveArgTag = ProveArgTagNat | ProveArgTagBase64 | ProveArgTagBytes + | ProveArgTagBytesUnJammed + | ProveArgTagBase64UnJammed deriving stock (Eq, Bounded, Enum, Data) instance Show ProveArgTag where @@ -16,12 +18,16 @@ instance Show ProveArgTag where ProveArgTagNat -> "nat" ProveArgTagBase64 -> "base64" ProveArgTagBytes -> "bytes" + ProveArgTagBytesUnJammed -> "bytes-unjammed" + ProveArgTagBase64UnJammed -> "base64-unjammed" type ProveArgType :: ProveArgTag -> GHCType type family ProveArgType s = res where ProveArgType 'ProveArgTagNat = Natural ProveArgType 'ProveArgTagBase64 = AppPath File ProveArgType 'ProveArgTagBytes = AppPath File + ProveArgType 'ProveArgTagBytesUnJammed = AppPath File + ProveArgType 'ProveArgTagBase64UnJammed = AppPath File $(genDefunSymbols [''ProveArgType]) $(genSingletons [''ProveArgTag]) @@ -32,10 +38,14 @@ proveArgTagHelp = itemize (tagHelp <$> allElements) tagHelp :: ProveArgTag -> AnsiDoc tagHelp t = let mvar, explain :: AnsiDoc + jammedNoun :: AnsiDoc = annotate bold "jammed noun" + unjammedAtom :: AnsiDoc = annotate bold "unjammed atom" (mvar, explain) = first sty $ case t of ProveArgTagNat -> ("NATURAL", "is passed verbatim as a nockma atom") - ProveArgTagBase64 -> ("FILE", "is a file containing a base64 encoded nockma atom that represents a jammed noun") - ProveArgTagBytes -> ("FILE", "is a file containing bytes of a nockma atom that represents a jammed noun") + ProveArgTagBase64 -> ("FILE", "is a file containing a base64 encoded nockma atom that represents a" <+> jammedNoun) + ProveArgTagBytes -> ("FILE", "is a file containing bytes of a nockma atom that represents a" <+> jammedNoun) + ProveArgTagBase64UnJammed -> ("FILE", "is a file containing a base64 encoded nockma atom that represents an" <+> unjammedAtom) + ProveArgTagBytesUnJammed -> ("FILE", "is a file containing bytes of a nockma atom that represents an" <+> unjammedAtom) sty = annotate (bold <> colorDull Blue) tagvar :: AnsiDoc tagvar = sty (show t <> ":" <> mvar)