consensus-test
Safe HaskellSafe-Inferred
LanguageHaskell2010

Test.Consensus.PointSchedule.SinglePeer.Indices

Description

Schedule generators for a single peer

These functions generate schedules for the tip points, the header points and the block points of a single peer. All of them are expressed in terms of block indices rather than actual block points.

The tip points are to be generated first with either of singleJumpTipPoints or rollbacksTipPoints. Then the tip points can be assigned times at which to announce them with tipPointSchedule. Then, the header points can be generated with headerPointSchedule. Finally, the block points can be generated with headerPointSchedule as well. See the implementation of singleJumpPeerSchedule for an example.

Synopsis

Documentation

data HeaderPointSchedule Source #

Constructors

HeaderPointSchedule 

Fields

  • hpsTrunk ∷ [(Time, Int)]

    header points up to the intersection

  • hpsBranch ∷ [(Time, Int)]

    header points after the intersection indices are relative to the branch

headerPointSchedule ∷ ∀ g m. (HasCallStack, StatefulGen g m) ⇒ g → (DiffTime, DiffTime) → [(Maybe Int, [(Time, Int)])] → m [HeaderPointSchedule] Source #

headerPointSchedule g msgDelayInterval tpSchedule generates a schedule of header points for a single peer.

msgDelayInterval is the interval from which to sample the delay of each header after offering the previous header.

tpSchedule is the tip point schedule for the peer. Tip points are grouped by the branch they point to. Each group has the index of the intersection block with the trunk. Then each group has a list of tip point block indices relative to the branch. Groups corresponding to tip points in trunk use Nothing as the intersection.

For each group of tip points, the schedule generates a HeaderPointSchedule, which provides the time at which each header should be offered.

If scheduled, header points are guaranteed to be scheduled after the tip point that enables them. They might not be generated if they cannot be delivered before a tip point of a different branch is announced. The header points on a same chain are never announced out of order.

headerPointSchedule
  :: g
  -> {msgDelayInterval:(DiffTime, DiffTime)
     | fst msgDelayInterval <= snd msgDelayInterval
     }
  -> {tpSchedule:[(Maybe Int, [(DiffTime, Int)]]
     | isSorted (catMaybes (map fst tpSchedule)) &&
       all (\(_, xs) -> isSorted xs) tpSchedule &&
       all (\(_, xs) -> all (0<=) (map snd xs)) tpSchedule &&
       all (\(_, xs) -> not (hasDuplicates (map snd xs))) tpSchedule &&
       all (\(_, xs) -> not (null xs)) tpSchedule &&
       all (\(_, xs) -> all (0<=) (map snd xs)) tpSchedule
     }
  -> m {v:[HeaderPointSchedule]
       | length v == length tpSchedule &&
         isSorted [ map fst (hpsTrunk hps) ++ map fst (hpsBranch hps) | hps <- v ] &&
         isSorted [ map snd (hpsTrunk hps) | hps <- v ] &&
         all (\hps -> isSorted (map snd $ hpsBranch hps)) v &&
         all (\hps -> all (0<=) (map snd (hpsTrunk hps))) v &&
         all (\hps -> all (0<=) (map snd (hpsBranch hps))) v &&
         all (\hps -> not (hasDuplicates (map snd (hpsTrunk hps)))) v &&
         all (\hps -> not (hasDuplicates (map snd (hpsBranch hps)))) v
       }

rollbacksTipPointsStatefulGen g m ⇒ g → Int → [Int] → m [[Int]] Source #

rollbacksTipPoints k bs g generates a schedule for a single peer serving from multiple alternative branches. The schedule is a list of block indices for the tip point of the peer state. Here the block indices are separated per branch in the result. Each index is relative to the branch it belongs to.

k is the security parameter.

bs are the length in blocks of the alternative branches of the block tree. The lengths need to be provided in the order in which the branches intersect with the trunk.

rollbacksTipPoints
  :: g
  -> {k:Int | k > 0}
  -> {bs:[Int] | all (0<=) bs}
  -> m {v:[Int]
       | isSorted v &&
         all (all (0<=)) v &&
         all (all (<k)) v &&
         all isSorted v &&
         all (not . hasDuplicates) v &&
         and [all (<bn) bbs | (bn, bbs) <- zip bs v] &&
         length v == length bs bracketChainSyncClient
       }

singleJumpTipPointsStatefulGen g m ⇒ g → IntInt → m [Int] Source #

singleJumpTipPoints g m n generates a list of tip points for a single peer serving a single branch between block indices m and n. The schedule is a list of block indices for the tip point of the peer state.

The first tip jumps to a block in the middle of the index range, and then updates the tip one block at a time.

singleJumpTipPoints
  :: g
  -> {m:Int | m >= 0}
  -> {n:Int | n >= 0}
  -> m {v:[Int]
       | isSorted v &&
         all (m<=) v &&
         all (<=n) v &&
         not (hasDuplicates v)
       }

tipPointSchedule ∷ ∀ g m. StatefulGen g m ⇒ g → DiffTime → (DiffTime, DiffTime) → [SlotNo] → m [Time] Source #

tipPointSchedule g slotLengh msgDelayInterval slots attaches times to a sequence of tip points. These times are the times at which the tip points should be offered to the node under test. Times are expressed as an offset from the time of slot 0.

slotLength is the length of a slot in seconds.

msgDelayInterval is the interval from which to sample the delay of each tip point after the slot in which it was minted.

slots are the slot numbers of the blocks in the tip point schedule. Because the slots might belong to different branches, they might be duplicated or not monotonically increasing. e.g.

If 0s and 1s signal the tip points that we want to announce

slot number: 0123456
trunk  :     011001
alternative:   01101

The slots of the tip points to serve could be [1, 2, 5, 3, 4, 6] Then the generated times could be close to

[1*20, 2*20, 5*20, t3, t4, 6*20]

where t3 and t4 are chosen randomly in the interval between the branch tips, that is between 5*20 and 6*20.

tipPointSchedule
  :: g
  -> {slotLength:DiffTime | slotLength >= 0}
  -> {msgDelayInterval:(DiffTime, DiffTime)
     | fst msgDelayInterval <= snd msgDelayInterval
     }
  -> {slots:[SlotNo] | all (0<=) slots}
  -> m {v:[DiffTime] | isSorted v && length v == length slots}

uniformRMDiffTimeStatefulGen g m ⇒ (DiffTime, DiffTime) → g → m DiffTime Source #

Uniformely choose a relative DiffTime in the given range.