Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mm/proposal procedures #422

Merged
merged 26 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions cooked-validators.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ library
Cooked.MockChain.BlockChain
Cooked.MockChain.Direct
Cooked.MockChain.GenerateTx
Cooked.MockChain.GenerateTx.Collateral
Cooked.MockChain.GenerateTx.Common
Cooked.MockChain.GenerateTx.Input
Cooked.MockChain.GenerateTx.Output
Cooked.MockChain.GenerateTx.Proposal
Cooked.MockChain.GenerateTx.Witness
Cooked.MockChain.MinAda
Cooked.MockChain.Staged
Cooked.MockChain.Testing
Expand Down Expand Up @@ -101,13 +107,21 @@ library
, bytestring
, cardano-api
, cardano-crypto
, cardano-data
, cardano-ledger-conway
, cardano-ledger-core
, cardano-ledger-shelley
, cardano-node-emulator
, cardano-strict-containers
, containers
, data-default
, either
, exceptions
, flat
, http-conduit
, lens
, list-t
, microlens
, monad-control
, mtl
, nonempty-containers
Expand Down Expand Up @@ -146,6 +160,7 @@ test-suite spec
Cooked.MinAdaSpec
Cooked.MockChain.BlockChainSpec
Cooked.MockChainSpec
Cooked.ProposingScriptSpec
Cooked.ReferenceInputsSpec
Cooked.ReferenceScriptsSpec
Cooked.ShowBSSpec
Expand Down Expand Up @@ -185,14 +200,22 @@ test-suite spec
, bytestring
, cardano-api
, cardano-crypto
, cardano-data
, cardano-ledger-conway
, cardano-ledger-core
, cardano-ledger-shelley
, cardano-node-emulator
, cardano-strict-containers
, containers
, cooked-validators
, data-default
, either
, exceptions
, flat
, http-conduit
, lens
, list-t
, microlens
, monad-control
, mtl
, nonempty-containers
Expand Down
1 change: 1 addition & 0 deletions doc/IMPORTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ dependencies:
### [`cardano-ledger`](https://github.com/IntersectMBO/cardano-ledger)

- package `cardano-ledger-shelley`, prefix `Shelley`
- package `cardano-ledger-conway`, prefix `Conway`

### Exception

Expand Down
8 changes: 8 additions & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,21 @@ dependencies:
- bytestring
- cardano-api
- cardano-crypto
- cardano-data
- cardano-ledger-core
- cardano-ledger-shelley
- cardano-ledger-conway
- cardano-node-emulator
- cardano-strict-containers
- containers
- data-default
- either
- exceptions
- flat
- http-conduit
mmontin marked this conversation as resolved.
Show resolved Hide resolved
- lens
- list-t
- microlens
mmontin marked this conversation as resolved.
Show resolved Hide resolved
- monad-control
- mtl
- nonempty-containers
Expand Down
3 changes: 3 additions & 0 deletions src/Cooked/Conversion/ToCredential.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class ToCredential a where
instance ToCredential Api.Credential where
toCredential = id

instance ToCredential Api.Address where
toCredential (Api.Address cred _) = cred

instance ToCredential Api.PubKeyHash where
toCredential = Api.PubKeyCredential

Expand Down
7 changes: 7 additions & 0 deletions src/Cooked/Conversion/ToScriptHash.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Cooked.Conversion.ToScriptHash where
import Cooked.Conversion.ToScript
import Plutus.Script.Utils.Scripts qualified as Script
import Plutus.Script.Utils.Typed qualified as Script
import Plutus.Script.Utils.V3.Scripts qualified
mmontin marked this conversation as resolved.
Show resolved Hide resolved
import PlutusLedgerApi.V3 qualified as Api

class ToScriptHash a where
Expand All @@ -12,6 +13,12 @@ class ToScriptHash a where
instance ToScriptHash Api.ScriptHash where
toScriptHash = id

instance ToScriptHash Script.Script where
toScriptHash = Plutus.Script.Utils.V3.Scripts.scriptHash
mmontin marked this conversation as resolved.
Show resolved Hide resolved

instance ToScriptHash Api.SerialisedScript where
toScriptHash = toScriptHash . Script.Script

instance ToScriptHash Script.ValidatorHash where
toScriptHash (Script.ValidatorHash h) = Script.ScriptHash h

Expand Down
3 changes: 3 additions & 0 deletions src/Cooked/Conversion/ToValue.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ instance ToValue Script.Ada where

instance ToValue Cardano.Coin where
toValue (Cardano.Coin x) = toValue (Script.Lovelace x)

instance ToValue Api.Lovelace where
toValue (Api.Lovelace lv) = toValue (Script.Lovelace lv)
11 changes: 6 additions & 5 deletions src/Cooked/MockChain/Balancing.hs
Original file line number Diff line number Diff line change
Expand Up @@ -276,18 +276,19 @@ estimateTxSkelFee skel fee collateralIns returnCollateralWallet = do
-- We return an accurate estimate of the resulting transaction fee
return $ Emulator.unCoin $ Cardano.evaluateTransactionFee Cardano.ShelleyBasedEraConway (Emulator.pEmulatorPParams params) txBody nkeys 0

-- | This creates a balanced skeleton from a given skeleton and fee
-- In other words, this ensures that the following equation holds:
-- input value + minted value = output value + burned value + fee
-- | This creates a balanced skeleton from a given skeleton and fee In other
mmontin marked this conversation as resolved.
Show resolved Hide resolved
-- words, this ensures that the following equation holds: input value + minted
-- value = output value + burned value + fee + deposits
computeBalancedTxSkel :: (MonadBlockChainBalancing m) => Wallet -> BalancingOutputs -> TxSkel -> Fee -> m TxSkel
computeBalancedTxSkel balancingWallet balancingUtxos txSkel@TxSkel {..} (lovelace -> feeValue) = do
-- We compute the necessary values from the skeleton that are part of the
-- equation, except for the `feeValue` which we already have.
let (burnedValue, mintedValue) = Api.split $ txSkelMintsValue txSkelMints
outValue = txSkelValueInOutputs txSkel
inValue <- txSkelInputValue txSkel
depositedValue <- toValue <$> txSkelProposalsDeposit txSkel
-- We compute the values missing in the left and right side of the equation
let (missingRight, missingLeft) = Api.split $ outValue <> burnedValue <> feeValue <> PlutusTx.negate (inValue <> mintedValue)
let (missingRight, missingLeft) = Api.split $ outValue <> burnedValue <> feeValue <> depositedValue <> PlutusTx.negate (inValue <> mintedValue)
-- This gives us what we need to run our `reachValue` algorithm and append to
-- the resulting values whatever payment was missing in the initial skeleton
let candidatesRaw = second (<> missingRight) <$> reachValue balancingUtxos missingLeft (toInteger $ length balancingUtxos)
Expand Down Expand Up @@ -324,5 +325,5 @@ computeBalancedTxSkel balancingWallet balancingUtxos txSkel@TxSkel {..} (lovelac
-- a new output at the end of the list, to keep the order intact.
(txOutRefs, val) <- getOptimalCandidate candidatesRaw balancingWallet balancingError
return (txOutRefs, txSkelOuts ++ [paysPK balancingWallet val])
let newTxSkelIns = txSkelIns <> Map.fromList ((,TxSkelNoRedeemerForPK) <$> additionalInsTxOutRefs)
let newTxSkelIns = txSkelIns <> Map.fromList ((,TxSkelNoRedeemer) <$> additionalInsTxOutRefs)
return $ (txSkel & txSkelOutsL .~ newTxSkelOuts) & txSkelInsL .~ newTxSkelIns
14 changes: 14 additions & 0 deletions src/Cooked/MockChain/BlockChain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,17 @@ module Cooked.MockChain.BlockChain
lookupUtxos,
lookupUtxosPl,
validateTxSkel',
txSkelProposalsDeposit,
govActionDeposit,
)
where

import Cardano.Api.Ledger qualified as Cardano
import Cardano.Ledger.Conway.PParams qualified as Conway
import Cardano.Node.Emulator qualified as Emulator
import Cardano.Node.Emulator.Internal.Node qualified as Emulator
import Control.Arrow
import Control.Lens qualified as Lens
import Control.Monad
import Control.Monad.Except
import Control.Monad.Reader
Expand Down Expand Up @@ -317,6 +322,15 @@ txSkelReferenceInputUtxos :: (MonadBlockChainBalancing m) => TxSkel -> m (Map Ap
txSkelReferenceInputUtxos TxSkel {..} =
lookupUtxos $ mapMaybe txSkelReferenceScript (Map.elems txSkelIns) ++ Set.toList txSkelInsReference

-- | Retrieves the required deposit amount for issuing governance actions.
govActionDeposit :: (MonadBlockChainBalancing m) => m Api.Lovelace
govActionDeposit = Api.Lovelace . Cardano.unCoin . Lens.view Conway.ppGovActionDepositL . Emulator.emulatorPParams <$> getParams
mmontin marked this conversation as resolved.
Show resolved Hide resolved

-- | Retrieves the total amount of lovelace deposited in proposals in this
-- skeleton (equal to `govActionDeposit` times the number of proposals).
txSkelProposalsDeposit :: (MonadBlockChainBalancing m) => TxSkel -> m Api.Lovelace
txSkelProposalsDeposit TxSkel {..} = Api.Lovelace . (toInteger (length txSkelProposals) *) . Api.getLovelace <$> govActionDeposit

-- | Helper to convert Nothing to an error
maybeErrM :: (MonadBlockChainBalancing m) => MockChainError -> (a -> b) -> m (Maybe a) -> m b
maybeErrM err f = (maybe (throwError err) (return . f) =<<)
Expand Down
Loading