Skip to content

Commit

Permalink
Remove cost of hashing from tx-cost
Browse files Browse the repository at this point in the history
The results are not so useful and have been historically recorded in our
reports and in #155
  • Loading branch information
ch1bo committed May 4, 2022
1 parent 4ee6f43 commit 52a2da3
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 101 deletions.
37 changes: 0 additions & 37 deletions hydra-node/exe/tx-cost/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import TxCost (
computeCollectComCost,
computeCommitCost,
computeFanOutCost,
computeHashingCost,
computeInitCost,
computeMerkleTreeCost,
maxCpu,
Expand Down Expand Up @@ -83,7 +82,6 @@ writeTransactionCostMarkdown hdl = do
abortC <- costOfAbort
fanout <- costOfFanOut
mt <- costOfMerkleTree
let h = costOfHashing
hPut hdl $
encodeUtf8 $
unlines $
Expand All @@ -97,7 +95,6 @@ writeTransactionCostMarkdown hdl = do
, abortC
, fanout
, mt
, h
]

-- NOTE: Github Actions CI depends on the number of header lines, see
Expand Down Expand Up @@ -289,37 +286,3 @@ costOfMerkleTree = markdownMerkleTreeCost <$> computeMerkleTreeCost
<> " |"
)
stats

costOfHashing :: Text
costOfHashing = markdownHashingCost computeHashingCost
where
markdownHashingCost stats =
unlines $
[ "## Cost of on-chain Hashing"
, ""
]
<> concatMap
( \(n, s, costs) ->
[ "### n = " <> show n <> ", s = " <> show s
, ""
, "| Algorithm | Cpu | Mem | Δcpu | Δmem |"
, "| :-------- | ---: | ---: | ---: | ---: |"
]
<> fmap
( \case
Right (algorithm, baseCpu, baseMem, cpu, mem) ->
"| " <> show algorithm
<> " | "
<> show baseCpu
<> " | "
<> show baseMem
<> " | "
<> show (toInteger cpu - toInteger baseCpu)
<> " | "
<> show (toInteger mem - toInteger baseMem)
<> " |"
Left _ -> "| - | - |"
)
costs
)
stats
64 changes: 0 additions & 64 deletions hydra-node/exe/tx-cost/TxCost.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE TypeApplications #-}

module TxCost where
Expand All @@ -16,26 +15,11 @@ import Data.Fixed (E2, Fixed)
import qualified Data.Map as Map
import Data.Maybe (fromJust)
import Hydra.Cardano.Api (
BuildTxWith (BuildTxWith),
ExecutionUnits (..),
NetworkId (Testnet),
NetworkMagic (NetworkMagic),
PlutusScriptV1,
UTxO,
fromLedgerExUnits,
fromLedgerValue,
fromPlutusScript,
lovelaceToValue,
mkScriptAddress,
mkScriptDatum,
mkScriptWitness,
mkTxOutDatum,
modifyTxOutValue,
scriptWitnessCtx,
toCtxUTxOTxOut,
toScriptData,
pattern ScriptWitness,
pattern TxOut,
)
import Hydra.Chain.Direct.Context (
HydraContext (ctxVerificationKeys),
Expand All @@ -58,22 +42,17 @@ import Hydra.Chain.Direct.State (
getKnownUTxO,
initialize,
)
import qualified Hydra.Contract.Hash as Hash
import Hydra.Ledger.Cardano (
adaOnly,
addInputs,
emptyTxBody,
genKeyPair,
genOneUTxOFor,
genTxIn,
simplifyUTxO,
unsafeBuildTransaction,
)
import Hydra.Ledger.Cardano.Evaluate (evaluateTx, pparams)
import Plutus.MerkleTree (rootHash)
import qualified Plutus.MerkleTree as MT
import Plutus.Orphans ()
import Plutus.V1.Ledger.Api (toBuiltin)
import qualified Plutus.V1.Ledger.Api as Plutus
import Test.Plutus.Validator (
ExUnits (ExUnits),
Expand Down Expand Up @@ -288,49 +267,6 @@ executionCostForBuilder utxo =
in evaluateScriptExecutionUnits mtBuilderValidator (utxo, root) <&> \case
ExUnits mem cpu -> (mem, cpu)

computeHashingCost :: [(Int, Int, [Either Text (Hash.HashAlgorithm, CpuUnit, MemUnit, CpuUnit, MemUnit)])]
computeHashingCost =
[0 .. 5] <&> \(power :: Integer) ->
let n = 8 ^ power
s = n `quot` 8
in ( n
, s
, [minBound .. maxBound] <&> \algorithm ->
costOfHashingFor n algorithm
)
where
costOfHashingFor n algorithm =
let ExecutionUnits
{ executionSteps = baseCpu
, executionMemory = baseMem
} = either (error . ("unexpected failure evaluating baseline " <>) . show) id $ calculateHashExUnits n Hash.Base
in calculateHashExUnits n algorithm <&> \ExecutionUnits{executionSteps = cpu, executionMemory = mem} ->
(algorithm, CpuUnit baseCpu, MemUnit baseMem, CpuUnit cpu, MemUnit mem)

calculateHashExUnits :: Int -> Hash.HashAlgorithm -> Either Text ExecutionUnits
calculateHashExUnits n algorithm =
case evaluateTx tx utxo of
Left basicFailure ->
Left ("Basic failure: " <> show basicFailure)
Right report ->
case Map.elems report of
[Right units] ->
Right $ fromLedgerExUnits units
_ ->
error $ "Too many redeemers in report: " <> show report
where
tx = unsafeBuildTransaction $ emptyTxBody & addInputs [(input, witness)]
utxo = UTxO.singleton (input, output)
input = generateWith arbitrary 42
output = toCtxUTxOTxOut $ TxOut address value (mkTxOutDatum datum)
value = lovelaceToValue 1_000_000
address = mkScriptAddress @PlutusScriptV1 networkId script
witness = BuildTxWith $ ScriptWitness scriptWitnessCtx $ mkScriptWitness script (mkScriptDatum datum) redeemer
script = fromPlutusScript @PlutusScriptV1 Hash.validatorScript
datum = Hash.datum $ toBuiltin bytes
redeemer = toScriptData $ Hash.redeemer algorithm
bytes = fold $ replicate n ("0" :: ByteString)

maxMem, maxCpu :: Fixed E2
Ledger.ExUnits (fromIntegral @_ @(Fixed E2) -> maxMem) (fromIntegral @_ @(Fixed E2) -> maxCpu) =
Ledger._maxTxExUnits pparams
Expand Down

0 comments on commit 52a2da3

Please sign in to comment.