Causal Delivery Check

easy · distributed-systems, clocks, ordering, causality

Causal Delivery Check

You are writing the gate that decides whether an incoming message can be delivered now or must stay buffered.

Given receiver vector clock local, message vector clock msg, and sender index s, return whether the message is causally deliverable.

Deliverability conditions:

  • msg[s] == local[s] + 1 (exact next message from sender)
  • For all i != s, msg[i] <= local[i] (all dependencies already seen)

Function signature

func IsCausallyDeliverable(local []int, msg []int, sender int) bool

Example

local  = [2,0,1]
msg    = [2,1,1]
sender = 1
output = true

Implementation blueprint (near-solution)

  1. Check sender rule first:
    • if msg[sender] != local[sender] + 1, return false.
  2. Scan all indices except sender:
    • if any msg[i] > local[i], return false.
  3. Otherwise return true.

Reference pseudocode:

if msg[s] != local[s] + 1: return false
for i in [0..n-1], i != s:
  if msg[i] > local[i]: return false
return true

Why this is correct

  • Sender equality enforces no gaps or duplicates in sender stream.
  • Non-sender <= enforces all causal prerequisites are already delivered.

Constraints

  • len(local) == len(msg)
  • 1 <= len(local) <= 100000
  • 0 <= sender < len(local)
  • 0 <= local[i], msg[i]

Common mistakes

  • Using >= for sender comparison.
  • Applying same rule to sender and non-sender components.
  • Mutating local in this check-only function.
Run tests to see results
No issues detected
    Join Discord