{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TupleSections #-}
{-# OPTIONS_HADDOCK hide #-}
module Cometa.Internal.TransactionBuilder (Config (..), defaultConfig, build, encodeConfig, withResult, withResultNamed, checkedEvaluator, validateBudget, pricedReferenceFee) where
import qualified Codec.CBOR.Decoding as D
import qualified Codec.CBOR.Encoding as E
import Cometa.Address (Address)
import qualified Cometa.Address as Address
import Cometa.AssetId (AssetId (Ada))
import Cometa.CoinSelection (Selector)
import Cometa.Error (Error (..))
import qualified Cometa.Error.Code as Code
import Cometa.Evaluation (Evaluator, Target (TopLevel))
import qualified Cometa.Evaluation as Eval
import qualified Cometa.Internal.BatchValidation as Batch
import qualified Cometa.Internal.Building as Building
import qualified Cometa.Internal.CoinSelection.Native as Selection
import qualified Cometa.Internal.Evaluation.Native as NativeEvaluation
import qualified Cometa.Internal.EvaluationNative as Evaluation
import qualified Cometa.Internal.FFI.Native as Native
import qualified Cometa.Internal.FFI.Runtime as Raw
import qualified Cometa.Internal.FFI.TransactionBuilder as FFI
import qualified Cometa.Internal.ParameterSnapshot as Parameters
import qualified Cometa.Internal.Runtime as Runtime
import qualified Cometa.Internal.TransactionCbor as C
import qualified Cometa.Internal.TransactionPlan as Plan
import qualified Cometa.Internal.TransactionValue as ValueCbor
import qualified Cometa.PlutusData as Data
import qualified Cometa.Protocol.CostModels as Models
import qualified Cometa.Protocol.ExUnitPrices as Prices
import qualified Cometa.Protocol.ExUnits as Ex
import Cometa.Protocol.Parameters (Parameters)
import qualified Cometa.Protocol.Parameters as P
import qualified Cometa.Protocol.Rational as Rational
import qualified Cometa.Protocol.Version as Version
import Cometa.SlotConfig (SlotConfig)
import qualified Cometa.SlotConfig as Slot
import qualified Cometa.SubTransaction as Sub
import qualified Cometa.SubTransaction.Body as SubBody
import Cometa.Transaction (Transaction)
import qualified Cometa.Transaction as Transaction
import qualified Cometa.Transaction.Body as Body
import qualified Cometa.Transaction.Fields as Fields
import qualified Cometa.Transaction.Output as Output
import Cometa.Transaction.Plan (Plan)
import qualified Cometa.Transaction.Redeemer as Redeemer
import Cometa.Transaction.Utxo (Utxo)
import qualified Cometa.Transaction.Utxo as Utxo
import qualified Cometa.Transaction.Witnesses as Witnesses
import qualified Cometa.Value as Value
import Control.Exception (bracket)
import Control.Monad (unless)
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import Data.Int (Int64)
import Data.List (sort, sortOn)
import qualified Data.Map.Strict as Map
import Data.Maybe (fromMaybe, isJust)
import qualified Data.Sequence as Seq
import qualified Data.Set as Set
import Data.Text (Text)
import qualified Data.Text.Encoding as Text
import Data.Word (Word64)
import Foreign.Marshal.Alloc (alloca)
import Foreign.Ptr (Ptr, nullPtr)
import Foreign.Storable (peek, poke)
data Config = Config
{
Config -> Parameters
parameters :: Parameters,
Config -> SlotConfig
slotConfig :: SlotConfig,
Config -> Address
changeAddress :: Address,
Config -> [Utxo]
availableUtxos :: [Utxo],
Config -> [Utxo]
collateralUtxos :: [Utxo],
Config -> Maybe Address
collateralChangeAddress :: Maybe Address,
Config -> Maybe Selector
selector :: Maybe Selector,
Config -> Maybe Evaluator
evaluator :: Maybe Evaluator,
Config -> Word64
minimumFee :: Word64,
:: Word64
}
defaultConfig :: Parameters -> SlotConfig -> Address -> Config
defaultConfig :: Parameters -> SlotConfig -> Address -> Config
defaultConfig Parameters
params SlotConfig
slots Address
change = Parameters
-> SlotConfig
-> Address
-> [Utxo]
-> [Utxo]
-> Maybe Address
-> Maybe Selector
-> Maybe Evaluator
-> Word64
-> Word64
-> Config
Config Parameters
params SlotConfig
slots Address
change [] [] Maybe Address
forall a. Maybe a
Nothing Maybe Selector
forall a. Maybe a
Nothing Maybe Evaluator
forall a. Maybe a
Nothing Word64
0 Word64
0
build :: Config -> Plan -> IO (Either Error Transaction)
build :: Config -> Plan -> IO (Either Error Transaction)
build Config
config Plan
plan = case Config -> Plan -> Either Error ()
validate Config
config Plan
plan of
Left Error
failure -> Either Error Transaction -> IO (Either Error Transaction)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Error -> Either Error Transaction
forall a b. a -> Either a b
Left Error
failure)
Right () -> do
Either Error ()
fees <- Config -> Plan -> IO (Either Error ())
validateFees Config
config Plan
plan
case Either Error ()
fees of
Left Error
failure -> Either Error Transaction -> IO (Either Error Transaction)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Error -> Either Error Transaction
forall a b. a -> Either a b
Left Error
failure)
Right () -> Config -> Plan -> IO (Either Error Transaction)
buildNative Config
config Plan
plan
buildNative :: Config -> Plan -> IO (Either Error Transaction)
buildNative :: Config -> Plan -> IO (Either Error Transaction)
buildNative Config
config Plan
plan =
Dispatch
-> (Runtime -> IO (Either Error Transaction))
-> IO (Either Error Transaction)
forall a.
Dispatch -> (Runtime -> IO (Either Error a)) -> IO (Either Error a)
Runtime.withRuntime Dispatch
selectDispatch ((Runtime -> IO (Either Error Transaction))
-> IO (Either Error Transaction))
-> (Runtime -> IO (Either Error Transaction))
-> IO (Either Error Transaction)
forall a b. (a -> b) -> a -> b
$ \Runtime
selection ->
Dispatch
-> (Runtime -> IO (Either Error Transaction))
-> IO (Either Error Transaction)
forall a.
Dispatch -> (Runtime -> IO (Either Error a)) -> IO (Either Error a)
Runtime.withRuntime Dispatch
evaluateDispatch ((Runtime -> IO (Either Error Transaction))
-> IO (Either Error Transaction))
-> (Runtime -> IO (Either Error Transaction))
-> IO (Either Error Transaction)
forall a b. (a -> b) -> a -> b
$ \Runtime
evaluation ->
Dispatch
-> (Runtime -> IO (Either Error Transaction))
-> IO (Either Error Transaction)
forall a.
Dispatch -> (Runtime -> IO (Either Error a)) -> IO (Either Error a)
Runtime.withRuntime (Plan -> Dispatch
deferredDispatch Plan
plan) ((Runtime -> IO (Either Error Transaction))
-> IO (Either Error Transaction))
-> (Runtime -> IO (Either Error Transaction))
-> IO (Either Error Transaction)
forall a b. (a -> b) -> a -> b
$ \Runtime
deferred -> do
Either Error (ByteString, Word64)
result <- [Runtime]
-> ([Ptr Context] -> IO (Either Error (ByteString, Word64)))
-> IO (Either Error (ByteString, Word64))
forall a.
[Runtime]
-> ([Ptr Context] -> IO (Either Error a)) -> IO (Either Error a)
Runtime.withOperations [Runtime
selection, Runtime
evaluation, Runtime
deferred] (([Ptr Context] -> IO (Either Error (ByteString, Word64)))
-> IO (Either Error (ByteString, Word64)))
-> ([Ptr Context] -> IO (Either Error (ByteString, Word64)))
-> IO (Either Error (ByteString, Word64))
forall a b. (a -> b) -> a -> b
$ \case
[Ptr Context
selectContext, Ptr Context
evaluateContext, Ptr Context
deferredContext] ->
ByteString
-> (Ptr Word8 -> CSize -> IO (Either Error (ByteString, Word64)))
-> IO (Either Error (ByteString, Word64))
forall a. ByteString -> (Ptr Word8 -> CSize -> IO a) -> IO a
Runtime.withBytes (Plan -> Config -> ByteString
encodeConfig Plan
plan Config
config) ((Ptr Word8 -> CSize -> IO (Either Error (ByteString, Word64)))
-> IO (Either Error (ByteString, Word64)))
-> (Ptr Word8 -> CSize -> IO (Either Error (ByteString, Word64)))
-> IO (Either Error (ByteString, Word64))
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
configBytes CSize
configSize ->
ByteString
-> (Ptr Word8 -> CSize -> IO (Either Error (ByteString, Word64)))
-> IO (Either Error (ByteString, Word64))
forall a. ByteString -> (Ptr Word8 -> CSize -> IO a) -> IO a
Runtime.withBytes (Word64 -> Plan -> ByteString
Plan.encodePlan (Word64 -> Maybe Word64 -> Word64
forall a. a -> Maybe a -> a
fromMaybe Word64
0 (Parameters -> Maybe Word64
P.parameterGovernanceActionDeposit (Config -> Parameters
parameters Config
config))) Plan
plan) ((Ptr Word8 -> CSize -> IO (Either Error (ByteString, Word64)))
-> IO (Either Error (ByteString, Word64)))
-> (Ptr Word8 -> CSize -> IO (Either Error (ByteString, Word64)))
-> IO (Either Error (ByteString, Word64))
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
planBytes CSize
planSize ->
(Ptr (Ptr Buffer) -> Ptr (Ptr Buffer) -> Ptr Word64 -> IO CUInt)
-> IO (Either Error (ByteString, Word64))
withResult
( Ptr Context
-> Ptr Context
-> Ptr Context
-> Ptr Word8
-> CSize
-> Ptr Word8
-> CSize
-> Ptr (Ptr Buffer)
-> Ptr (Ptr Buffer)
-> Ptr Word64
-> IO CUInt
FFI.cBuild
(if Maybe Selector -> Bool
forall a. Maybe a -> Bool
isJust (Config -> Maybe Selector
selector Config
config) then Ptr Context
selectContext else Ptr Context
forall a. Ptr a
nullPtr)
(if Maybe Evaluator -> Bool
forall a. Maybe a -> Bool
isJust (Config -> Maybe Evaluator
evaluator Config
config) then Ptr Context
evaluateContext else Ptr Context
forall a. Ptr a
nullPtr)
Ptr Context
deferredContext
Ptr Word8
configBytes
CSize
configSize
Ptr Word8
planBytes
CSize
planSize
)
[Ptr Context]
_ -> Either Error (ByteString, Word64)
-> IO (Either Error (ByteString, Word64))
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Error -> Either Error (ByteString, Word64)
forall a b. a -> Either a b
Left (Text -> Error
NativeContractViolation Text
"builder.contexts"))
case Either Error (ByteString, Word64)
result Either Error (ByteString, Word64)
-> ((ByteString, Word64) -> Either Error Transaction)
-> Either Error Transaction
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, Word64) -> Either Error Transaction
decodeBuilt of
Left Error
failure -> Either Error Transaction -> IO (Either Error Transaction)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Error -> Either Error Transaction
forall a b. a -> Either a b
Left Error
failure)
Right Transaction
transaction -> do
Either Error ()
checked <- Evaluator -> [Utxo] -> Transaction -> IO (Either Error ())
Batch.validateSubBudgets (Evaluator -> Maybe Evaluator -> Evaluator
forall a. a -> Maybe a -> a
fromMaybe Evaluator
nativeEvaluator (Config -> Maybe Evaluator
evaluator Config
config)) [Utxo]
resolved Transaction
transaction
Either Error Transaction -> IO (Either Error Transaction)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Transaction
transaction Transaction -> Either Error () -> Either Error Transaction
forall a b. a -> Either Error b -> Either Error a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Either Error ()
checked)
where
decodeBuilt :: (ByteString, Word64) -> Either Error Transaction
decodeBuilt (ByteString
bytes, Word64
keyCount) = do
Transaction
transaction <- ByteString -> Either Error Transaction
Transaction.decodeCbor ByteString
bytes
Config -> Plan -> Word64 -> Transaction -> Either Error ()
validateBuilt Config
config Plan
plan Word64
keyCount Transaction
transaction
Transaction -> Either Error Transaction
forall a. a -> Either Error a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Transaction
transaction
resolved :: [Utxo]
resolved = Map Input Utxo -> [Utxo]
forall k a. Map k a -> [a]
Map.elems ([(Input, Utxo)] -> Map Input Utxo
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(Utxo -> Input
Utxo.input Utxo
input, Utxo
input) | Utxo
input <- Config -> [Utxo]
availableUtxos Config
config [Utxo] -> [Utxo] -> [Utxo]
forall a. Semigroup a => a -> a -> a
<> Config -> [Utxo]
collateralUtxos Config
config [Utxo] -> [Utxo] -> [Utxo]
forall a. Semigroup a => a -> a -> a
<> Plan -> [Utxo]
Plan.resolvedInputs Plan
plan])
nativeEvaluator :: Evaluator
nativeEvaluator =
let Version.ProtocolVersion Word64
major Word32
_ = Parameters -> ProtocolVersion
P.parameterProtocolVersion (Config -> Parameters
parameters Config
config)
in Config -> Evaluator
NativeEvaluation.evaluator (SlotConfig -> Word64 -> CostModels -> Config
NativeEvaluation.Config (Config -> SlotConfig
slotConfig Config
config) Word64
major (CostModels -> Maybe CostModels -> CostModels
forall a. a -> Maybe a -> a
fromMaybe CostModels
Models.empty (Parameters -> Maybe CostModels
P.parameterCostModels (Config -> Parameters
parameters Config
config))))
selectDispatch :: Dispatch
selectDispatch = Dispatch -> (Selector -> Dispatch) -> Maybe Selector -> Dispatch
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Dispatch
forall {f :: * -> *} {p} {p} {b}.
Applicative f =>
p -> p -> f (Either Error b)
unavailable Selector -> Dispatch
Selection.dispatch (Config -> Maybe Selector
selector Config
config)
evaluateDispatch :: Dispatch
evaluateDispatch = Dispatch -> (Evaluator -> Dispatch) -> Maybe Evaluator -> Dispatch
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Dispatch
forall {f :: * -> *} {p} {p} {b}.
Applicative f =>
p -> p -> f (Either Error b)
unavailable (\Evaluator
backend -> Evaluator -> Target -> Dispatch
Evaluation.dispatch (Config -> Evaluator -> Evaluator
checkedEvaluator Config
config Evaluator
backend) Target
TopLevel) (Config -> Maybe Evaluator
evaluator Config
config)
unavailable :: p -> p -> f (Either Error b)
unavailable p
_ p
_ = Either Error b -> f (Either Error b)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Error -> Either Error b
forall a b. a -> Either a b
Left (Text -> Error
NativeContractViolation Text
"builder.unconfiguredCallback"))
validate :: Config -> Plan -> Either Error ()
validate :: Config -> Plan -> Either Error ()
validate Config
config Plan
plan = do
Plan -> Either Error ()
Plan.validatePlan Plan
plan
Plan -> Either Error ()
Plan.validateTopLevel Plan
plan
(Output -> Either Error ()) -> [Output] -> Either Error ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_
(Maybe Word64 -> Output -> Either Error ()
checkSubOutputSize (Parameters -> Maybe Word64
P.parameterMaxValueSize (Config -> Parameters
parameters Config
config)))
[Output
output | (SubTransaction
transaction, [Utxo]
_) <- Plan -> [(SubTransaction, [Utxo])]
Plan.subTransactions Plan
plan, Output
output <- Fields -> [Output]
Fields.fieldOutputs (Body -> Fields
SubBody.fields (EncodedBody -> Body
SubBody.toBody (SubTransaction -> EncodedBody
Sub.body SubTransaction
transaction)))]
(Word64 -> Either Error Word64) -> [Word64] -> Either Error ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (SlotConfig -> Word64 -> Either Error Word64
Slot.slotToTime (Config -> SlotConfig
slotConfig Config
config)) (Plan -> [Word64]
Plan.validitySlots Plan
plan)
Parameters -> Either Error ()
Parameters.validateNative (Config -> Parameters
parameters Config
config)
[Utxo] -> Either Error ()
forall {t :: * -> *}. Foldable t => t Utxo -> Either Error ()
consistent (Config -> [Utxo]
availableUtxos Config
config [Utxo] -> [Utxo] -> [Utxo]
forall a. Semigroup a => a -> a -> a
<> Config -> [Utxo]
collateralUtxos Config
config [Utxo] -> [Utxo] -> [Utxo]
forall a. Semigroup a => a -> a -> a
<> Plan -> [Utxo]
Plan.resolvedInputs Plan
plan)
[Utxo] -> Either Error ()
unique (Config -> [Utxo]
availableUtxos Config
config)
[Utxo] -> Either Error ()
unique (Config -> [Utxo]
collateralUtxos Config
config)
Text -> Integer -> Either Error ()
bounded Text
"Fee exceeds signed native balance bounds" (Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Config -> Word64
minimumFee Config
config))
let funds :: [Utxo]
funds = Map Input Utxo -> [Utxo]
forall k a. Map k a -> [a]
Map.elems ([(Input, Utxo)] -> Map Input Utxo
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(Utxo -> Input
Utxo.input Utxo
u, Utxo
u) | Utxo
u <- Plan -> [Utxo] -> [Utxo]
eligible Plan
plan (Config -> [Utxo]
availableUtxos Config
config) [Utxo] -> [Utxo] -> [Utxo]
forall a. Semigroup a => a -> a -> a
<> Plan -> [Utxo]
Plan.spendingInputs Plan
plan])
inputValue :: Value
inputValue = (Utxo -> Value) -> [Utxo] -> Value
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (Output -> Value
Output.value (Output -> Value) -> (Utxo -> Output) -> Utxo -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Utxo -> Output
Utxo.output) [Utxo]
funds
outputValue :: Value
outputValue = (Output -> Value) -> [Output] -> Value
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap Output -> Value
Output.value (Plan -> [Output]
Plan.planOutputs Plan
plan)
collateralValue :: Value
collateralValue = (Utxo -> Value) -> [Utxo] -> Value
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (Output -> Value
Output.value (Output -> Value) -> (Utxo -> Output) -> Utxo -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Utxo -> Output
Utxo.output) (Plan -> [Utxo] -> [Utxo]
eligible Plan
plan (Config -> [Utxo]
collateralUtxos Config
config))
witnessBytes :: [Utxo] -> Integer
witnessBytes [Utxo]
values =
[Integer] -> Integer
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum
[ case Output -> Address
Output.address (Utxo -> Output
Utxo.output Utxo
u) of
Address.ByronAddress Address
_ -> Integer
180 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Int -> Integer
forall a. Integral a => a -> Integer
toInteger (ByteString -> Int
B.length (Address -> ByteString
Address.toBytes (Output -> Address
Output.address (Utxo -> Output
Utxo.output Utxo
u))))
Address
_ -> Integer
101
| Utxo
u <- [Utxo]
values
]
potentialWitnessBytes :: Integer
potentialWitnessBytes = [Utxo] -> Integer
witnessBytes ([Utxo]
funds [Utxo] -> [Utxo] -> [Utxo]
forall a. Semigroup a => a -> a -> a
<> Config -> [Utxo]
collateralUtxos Config
config) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
101 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* (Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Config -> Word64
extraSigners Config
config) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Plan -> Integer
Plan.witnessCount Plan
plan) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
32
maximumFee :: Integer
maximumFee =
Integer -> Integer -> Integer
forall a. Ord a => a -> a -> a
max
(Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Config -> Word64
minimumFee Config
config))
(Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Parameters -> Word64
P.parameterMinFeeA (Config -> Parameters
parameters Config
config)) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Parameters -> Word64
P.parameterMaxTxSize (Config -> Parameters
parameters Config
config)) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Parameters -> Word64
P.parameterMinFeeB (Config -> Parameters
parameters Config
config)))
Text -> Integer -> Either Error ()
bounded
Text
"Potential native witness fee exceeds signed balance bounds"
(Integer
maximumFee Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
potentialWitnessBytes Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Parameters -> Word64
P.parameterMinFeeA (Config -> Parameters
parameters Config
config)))
Text -> Integer -> Either Error ()
bounded Text
"Maximum linear transaction fee exceeds signed native balance bounds" Integer
maximumFee
case Parameters -> Maybe Word64
P.parameterAdaPerUtxoByte (Config -> Parameters
parameters Config
config) of
Maybe Word64
Nothing -> () -> Either Error ()
forall a. a -> Either Error a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
Just Word64
price ->
Text -> Integer -> Either Error ()
bounded
Text
"Minimum Ada multiplication exceeds signed bounds"
(Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger Word64
price Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* (Integer
160 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Parameters -> Word64
P.parameterMaxTxSize (Config -> Parameters
parameters Config
config))))
Value -> Either Error ()
boundedValue (Value
inputValue Value -> Value -> Value
forall a. Semigroup a => a -> a -> a
<> Parameters -> Plan -> Value
Plan.sourceExtras (Config -> Parameters
parameters Config
config) Plan
plan)
Value -> Either Error ()
boundedValue (Value
outputValue Value -> Value -> Value
forall a. Semigroup a => a -> a -> a
<> Parameters -> Plan -> Value
Plan.sinkExtras (Config -> Parameters
parameters Config
config) Plan
plan Value -> Value -> Value
forall a. Semigroup a => a -> a -> a
<> Integer -> Value
Value.lovelace Integer
maximumFee)
Value -> Either Error ()
boundedValue Value
collateralValue
Text -> Integer -> Either Error ()
bounded Text
"Stake key deposit exceeds signed native bounds" (Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Parameters -> Word64
P.parameterKeyDeposit (Config -> Parameters
parameters Config
config)))
Text -> Integer -> Either Error ()
bounded Text
"Governance proposal deposit exceeds signed native bounds" (Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Word64 -> Maybe Word64 -> Word64
forall a. a -> Maybe a -> a
fromMaybe Word64
0 (Parameters -> Maybe Word64
P.parameterGovernanceActionDeposit (Config -> Parameters
parameters Config
config))))
Text -> Integer -> Either Error ()
bounded Text
"Pool deposit exceeds signed native bounds" (Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Parameters -> Word64
P.parameterPoolDeposit (Config -> Parameters
parameters Config
config)))
Bool -> Either Error () -> Either Error ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless
(Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Config -> Word64
extraSigners Config
config) Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Parameters -> Word64
P.parameterMaxTxSize (Config -> Parameters
parameters Config
config)) Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`div` Integer
101)
(Error -> Either Error ()
forall a b. a -> Either a b
Left (Text -> Error
InvalidBuildingRequest Text
"Additional signer witnesses exceed maximum transaction size"))
where
bounded :: Text -> Integer -> Either Error ()
bounded Text
message Integer
n = Bool -> Either Error () -> Either Error ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Integer
n Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer
0 Bool -> Bool -> Bool
&& Integer
n Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Int64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Int64
forall a. Bounded a => a
maxBound :: Int64)) (Error -> Either Error ()
forall a b. a -> Either a b
Left (Text -> Error
InvalidBuildingRequest Text
message))
boundedValue :: Value -> Either Error ()
boundedValue Value
value = ((AssetId, Integer) -> Either Error ())
-> [(AssetId, Integer)] -> Either Error ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Text -> Integer -> Either Error ()
bounded Text
"Aggregate native balance exceeds signed bounds" (Integer -> Either Error ())
-> ((AssetId, Integer) -> Integer)
-> (AssetId, Integer)
-> Either Error ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (AssetId, Integer) -> Integer
forall a b. (a, b) -> b
snd) (Value -> [(AssetId, Integer)]
Value.toList Value
value)
unique :: [Utxo] -> Either Error ()
unique [Utxo]
values =
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]
values)) 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]
values)
(Error -> Either Error ()
forall a b. a -> Either a b
Left (Text -> Error
InvalidBuildingRequest Text
"Duplicate input reference in wallet snapshot"))
consistent :: t Utxo -> Either Error ()
consistent t Utxo
values =
Bool -> Either Error () -> Either Error ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless
((Utxo -> Bool) -> t Utxo -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (\Utxo
a -> (Utxo -> Bool) -> t Utxo -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (\Utxo
b -> Utxo -> Input
Utxo.input Utxo
a Input -> Input -> Bool
forall a. Eq a => a -> a -> Bool
/= Utxo -> Input
Utxo.input Utxo
b Bool -> Bool -> Bool
|| Utxo
a Utxo -> Utxo -> Bool
forall a. Eq a => a -> a -> Bool
== Utxo
b) t Utxo
values) t Utxo
values)
(Error -> Either Error ()
forall a b. a -> Either a b
Left (Text -> Error
InvalidBuildingRequest Text
"Conflicting snapshots for one wallet input"))
validateFees :: Config -> Plan -> IO (Either Error ())
validateFees :: Config -> Plan -> IO (Either Error ())
validateFees Config
config Plan
plan = do
let possible :: [Utxo]
possible = Map Input Utxo -> [Utxo]
forall k a. Map k a -> [a]
Map.elems ([(Input, Utxo)] -> Map Input Utxo
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(Utxo -> Input
Utxo.input Utxo
u, Utxo
u) | Utxo
u <- Plan -> [Utxo] -> [Utxo]
eligible Plan
plan (Config -> [Utxo]
availableUtxos Config
config) [Utxo] -> [Utxo] -> [Utxo]
forall a. Semigroup a => a -> a -> a
<> Plan -> [Utxo]
Plan.outerResolvedInputs Plan
plan])
subPriced :: [Utxo]
subPriced = ((SubTransaction, [Utxo]) -> [Utxo])
-> [(SubTransaction, [Utxo])] -> [Utxo]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (SubTransaction, [Utxo]) -> [Utxo]
pricedSub (Plan -> [(SubTransaction, [Utxo])]
Plan.subTransactions Plan
plan)
allPriced :: [Utxo]
allPriced = [Utxo]
possible [Utxo] -> [Utxo] -> [Utxo]
forall a. Semigroup a => a -> a -> a
<> [Utxo]
subPriced
hasReferenceScript :: Bool
hasReferenceScript = (Utxo -> Bool) -> [Utxo] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (Maybe Script -> Bool
forall a. Maybe a -> Bool
isJust (Maybe Script -> Bool) -> (Utxo -> Maybe Script) -> Utxo -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Output -> Maybe Script
Output.referenceScript (Output -> Maybe Script)
-> (Utxo -> Output) -> Utxo -> Maybe Script
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Utxo -> Output
Utxo.output) [Utxo]
allPriced
Either Error Word64
reference <- if Bool
hasReferenceScript then Parameters -> [Utxo] -> IO (Either Error Word64)
pricedReferenceFee (Config -> Parameters
parameters Config
config) [Utxo]
allPriced else Either Error Word64 -> IO (Either Error Word64)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Word64 -> Either Error Word64
forall a b. b -> Either a b
Right Word64
0)
Either Error () -> IO (Either Error ())
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error () -> IO (Either Error ()))
-> Either Error () -> IO (Either Error ())
forall a b. (a -> b) -> a -> b
$ do
Word64
referenceFee <- Either Error Word64
reference
Integer
executionFee <- Config -> Plan -> Either Error Integer
executionLimitFee Config
config Plan
plan
let params :: Parameters
params = Config -> Parameters
parameters Config
config
witnessBytes :: Integer
witnessBytes =
Integer
101 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* (Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Config -> Word64
extraSigners Config
config) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Plan -> Integer
Plan.witnessCount Plan
plan)
Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ [Integer] -> Integer
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum
[ case Output -> Address
Output.address (Utxo -> Output
Utxo.output Utxo
u) of
Address.ByronAddress Address
_ -> Integer
180 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Int -> Integer
forall a. Integral a => a -> Integer
toInteger (ByteString -> Int
B.length (Address -> ByteString
Address.toBytes (Output -> Address
Output.address (Utxo -> Output
Utxo.output Utxo
u))))
Address
_ -> Integer
101
| Utxo
u <- [Utxo]
possible [Utxo] -> [Utxo] -> [Utxo]
forall a. Semigroup a => a -> a -> a
<> Config -> [Utxo]
collateralUtxos Config
config
]
Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
32
snapshotBytes :: Integer
snapshotBytes =
Int -> Integer
forall a. Integral a => a -> Integer
toInteger (ByteString -> Int
B.length (Word64 -> Plan -> ByteString
Plan.encodePlan (Word64 -> Maybe Word64 -> Word64
forall a. a -> Maybe a -> a
fromMaybe Word64
0 (Parameters -> Maybe Word64
P.parameterGovernanceActionDeposit (Config -> Parameters
parameters Config
config))) Plan
plan))
Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ [Integer] -> Integer
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Int -> Integer
forall a. Integral a => a -> Integer
toInteger (ByteString -> Int
B.length (Utxo -> ByteString
Utxo.encodeCbor Utxo
u)) | Utxo
u <- [Utxo]
possible [Utxo] -> [Utxo] -> [Utxo]
forall a. Semigroup a => a -> a -> a
<> Config -> [Utxo]
collateralUtxos Config
config]
changeCount :: Integer
changeCount = (Int -> Integer
forall a. Integral a => a -> Integer
toInteger ([Output] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (Plan -> [Output]
Plan.planOutputs Plan
plan)) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
1) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* (Integer
snapshotBytes Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
1)
changeBytes :: Integer
changeBytes = Integer
changeCount Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* (Integer
snapshotBytes Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Int -> Integer
forall a. Integral a => a -> Integer
toInteger (ByteString -> Int
B.length (Address -> ByteString
Address.toBytes (Config -> Address
changeAddress Config
config))) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
256)
sizeBound :: Integer
sizeBound = Integer
snapshotBytes Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
changeBytes Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
1024 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
witnessBytes
linearFee :: Integer
linearFee = Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Parameters -> Word64
P.parameterMinFeeB Parameters
params) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Parameters -> Word64
P.parameterMinFeeA Parameters
params) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
sizeBound
fee :: Integer
fee = Integer -> Integer -> Integer
forall a. Ord a => a -> a -> a
max (Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Config -> Word64
minimumFee Config
config)) (Integer
linearFee Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
executionFee Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger Word64
referenceFee)
sink :: Value
sink = (Output -> Value) -> [Output] -> Value
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap Output -> Value
Output.value (Plan -> [Output]
Plan.planOutputs Plan
plan) Value -> Value -> Value
forall a. Semigroup a => a -> a -> a
<> Parameters -> Plan -> Value
Plan.sinkExtras Parameters
params Plan
plan Value -> Value -> Value
forall a. Semigroup a => a -> a -> a
<> Integer -> Value
Value.lovelace Integer
fee
Bool -> Either Error () -> Either Error ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless
(Integer
fee Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Int64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Int64
forall a. Bounded a => a
maxBound :: Int64) Bool -> Bool -> Bool
&& ((AssetId, Integer) -> Bool) -> [(AssetId, Integer)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ((Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Int64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Int64
forall a. Bounded a => a
maxBound :: Int64)) (Integer -> Bool)
-> ((AssetId, Integer) -> Integer) -> (AssetId, Integer) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (AssetId, Integer) -> Integer
forall a b. (a, b) -> b
snd) (Value -> [(AssetId, Integer)]
Value.toList Value
sink))
(Error -> Either Error ()
forall a b. a -> Either a b
Left (Text -> Error
InvalidBuildingRequest Text
"Complete priced fee or target exceeds signed native bounds"))
executionLimitFee :: Config -> Plan -> Either Error Integer
executionLimitFee :: Config -> Plan -> Either Error Integer
executionLimitFee Config
config Plan
plan
| Bool -> Bool
not (Plan -> Bool
Plan.requiresEvaluation Plan
plan) = Integer -> Either Error Integer
forall a b. b -> Either a b
Right Integer
0
| Bool
otherwise = do
ExUnitPrices
prices <- Either Error ExUnitPrices
-> (ExUnitPrices -> Either Error ExUnitPrices)
-> Maybe ExUnitPrices
-> Either Error ExUnitPrices
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Error -> Either Error ExUnitPrices
forall a b. a -> Either a b
Left (Text -> Error
InvalidBuildingRequest Text
"Script execution prices are required")) ExUnitPrices -> Either Error ExUnitPrices
forall a b. b -> Either a b
Right (Parameters -> Maybe ExUnitPrices
P.parameterExecutionCosts (Config -> Parameters
parameters Config
config))
ExUnits
cap <- case Config -> Maybe Evaluator
evaluator Config
config of
Maybe Evaluator
Nothing -> ExUnits -> Either Error ExUnits
forall a b. b -> Either a b
Right (Word64 -> Word64 -> ExUnits
Ex.ExUnits Word64
100000000 Word64
100000000000)
Just Evaluator
_ -> Either Error ExUnits
-> (ExUnits -> Either Error ExUnits)
-> Maybe ExUnits
-> Either Error ExUnits
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Error -> Either Error ExUnits
forall a b. a -> Either a b
Left (Text -> Error
InvalidBuildingRequest Text
"Maximum transaction execution units are required")) ExUnits -> Either Error ExUnits
forall a b. b -> Either a b
Right (Parameters -> Maybe ExUnits
P.parameterMaxTxExUnits (Config -> Parameters
parameters Config
config))
Integer -> Either Error Integer
forall a. a -> Either Error a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
( Rational -> Integer
forall b. Integral b => Rational -> b
forall a b. (RealFrac a, Integral b) => a -> b
ceiling
( Word64 -> Rational
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ExUnits -> Word64
Ex.memory ExUnits
cap) Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
* NonNegativeRational -> Rational
Rational.toRational (ExUnitPrices -> NonNegativeRational
Prices.memoryPrice ExUnitPrices
prices)
Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
+ Word64 -> Rational
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ExUnits -> Word64
Ex.steps ExUnits
cap) Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
* NonNegativeRational -> Rational
Rational.toRational (ExUnitPrices -> NonNegativeRational
Prices.stepPrice ExUnitPrices
prices)
)
)
checkedEvaluator :: Config -> Evaluator -> Evaluator
checkedEvaluator :: Config -> Evaluator -> Evaluator
checkedEvaluator Config
config Evaluator
backend = (Target -> Transaction -> [Utxo] -> IO (Either Error Budgets))
-> Evaluator
Eval.Evaluator ((Target -> Transaction -> [Utxo] -> IO (Either Error Budgets))
-> Evaluator)
-> (Target -> Transaction -> [Utxo] -> IO (Either Error Budgets))
-> Evaluator
forall a b. (a -> b) -> a -> b
$ \Target
target Transaction
transaction [Utxo]
inputs -> do
Either Error Budgets
result <- Evaluator
-> Target -> Transaction -> [Utxo] -> IO (Either Error Budgets)
Eval.evaluate Evaluator
backend Target
target Transaction
transaction [Utxo]
inputs
Either Error Budgets -> IO (Either Error Budgets)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error Budgets -> IO (Either Error Budgets))
-> Either Error Budgets -> IO (Either Error Budgets)
forall a b. (a -> b) -> a -> b
$ do
Budgets
computed <- Either Error Budgets
result
Config -> [ExUnits] -> Either Error ()
validateBudget Config
config (((RedeemerId, ExUnits) -> ExUnits)
-> [(RedeemerId, ExUnits)] -> [ExUnits]
forall a b. (a -> b) -> [a] -> [b]
map (RedeemerId, ExUnits) -> ExUnits
forall a b. (a, b) -> b
snd (Budgets -> [(RedeemerId, ExUnits)]
Eval.budgets Budgets
computed))
Budgets -> Either Error Budgets
forall a. a -> Either Error a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Budgets
computed
validateBudget :: Config -> [Ex.ExUnits] -> Either Error ()
validateBudget :: Config -> [ExUnits] -> Either Error ()
validateBudget Config
config [ExUnits]
units = case ([ExUnits]
units, Parameters -> Maybe ExUnits
P.parameterMaxTxExUnits (Config -> Parameters
parameters Config
config)) of
([], Maybe ExUnits
_) -> () -> Either Error ()
forall a. a -> Either Error a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
([ExUnits]
_, Maybe ExUnits
Nothing) -> Error -> Either Error ()
forall a b. a -> Either a b
Left (Text -> Error
InvalidBuildingRequest Text
"Maximum transaction execution units are required")
([ExUnits]
_, Just ExUnits
cap) ->
Bool -> Either Error () -> Either Error ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless
( [Integer] -> Integer
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ((ExUnits -> Integer) -> [ExUnits] -> [Integer]
forall a b. (a -> b) -> [a] -> [b]
map (Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Word64 -> Integer) -> (ExUnits -> Word64) -> ExUnits -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ExUnits -> Word64
Ex.memory) [ExUnits]
units) Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (ExUnits -> Word64
Ex.memory ExUnits
cap)
Bool -> Bool -> Bool
&& [Integer] -> Integer
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ((ExUnits -> Integer) -> [ExUnits] -> [Integer]
forall a b. (a -> b) -> [a] -> [b]
map (Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Word64 -> Integer) -> (ExUnits -> Word64) -> ExUnits -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ExUnits -> Word64
Ex.steps) [ExUnits]
units) Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (ExUnits -> Word64
Ex.steps ExUnits
cap)
)
(Error -> Either Error ()
forall a b. a -> Either a b
Left (Text -> Error
InvalidEvaluationResult Text
"Evaluation exceeds maximum transaction execution units"))
validateBuilt :: Config -> Plan -> Word64 -> Transaction -> Either Error ()
validateBuilt :: Config -> Plan -> Word64 -> Transaction -> Either Error ()
validateBuilt Config
config Plan
plan Word64
keyCount Transaction
transaction = do
let body :: Body
body = EncodedBody -> Body
Body.toBody (Transaction -> EncodedBody
Transaction.body Transaction
transaction)
fields :: Fields
fields = Body -> Fields
Body.bodyFields Body
body
used :: [Input]
used = 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 [] (Body -> Maybe [Input]
Body.bodyCollateral Body
body)
sourceMap :: Map Input Utxo
sourceMap = [(Input, Utxo)] -> Map Input Utxo
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(Utxo -> Input
Utxo.input Utxo
u, Utxo
u) | Utxo
u <- Config -> [Utxo]
availableUtxos Config
config [Utxo] -> [Utxo] -> [Utxo]
forall a. Semigroup a => a -> a -> a
<> Config -> [Utxo]
collateralUtxos Config
config [Utxo] -> [Utxo] -> [Utxo]
forall a. Semigroup a => a -> a -> a
<> Plan -> [Utxo]
Plan.resolvedInputs Plan
plan]
selectedInputs :: [Utxo]
selectedInputs = [Utxo
u | Input
reference <- [Input]
used, Just Utxo
u <- [Input -> Map Input Utxo -> Maybe Utxo
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Input
reference Map Input Utxo
sourceMap]]
byronAddresses :: Set ByteString
byronAddresses =
[ByteString] -> Set ByteString
forall a. Ord a => [a] -> Set a
Set.fromList
[ Address -> ByteString
Address.toBytes Address
destination
| Utxo
u <- [Utxo]
selectedInputs,
let destination :: Address
destination = Output -> Address
Output.address (Utxo -> Output
Utxo.output Utxo
u),
case Address
destination of Address.ByronAddress Address
_ -> Bool
True; Address
_ -> Bool
False
]
reserve :: Integer
reserve = Integer
101 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger Word64
keyCount Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ [Integer] -> Integer
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Integer
180 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Int -> Integer
forall a. Integral a => a -> Integer
toInteger (ByteString -> Int
B.length ByteString
bytes) | ByteString
bytes <- Set ByteString -> [ByteString]
forall a. Set a -> [a]
Set.toList Set ByteString
byronAddresses] Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
32
size :: Integer
size = Int -> Integer
forall a. Integral a => a -> Integer
toInteger (ByteString -> Int
B.length (Transaction -> ByteString
Transaction.encodeCbor Transaction
transaction)) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
reserve
Config -> [ExUnits] -> Either Error ()
validateBudget
Config
config
( (Redeemer -> ExUnits) -> [Redeemer] -> [ExUnits]
forall a b. (a -> b) -> [a] -> [b]
map Redeemer -> ExUnits
Redeemer.executionUnits (Witnesses -> [Redeemer]
Witnesses.redeemers (EncodedWitnesses -> Witnesses
Witnesses.toWitnesses (Transaction -> EncodedWitnesses
Transaction.witnesses Transaction
transaction)))
[ExUnits] -> [ExUnits] -> [ExUnits]
forall a. Semigroup a => a -> a -> a
<> ((SubTransaction, [Utxo]) -> [ExUnits])
-> [(SubTransaction, [Utxo])] -> [ExUnits]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap ((Redeemer -> ExUnits) -> [Redeemer] -> [ExUnits]
forall a b. (a -> b) -> [a] -> [b]
map Redeemer -> ExUnits
Redeemer.executionUnits ([Redeemer] -> [ExUnits])
-> ((SubTransaction, [Utxo]) -> [Redeemer])
-> (SubTransaction, [Utxo])
-> [ExUnits]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Witnesses -> [Redeemer]
Witnesses.redeemers (Witnesses -> [Redeemer])
-> ((SubTransaction, [Utxo]) -> Witnesses)
-> (SubTransaction, [Utxo])
-> [Redeemer]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. EncodedWitnesses -> Witnesses
Witnesses.toWitnesses (EncodedWitnesses -> Witnesses)
-> ((SubTransaction, [Utxo]) -> EncodedWitnesses)
-> (SubTransaction, [Utxo])
-> Witnesses
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SubTransaction -> EncodedWitnesses
Sub.witnesses (SubTransaction -> EncodedWitnesses)
-> ((SubTransaction, [Utxo]) -> SubTransaction)
-> (SubTransaction, [Utxo])
-> EncodedWitnesses
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (SubTransaction, [Utxo]) -> SubTransaction
forall a b. (a, b) -> a
fst) (Plan -> [(SubTransaction, [Utxo])]
Plan.subTransactions Plan
plan)
)
Bool -> Either Error () -> Either Error ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless
(Integer
size Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Parameters -> Word64
P.parameterMaxTxSize (Config -> Parameters
parameters Config
config)))
(Error -> Either Error ()
forall a b. a -> Either a b
Left (Text -> Error
InvalidBuildingRequest Text
"Transaction and required witness reserve exceed maximum transaction size"))
encodeConfig :: Plan -> Config -> ByteString
encodeConfig :: Plan -> Config -> ByteString
encodeConfig Plan
plan Config
config =
Encoding -> ByteString
C.encode
( Word -> Encoding
E.encodeListLen Word
10
Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> [Encoding] -> Encoding
forall a. Monoid a => [a] -> a
mconcat
[ ByteString -> Encoding
E.encodeBytes (Parameters -> ByteString
Parameters.encodeSnapshot (Config -> Parameters
parameters Config
config)),
ByteString -> Encoding
E.encodeBytes (Address -> ByteString
Address.toBytes (Config -> Address
changeAddress Config
config)),
[Utxo] -> Encoding
forall {t :: * -> *}. Foldable t => t Utxo -> Encoding
utxos ((Utxo -> Bool) -> [Utxo] -> [Utxo]
forall a. (a -> Bool) -> [a] -> [a]
filter (\Utxo
u -> Utxo -> Input
Utxo.input Utxo
u Input -> [Input] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` (Utxo -> Input) -> [Utxo] -> [Input]
forall a b. (a -> b) -> [a] -> [b]
map Utxo -> Input
Utxo.input (Plan -> [Utxo]
Plan.spendingInputs Plan
plan)) (Plan -> [Utxo] -> [Utxo]
eligible Plan
plan (Config -> [Utxo]
availableUtxos Config
config))),
[Utxo] -> Encoding
forall {t :: * -> *}. Foldable t => t Utxo -> Encoding
utxos (Plan -> [Utxo] -> [Utxo]
eligible Plan
plan (Config -> [Utxo]
collateralUtxos Config
config)),
ByteString -> Encoding
E.encodeBytes (ByteString
-> (Address -> ByteString) -> Maybe Address -> ByteString
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ByteString
forall a. Monoid a => a
mempty Address -> ByteString
Address.toBytes (Config -> Maybe Address
collateralChangeAddress Config
config)),
Word64 -> Encoding
E.encodeWord64 (Config -> Word64
minimumFee Config
config),
Word64 -> Encoding
E.encodeWord64 (Config -> Word64
extraSigners Config
config),
Word64 -> Encoding
E.encodeWord64 (SlotConfig -> Word64
Slot.zeroTime (Config -> SlotConfig
slotConfig Config
config)),
Word64 -> Encoding
E.encodeWord64 (SlotConfig -> Word64
Slot.zeroSlot (Config -> SlotConfig
slotConfig Config
config)),
Word64 -> Encoding
E.encodeWord64 (SlotConfig -> Word64
Slot.slotLength (Config -> SlotConfig
slotConfig Config
config))
]
)
where
utxos :: t Utxo -> Encoding
utxos t Utxo
values = Word -> Encoding
E.encodeListLen (Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral (t Utxo -> Int
forall a. t a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length t Utxo
values)) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> (Utxo -> Encoding) -> t Utxo -> Encoding
forall m a. Monoid m => (a -> m) -> t a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (ByteString -> Encoding
C.raw (ByteString -> Encoding)
-> (Utxo -> ByteString) -> Utxo -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Utxo -> ByteString
Utxo.encodeCbor) t Utxo
values
withResult :: (Ptr (Ptr Raw.Buffer) -> Ptr (Ptr Raw.Buffer) -> Ptr Word64 -> IO Native.NativeCode) -> IO (Either Error (ByteString, Word64))
withResult :: (Ptr (Ptr Buffer) -> Ptr (Ptr Buffer) -> Ptr Word64 -> IO CUInt)
-> IO (Either Error (ByteString, Word64))
withResult = Text
-> (Ptr (Ptr Buffer) -> Ptr (Ptr Buffer) -> Ptr Word64 -> IO CUInt)
-> IO (Either Error (ByteString, Word64))
withResultNamed Text
"builder.build"
withResultNamed :: Text -> (Ptr (Ptr Raw.Buffer) -> Ptr (Ptr Raw.Buffer) -> Ptr Word64 -> IO Native.NativeCode) -> IO (Either Error (ByteString, Word64))
withResultNamed :: Text
-> (Ptr (Ptr Buffer) -> Ptr (Ptr Buffer) -> Ptr Word64 -> IO CUInt)
-> IO (Either Error (ByteString, Word64))
withResultNamed Text
operation Ptr (Ptr Buffer) -> Ptr (Ptr Buffer) -> Ptr Word64 -> IO CUInt
call = (Ptr (Ptr Buffer) -> IO (Either Error (ByteString, Word64)))
-> IO (Either Error (ByteString, Word64))
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr (Ptr Buffer) -> IO (Either Error (ByteString, Word64)))
-> IO (Either Error (ByteString, Word64)))
-> (Ptr (Ptr Buffer) -> IO (Either Error (ByteString, Word64)))
-> IO (Either Error (ByteString, Word64))
forall a b. (a -> b) -> a -> b
$ \Ptr (Ptr Buffer)
output -> (Ptr (Ptr Buffer) -> IO (Either Error (ByteString, Word64)))
-> IO (Either Error (ByteString, Word64))
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr (Ptr Buffer) -> IO (Either Error (ByteString, Word64)))
-> IO (Either Error (ByteString, Word64)))
-> (Ptr (Ptr Buffer) -> IO (Either Error (ByteString, Word64)))
-> IO (Either Error (ByteString, Word64))
forall a b. (a -> b) -> a -> b
$ \Ptr (Ptr Buffer)
diagnostic -> (Ptr Word64 -> IO (Either Error (ByteString, Word64)))
-> IO (Either Error (ByteString, Word64))
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr Word64 -> IO (Either Error (ByteString, Word64)))
-> IO (Either Error (ByteString, Word64)))
-> (Ptr Word64 -> IO (Either Error (ByteString, Word64)))
-> IO (Either Error (ByteString, Word64))
forall a b. (a -> b) -> a -> b
$ \Ptr Word64
keyCount ->
IO ()
-> (() -> IO ())
-> (() -> IO (Either Error (ByteString, Word64)))
-> IO (Either Error (ByteString, Word64))
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket
(Ptr (Ptr Buffer) -> Ptr Buffer -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr (Ptr Buffer)
output Ptr Buffer
forall a. Ptr a
nullPtr IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Ptr (Ptr Buffer) -> Ptr Buffer -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr (Ptr Buffer)
diagnostic Ptr Buffer
forall a. Ptr a
nullPtr IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Ptr Word64 -> Word64 -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr Word64
keyCount Word64
0)
(\() -> Ptr (Ptr Buffer) -> IO ()
Raw.cBufferRelease Ptr (Ptr Buffer)
output IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Ptr (Ptr Buffer) -> IO ()
Raw.cBufferRelease Ptr (Ptr Buffer)
diagnostic)
((() -> IO (Either Error (ByteString, Word64)))
-> IO (Either Error (ByteString, Word64)))
-> (() -> IO (Either Error (ByteString, Word64)))
-> IO (Either Error (ByteString, Word64))
forall a b. (a -> b) -> a -> b
$ \() -> do
CUInt
status <- Ptr (Ptr Buffer) -> Ptr (Ptr Buffer) -> Ptr Word64 -> IO CUInt
call Ptr (Ptr Buffer)
output Ptr (Ptr Buffer)
diagnostic Ptr Word64
keyCount
if CUInt
status CUInt -> CUInt -> Bool
forall a. Eq a => a -> a -> Bool
== CUInt
Native.success
then do
Either Error ByteString
bytes <- Ptr (Ptr Buffer) -> IO (Ptr Buffer)
forall a. Storable a => Ptr a -> IO a
peek Ptr (Ptr Buffer)
output IO (Ptr Buffer)
-> (Ptr Buffer -> IO (Either Error ByteString))
-> IO (Either Error ByteString)
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Text -> Ptr Buffer -> IO (Either Error ByteString)
Runtime.copyBuffer Text
operation
Word64
count <- Ptr Word64 -> IO Word64
forall a. Storable a => Ptr a -> IO a
peek Ptr Word64
keyCount
Either Error (ByteString, Word64)
-> IO (Either Error (ByteString, Word64))
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((,Word64
count) (ByteString -> (ByteString, Word64))
-> Either Error ByteString -> Either Error (ByteString, Word64)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Either Error ByteString
bytes)
else do
Ptr Buffer
pointer <- Ptr (Ptr Buffer) -> IO (Ptr Buffer)
forall a. Storable a => Ptr a -> IO a
peek Ptr (Ptr Buffer)
diagnostic
if Ptr Buffer
pointer Ptr Buffer -> Ptr Buffer -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr Buffer
forall a. Ptr a
nullPtr
then do
Either Error ()
checked <- Text -> CUInt -> IO (Either Error ())
Runtime.checkStatus Text
operation CUInt
status
Either Error (ByteString, Word64)
-> IO (Either Error (ByteString, Word64))
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error ()
checked Either Error ()
-> Either Error (ByteString, Word64)
-> Either Error (ByteString, Word64)
forall a b. Either Error a -> Either Error b -> Either Error b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Error -> Either Error (ByteString, Word64)
forall a b. a -> Either a b
Left (Text -> Error
NativeContractViolation Text
"builder.status"))
else do
Either Error ByteString
copied <- Text -> Ptr Buffer -> IO (Either Error ByteString)
Runtime.copyBuffer Text
"builder.diagnostic" Ptr Buffer
pointer
Either Error (ByteString, Word64)
-> IO (Either Error (ByteString, Word64))
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error (ByteString, Word64)
-> IO (Either Error (ByteString, Word64)))
-> Either Error (ByteString, Word64)
-> IO (Either Error (ByteString, Word64))
forall a b. (a -> b) -> a -> b
$ case Either Error ByteString
copied of
Left Error
failure -> Error -> Either Error (ByteString, Word64)
forall a b. a -> Either a b
Left Error
failure
Right ByteString
bytes -> Error -> Either Error (ByteString, Word64)
forall a b. a -> Either a b
Left (Text -> ErrorCode -> Text -> Error
NativeError Text
operation (Integer -> ErrorCode
Code.fromNumber (CUInt -> Integer
forall a. Integral a => a -> Integer
toInteger CUInt
status)) (OnDecodeError -> ByteString -> Text
Text.decodeUtf8With (\String
_ Maybe Word8
_ -> Char -> Maybe Char
forall a. a -> Maybe a
Just Char
'\xfffd') ByteString
bytes))
deferredDispatch :: Plan -> Runtime.Dispatch
deferredDispatch :: Plan -> Dispatch
deferredDispatch Plan
plan Word32
operation ByteString
bytes
| Word32
operation Word32 -> Word32 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word32
110 = Either Error ByteString -> IO (Either Error ByteString)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Error -> Either Error ByteString
forall a b. a -> Either a b
Left (Text -> Error
UnsupportedOperation Text
"Unknown deferred redeemer callback operation"))
| Bool
otherwise = Either Error ByteString -> IO (Either Error ByteString)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error ByteString -> IO (Either Error ByteString))
-> Either Error ByteString -> IO (Either Error ByteString)
forall a b. (a -> b) -> a -> b
$ do
(Int
index, Transaction
transaction, [Utxo]
inputs) <- (Text -> Error)
-> (forall s. Decoder s (Int, Transaction, [Utxo]))
-> ByteString
-> Either Error (Int, Transaction, [Utxo])
forall a.
(Text -> Error)
-> (forall s. Decoder s a) -> ByteString -> Either Error a
C.decodeAs Text -> Error
InvalidTransactionPlan Decoder s (Int, Transaction, [Utxo])
forall s. Decoder s (Int, Transaction, [Utxo])
parser ByteString
bytes
DeferredRedeemer
callback <- Either Error DeferredRedeemer
-> (DeferredRedeemer -> Either Error DeferredRedeemer)
-> Maybe DeferredRedeemer
-> Either Error DeferredRedeemer
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Error -> Either Error DeferredRedeemer
forall a b. a -> Either a b
Left (Text -> Error
NativeContractViolation Text
"builder.deferredIndex")) DeferredRedeemer -> Either Error DeferredRedeemer
forall a b. b -> Either a b
Right (Int -> Seq DeferredRedeemer -> Maybe DeferredRedeemer
forall a. Int -> Seq a -> Maybe a
Seq.lookup Int
index (Plan -> Seq DeferredRedeemer
Plan.deferredRedeemers Plan
plan))
let canonical :: [Utxo]
canonical = (Utxo -> Input) -> [Utxo] -> [Utxo]
forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn Utxo -> Input
Utxo.input [Utxo]
inputs
references :: [Input]
references = [Input] -> [Input]
forall a. Ord a => [a] -> [a]
sort (Fields -> [Input]
Fields.fieldInputs (Body -> Fields
Body.bodyFields (EncodedBody -> Body
Body.toBody (Transaction -> EncodedBody
Transaction.body Transaction
transaction))))
Bool -> Either Error () -> Either Error ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ((Utxo -> Input) -> [Utxo] -> [Input]
forall a b. (a -> b) -> [a] -> [b]
map Utxo -> Input
Utxo.input [Utxo]
canonical [Input] -> [Input] -> Bool
forall a. Eq a => a -> a -> Bool
== [Input]
references) (Error -> Either Error ()
forall a b. a -> Either a b
Left (Text -> Error
NativeContractViolation Text
"builder.deferredInputs"))
EncodedPlutusData -> ByteString
Data.toCbor (EncodedPlutusData -> ByteString)
-> Either Error EncodedPlutusData -> Either Error ByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> DeferredRedeemer
callback Transaction
transaction [Utxo]
canonical
where
parser :: Decoder s (Int, Transaction, [Utxo])
parser = Int
-> Decoder s (Int, Transaction, [Utxo])
-> Decoder s (Int, Transaction, [Utxo])
forall s a. Int -> Decoder s a -> Decoder s a
C.array Int
3 ((,,) (Int -> Transaction -> [Utxo] -> (Int, Transaction, [Utxo]))
-> Decoder s Int
-> Decoder s (Transaction -> [Utxo] -> (Int, Transaction, [Utxo]))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Int
forall s. Decoder s Int
D.decodeInt Decoder s (Transaction -> [Utxo] -> (Int, Transaction, [Utxo]))
-> Decoder s Transaction
-> Decoder s ([Utxo] -> (Int, Transaction, [Utxo]))
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
<*> ByteString
-> (ByteString -> Either Error Transaction)
-> Decoder s Transaction
forall a s.
ByteString -> (ByteString -> Either Error a) -> Decoder s a
C.nested ByteString
bytes ByteString -> Either Error Transaction
Transaction.decodeCbor Decoder s ([Utxo] -> (Int, Transaction, [Utxo]))
-> Decoder s [Utxo] -> Decoder s (Int, Transaction, [Utxo])
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 Utxo -> Decoder s [Utxo]
forall s a. Decoder s a -> Decoder s [a]
C.list (ByteString -> (ByteString -> Either Error Utxo) -> Decoder s Utxo
forall a s.
ByteString -> (ByteString -> Either Error a) -> Decoder s a
C.nested ByteString
bytes ByteString -> Either Error Utxo
Utxo.decodeCbor))
eligible :: Plan -> [Utxo] -> [Utxo]
eligible :: Plan -> [Utxo] -> [Utxo]
eligible Plan
plan = (Utxo -> Bool) -> [Utxo] -> [Utxo]
forall a. (a -> Bool) -> [a] -> [a]
filter (\Utxo
input -> Utxo -> Input
Utxo.input Utxo
input Input -> [Input] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` Plan -> [Input]
Plan.reservedInputs Plan
plan)
pricedSub :: (Sub.SubTransaction, [Utxo]) -> [Utxo]
pricedSub :: (SubTransaction, [Utxo]) -> [Utxo]
pricedSub (SubTransaction
transaction, [Utxo]
resolved) =
let fields :: Fields
fields = Body -> Fields
SubBody.fields (EncodedBody -> Body
SubBody.toBody (SubTransaction -> EncodedBody
Sub.body SubTransaction
transaction))
references :: [Input]
references = 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)
in (Utxo -> Bool) -> [Utxo] -> [Utxo]
forall a. (a -> Bool) -> [a] -> [a]
filter (\Utxo
input -> Utxo -> Input
Utxo.input Utxo
input Input -> [Input] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Input]
references) [Utxo]
resolved
pricedReferenceFee :: Parameters -> [Utxo] -> IO (Either Error Word64)
pricedReferenceFee :: Parameters -> [Utxo] -> IO (Either Error Word64)
pricedReferenceFee Parameters
params [Utxo]
inputs = case Parameters -> Maybe NonNegativeRational
P.parameterRefScriptCostPerByte Parameters
params of
Maybe NonNegativeRational
Nothing -> 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 (Text -> Error
InvalidBuildingRequest Text
"Reference script byte price is required"))
Just NonNegativeRational
_ -> do
Either Error ByteString
result <-
CUInt -> ByteString -> IO (Either Error ByteString)
Building.runUnchecked
CUInt
1
( Encoding -> ByteString
C.encode
( Word -> Encoding
E.encodeListLen Word
2
Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
C.raw (Parameters -> ByteString
Parameters.encodeSnapshot Parameters
params)
Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> 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]
inputs))
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
C.raw (ByteString -> Encoding)
-> (Utxo -> ByteString) -> Utxo -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Utxo -> ByteString
Utxo.encodeCbor) [Utxo]
inputs
)
)
Either Error Word64 -> IO (Either Error Word64)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error ByteString
result Either Error ByteString
-> (ByteString -> Either Error Word64) -> Either Error Word64
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 Word64)
-> ByteString
-> Either Error Word64
forall a.
(Text -> Error)
-> (forall s. Decoder s a) -> ByteString -> Either Error a
C.decodeAs Text -> Error
InvalidBuildingRequest Decoder s Word64
forall s. Decoder s Word64
D.decodeWord64)
checkSubOutputSize :: Maybe Word64 -> Output.Output -> Either Error ()
checkSubOutputSize :: Maybe Word64 -> Output -> Either Error ()
checkSubOutputSize Maybe Word64
Nothing Output
_ = () -> Either Error ()
forall a. a -> Either Error a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
checkSubOutputSize (Just Word64
limit) Output
output =
Bool -> Either Error () -> Either Error ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless
(Int -> Integer
forall a. Integral a => a -> Integer
toInteger (ByteString -> Int
B.length (Encoding -> ByteString
C.encode Encoding
encoding)) Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger Word64
limit)
(Error -> Either Error ()
forall a b. a -> Either a b
Left (Text -> Error
InvalidBuildingRequest Text
"Attached subtransaction output exceeds maximum value size"))
where
value :: Value
value = Output -> Value
Output.value Output
output
coin :: Encoding
coin = Integer -> Encoding
E.encodeInteger (AssetId -> Value -> Integer
Value.quantity AssetId
Ada Value
value)
assets :: [(AssetId, Integer)]
assets = Value -> [(AssetId, Integer)]
Value.toList (Value -> Value -> Value
Value.difference Value
value (Integer -> Value
Value.lovelace (AssetId -> Value -> Integer
Value.quantity AssetId
Ada Value
value)))
encoding :: Encoding
encoding = if [(AssetId, Integer)] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(AssetId, Integer)]
assets then Encoding
coin else Word -> Encoding
E.encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Encoding
coin Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Value -> Encoding
ValueCbor.encodeAssets Value
value