{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# OPTIONS_HADDOCK hide #-}
module Cometa.Internal.Building
( ImplicitCoin (..),
minimumAda,
baseFee,
minimumFee,
referenceScriptFee,
scriptDataHash,
implicitCoin,
subTransactionImplicitCoin,
runUnchecked,
)
where
import qualified Codec.CBOR.Decoding as D
import qualified Codec.CBOR.Encoding as E
import Cometa.Error (Error (..))
import qualified Cometa.Internal.FFI.Building as FFI
import qualified Cometa.Internal.FFI.CoinSelection as Selection
import qualified Cometa.Internal.ParameterSnapshot as Snapshot
import qualified Cometa.Internal.Runtime as Runtime
import qualified Cometa.Internal.TransactionCbor as Cbor
import qualified Cometa.Internal.TransactionFields as Fields
import qualified Cometa.Protocol.CostModels as Models
import qualified Cometa.Protocol.Parameters as Parameters
import qualified Cometa.SubTransaction as Sub
import qualified Cometa.SubTransaction.Body as SubBody
import qualified Cometa.Transaction as Tx
import qualified Cometa.Transaction.Body as Body
import Cometa.Transaction.Datum (Hash32)
import qualified Cometa.Transaction.Datum as Datum
import Cometa.Transaction.Input (Input)
import qualified Cometa.Transaction.Output as Output
import qualified Cometa.Transaction.Utxo as Utxo
import qualified Cometa.Transaction.Witnesses as Witnesses
import Control.Monad (unless)
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import Data.Maybe (fromMaybe)
import qualified Data.Set as Set
import qualified Data.Text
import Data.Word (Word64)
import Foreign.C.Types (CUInt)
import Foreign.Marshal.Alloc (alloca)
import Foreign.Ptr (castPtr)
import Foreign.Storable (peek)
data ImplicitCoin = ImplicitCoin
{
ImplicitCoin -> Word64
withdrawals :: !Word64,
ImplicitCoin -> Word64
deposits :: !Word64,
ImplicitCoin -> Word64
reclaimedDeposits :: !Word64
}
deriving (ImplicitCoin -> ImplicitCoin -> Bool
(ImplicitCoin -> ImplicitCoin -> Bool)
-> (ImplicitCoin -> ImplicitCoin -> Bool) -> Eq ImplicitCoin
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ImplicitCoin -> ImplicitCoin -> Bool
== :: ImplicitCoin -> ImplicitCoin -> Bool
$c/= :: ImplicitCoin -> ImplicitCoin -> Bool
/= :: ImplicitCoin -> ImplicitCoin -> Bool
Eq, Int -> ImplicitCoin -> ShowS
[ImplicitCoin] -> ShowS
ImplicitCoin -> String
(Int -> ImplicitCoin -> ShowS)
-> (ImplicitCoin -> String)
-> ([ImplicitCoin] -> ShowS)
-> Show ImplicitCoin
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ImplicitCoin -> ShowS
showsPrec :: Int -> ImplicitCoin -> ShowS
$cshow :: ImplicitCoin -> String
show :: ImplicitCoin -> String
$cshowList :: [ImplicitCoin] -> ShowS
showList :: [ImplicitCoin] -> ShowS
Show)
minimumAda :: Word64 -> Output.Output -> IO (Either Error Word64)
minimumAda :: Word64 -> Output -> IO (Either Error Word64)
minimumAda Word64
cost Output
output = ByteString
-> (Ptr Word8 -> CSize -> IO (Either Error Word64))
-> IO (Either Error Word64)
forall a. ByteString -> (Ptr Word8 -> CSize -> IO a) -> IO a
Runtime.withBytes (Output -> ByteString
Output.encodeCbor Output
output) ((Ptr Word8 -> CSize -> IO (Either Error Word64))
-> IO (Either Error Word64))
-> (Ptr Word8 -> CSize -> IO (Either Error Word64))
-> IO (Either Error Word64)
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
bytes CSize
size -> (Ptr Word64 -> IO (Either Error Word64))
-> IO (Either Error Word64)
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr Word64 -> IO (Either Error Word64))
-> IO (Either Error Word64))
-> (Ptr Word64 -> IO (Either Error Word64))
-> IO (Either Error Word64)
forall a b. (a -> b) -> a -> b
$ \Ptr Word64
result -> do
CUInt
code <- Ptr Word8 -> CSize -> Word64 -> Ptr Word64 -> IO CUInt
Selection.cMinimumAda Ptr Word8
bytes CSize
size Word64
cost Ptr Word64
result
Either Error ()
status <- Text -> CUInt -> IO (Either Error ())
Runtime.checkStatus Text
"building.minimumAda" CUInt
code
case Either Error ()
status of
Left Error
failure -> Either Error Word64 -> IO (Either Error Word64)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Error -> Either Error Word64
forall a b. a -> Either a b
Left Error
failure)
Right () -> Word64 -> Either Error Word64
forall a b. b -> Either a b
Right (Word64 -> Either Error Word64)
-> IO Word64 -> IO (Either Error Word64)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr Word64 -> IO Word64
forall a. Storable a => Ptr a -> IO a
peek Ptr Word64
result
baseFee :: Word64 -> Word64 -> Tx.Transaction -> IO (Either Error Word64)
baseFee :: Word64 -> Word64 -> Transaction -> IO (Either Error Word64)
baseFee Word64
constant Word64
coefficient Transaction
transaction = CUInt
-> (forall s. Decoder s Word64)
-> [Encoding]
-> IO (Either Error Word64)
forall a.
CUInt
-> (forall s. Decoder s a) -> [Encoding] -> IO (Either Error a)
compute CUInt
4 Decoder s Word64
forall s. Decoder s Word64
D.decodeWord64 [ByteString -> Encoding
Cbor.raw (Transaction -> ByteString
Tx.encodeCbor Transaction
transaction), Word64 -> Encoding
E.encodeWord64 Word64
constant, Word64 -> Encoding
E.encodeWord64 Word64
coefficient]
minimumFee :: Parameters.Parameters -> [Utxo.Utxo] -> Tx.Transaction -> IO (Either Error Word64)
minimumFee :: Parameters -> [Utxo] -> Transaction -> IO (Either Error Word64)
minimumFee Parameters
parameters [Utxo]
resolved Transaction
transaction = case Either Error ()
validate of
Left Error
failure -> Either Error Word64 -> IO (Either Error Word64)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Error -> Either Error Word64
forall a b. a -> Either a b
Left Error
failure)
Right () -> CUInt
-> (forall s. Decoder s Word64)
-> [Encoding]
-> IO (Either Error Word64)
forall a.
CUInt
-> (forall s. Decoder s a) -> [Encoding] -> IO (Either Error a)
compute CUInt
0 Decoder s Word64
forall s. Decoder s Word64
D.decodeWord64 [ByteString -> Encoding
Cbor.raw (Transaction -> ByteString
Tx.encodeCbor Transaction
transaction), ByteString -> Encoding
Cbor.raw (Parameters -> ByteString
Snapshot.encodeSnapshot Parameters
parameters), [Utxo] -> Encoding
encodeUtxos (Transaction -> [Utxo] -> [Utxo]
pricedInputs Transaction
transaction [Utxo]
resolved)]
where
validate :: Either Error ()
validate = do
Parameters -> Either Error ()
validateParameters Parameters
parameters
[Utxo] -> Either Error ()
validateReferences [Utxo]
resolved
Bool -> Either Error () -> Either Error ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([Input] -> Set Input
forall a. Ord a => [a] -> Set a
Set.fromList ((Utxo -> Input) -> [Utxo] -> [Input]
forall a b. (a -> b) -> [a] -> [b]
map Utxo -> Input
Utxo.input [Utxo]
resolved) Set Input -> Set Input -> Bool
forall a. Eq a => a -> a -> Bool
== [Set Input] -> Set Input
forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
Set.unions (Transaction -> [Set Input]
pricedInputSets Transaction
transaction)) (Error -> Either Error ()
forall a b. a -> Either a b
Left (Text -> Error
InvalidBuildingRequest Text
"Resolved inputs must exactly cover top-level and subtransaction spending and reference inputs"))
Maybe ExUnitPrices -> Text -> Either Error ()
forall a. Maybe a -> Text -> Either Error ()
require (Parameters -> Maybe ExUnitPrices
Parameters.parameterExecutionCosts Parameters
parameters) Text
"Execution prices are required"
Maybe NonNegativeRational -> Text -> Either Error ()
forall a. Maybe a -> Text -> Either Error ()
require (Parameters -> Maybe NonNegativeRational
Parameters.parameterRefScriptCostPerByte Parameters
parameters) Text
"Reference script byte price is required"
referenceScriptFee :: Parameters.Parameters -> [Utxo.Utxo] -> IO (Either Error Word64)
referenceScriptFee :: Parameters -> [Utxo] -> IO (Either Error Word64)
referenceScriptFee Parameters
parameters [Utxo]
resolved = case Either Error ()
validate of
Left Error
failure -> Either Error Word64 -> IO (Either Error Word64)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Error -> Either Error Word64
forall a b. a -> Either a b
Left Error
failure)
Right () -> CUInt
-> (forall s. Decoder s Word64)
-> [Encoding]
-> IO (Either Error Word64)
forall a.
CUInt
-> (forall s. Decoder s a) -> [Encoding] -> IO (Either Error a)
compute CUInt
1 Decoder s Word64
forall s. Decoder s Word64
D.decodeWord64 [ByteString -> Encoding
Cbor.raw (Parameters -> ByteString
Snapshot.encodeSnapshot Parameters
parameters), [Utxo] -> Encoding
encodeUtxos [Utxo]
resolved]
where
validate :: Either Error ()
validate = do
Parameters -> Either Error ()
validateParameters Parameters
parameters
[Utxo] -> Either Error ()
validateReferences [Utxo]
resolved
Maybe NonNegativeRational -> Text -> Either Error ()
forall a. Maybe a -> Text -> Either Error ()
require (Parameters -> Maybe NonNegativeRational
Parameters.parameterRefScriptCostPerByte Parameters
parameters) Text
"Reference script byte price is required"
scriptDataHash :: Models.CostModels -> Witnesses.EncodedWitnesses -> IO (Either Error (Maybe Hash32))
scriptDataHash :: CostModels -> EncodedWitnesses -> IO (Either Error (Maybe Hash32))
scriptDataHash CostModels
models EncodedWitnesses
witnesses = do
Either Error (Maybe ByteString)
result <- CUInt
-> (forall s. Decoder s (Maybe ByteString))
-> [Encoding]
-> IO (Either Error (Maybe ByteString))
forall a.
CUInt
-> (forall s. Decoder s a) -> [Encoding] -> IO (Either Error a)
compute CUInt
3 (Decoder s ByteString -> Decoder s (Maybe ByteString)
forall s a. Decoder s a -> Decoder s (Maybe a)
Cbor.optional Decoder s ByteString
forall s. Decoder s ByteString
D.decodeBytes) [ByteString -> Encoding
Cbor.raw (CostModels -> ByteString
Models.encodeCbor CostModels
models), ByteString -> Encoding
Cbor.raw (EncodedWitnesses -> ByteString
Witnesses.toCbor EncodedWitnesses
witnesses)]
Either Error (Maybe Hash32) -> IO (Either Error (Maybe Hash32))
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error (Maybe ByteString)
result Either Error (Maybe ByteString)
-> (Maybe ByteString -> Either Error (Maybe Hash32))
-> Either Error (Maybe Hash32)
forall a b.
Either Error a -> (a -> Either Error b) -> Either Error b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (ByteString -> Either Error Hash32)
-> Maybe ByteString -> Either Error (Maybe Hash32)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Maybe a -> f (Maybe b)
traverse ByteString -> Either Error Hash32
Datum.hashFromBytes)
implicitCoin :: Parameters.Parameters -> Tx.Transaction -> IO (Either Error ImplicitCoin)
implicitCoin :: Parameters -> Transaction -> IO (Either Error ImplicitCoin)
implicitCoin Parameters
parameters Transaction
transaction = case Parameters -> Either Error ()
validateParameters Parameters
parameters of
Left Error
failure -> Either Error ImplicitCoin -> IO (Either Error ImplicitCoin)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Error -> Either Error ImplicitCoin
forall a b. a -> Either a b
Left Error
failure)
Right () -> CUInt
-> (forall s. Decoder s ImplicitCoin)
-> [Encoding]
-> IO (Either Error ImplicitCoin)
forall a.
CUInt
-> (forall s. Decoder s a) -> [Encoding] -> IO (Either Error a)
compute CUInt
2 (Int -> Decoder s ImplicitCoin -> Decoder s ImplicitCoin
forall s a. Int -> Decoder s a -> Decoder s a
Cbor.array Int
3 (Word64 -> Word64 -> Word64 -> ImplicitCoin
ImplicitCoin (Word64 -> Word64 -> Word64 -> ImplicitCoin)
-> Decoder s Word64 -> Decoder s (Word64 -> Word64 -> ImplicitCoin)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Word64
forall s. Decoder s Word64
D.decodeWord64 Decoder s (Word64 -> Word64 -> ImplicitCoin)
-> Decoder s Word64 -> Decoder s (Word64 -> ImplicitCoin)
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s Word64
forall s. Decoder s Word64
D.decodeWord64 Decoder s (Word64 -> ImplicitCoin)
-> Decoder s Word64 -> Decoder s ImplicitCoin
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s Word64
forall s. Decoder s Word64
D.decodeWord64)) [ByteString -> Encoding
Cbor.raw (Transaction -> ByteString
Tx.encodeCbor Transaction
transaction), ByteString -> Encoding
Cbor.raw (Parameters -> ByteString
Snapshot.encodeSnapshot Parameters
parameters)]
subTransactionImplicitCoin :: Parameters.Parameters -> Sub.SubTransaction -> IO (Either Error ImplicitCoin)
subTransactionImplicitCoin :: Parameters -> SubTransaction -> IO (Either Error ImplicitCoin)
subTransactionImplicitCoin Parameters
parameters SubTransaction
transaction = case Parameters -> Either Error ()
validateParameters Parameters
parameters of
Left Error
failure -> Either Error ImplicitCoin -> IO (Either Error ImplicitCoin)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Error -> Either Error ImplicitCoin
forall a b. a -> Either a b
Left Error
failure)
Right () -> CUInt
-> (forall s. Decoder s ImplicitCoin)
-> [Encoding]
-> IO (Either Error ImplicitCoin)
forall a.
CUInt
-> (forall s. Decoder s a) -> [Encoding] -> IO (Either Error a)
compute CUInt
5 (Int -> Decoder s ImplicitCoin -> Decoder s ImplicitCoin
forall s a. Int -> Decoder s a -> Decoder s a
Cbor.array Int
3 (Word64 -> Word64 -> Word64 -> ImplicitCoin
ImplicitCoin (Word64 -> Word64 -> Word64 -> ImplicitCoin)
-> Decoder s Word64 -> Decoder s (Word64 -> Word64 -> ImplicitCoin)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Word64
forall s. Decoder s Word64
D.decodeWord64 Decoder s (Word64 -> Word64 -> ImplicitCoin)
-> Decoder s Word64 -> Decoder s (Word64 -> ImplicitCoin)
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s Word64
forall s. Decoder s Word64
D.decodeWord64 Decoder s (Word64 -> ImplicitCoin)
-> Decoder s Word64 -> Decoder s ImplicitCoin
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s Word64
forall s. Decoder s Word64
D.decodeWord64)) [ByteString -> Encoding
Cbor.raw (SubTransaction -> ByteString
Sub.encodeCbor SubTransaction
transaction), ByteString -> Encoding
Cbor.raw (Parameters -> ByteString
Snapshot.encodeSnapshot Parameters
parameters)]
validateParameters :: Parameters.Parameters -> Either Error ()
validateParameters :: Parameters -> Either Error ()
validateParameters = Parameters -> Either Error ()
Snapshot.validateNative
require :: Maybe a -> Data.Text.Text -> Either Error ()
require :: forall a. Maybe a -> Text -> Either Error ()
require Maybe a
value Text
message = case Maybe a
value of
Maybe a
Nothing -> Error -> Either Error ()
forall a b. a -> Either a b
Left (Text -> Error
InvalidBuildingRequest Text
message)
Just a
_ -> () -> Either Error ()
forall a b. b -> Either a b
Right ()
validateReferences :: [Utxo.Utxo] -> Either Error ()
validateReferences :: [Utxo] -> Either Error ()
validateReferences [Utxo]
resolved = Bool -> Either Error () -> Either Error ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Set Input -> Int
forall a. Set a -> Int
Set.size ([Input] -> Set Input
forall a. Ord a => [a] -> Set a
Set.fromList ((Utxo -> Input) -> [Utxo] -> [Input]
forall a b. (a -> b) -> [a] -> [b]
map Utxo -> Input
Utxo.input [Utxo]
resolved)) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== [Utxo] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Utxo]
resolved) (Error -> Either Error ()
forall a b. a -> Either a b
Left (Text -> Error
InvalidBuildingRequest Text
"Duplicate resolved reference input"))
pricedInputSets :: Tx.Transaction -> [Set.Set Input]
pricedInputSets :: Transaction -> [Set Input]
pricedInputSets Transaction
transaction = (Fields -> Set Input) -> [Fields] -> [Set Input]
forall a b. (a -> b) -> [a] -> [b]
map Fields -> Set Input
inputs (Body -> Fields
Body.bodyFields Body
body Fields -> [Fields] -> [Fields]
forall a. a -> [a] -> [a]
: [Fields]
subFields)
where
body :: Body
body = EncodedBody -> Body
Body.toBody (Transaction -> EncodedBody
Tx.body Transaction
transaction)
subFields :: [Fields]
subFields = [Fields]
-> ([SubTransaction] -> [Fields])
-> Maybe [SubTransaction]
-> [Fields]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] ((SubTransaction -> Fields) -> [SubTransaction] -> [Fields]
forall a b. (a -> b) -> [a] -> [b]
map (Body -> Fields
SubBody.fields (Body -> Fields)
-> (SubTransaction -> Body) -> SubTransaction -> Fields
forall b c a. (b -> c) -> (a -> b) -> a -> c
. EncodedBody -> Body
SubBody.toBody (EncodedBody -> Body)
-> (SubTransaction -> EncodedBody) -> SubTransaction -> Body
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SubTransaction -> EncodedBody
Sub.body)) (Body -> Maybe [SubTransaction]
Body.bodySubTransactions Body
body)
inputs :: Fields -> Set Input
inputs Fields
fields = [Input] -> Set Input
forall a. Ord a => [a] -> Set a
Set.fromList (Fields -> [Input]
Fields.fieldInputs Fields
fields [Input] -> [Input] -> [Input]
forall a. Semigroup a => a -> a -> a
<> [Input] -> Maybe [Input] -> [Input]
forall a. a -> Maybe a -> a
fromMaybe [] (Fields -> Maybe [Input]
Fields.fieldReferenceInputs Fields
fields))
pricedInputs :: Tx.Transaction -> [Utxo.Utxo] -> [Utxo.Utxo]
pricedInputs :: Transaction -> [Utxo] -> [Utxo]
pricedInputs Transaction
transaction [Utxo]
resolved = (Set Input -> [Utxo]) -> [Set Input] -> [Utxo]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\Set Input
refs -> (Utxo -> Bool) -> [Utxo] -> [Utxo]
forall a. (a -> Bool) -> [a] -> [a]
filter (\Utxo
utxo -> Input -> Set Input -> Bool
forall a. Ord a => a -> Set a -> Bool
Set.member (Utxo -> Input
Utxo.input Utxo
utxo) Set Input
refs) [Utxo]
resolved) (Transaction -> [Set Input]
pricedInputSets Transaction
transaction)
encodeUtxos :: [Utxo.Utxo] -> E.Encoding
encodeUtxos :: [Utxo] -> Encoding
encodeUtxos [Utxo]
values = Word -> Encoding
E.encodeListLen (Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral ([Utxo] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Utxo]
values)) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> (Utxo -> Encoding) -> [Utxo] -> Encoding
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (ByteString -> Encoding
Cbor.raw (ByteString -> Encoding)
-> (Utxo -> ByteString) -> Utxo -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Utxo -> ByteString
Utxo.encodeCbor) [Utxo]
values
compute :: CUInt -> (forall s. D.Decoder s a) -> [E.Encoding] -> IO (Either Error a)
compute :: forall a.
CUInt
-> (forall s. Decoder s a) -> [Encoding] -> IO (Either Error a)
compute CUInt
operation forall s. Decoder s a
decoder [Encoding]
fields = do
Either Error ByteString
result <- CUInt -> ByteString -> IO (Either Error ByteString)
runUnchecked CUInt
operation (Encoding -> ByteString
Cbor.encode (Word -> Encoding
E.encodeListLen (Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral ([Encoding] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Encoding]
fields)) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> [Encoding] -> Encoding
forall a. Monoid a => [a] -> a
mconcat [Encoding]
fields))
Either Error a -> IO (Either Error a)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error ByteString
result Either Error ByteString
-> (ByteString -> Either Error a) -> Either Error a
forall a b.
Either Error a -> (a -> Either Error b) -> Either Error b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Text -> Error)
-> (forall s. Decoder s a) -> ByteString -> Either Error a
forall a.
(Text -> Error)
-> (forall s. Decoder s a) -> ByteString -> Either Error a
Cbor.decodeAs Text -> Error
InvalidBuildingRequest Decoder s a
forall s. Decoder s a
decoder)
runUnchecked :: CUInt -> ByteString -> IO (Either Error ByteString)
runUnchecked :: CUInt -> ByteString -> IO (Either Error ByteString)
runUnchecked CUInt
operation ByteString
request = ByteString
-> (CStringLen -> IO (Either Error ByteString))
-> IO (Either Error ByteString)
forall a. ByteString -> (CStringLen -> IO a) -> IO a
B.useAsCStringLen ByteString
request ((CStringLen -> IO (Either Error ByteString))
-> IO (Either Error ByteString))
-> (CStringLen -> IO (Either Error ByteString))
-> IO (Either Error ByteString)
forall a b. (a -> b) -> a -> b
$ \(Ptr CChar
bytes, Int
size) -> Text
-> (Ptr (Ptr Buffer) -> IO CUInt) -> IO (Either Error ByteString)
Runtime.withOwnedBuffer Text
"building.compute" (CUInt -> Ptr Word8 -> CSize -> Ptr (Ptr Buffer) -> IO CUInt
FFI.cCompute CUInt
operation (Ptr CChar -> Ptr Word8
forall a b. Ptr a -> Ptr b
castPtr Ptr CChar
bytes) (Int -> CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
size))