Skip to content

Commit

Permalink
renaming add! to init!
Browse files Browse the repository at this point in the history
  • Loading branch information
Janis Erdmanis committed Mar 28, 2024
1 parent c7fd5ae commit ffc2daa
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 74 deletions.
94 changes: 41 additions & 53 deletions src/Server/Controllers/ballotbox.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import ..Core.Model: uuid, voters, seed, receipt, tally, tallyview, istallied, i
Represents a ballot box for a proposal. Contains `proposal`, a set of eligiable `voters` a `collector` who collects the votes and a `seed` which is selected at random when the voting starts. `queue` contains a list of valid votes which yet to be comitted to a `ledger`. A history `tree` is built on leafs of ledger's receipts (see a [`receipt`](@ref) method). A `commit` contains a collector seal on the current ballotbox state.
**Interface:** [`reset_tree!`](@ref), [`generator`](@ref), [`uuid`](@ref), [`members`](@ref), [`ledger`](@ref), [`spine`](@ref), [`index`](@ref), [`seed`](@ref), [`leaf`](@ref), [`root`](@ref), [`record`](@ref), [`receipt`](@ref), [`commit`](@ref), [`tally`](@ref), [`set_seed!`](@ref), [`ack_leaf`](@ref), [`ack_root`](@ref), [`ack_cast`](@ref), [`commit_index`](@ref), [`commit_state`](@ref), [`push!`](@ref), [`state`](@ref), [`validate`](@ref), [`record!`](@ref), [`commit!`](@ref)
**Interface:** [`reset_tree!`](@ref), [`generator`](@ref), [`uuid`](@ref), [`members`](@ref), [`ledger`](@ref), [`spine`](@ref), [`index`](@ref), [`seed`](@ref), [`leaf`](@ref), [`root`](@ref), [`receipt`](@ref), [`commit`](@ref), [`tally`](@ref), [`set_seed!`](@ref), [`ack_leaf`](@ref), [`ack_root`](@ref), [`ack_cast`](@ref), [`commit_index`](@ref), [`commit_state`](@ref), [`push!`](@ref), [`state`](@ref), [`validate`](@ref), [`record!`](@ref), [`commit!`](@ref)
"""
mutable struct BallotBoxController
ledger::BallotBoxLedger
Expand Down Expand Up @@ -156,11 +156,12 @@ root(ballotbox::BallotBoxController, N::Int) = root(ballotbox.tree, N)
root(ballotbox::BallotBoxController) = root(ballotbox.tree)

"""
record(ledger::BallotBoxController, index::Int)::CastRecord
getindex(ledger::BallotBoxController, index::Int)::CastRecord
Return a ledger record at provided `index`.
"""
record(ballotbox::BallotBoxController, N::Int) = ledger(ballotbox)[N]
Base.getindex(ballotbox::BallotBoxController, N::Int) = ledger(ballotbox)[N]


"""
receipt(ledger::BallotBoxController, index::Int)::CastReceipt
Expand Down Expand Up @@ -205,8 +206,6 @@ function ack_leaf(ballotbox::BallotBoxController, index::Int)
end


#isbinding(vote::Vote, ack::AckInclusion{BallotBoxState}, crypto::Crypto) = digest(vote, crypto) == leaf(ack)

"""
isbinding(vote::Vote, ack::CastAck, hasher)
Expand Down Expand Up @@ -270,8 +269,6 @@ function Base.push!(ballotbox::BallotBoxController, record::CastRecord)
return
end

Base.getindex(bbox::BallotBoxController, index::Int) = bbox.ledger[index]

function state(bbox::BallotBoxController; with_tally::Union{Nothing, Bool} = nothing)

# nothing follows the current state
Expand Down Expand Up @@ -341,6 +338,7 @@ function record!(ballotbox::BallotBoxController, vote::Vote)
return N
end


"""
record!(ledger::BallotBoxController, record::CastRecord)
Expand Down Expand Up @@ -402,17 +400,14 @@ commit!(ballotbox::BallotBoxController, signer::Signer; with_tally::Union{Nothin
Represents a pooling station which hosts ballotbox ledgers for every proposal collector manages.
**Interface:** [`add!`](@ref), [`ballotbox`](@ref), [`record!`](@ref), [`commit!`](@ref), [`commit`](@ref), [`ack_leaf`](@ref), [`ack_root`](@ref), [`ack_cast`](@ref), [`record`](@ref), [`receipt`](@ref), [`spine`](@ref), [`ledger`](@ref), [`tally`](@ref), [`set_seed!`](@ref)
**Interface:** [`init!`](@ref), [`record!`](@ref), [`commit!`](@ref), [`commit`](@ref), [`ack_leaf`](@ref), [`ack_root`](@ref), [`ack_cast`](@ref), [`receipt`](@ref), [`spine`](@ref), [`ledger`](@ref), [`tally`](@ref), [`set_seed!`](@ref)
"""
struct PollingStation
halls::Vector{BallotBoxController}
#crypto::CryptoSpec
end

#PollingStation(crypto::CryptoSpec) = PollingStation(BallotBoxController[], crypto)
PollingStation() = PollingStation(BallotBoxController[])


function Base.show(io::IO, station::PollingStation)

println(io, "PollingStation:")
Expand All @@ -424,31 +419,29 @@ function Base.show(io::IO, station::PollingStation)

end


# init! would be a better name here!
"""
add!(station::PollingStation, proposal::Proposal, voters::Set{Pseudonym}[, collector::Pseudonym])
init!(station::PollingStation, proposal::Proposal, voters::Set{Pseudonym}[, collector::Pseudonym])
Creates a new ballotbox for given proposal with provided member pseudonyms at a relative generator anchored in the proposal.
A collector is optional and provided only when it differs from one specified in the proposal.
"""
function add!(station::PollingStation, spec::DemeSpec, proposal::Proposal, voters::Vector{Pseudonym}, collector::Pseudonym)
function init!(station::PollingStation, spec::DemeSpec, proposal::Proposal, voters::Vector{Pseudonym}, collector::Pseudonym)
bbox = BallotBoxController(proposal, voters, spec, collector)
push!(station.halls, bbox)
return
end

add!(station::PollingStation, spec::DemeSpec, proposal::Proposal, voters::Vector{Pseudonym}) = add!(station, spec, proposal, voters, proposal.collector)
init!(station::PollingStation, spec::DemeSpec, proposal::Proposal, voters::Vector{Pseudonym}) = init!(station, spec, proposal, voters, proposal.collector)


add!(station::PollingStation, bbox::BallotBoxController) = push!(station.halls, bbox)
init!(station::PollingStation, bbox::BallotBoxController) = push!(station.halls, bbox)

"""
ballotbox(station::PollingStation, uuid::UUID)::BallotBoxController
get(station::PollingStation, uuid::UUID)::BallotBoxController
Return a ballotbox ledger with a provided UUID. If none is found throws an error.
"""
function ballotbox(station::PollingStation, _uuid::UUID)
function Base.get(station::PollingStation, _uuid::UUID)

for hall in station.halls
if uuid(hall) == _uuid
Expand All @@ -460,25 +453,14 @@ function ballotbox(station::PollingStation, _uuid::UUID)
return
end

"""
record!(station::PollingStation, uuid::UUID, vote::Vote)::Int
Records a `vote` in a ballotbox with provided proposal UUID. Throws an error
if a ballotbox can't be found.
"""
function record!(station::PollingStation, uuid::UUID, vote::Vote)

bbox = ballotbox(station, uuid)

return record!(bbox, vote)
end
Base.get(station::PollingStation, proposal::Proposal) = get(station, uuid(proposal))

"""
ballotbox(station::PollingStation, proposal::Digest)::BallotBoxController
get(station::PollingStation, proposal::Digest)::BallotBoxController
Return a ballotbox which has proposal with provided digest.
"""
function ballotbox(station::PollingStation, proposal::Digest)
function Base.get(station::PollingStation, proposal::Digest)

for hall in station.halls

Expand All @@ -491,6 +473,22 @@ function ballotbox(station::PollingStation, proposal::Digest)
error("BallotBoxController with proposal diggest $(proposal) not found")
end



"""
record!(station::PollingStation, uuid::UUID, vote::Vote)::Int
Records a `vote` in a ballotbox with provided proposal UUID. Throws an error
if a ballotbox can't be found.
"""
function record!(station::PollingStation, uuid::UUID, vote::Vote)

bbox = get(station, uuid)

return record!(bbox, vote)
end


"""
record!(station::PollingStation, uuid::UUID, vote::Vote)::Int
Expand All @@ -510,81 +508,71 @@ end
Select a ballotbox with provided uuid and commit it's state with collector.
"""
commit!(station::PollingStation, uuid::UUID, signer::Signer; with_tally::Union{Bool, Nothing} = nothing) = commit!(ballotbox(station, uuid), signer; with_tally)
commit!(station::PollingStation, uuid::UUID, signer::Signer; with_tally::Union{Bool, Nothing} = nothing) = commit!(get(station, uuid), signer; with_tally)

"""
commit(station::PollingStation, uuid::UUID)::Commit
Return a ballotbox commit.
"""
commit(station::PollingStation, uuid::UUID) = commit(ballotbox(station, uuid))
commit(station::PollingStation, uuid::UUID) = commit(get(station, uuid))

"""
ack_leaf(station::PollingStation, uuid::UUID, N::Int)::AckInclusion
Return history tree inclusion proof for a tree leaf at index `N` in ballotbox with `uuid`.
"""
ack_leaf(station::PollingStation, uuid::UUID, N::Int) = ack_leaf(ballotbox(station, uuid), N)
ack_leaf(station::PollingStation, uuid::UUID, N::Int) = ack_leaf(get(station, uuid), N)

"""
ack_root(station::PollingStation, uuid::UUID, N::Int)::AckConsistency
Return history tree consitency proof tree root at index `N` in ballotbox with `uuid`.
"""
ack_root(station::PollingStation, uuid::UUID, N::Int) = ack_root(ballotbox(station, uuid), N)
ack_root(station::PollingStation, uuid::UUID, N::Int) = ack_root(get(station, uuid), N)

"""
ack_cast(station::PollingStation, uuid::UUID, N::Int)::CastAck
Return inclusion proof with receipt and current tree commit for a leaf at index `N` and ballotbox with `uuid`.
"""
ack_cast(station::PollingStation, uuid::UUID, N::Int) = ack_cast(ballotbox(station, uuid), N)

# """
# record(station::PollingStation, uuid::UUID, N::Int)::CastRecord

# Return a record with an index `N` at ballotbox with `uuid`.
# """
# record(station::PollingStation, uuid::UUID, N::Int) = record(ballotbox(station, uuid), N)

Base.get(station::PollingStation, uuid::UUID) = ballotbox(station, uuid) # TODO: use select
Base.get(station::PollingStation, proposal::Proposal) = get(station, uuid(proposal))
ack_cast(station::PollingStation, uuid::UUID, N::Int) = ack_cast(get(station, uuid), N)


"""
spine(station::PollingStation, uuid::UUID)::Vector{Digest}
Return a leaf vector for a ballotbox with proposal `uuid`.
"""
spine(station::PollingStation, uuid::UUID) = spine(ballotbox(station, uuid))
spine(station::PollingStation, uuid::UUID) = spine(get(station, uuid))

"""
receipt(station::PollingStation, uuid::UUID, N::Int)::CastReceipt
Return a receipt for a record with index `N` at ballotbox with `uuid`.
"""
receipt(station::PollingStation, uuid::UUID, N::Int) = receipt(ballotbox(station, uuid), N)
receipt(station::PollingStation, uuid::UUID, N::Int) = receipt(get(station, uuid), N)

"""
ledger(station::PollingStation, uuid::UUID)::Vector{CastRecord}
Return a vector of records from a ballotbox with `uuid`.
"""
ledger(station::PollingStation, uuid::UUID) = ledger(ballotbox(station, uuid))
ledger(station::PollingStation, uuid::UUID) = ledger(get(station, uuid))

"""
tally(station::PollingStation, uuid::UUID)
Compute a tally from ledger records a ballotbox with `uuid`.
"""
tally(station::PollingStation, uuid::UUID) = tally(ballotbox(station, uuid))
tally(station::PollingStation, uuid::UUID) = tally(get(station, uuid))

"""
set_seed!(station::PollingStation, uuid::UUID, seed::Digest)
Sets a seed for a ballotbox with provided `uuid`.
"""
set_seed!(station::PollingStation, uuid::UUID, seed::Digest) = set_seed!(ballotbox(station, uuid), seed)
set_seed!(station::PollingStation, uuid::UUID, seed::Digest) = set_seed!(get(station, uuid), seed)



Expand Down
29 changes: 14 additions & 15 deletions src/Server/Mapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ function load_system() # a kwarg could be passed on whether to audit the system
@warn "BallotBox commit not found. A new seed will be set."
end

Controllers.add!(POLLING_STATION[], bbox)
Controllers.init!(POLLING_STATION[], bbox)

Schedulers.schedule!(ENTROPY_SCHEDULER, proposal.open, proposal.uuid)
Schedulers.schedule!(TALLY_SCHEDULER, proposal.closed, proposal.uuid)
Expand Down Expand Up @@ -585,7 +585,7 @@ function submit_chain_record!(proposal::Proposal)
spec = get_demespec()
#anchored_members = Model.members(BRAID_CHAIN[], proposal)
anchored_members = Model.voters(BRAID_CHAIN[], proposal) # I could get a braid output_members
Controllers.add!(POLLING_STATION[], spec, proposal, anchored_members)
Controllers.init!(POLLING_STATION[], spec, proposal, anchored_members)

init_bbox_store(Controllers.ledger(get(POLLING_STATION[], proposal)))

Expand All @@ -599,19 +599,19 @@ end

function cast_vote(uuid::UUID, vote::Vote; late_votes = false)

if !(Model.isstarted(proposal(uuid); time = Dates.now()))
if !(Model.isstarted(get_proposal(uuid); time = Dates.now()))

error("Voting have not yet started")

elseif !late_votes && Model.isdone(proposal(uuid); time = Dates.now())
elseif !late_votes && Model.isdone(get_proposal(uuid); time = Dates.now())

error("Vote received for proposal too late")

else
# Concurency can be used with a following API but it requires defining
# a new vector type which has a write lock.

bbox = Controllers.ballotbox(POLLING_STATION[], uuid)
bbox = get(POLLING_STATION[], uuid)
N = Controllers.record!(bbox, vote)

# commit! may make dublicates in cases when record! executed async
Expand All @@ -630,23 +630,22 @@ function cast_vote(uuid::UUID, vote::Vote; late_votes = false)
end
end

@deprecate cast_vote! cast_vote
#@deprecate cast_vote! cast_vote

#ballotbox(uuid::UUID) = get(POLLING_STATION[], uuid)
get_ballotbox(uuid::UUID) = get(POLLING_STATION[], uuid)

ballotbox(uuid::UUID) = Controllers.ballotbox(POLLING_STATION[], uuid)
get_ballotbox(uuid::UUID) = Controllers.ballotbox(POLLING_STATION[], uuid)
#@deprecate ballotbox get_ballotbox

@deprecate ballotbox get_ballotbox
#proposal(uuid::UUID) = get_ballotbox(uuid).ledger.proposal
get_proposal(uuid::UUID) = get_ballotbox(uuid).ledger.proposal

proposal(uuid::UUID) = ballotbox(uuid).ledger.proposal
get_proposal(uuid::UUID) = ballotbox(uuid).ledger.proposal
#@deprecate proposal get_proposal

@deprecate proposal get_proposal

tally(uuid::UUID) = ballotbox(uuid).tally
#tally(uuid::UUID) = ballotbox(uuid).tally
get_tally(uuid::UUID) = ballotbox(uuid).tally

@deprecate tally get_tally
#@deprecate tally get_tally

get_ballotbox_commit(uuid::UUID) = Model.commit(POLLING_STATION[], uuid)

Expand Down
2 changes: 1 addition & 1 deletion test/mapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ spine = Mapper.get_ballotbox_spine(proposal.uuid)

#Mapper.tally_votes!(proposal.uuid)

ballotbox = Mapper.ballotbox(proposal.uuid)
ballotbox = Mapper.get_ballotbox(proposal.uuid)
@test istallied(ballotbox) == false
sleep(2)
@test istallied(ballotbox) == true
Expand Down
4 changes: 2 additions & 2 deletions test/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import .Model: CryptoSpec, pseudonym, TicketID, id, commit, verify, generator, M
import .Controllers: Registrar, admit!, enlist!, set_demehash!, Ticket, tokenid
import .Controllers: record!, commit!, ack_leaf
import .Controllers: BraidChainController, roll, constituents, state, ledger
import .Controllers: BallotBoxController, PollingStation, add!, ack_cast, set_seed!, spine
import .Controllers: BallotBoxController, PollingStation, init!, ack_cast, set_seed!, spine


crypto = CryptoSpec("sha256", "EC: P_192")
Expand Down Expand Up @@ -165,7 +165,7 @@ ack = ack_leaf(BRAID_CHAIN, N)
@test verify(ack, crypto)

#add!(POLLING_STATION, proposal, members(BRAID_CHAIN, proposal))
add!(POLLING_STATION, demespec, proposal, voters(BRAID_CHAIN, proposal))
init!(POLLING_STATION, demespec, proposal, voters(BRAID_CHAIN, proposal))

# Ideally the seed would be a Pulse from the League of Entropy
_seed = digest(rand(UInt8, 16), hasher(demespec))
Expand Down
2 changes: 1 addition & 1 deletion test/service.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ Client.check_vote!(eve, proposal.uuid)

# ------------- collector maliciously drops Alice's vote --------------

ballotbox = Mapper.ballotbox(proposal.uuid)
ballotbox = Mapper.get_ballotbox(proposal.uuid)
deleteat!(ballotbox.ledger.records, 1) # deleting alice's vote
Controllers.reset_tree!(ballotbox)
Controllers.commit!(Mapper.POLLING_STATION[], proposal.uuid, Mapper.COLLECTOR[])
Expand Down
4 changes: 2 additions & 2 deletions test/store.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import .Model: CryptoSpec, pseudonym, TicketID, id, commit, verify, generator, M
import .Controllers: Registrar, admit!, enlist!, set_demehash!, Ticket, tokenid
import .Controllers: record!, commit!, ack_leaf
import .Controllers: BraidChainController, roll, constituents, state, ledger
import .Controllers: BallotBoxController, PollingStation, add!, ack_cast, set_seed!, spine
import .Controllers: BallotBoxController, PollingStation, init!, ack_cast, set_seed!, spine


crypto = CryptoSpec("sha256", "EC: P_192")
Expand Down Expand Up @@ -117,7 +117,7 @@ proposal = Proposal(
N = record!(BRAID_CHAIN, proposal)
commit!(BRAID_CHAIN, BRAID_CHAIN_RECORDER)

add!(POLLING_STATION, demespec, proposal, voters(BRAID_CHAIN, proposal))
init!(POLLING_STATION, demespec, proposal, voters(BRAID_CHAIN, proposal))

_seed = digest(rand(UInt8, 16), hasher(demespec))
set_seed!(POLLING_STATION, proposal.uuid, _seed)
Expand Down

0 comments on commit ffc2daa

Please sign in to comment.