Skip to content

Commit

Permalink
finalizing braidchain ledger storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Janis Erdmanis committed Mar 25, 2024
1 parent 9c1132e commit c03a178
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 25 deletions.
2 changes: 2 additions & 0 deletions src/Core/Model/braidchains.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ struct BraidChainLedger
records::AbstractVector{Transaction}
end

@batteries BraidChainLedger

Base.push!(ledger::BraidChainLedger, record::Transaction) = push!(ledger.records, record)
Base.getindex(ledger::BraidChainLedger, index::Int) = ledger.records[index]
Base.length(ledger::BraidChainLedger) = length(ledger.records)
Expand Down
15 changes: 7 additions & 8 deletions src/Core/Model/braids.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@ group(spec::MODP) = CryptoGroups.specialize(PGroup, spec)
"""
struct BraidReceipt <: Transaction
braid::Simulator
consumer::DemeSpec
producer::DemeSpec
approval::Union{Seal, Nothing}
end
Represents a braider's computation which is supported with zero knowledge proof of shuffle and decryption assuring it's corectness
stored in a `braid` field; `consumer` denotes a deme for which the braid is intended and `producer` denotes a deme where
the braid is made. To assert latter the the braider signs the braidwork and stores that in the `aproval` field.
See a [`braid`](@ref) method.
stored in a `braid` field; `producer` denotes a deme where the braid is made. To assert latter the the braider signs the
braidwork and stores that in the `aproval` field. See a [`braid`](@ref) method.
**Interface:** [`approve`](@ref), [`verify`](@ref), [`input_generator`](@ref), [`input_members`](@ref), [`output_generator`](@ref), [`output_members`](@ref)
"""
Expand All @@ -47,6 +45,8 @@ struct BraidReceipt <: Transaction
BraidReceipt(braid::Simulator, producer::DemeSpec, approval::Seal) = new(braid, producer, approval)
end

@batteries BraidReceipt

@doc raw"""
braid(generator::Generator, members::Union{Vector{Pseudonym}, Set{Pseudonym}}, consumer::DemeSpec, producer::DemeSpec; verifier = (g) -> ProtocolSpec(; g))
Expand Down Expand Up @@ -109,10 +109,9 @@ function verify(braidwork::BraidReceipt, crypto::CryptoSpec; skip_braid::Bool =
verify(bytes(_digest), braidwork.approval, braidwork.producer.crypto) || return false

pseudonym(braidwork.approval) == braidwork.producer.braider || return false

# TODO: check that ProtocolSpec is compatable with the chain
# braidwork.braid.verifier == ProtocolSpec(g = braidwork.braid.verifier.g) || return false
# typeof(braidwork.braid.verifier.g) == group(crypto.group)

braidwork.braid.verifier == ProtocolSpec(g = braidwork.braid.verifier.g) || return false
typeof(braidwork.braid.verifier.g) == group(crypto.group) || return false

if !skip_braid
ShuffleProofs.verify(braidwork.braid) || return false
Expand Down
2 changes: 0 additions & 2 deletions src/Core/Model/proposals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,6 @@ Return a pseudonym with which vote is sealed.
pseudonym(vote::Vote) = isnothing(vote.seal) ? nothing : pseudonym(vote.seal)




"""
struct BallotBoxState
proposal::Digest
Expand Down
42 changes: 31 additions & 11 deletions src/Core/Store.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Store

using ..Model: BraidChainLedger, DemeSpec, Membership, BraidReceipt, Proposal, Transaction, BallotBoxLedger, CastRecord
using ShuffleProofs: ShuffleProofs
using ..Model: BraidChainLedger, DemeSpec, Membership, BraidReceipt, Proposal, Transaction, BallotBoxLedger, CastRecord, Seal
using ..Parser: marshal, unmarshal


Expand Down Expand Up @@ -55,22 +56,30 @@ function save(record::Union{DemeSpec, Membership, Proposal}, dir::String, index:
end


function save(record::BraidReceipt, path::String; force=false)

path *= ".json" # A quick fix
function save(record::BraidReceipt, dir::String; force=false)

if isfile(path)
if isdir(dir)
if force
rm(path)
rm(dir, recursive=true)
else
error("$path already exists; use `force` to overwrite.")
error("$dir already exists; use `force` to overwrite.")
end
end

open(path, "w") do file
marshal(file, record)

mkdir(dir)

open(joinpath(dir, "demespec.json"), "w") do file
marshal(file, record.producer)
end

open(joinpath(dir, "seal.json"), "w") do file
marshal(file, record.approval)
end

mkdir(joinpath(dir, "braid"))

ShuffleProofs.save(record.braid, joinpath(dir, "braid"))

return
end

Expand All @@ -82,6 +91,17 @@ function save(record::BraidReceipt, dir::String, index::Int; force=false)

end

function load(::Type{BraidReceipt}, dir::String)

braid = ShuffleProofs.load(joinpath(dir, "braid"))
spec = unmarshal(read(joinpath(dir, "demespec.json")), DemeSpec)
seal = unmarshal(read(joinpath(dir, "seal.json")), Seal)

return BraidReceipt(braid, spec, seal)
end

load(::Type{BraidReceipt}, dir::String, index::UInt16) = load(BraidReceipt, joinpath(dir, BRAIDRECEIPT_DIR, index2name(index)))


function save(record::CastRecord, path::String; force=false)

Expand Down Expand Up @@ -206,7 +226,7 @@ function load_braidchain(dir::String)
bytes = read(joinpath(dir, MEMBERSHIP_DIR, index2name(i)) * ".json")
record = unmarshal(bytes, Membership)
elseif i in braidreceipt_keys
@warn "Skiping as unimplemented"
record = load(BraidReceipt, dir, i)
elseif i in proposal_keys
bytes = read(joinpath(dir, PROPOSAL_DIR, index2name(i)) * ".json")
record = unmarshal(bytes, Proposal)
Expand Down
3 changes: 0 additions & 3 deletions src/Server/Controllers/ballotbox.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ using ..Core.ProtocolSchema: AckInclusion, AckConsistency, CastAck
import ..Core.Model: uuid, voters, seed, receipt, tally, tallyview, istallied, isbinding


#import ..LedgerInterface: record!, commit!, ack_leaf, ack_root, commit_index, reset_tree!, root, commit, select, state, leaf


"""
mutable struct BallotBoxController
proposal::Proposal
Expand Down
2 changes: 1 addition & 1 deletion test/store.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ STORE_DIR = joinpath(tempdir(), "braidchain")
save(ledger(BRAID_CHAIN), STORE_DIR; force=true)
loaded_ledger = load(STORE_DIR)

# TODO: Fix braidreceipt and test ledger equality
@test loaded_ledger == ledger(BRAID_CHAIN)

# BALLOTBOX

Expand Down

0 comments on commit c03a178

Please sign in to comment.