Skip to content

Commit

Permalink
Improve performance of translating an address in TxInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
lehins committed Jan 27, 2022
1 parent e0ae6bb commit 540a566
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions eras/alonzo/impl/src/Cardano/Ledger/Alonzo/TxInfo.hs
Expand Up @@ -24,7 +24,7 @@ import Cardano.Ledger.Coin (Coin (..))
import Cardano.Ledger.Core as Core (PParams, TxBody, TxOut, Value)
import Cardano.Ledger.Credential (Credential (KeyHashObj, ScriptHashObj), Ptr (..), StakeReference (..))
import qualified Cardano.Ledger.Crypto as CC (Crypto)
import Cardano.Ledger.Era (Era (..))
import Cardano.Ledger.Era (Era (..), getTxOutBootstrapAddress)
import Cardano.Ledger.Hashes (EraIndependentData)
import Cardano.Ledger.Keys (KeyHash (..), KeyRole (Witness), hashKey)
import qualified Cardano.Ledger.Mary.Value as Mary (AssetName (..), PolicyID (..), Value (..))
Expand Down Expand Up @@ -131,6 +131,12 @@ transAddr :: Addr crypto -> Maybe PV1.Address
transAddr (Addr _net object stake) = Just (PV1.Address (transCred object) (transStakeReference stake))
transAddr (AddrBootstrap _bootaddr) = Nothing

transTxOutAddr :: Era era => TxOut era -> Maybe PV1.Address
transTxOutAddr txOut = do
-- filter out Byron addresses without uncompacting them
Nothing <- Just (getTxOutBootstrapAddress txOut)
transAddr $ getTxOutAddr txOut

slotToPOSIXTime ::
(Monad m, HasField "_protocolVersion" (PParams era) ProtVer) =>
Core.PParams era ->
Expand All @@ -147,7 +153,7 @@ slotToPOSIXTime pp ei sysS s = do
then
truncate
-- Convert to milliseconds
. (* fromInteger 1000)
. (* 1000)
else resolution

-- | translate a validity interval to POSIX time
Expand Down Expand Up @@ -193,12 +199,11 @@ txInfoIn ::
txInfoIn (UTxO mp) txin =
case SplitMap.lookup txin mp of
Nothing -> Nothing
Just txout -> case transAddr addr of
Just txout -> case transTxOutAddr txout of
Just ad -> Just (PV1.TxInInfo (txInfoIn' txin) (PV1.TxOut ad valout dhash))
Nothing -> Nothing
where
valout = transValue (getField @"value" txout)
addr = getTxOutAddr txout
dhash = case getField @"datahash" txout of
SNothing -> Nothing
SJust safehash -> Just (PV1.DatumHash (transSafeHash safehash))
Expand All @@ -214,9 +219,10 @@ txInfoOut ::
) =>
Core.TxOut era ->
Maybe PV1.TxOut
txInfoOut txo =
let (addr, val, datahash) = (getTxOutAddr txo, getField @"value" txo, getField @"datahash" txo)
in case transAddr addr of
txInfoOut txout =
let val = getField @"value" txout
datahash = getField @"datahash" txout
in case transTxOutAddr txout of
Just ad -> Just (PV1.TxOut ad (transValue @(Crypto era) val) (transDataHash datahash))
Nothing -> Nothing

Expand Down

0 comments on commit 540a566

Please sign in to comment.