Skip to content

Commit

Permalink
TOSQUASH extract txout sizes too
Browse files Browse the repository at this point in the history
  • Loading branch information
nfrisby committed Jan 18, 2022
1 parent 601daee commit 728b4db
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 27 deletions.
4 changes: 2 additions & 2 deletions ouroboros-consensus-cardano/tools/db-analyser/Analysis.hs
Expand Up @@ -210,11 +210,11 @@ instance HasAnalysis blk => Show (TraceEvent blk) where
: show (unSlotNo sn)
: show count
: show (length consumed)
: show (sum $ map HasAnalysis.txOutputIdsCount created)
: show (sum $ map (length . HasAnalysis.txOutputIdsSizes) created)
: (map show consumed <> map show created)
show (ExtractGenesisTxOutputIdsEvent count created) = intercalate "\t" $
show count
: show (sum $ map HasAnalysis.txOutputIdsCount created)
: show (sum $ map (length . HasAnalysis.txOutputIdsSizes) created)
: map show created
show (HeaderSizeEvent bn sn headerSize) = intercalate "\t" $ [
show bn
Expand Down
28 changes: 17 additions & 11 deletions ouroboros-consensus-cardano/tools/db-analyser/Block/Byron.hs
Expand Up @@ -13,13 +13,14 @@ module Block.Byron (

import Control.Monad.Except
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BL
import Data.Foldable (asum, toList)
import qualified Data.Map as Map
import Data.Maybe (mapMaybe)
import Options.Applicative

import Cardano.Binary (Raw, unAnnotated)
import Cardano.Binary (Raw, serialize', unAnnotated)

import Cardano.Crypto (RequiresNetworkMagic (..))
import qualified Cardano.Crypto as Crypto
Expand All @@ -44,20 +45,22 @@ instance HasAnalysis ByronBlock where
countTxOutputs = aBlockOrBoundary (const 0) countTxOutputsByron
extractTxOutputIdDelta = aBlockOrBoundary (const (IsEBB, 0, [], [])) extractTxOutputIdDeltaByron
genesisTxOutputIds st =
(Map.size maxes, txOutputIds)
(Map.size txs, txOutputIds)
where
utxo :: Chain.UTxO
utxo = Chain.cvsUtxo $ Byron.byronLedgerState st

txins :: [TxIn]
txins =
map (cnvTxIn . Chain.fromCompactTxIn) $ Map.keys $ Chain.unUTxO utxo

-- the count is one greater than the maximum index
maxes = Map.fromListWith max $ map (\(TxIn h i) -> (h, i + 1)) txins
txs =
Map.fromListWith (Map.unionWith (error "collision!"))
$ [ (h, Map.singleton i $ toEnum $ BS.length $ serialize' txout)
| (txin, txout) <- Map.toList $ Chain.unUTxO utxo
, let TxIn h i = cnvTxIn $ Chain.fromCompactTxIn txin
]

txOutputIds :: [TxOutputIds]
txOutputIds = map (uncurry TxOutputIds) $ Map.toList maxes
txOutputIds =
map (\(h, m) -> TxOutputIds h (Map.elems m))
$ Map.toList txs
blockTxSizes = aBlockOrBoundary (const []) blockTxSizesByron
knownEBBs = const Byron.knownEBBs
emitTraces _ = []
Expand Down Expand Up @@ -148,10 +151,13 @@ extractTxOutputIdDeltaByron Chain.ABlock{..} =

outputs :: Chain.ATxAux ByteString -> Maybe TxOutputIds
outputs atx =
mkTxOutputIds (Crypto.abstractHashToShort txid) n
mkTxOutputIds (Crypto.abstractHashToShort txid) lens
where
txid = Crypto.hashDecoded $ Chain.aTaTx $ atx
n = length $ Chain.txOutputs $ unAnnotated $ Chain.aTaTx atx
lens =
map (toEnum . BS.length . serialize')
$ toList
$ Chain.txOutputs $ unAnnotated $ Chain.aTaTx atx

cnvTxIn :: Chain.TxIn -> TxIn
cnvTxIn (Chain.TxInUtxo txid nat) =
Expand Down
19 changes: 12 additions & 7 deletions ouroboros-consensus-cardano/tools/db-analyser/Block/Shelley.hs
Expand Up @@ -15,6 +15,7 @@ module Block.Shelley (
) where

import qualified Data.Aeson as Aeson
import qualified Data.ByteString as BS
import qualified Data.ByteString.Short as Short
import Data.Foldable (asum, toList)
import qualified Data.Map.Strict as Map
Expand All @@ -25,6 +26,8 @@ import qualified Data.Set as Set
import GHC.Records (HasField, getField)
import Options.Applicative

import Cardano.Binary (serialize')

import qualified Cardano.Ledger.Core as Core
import qualified Cardano.Ledger.TxIn as Core
import qualified Cardano.Ledger.Era as CL
Expand Down Expand Up @@ -64,7 +67,7 @@ class ActualTxOutputIdDelta era where
-- | Which UTxO this transaction consumed
consumedTxIn :: Core.Tx era -> Set.Set (SL.TxIn (CL.Crypto era))
-- | How many UTxO this transaction created
createdTxOutputIds :: Core.Tx era -> Int
createdTxOutputIds :: Core.Tx era -> [Int]

preAlonzo_consumedTxIn ::
( ShelleyBasedEra era
Expand All @@ -74,8 +77,11 @@ preAlonzo_consumedTxIn = getField @"inputs" . getField @"body"

preAlonzo_createdTxOutputIds ::
ShelleyBasedEra era
=> Core.Tx era -> Int
preAlonzo_createdTxOutputIds = length . getField @"outputs" . getField @"body"
=> Core.Tx era -> [Int]
preAlonzo_createdTxOutputIds =
map getSize . toList . getField @"outputs" . getField @"body"
where
getSize = BS.length . serialize'

instance SL.PraosCrypto c => ActualTxOutputIdDelta (ShelleyEra c) where
consumedTxIn = preAlonzo_consumedTxIn
Expand All @@ -98,7 +104,7 @@ instance SL.PraosCrypto c => ActualTxOutputIdDelta (AlonzoEra c) where
then preAlonzo_consumedTxIn vtx
else getField @"collateral" (getField @"body" vtx)
createdTxOutputIds vtx =
if not (isValidTx vtx) then 0 else preAlonzo_createdTxOutputIds vtx
if not (isValidTx vtx) then [] else preAlonzo_createdTxOutputIds vtx

{-------------------------------------------------------------------------------
HasAnalysis instance
Expand Down Expand Up @@ -138,10 +144,9 @@ instance ( ShelleyBasedEra era

outputs :: Core.Tx era -> Maybe TxOutputIds
outputs tx =
mkTxOutputIds (asShort txid) n
mkTxOutputIds (asShort txid) (createdTxOutputIds tx)
where
txid = Core.txid $ getField @"body" tx
n = createdTxOutputIds tx
txid = Core.txid $ getField @"body" tx

-- TODO this is dead-code for a Cardano chain, but inaccurate for a pure
-- Shelley chain
Expand Down
17 changes: 10 additions & 7 deletions ouroboros-consensus-cardano/tools/db-analyser/HasAnalysis.hs
Expand Up @@ -13,7 +13,7 @@ import Data.ByteString.Short (ShortByteString)
import Data.ByteString.Short.Base64 (encodeBase64)
import Data.Map.Strict (Map)
import qualified Data.Text.Short as TextShort
import Data.Word (Word32)
import Data.Word (Word32, Word64)
import Options.Applicative

import Ouroboros.Consensus.Block
Expand Down Expand Up @@ -56,24 +56,27 @@ instance Show TxIn where
data TxOutputIds = TxOutputIds {
txOutputIdsTxId :: !TxIdBytes
-- ^ the transaction id that created some outputs
, txOutputIdsCount :: !Word32
-- ^ how many outputs it created
, txOutputIdsSizes :: [Word64]
-- ^ the serialization size of each tx output it created
--
-- INVARIANT: >0
}

mkTxOutputIds :: TxIdBytes -> Int -> Maybe TxOutputIds
mkTxOutputIds txid n =
if 0 >= n then Nothing else Just $ TxOutputIds txid (toEnum n)
mkTxOutputIds :: TxIdBytes -> [Int] -> Maybe TxOutputIds
mkTxOutputIds txid sizes =
if null sizes then Nothing else Just $ TxOutputIds txid (map toEnum sizes)

instance Show TxOutputIds where
show (TxOutputIds bs i) = showTxIdBytes bs <> "#" <> show i
show (TxOutputIds bs sizes) =
showTxIdBytes bs <> "#" <> show (length sizes)
<> concatMap ((',':) . show) sizes

class (HasAnnTip blk, GetPrevHash blk) => HasAnalysis blk where

countTxOutputs :: blk -> Int
-- | Is it an EBB, how many transactions, which txins consumed, which txouts created
extractTxOutputIdDelta :: blk -> (IsEBB, Int, [TxIn], [TxOutputIds])
-- | How many transactions, which txouts created
genesisTxOutputIds :: LedgerState blk -> (Int, [TxOutputIds])
blockTxSizes :: blk -> [SizeInBytes]
knownEBBs :: proxy blk -> Map (HeaderHash blk) (ChainHash blk)
Expand Down

0 comments on commit 728b4db

Please sign in to comment.