Skip to content

Commit

Permalink
support unjammed arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
janmasrovira committed Jan 31, 2025
1 parent 345b8c6 commit e24c766
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 17 deletions.
31 changes: 20 additions & 11 deletions app/Commands/Dev/Anoma/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ::
Expand Down
47 changes: 43 additions & 4 deletions app/Commands/Dev/Anoma/Prove/Options/ProveArg.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
module Commands.Dev.Anoma.Prove.Options.ProveArg
( ProveArg (..),
ArgFileSpec (..),
EncodingLayout (..),
Encoding (..),
encodingLayout,
encodingJammed,
parseProveArg,
)
where
Expand All @@ -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
Expand All @@ -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' =
Expand Down Expand Up @@ -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'
Expand Down
14 changes: 12 additions & 2 deletions app/Commands/Dev/Anoma/Prove/Options/ProveArgTag.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,25 @@ data ProveArgTag
= ProveArgTagNat
| ProveArgTagBase64
| ProveArgTagBytes
| ProveArgTagBytesUnJammed
| ProveArgTagBase64UnJammed
deriving stock (Eq, Bounded, Enum, Data)

instance Show ProveArgTag where
show = \case
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])
Expand All @@ -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)
Expand Down

0 comments on commit e24c766

Please sign in to comment.