paxos.js
function Paxos (now, id, options) {
    assert(arguments.length == 3)
    // Uniquely identify ourselves relative to the other participants.
    this.id = String(id)

    // Use the create time as a cookie to identify this instance of this id.
    this.cookie = null

    // Maximum size of a parliament. The government will grow to this size.
    this.parliamentSize = coalesce(options.parliamentSize, 5)

    // The atomic log is a linked list. When head of the list is advanced the
    // entries in the list become unreachable and can be collected by the
    // garbage collector. We advance the head of the list when we are certain
    // that all participants have received a copy of the entry and added it to
    // their logs. Outstanding user iterators prevent garbage collection.
    this.log = new Window
    this.log.addListener(this.indexer = new Indexer(function (left, right) {
        assert(left && right)
        assert(left.body && right.body)
        return Monotonic.compare(left.body.promise, right.body.promise)
    }))
    this.pinged = new Procession

    // Implements a calendar for events that we can check during runtime or
    // ignore during debugging playback.
    this.scheduler = new Scheduler

    // Initial government. A null government.
    this.government = {
        republic: null,
        promise: '0/0',
        majority: [],
        minority: [],
        acclimated: [],
        constituents: [],
        properties: {},
        arrived: { id: {}, promise: {} }
    }
    // Keep track of governments for bootstrapping.
    this._governments = [ null ]

    // Last promise issued.
    this._promised = null

    // Ping is the frequency of keep alive pings.
    this.ping = coalesce(options.ping, 1000)

    // Timeout when that reached marks a islander for departure.
    this.timeout = coalesce(options.timeout, 5000)

    // The islanders that this islander updates with new log entries.
    this.constituency = []

    // Entire population.
    this.population = []

    // Upper bound of the atomic log.
    this._committed = {}

    // Propagated lower bound of the atomic log.
    this._minimum = { propagated: '0/0', version: '0/0', reduced: '0/0' }
    this._minimums = {}
    this._minimums[this.id] = this._minimum

    this._acclimating = {}

    // Network message queue.
    this.outbox = new Procession

    // Push the null government onto the atomic log.
    this.log.push({
        module: 'paxos',
        method: 'government',
        promise: '0/0',
        body: this.government,
        previous: null
    })

    // Write strategy is polymorphic, changes based on whether we're recovering
    // from collapse using Paxos or writing values using two-phase commit.
    this._writer = new Writer(this, '1/0', [])
    this._recorder = new Recorder(this, this.log.head.body)

    // Shape a new government by fiat based on whose available to grow to the
    // desired government size and who is unreachable.
    this._shaper = new Shaper(this.parliamentSize, this.government, false)

    // Used for our pseudo-random number generator to vary retry times.
    this._seed = 1

    // Track unreachable islanders.
    this._disappeared = {}
    this._unreachable = {}
}
function Paxos (now, id, options) {
    assert(arguments.length == 3)
    // Uniquely identify ourselves relative to the other participants.
    this.id = String(id)

    // Use the create time as a cookie to identify this instance of this id.
    this.cookie = null

    // Maximum size of a parliament. The government will grow to this size.
    this.parliamentSize = coalesce(options.parliamentSize, 5)

    // The atomic log is a linked list. When head of the list is advanced the
    // entries in the list become unreachable and can be collected by the
    // garbage collector. We advance the head of the list when we are certain
    // that all participants have received a copy of the entry and added it to
    // their logs. Outstanding user iterators prevent garbage collection.
    this.log = new Window
    this.log.addListener(this.indexer = new Indexer(function (left, right) {
        assert(left && right)
        assert(left.body && right.body)
        return Monotonic.compare(left.body.promise, right.body.promise)
    }))
    this.pinged = new Procession

    // Implements a calendar for events that we can check during runtime or
    // ignore during debugging playback.
    this.scheduler = new Scheduler

    // Initial government. A null government.
    this.government = {
        republic: null,
        promise: '0/0',
        majority: [],
        minority: [],
        acclimated: [],
        constituents: [],
        properties: {},
        arrived: { id: {}, promise: {} }
    }
    // Keep track of governments for bootstrapping.
    this._governments = [ null ]

    // Last promise issued.
    this._promised = null

    // Ping is the frequency of keep alive pings.
    this.ping = coalesce(options.ping, 1000)

    // Timeout when that reached marks a islander for departure.
    this.timeout = coalesce(options.timeout, 5000)

    // The islanders that this islander updates with new log entries.
    this.constituency = []

    // Entire population.
    this.population = []

    // Upper bound of the atomic log.
    this._committed = {}

    // Propagated lower bound of the atomic log.
    this._minimum = { propagated: '0/0', version: '0/0', reduced: '0/0' }
    this._minimums = {}
    this._minimums[this.id] = this._minimum

    this._acclimating = {}

    // Network message queue.
    this.outbox = new Procession

    // Push the null government onto the atomic log.
    this.log.push({
        module: 'paxos',
        method: 'government',
        promise: '0/0',
        body: this.government,
        previous: null
    })

    // Write strategy is polymorphic, changes based on whether we're recovering
    // from collapse using Paxos or writing values using two-phase commit.
    this._writer = new Writer(this, '1/0', [])
    this._recorder = new Recorder(this, this.log.head.body)

    // Shape a new government by fiat based on whose available to grow to the
    // desired government size and who is unreachable.
    this._shaper = new Shaper(this.parliamentSize, this.government, false)

    // Used for our pseudo-random number generator to vary retry times.
    this._seed = 1

    // Track unreachable islanders.
    this._disappeared = {}
    this._unreachable = {}
}
### Constructor