Proof-of-Stake Consensus

Models a multi-node proof-of-stake consensus protocol with validator selection and block creation.

Run#

Shell
saple analyze examples/consensus.spl

The system declares the property noDoubleBlock and no patterns; the report ends with No vulnerabilities detected.

Known limitation
In expressions, Node(i).state is read as Node.state: the instance argument is dropped (see Expressions). Guards and properties of the model below that select an instance this way do not distinguish the instances today.

Complete model#

examples/consensus.spl, as shipped:

SPL consensus.spl
// Proof-of-Stake Consensus Protocol — Full SPL Model
//
// Ten nodes take turns: in each time slot one idle node is selected as the
// validator, a block is created, and the block is sent to the network.
// The model checks that the block counter never becomes negative.
//
// What the analysis should find: nothing. The system declares one property
// (noDoubleBlock) and no patterns; `saple analyze` reports
// "No vulnerabilities detected."

// Lifecycle of a node: idle, currently validating, or blocked.
type NodeState = { IDLE, VALIDATING, BLOCKED };

// ─── Transitions ───

// Pick the validator for a time slot: enabled while some node is idle;
// the node at index `slot` becomes the validator.
transition selectValidator(slot : Int) {
    guard:  exists (i : Int) in [0..numNodes) .
              Node(i).state == IDLE ;
    effect: Node(slot).state := VALIDATING ;
}

// A validator creates the next block: the chain height grows by one.
transition createBlock() {
    guard:  exists (i : Int) in [0..numNodes) . Node(i).isValidator == true ;
    effect: BlockNumber := BlockNumber + 1 ;
}

// A node that created a block broadcasts it; the protocol moves to the next slot.
transition sendBlock() {
    guard:  exists (i : Int) in [0..numNodes) . Node(i).blockCreated > 0 ;
    effect: timeSlot := timeSlot + 1 ;
}

// ─── Agents ───

// One consensus participant.
agent Node(id : Int) {
    attributes {
        isValidator : Bool = false ;     // selected as validator in this slot
        blockCreated : Int = 0 ;         // blocks this node has created
        stake : Real = 100.0 ;           // stake that weighs validator selection
        state : NodeState = IDLE ;       // lifecycle state
        isMalicious : Bool = false ;     // reserved for adversarial variants
    }
    process {
        // B0: be selected as validator for the current slot, or stop.
        B0 = selectValidator(timeSlot) . B1 + done ;
        // B1: create and send a block, then wait for the next slot.
        B1 = createBlock . sendBlock . B0 ;
    }
}

// ─── Environment ───

// Shared protocol state.
schema ConsensusEnv {
    attributes {
        BlockNumber : Int = 0 ;   // height of the chain
        timeSlot : Int = 0 ;      // current slot
        numNodes : Int = 10 ;     // number of participants
    }
    invariants {
        // Counters only grow.
        BlockNumber >= 0 ;
        timeSlot >= 0 ;
    }
}

// ─── System ───

// Ten nodes running the protocol over the shared environment.
system PoSConsensus {
    schema: ConsensusEnv ;
    agents {
        Node(i) * 10 ;
    }
    init {
        numNodes == 10 ;
    }
    properties {
        // The chain height never becomes negative.
        noDoubleBlock : BlockNumber >= 0 ;
    }
}
↑ ↓ to moveEnter to openEsc to close