Git Product home page Git Product logo

Comments (4)

CodiePP avatar CodiePP commented on June 12, 2024

definition of chain quality in CSL: https://github.com/input-output-hk/cardano-sl/blob/develop/db/src/Pos/DB/Block/Logic/Util.hs#L93
(?)

from cardano-node.

mrBliss avatar mrBliss commented on June 12, 2024

Some pointers on how to implement this:

The traced event to watch for is ChainDB.SwitchedToChain, which is part of ChainDB.TraceAddBlockEvent

You can use this snippet to calculate the chain density (not quality, which is a different concept in the Ouroboros paper, which we can't measure):

import Ouroboros.Network.Point (fromWithOrigin)

chainDensity :: HasHeader blk
             => AnchoredFragment (Header blk) -> Maybe Rational
chainDensity frag
    | slots == 0
    = Nothing
    | otherwise
    = Just $ toRational blocks / toRational slots
  where
    -- Slot of the tip - slot @k@ blocks back. Use 0 as the slot for genesis
    slots  = unSlotNo (fromWithOrigin 0 (AF.headSlot frag))
           - unSlotNo (fromWithOrigin 0 (pointSlot (AF.anchorPoint frag)))
    -- Includes EBBs
    blocks = AF.length frag

from cardano-node.

mrBliss avatar mrBliss commented on June 12, 2024

Update:

import Ouroboros.Network.Point (fromWithOrigin)

chainDensity :: HasHeader blk
             => AnchoredFragment (Header blk) -> Maybe Rational
chainDensity frag
    | slots == 0
    = Nothing
    | otherwise
    = Just $ toRational blocks / toRational slots
  where
    -- Slot of the tip - slot @k@ blocks back. Use 0 as the slot for genesis
    slots  = unSlotNo (fromWithOrigin 0 (AF.headSlot frag))
           - unSlotNo (fromWithOrigin 0 (AF.lastSlot frag))
    blocks = unBlockNo (fromMaybe 0 (AF.headBlockNo frag))
           - unBlockNo (either (const 0) blockNo (AF.last frag))

(I did not typecheck this)

from cardano-node.

mrBliss avatar mrBliss commented on June 12, 2024

My findings:

After three blocks we have the following fragment:

block:        E R R
slot number:  0 0 1
block number: 0 1 2

where E = EBB and R = Regular block.

So using the code snippet above: slots = 1 - 0 = 1, blocks = 2 - 0 = 2, hence chain density = 2 / 1 = 2.0 😕

The reason for this is the genesis EBB with block number 0, which should not contribute to the total number of blocks. Looking at the next EBBs, we see:

blocks:       E R R ...     R |     E     R     R ...     R | E         R     R
slot number:  0 0 1 ... 21599 | 21600 21600 21601 ... 43199 | 43200 43200 43201
block number: 0 1 2 ... 21586 | 21586 21587 21588 ... 43175 | 43175 43176 43177

So EBBs have the block number of their predecessor, or 0 in case of the genesis EBB, but the slot number of their successor 😖. Fortunately, the chain density is only confused by the genesis EBB. So we can special case the genesis EBB:

    ... -- unchanged
    -- Block numbers start at 1. We ignore the genesis EBB, which has block number 0.
    blocks = unBlockNo (fromMaybe 1 (AF.headBlockNo frag))
           - case unBlockNo . blockNo <$> AF.last frag of
               -- Empty fragment, no blocks. We have that @blocks = 1 - 1 = 0@
               Left _ -> 1
               -- The oldest block is the genesis EBB with block number 0,
               -- don't let it contribute to the number of blocks
               Right 0 -> 1
               Right b -> b

(not typechecked)

from cardano-node.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.