Skip to content

Commit

Permalink
Remove unused functions (#1762)
Browse files Browse the repository at this point in the history
Every function that is not used in this codebase has been removed, with
the few exceptions listed in the weeder roots.

A weeder check has been added to ensure no unused functions sneak back
in.

Anything that is actually really needed can either be readded from
history, or upstreamed to a library or form a new library so that it can
be discovered in hoogle.

---

<!-- Consider each and tick it off one way or the other -->
* [ ] CHANGELOG updated or not needed
* [ ] Documentation updated or not needed
* [ ] Haddocks updated or not needed
* [ ] No new TODOs introduced or explained herafter
  • Loading branch information
noonio authored Dec 17, 2024
2 parents d85f526 + 0a7d463 commit 9bb093a
Show file tree
Hide file tree
Showing 63 changed files with 141 additions and 1,113 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/weeder.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Check code weeds

on:
# We're using merge-chains; so this needs to run then.
merge_group:
pull_request:

jobs:
weeder:
name: Check code weeds
runs-on: ubuntu-latest
steps:
- name: 📥 Checkout repository
uses: actions/checkout@v4

- name: ❄ Prepare nix
uses: cachix/install-nix-action@v30
with:
extra_nix_config: |
accept-flake-config = true
log-lines = 1000
- name: ❄ Cachix cache of nix derivations
uses: cachix/cachix-action@v15
with:
name: cardano-scaling
authToken: '${{ secrets.CACHIX_CARDANO_SCALING_AUTH_TOKEN }}'

- name: 📐 Check weeder
run: |
nix develop --command -- bash -c "cabal update"
nix develop --command -- bash -c "cabal build all"
nix develop --command -- bash -c "weeder --require-hs-files"
1 change: 0 additions & 1 deletion hydra-cardano-api/hydra-cardano-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ library
Hydra.Cardano.Api.CtxUTxO
Hydra.Cardano.Api.ExecutionUnits
Hydra.Cardano.Api.Hash
Hydra.Cardano.Api.KeyWitness
Hydra.Cardano.Api.Network
Hydra.Cardano.Api.NetworkId
Hydra.Cardano.Api.PlutusScript
Expand Down
10 changes: 0 additions & 10 deletions hydra-cardano-api/src/Cardano/Api/UTxO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,10 @@ render :: (TxIn, TxOut ctx era) -> Text
render (k, TxOut _ (txOutValueToValue -> v) _ _) =
T.drop 54 (renderTxIn k) <> "" <> renderValue v

-- | Select the minimum (by TxIn) utxo entry from the UTxO map.
--
-- This function is partial.
min :: UTxO -> UTxO
min = UTxO . uncurry Map.singleton . Map.findMin . toMap

-- | Remove the right hand side from the left hand side.
difference :: UTxO' out -> UTxO' out -> UTxO' out
difference a b = UTxO $ Map.difference (toMap a) (toMap b)

-- | Infix version of 'difference'.
(\\) :: UTxO' out -> UTxO' out -> UTxO' out
a \\ b = difference a b

-- * Type Conversions

-- | Transforms a UTxO containing tx outs from any era into Babbage era.
Expand Down
1 change: 0 additions & 1 deletion hydra-cardano-api/src/Hydra/Cardano/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ import Hydra.Cardano.Api.CtxTx as Extras
import Hydra.Cardano.Api.CtxUTxO as Extras
import Hydra.Cardano.Api.ExecutionUnits as Extras
import Hydra.Cardano.Api.Hash as Extras
import Hydra.Cardano.Api.KeyWitness as Extras
import Hydra.Cardano.Api.NetworkId ()
import Hydra.Cardano.Api.PlutusScript as Extras
import Hydra.Cardano.Api.PolicyId as Extras
Expand Down
4 changes: 0 additions & 4 deletions hydra-cardano-api/src/Hydra/Cardano/Api/ExecutionUnits.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,3 @@ import Cardano.Ledger.Alonzo.Scripts qualified as Ledger
-- | Convert a cardano-api 'ExecutionUnits' into a cardano-ledger 'ExUnits'
toLedgerExUnits :: ExecutionUnits -> Ledger.ExUnits
toLedgerExUnits = toAlonzoExUnits

-- | Convert a cardano-ledger 'ExUnits' into a cardano-api 'ExecutionUnits'
fromLedgerExUnits :: Ledger.ExUnits -> ExecutionUnits
fromLedgerExUnits = fromAlonzoExUnits
17 changes: 0 additions & 17 deletions hydra-cardano-api/src/Hydra/Cardano/Api/Hash.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import Hydra.Cardano.Api.Prelude
import Cardano.Ledger.Keys qualified as Ledger
import Cardano.Ledger.Plutus.TxInfo (transKeyHash)
import Cardano.Ledger.SafeHash (unsafeMakeSafeHash)
import Cardano.Ledger.Shelley.Scripts qualified as Ledger
import Data.ByteString qualified as BS
import PlutusLedgerApi.V3 qualified as Plutus

Expand Down Expand Up @@ -33,22 +32,6 @@ unsafePaymentKeyHashFromBytes bytes
| otherwise =
PaymentKeyHash $ Ledger.KeyHash $ unsafeHashFromBytes bytes

-- | Unsafe wrap some bytes as a 'ScriptHash', relying on the fact that Plutus
-- is using Blake2b_224 for hashing data (according to 'cardano-ledger').
--
-- Pre-condition: the input bytestring MUST be of length 28.
unsafeScriptHashFromBytes ::
HasCallStack =>
ByteString ->
ScriptHash
unsafeScriptHashFromBytes bytes
| BS.length bytes /= 28 =
error $ "unsafeScriptHashFromBytes: pre-condition failed: " <> show (BS.length bytes) <> " bytes."
| otherwise =
fromShelleyScriptHash
. Ledger.ScriptHash
$ unsafeHashFromBytes bytes

-- | Unsafe wrap some bytes as a 'Hash ScriptData', relying on the fact that
-- Plutus is using Blake2b_256 for hashing data (according to 'cardano-ledger').
--
Expand Down
74 changes: 0 additions & 74 deletions hydra-cardano-api/src/Hydra/Cardano/Api/KeyWitness.hs

This file was deleted.

18 changes: 0 additions & 18 deletions hydra-cardano-api/src/Hydra/Cardano/Api/PlutusScript.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,12 @@ module Hydra.Cardano.Api.PlutusScript where

import Hydra.Cardano.Api.Prelude

import Cardano.Ledger.Alonzo.Scripts qualified as Ledger
import Cardano.Ledger.Plutus.Language qualified as Ledger
import Data.ByteString.Short qualified as SBS
import PlutusLedgerApi.Common qualified as Plutus
import Test.QuickCheck (listOf)

-- * Type Conversions

-- | Convert a cardano-ledger 'Script' into a cardano-api 'PlutusScript'
--
-- NOTE: This function is unsafe in two manners:
--
-- (a) If the given script is a timelock script, it throws an impure exception;
-- (b) If the given script is in a wrong language, it silently coerces it.
fromLedgerScript ::
( HasCallStack
, Ledger.AlonzoEraScript era
) =>
Ledger.AlonzoScript era ->
PlutusScript lang
fromLedgerScript = \case
Ledger.TimelockScript{} -> error "fromLedgerScript: TimelockScript"
Ledger.PlutusScript x -> Ledger.withPlutusScript x (\(Ledger.Plutus (Ledger.PlutusBinary bytes)) -> PlutusScriptSerialised bytes)

-- | Convert a serialized plutus script into a cardano-api 'PlutusScript'.
fromPlutusScript :: Plutus.SerialisedScript -> PlutusScript lang
fromPlutusScript =
Expand Down
4 changes: 0 additions & 4 deletions hydra-cardano-api/src/Hydra/Cardano/Api/PolicyId.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module Hydra.Cardano.Api.PolicyId where
import Hydra.Cardano.Api.Prelude

import Cardano.Ledger.Alonzo.Plutus.TxInfo qualified as Ledger
import Cardano.Ledger.Hashes qualified as Ledger
import Cardano.Ledger.Mary.Value qualified as Ledger
import Hydra.Cardano.Api.ScriptHash ()
import PlutusLedgerApi.V3 (CurrencySymbol, fromBuiltin, unCurrencySymbol)
Expand All @@ -17,9 +16,6 @@ instance Arbitrary PolicyId where

-- * Type conversions

toLedgerScriptHash :: PolicyId -> Ledger.ScriptHash StandardCrypto
toLedgerScriptHash (PolicyId scriptHash) = toShelleyScriptHash scriptHash

-- | Convert Cardano api 'PolicyId' to Cardano ledger `PolicyID`.
toLedgerPolicyID :: PolicyId -> Ledger.PolicyID StandardCrypto
toLedgerPolicyID (PolicyId sh) = Ledger.PolicyID (toShelleyScriptHash sh)
Expand Down
5 changes: 1 addition & 4 deletions hydra-cardano-api/src/Hydra/Cardano/Api/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Cardano.Ledger.SafeHash qualified as Ledger
import Data.Aeson qualified as Aeson
import Data.ByteString.Lazy qualified as BL
import Data.Function (on)
import Data.List (intercalate, sort, sortBy)
import Data.List (sort, sortBy)
import Data.Map.Strict qualified as Map
import Data.Text qualified as T
import GHC.IsList (IsList (..))
Expand All @@ -23,9 +23,6 @@ import Hydra.Cardano.Api.ScriptData (fromLedgerData)
renderTx :: Api.Tx -> String
renderTx = renderTxWithUTxO mempty

renderTxs :: [Api.Tx] -> String
renderTxs xs = intercalate "\n\n" (renderTx <$> xs)

-- | Like 'renderTx', but uses the given UTxO to resolve inputs.
renderTxWithUTxO :: UTxO -> Api.Tx -> String
renderTxWithUTxO utxo (Tx body _wits) =
Expand Down
29 changes: 1 addition & 28 deletions hydra-cardano-api/src/Hydra/Cardano/Api/ScriptData.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ module Hydra.Cardano.Api.ScriptData where

import Hydra.Cardano.Api.Prelude hiding (left)

import Cardano.Ledger.Alonzo.TxWits qualified as Ledger
import Cardano.Ledger.Era qualified as Ledger
import Cardano.Ledger.Plutus.Data qualified as Ledger
import Codec.Serialise (deserialiseOrFail, serialise)
import Control.Arrow (left)
import Data.Aeson (Value (String), withText)
import Data.ByteString qualified as BS
import Data.ByteString.Base16 qualified as Base16
import Data.Map qualified as Map
import PlutusLedgerApi.V3 qualified as Plutus
import Test.QuickCheck (arbitrarySizedNatural, choose, oneof, scale, sized, vector)

Expand All @@ -36,39 +34,14 @@ fromScriptData =
Plutus.fromData . toPlutusData . getScriptData

-- | Get the 'HashableScriptData' associated to the a 'TxOut'. Note that this
-- requires the 'CtxTx' context. To get script data in a 'CtxUTxO' context, see
-- 'lookupScriptData'.
-- requires the 'CtxTx' context.
txOutScriptData :: TxOut CtxTx era -> Maybe HashableScriptData
txOutScriptData (TxOut _ _ d _) =
case d of
TxOutDatumInTx _ sd -> Just sd
TxOutDatumInline _ sd -> Just sd
_ -> Nothing

-- | Lookup included datum of given 'TxOut'.
lookupScriptData ::
forall era.
( UsesStandardCrypto era
, Ledger.Era (ShelleyLedgerEra era)
) =>
Tx era ->
TxOut CtxUTxO era ->
Maybe HashableScriptData
lookupScriptData (Tx (ShelleyTxBody _ _ _ scriptsData _ _) _) (TxOut _ _ datum _) =
case datum of
TxOutDatumNone ->
Nothing
(TxOutDatumHash _ (ScriptDataHash h)) ->
fromLedgerData <$> Map.lookup h datums
(TxOutDatumInline _ dat) ->
Just dat
where
datums :: Map (Ledger.DataHash StandardCrypto) (Ledger.Data (ShelleyLedgerEra era))
datums =
case (scriptsData :: TxBodyScriptData era) of
TxBodyNoScriptData -> mempty
TxBodyScriptData _ (Ledger.TxDats m) _ -> m

-- * Type Conversions

-- | Convert a cardano-ledger script 'Data' into a cardano-api 'ScriptDatum'.
Expand Down
9 changes: 0 additions & 9 deletions hydra-cardano-api/src/Hydra/Cardano/Api/ScriptHash.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,10 @@ module Hydra.Cardano.Api.ScriptHash where

import Hydra.Cardano.Api.Prelude

import Cardano.Ledger.Credential qualified as Ledger
import Hydra.Cardano.Api.PlutusScript ()

-- * Extras

-- | Extract the payment part of an address, as a script hash.
getPaymentScriptHash :: AddressInEra era -> Maybe ScriptHash
getPaymentScriptHash = \case
AddressInEra _ (ShelleyAddress _ (Ledger.ScriptHashObj h) _) ->
Just (fromShelleyScriptHash h)
_ ->
Nothing

-- | Like 'hashScript', but for a 'ScriptInAnyLang'.
hashScriptInAnyLang :: ScriptInAnyLang -> ScriptHash
hashScriptInAnyLang (ScriptInAnyLang _ script) =
Expand Down
34 changes: 1 addition & 33 deletions hydra-cardano-api/src/Hydra/Cardano/Api/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,13 @@ where
import Hydra.Cardano.Api.Prelude

import Cardano.Api.UTxO qualified as UTxO
import Cardano.Ledger.Alonzo.TxWits qualified as Ledger
import Cardano.Ledger.Api (
EraTx (mkBasicTx),
bodyTxL,
datsTxWitsL,
getLanguageView,
inputsTxBodyL,
mkBasicTxBody,
rdmrsTxWitsL,
scriptIntegrityHashTxBodyL,
witsTxL,
)
import Cardano.Ledger.Api qualified as Ledger
import Cardano.Ledger.Babbage.Tx (hashScriptIntegrity)
import Cardano.Ledger.Coin (Coin (..))
import Cardano.Ledger.Plutus.Language qualified as Ledger
import Control.Lens ((&), (.~), (^.))
import Control.Lens ((&), (.~))
import Data.Bifunctor (bimap)
import Data.Functor ((<&>))
import Data.Set qualified as Set
Expand Down Expand Up @@ -68,12 +58,6 @@ utxoProducedByTx tx =
where
TxBody body = getTxBody tx

-- | Get explicit fees allocated to a transaction.
txFee' :: Tx era -> Coin
txFee' (getTxBody -> TxBody body) =
case txFee body of
TxFeeExplicit _ y -> y

-- * Type Conversions

-- | Convert a cardano-api 'Tx' into a matching cardano-ledger 'Tx'.
Expand All @@ -89,19 +73,3 @@ fromLedgerTx ::
Tx era
fromLedgerTx =
ShelleyTx shelleyBasedEra

-- | Compute the integrity hash of a transaction using a list of plutus languages.
recomputeIntegrityHash ::
(Ledger.AlonzoEraPParams ppera, Ledger.AlonzoEraTxWits txera, Ledger.AlonzoEraTxBody txera, EraTx txera) =>
Ledger.PParams ppera ->
[Ledger.Language] ->
Ledger.Tx txera ->
Ledger.Tx txera
recomputeIntegrityHash pp languages tx = do
tx & bodyTxL . scriptIntegrityHashTxBodyL .~ integrityHash
where
integrityHash =
hashScriptIntegrity
(Set.fromList $ getLanguageView pp <$> languages)
(tx ^. witsTxL . rdmrsTxWitsL)
(tx ^. witsTxL . datsTxWitsL)
Loading

0 comments on commit 9bb093a

Please sign in to comment.