Skip to content

Commit

Permalink
Add Flat instance for CompiledCodeIn
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelpj committed Oct 14, 2021
1 parent 681b986 commit fb06ec3
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion plutus-tx/src/PlutusTx/Code.hs
Expand Up @@ -19,7 +19,7 @@ import qualified PlutusCore.Pretty as PLC
import qualified UntypedPlutusCore as UPLC

import Control.Exception
import Flat (Flat, unflat)
import Flat (Flat (..), unflat)
import Flat.Decoder (DecodeException)

import qualified Data.ByteString as BS
Expand Down Expand Up @@ -56,6 +56,16 @@ applyCode fun arg = DeserializedCode (UPLC.applyProgram (getPlc fun) (getPlc arg
sizePlc :: (PLC.Closed uni, uni `PLC.Everywhere` Flat, Flat fun, uni `PLC.Everywhere` PLC.PrettyConst, PLC.GShow uni, PLC.Pretty fun) => CompiledCodeIn uni fun a -> Integer
sizePlc = UPLC.programSize . getPlc

instance (PLC.Closed uni, uni `PLC.Everywhere` Flat, Flat fun, uni `PLC.Everywhere` PLC.PrettyConst, PLC.GShow uni, PLC.Pretty fun)
=> Flat (CompiledCodeIn uni fun a) where
encode c = encode (getPlc c)

decode = do
p <- decode
pure $ DeserializedCode p Nothing

size c = size (getPlc c)

{- Note [Deserializing the AST]
The types suggest that we can fail to deserialize the AST that we embedded in the program.
However, we just did it ourselves, so this should be impossible, and we signal this with an
Expand Down

0 comments on commit fb06ec3

Please sign in to comment.