Skip to content

Commit

Permalink
finishing refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
JanisErdmanis committed Mar 8, 2020
1 parent 5d22c44 commit b1b5259
Show file tree
Hide file tree
Showing 26 changed files with 559 additions and 403 deletions.
1 change: 1 addition & 0 deletions .#Project.toml
4 changes: 2 additions & 2 deletions Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ version = "0.2.4"

[[PeaceVote]]
deps = ["Pkg", "Serialization"]
git-tree-sha1 = "f9936be4741448f021e4d16a1c87ca9bf4d8acdf"
git-tree-sha1 = "5c956ce380d418139237dff581d81bab1d7a8284"
uuid = "129db535-0459-46ad-8d3d-b1fb8f318c74"
version = "0.1.1"
version = "0.2.0"

[[Pkg]]
deps = ["Dates", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "UUIDs"]
Expand Down
9 changes: 8 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ authors = ["akels <[email protected]>"]
version = "0.2.0"

[deps]
BitConverter = "3a3ce9e8-98e7-11e9-0fa0-055639f146d3"
DiffieHellman = "0e61fee8-20a4-4374-8233-5198f28d9d66"
PeaceVote = "129db535-0459-46ad-8d3d-b1fb8f318c74"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Expand All @@ -14,6 +13,14 @@ SynchronicBallot = "c4fd2470-c9f5-491f-b4e2-53b9059945a5"
Synchronizers = "9279c0cf-0ddb-4a8a-93c9-19d312620a08"

[compat]
DiffieHellman = "^0.3.0"
PeaceVote = "^0.2.0"
SynchronicBallot = "^0.2.1"
Synchronizers = "^0.1"
julia = "1"

[extras]
PeaceCypher = "7743456c-e0c5-4477-a2b6-196c240b0368"

[targets]
test = ["PeaceCypher",]
5 changes: 1 addition & 4 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,4 @@ The focus thus for PeaceVote is voluntarily democracies. It is the democracies o

# The design of PeaceFounder

The Julia PeaceFounder is aiming to be a universal swiss knife for community maintainers. It aims to be the module from which communities take first the backend methods for the server and second the frontend methods defining how members register, propose, vote and braid. This package is successful if every line which you write for the community represents a political decision. A declarative API is a goal.



The Julia PeaceFounder is aiming to be a universal swiss knife for deme maintainers. It aims to be the module from which demes take first the backend methods for the server and second the frontend methods defining how members register, propose, vote and braid. This package is successful if every line which you write for the community represents a political decision. A declarative API is a goal.
108 changes: 108 additions & 0 deletions research/braidchain.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using PeaceVote
using PeaceCypher


using PeaceFounder.Braiders: BraiderConfig
using PeaceFounder.BraidChains: BraidChainConfig


#setnamespace(@__MODULE__)
#uuid = PeaceVote.uuid("Community")

# Cleanup from the previous tests
#dirs = [PeaceVote.keydir(uuid), PeaceVote.datadir(uuid), PeaceVote.communitydir(uuid)]

for dir in [homedir() * "/.peacevote/"]
isdir(dir) && rm(dir,recursive=true)
end

demespec = PeaceVote.DemeSpec("PeaceDeme",:default,:PeaceCypher,:default,:PeaceCypher,:PeaceFounder)
save(demespec) ### Necessary to connect with Mixer

uuid = demespec.uuid
### I could actually rely on DemeSpec file for creating signers
maintainer = PeaceVote.Signer(uuid,"maintainer")

#mixer = PeaceVote.Signer(uuid,"mixer")
#mixerserver = PeaceFounder.Mixer(1999,deme,mixer)

server = PeaceVote.Signer(uuid,"server")

MIXER_ID = server.id ### Self mixing
SERVER_ID = server.id

certifierconfig = nothing
braiderconfig = PeaceFounder.BraiderConfig(2000,2001,3,SERVER_ID,(uuid,MIXER_ID))
braidchainconfig = PeaceFounder.BraidChainConfig(maintainer.id,[(uuid,maintainer.id),],server.id,2002,2003,2004)
systemconfig = PeaceFounder.SystemConfig(2001,2005,certifierconfig,braiderconfig,braidchainconfig)

PeaceFounder.save(systemconfig,maintainer)

### Starting the server

system = PeaceFounder.System(demespec,server)

sleep(2) # for waiting until server is ready

### Initializing without a ledger. I will change that shortly.
deme = Deme(demespec,nothing)

### Theese are our members

for i in 1:9
account = "account$i"
keychain = PeaceVote.KeyChain(deme,account)
identification = PeaceVote.ID("$i","today",keychain.member.id)
cert = PeaceVote.Certificate(identification,maintainer)
@show register(deme,cert)
end

# First braiding
sleep(1)

@sync for i in 1:9
account = "account$i"
keychain = PeaceVote.KeyChain(deme,account) ### The issue is perhaps
@async PeaceVote.braid!(keychain)
end


error("STOP") ### Now I should see members and braids.

pmember = PeaceVote.Member(uuid,"account2")
propose("Found peace for a change?",["yes","no","maybe"],pmember);

# Second braiding

@sync for i in 1:9
account = "account$i"
keychain = PeaceVote.KeyChain(uuid,account)
@async PeaceVote.braid!(keychain)
end

# Now someone sends the proposal

pmember = PeaceVote.Member(uuid,"account1")
propose("Let's vote for a real change",["yes","no"],pmember);

sleep(1)

messages = braidchain()
proposals = PeaceVote.proposals(messages)

# Voting

for i in 1:9
account = "account$i"
keychain = PeaceVote.KeyChain(uuid,account)

# Notice that this is after braiding
### We need to also update the registrator
voter = PeaceVote.Voter(keychain,proposals[1],messages)
option = PeaceVote.Option(proposals[1],rand(1:3))
vote(option,voter)

voter = PeaceVote.Voter(keychain,proposals[2],messages)
option = PeaceVote.Option(proposals[2],rand(1:2))
vote(option,voter)
end
File renamed without changes.
9 changes: 9 additions & 0 deletions research/ledgertest.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### SO THIS WORKS

using PeaceVote
using PeaceCypher
import PeaceFounder

demespec = DemeSpec("PeaceDeme",:default,:PeaceCypher,:default,:PeaceCypher,:PeaceFounder)
deme = Deme(demespec)

File renamed without changes.
31 changes: 31 additions & 0 deletions src/Analysis/Analysis.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module Analysis

using PeaceVote: Proposal, voters!, Vote, Option, BraidChain, Deme

function normalcount(proposal::Proposal,deme::Deme) ### Here I could have
messages = BraidChain(deme).records

index = findfirst(item -> item==proposal,messages)
voters = Set()
voters!(voters,messages[1:index])

ispvote(msg) = typeof(msg)==Vote && msg.id in voters && typeof(msg.msg)==Option && msg.msg.pid==proposal.uuid

tally = zeros(Int,length(proposal.options))

for msg in messages[end:-1:index]
if ispvote(msg)
tally[msg.msg.vote] += 1
pop!(voters,msg.id)
end
end

return tally
end

preferentialcount(proposal::Proposal,deme::Deme) = error("Not yet implemented")

quadraticcount(proposal::Proposal,deme::Deme) = error("Not yet implemented")


end
9 changes: 1 addition & 8 deletions src/BraidChains/BraidChains.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ using PeaceVote: record!, records, loadrecord
using ..Braiders: Braider
using ..Crypto
using ..DataFormat ###
using ..Types: RecorderConfig

const ThisDeme = Deme

Expand All @@ -20,14 +21,6 @@ const ThisDeme = Deme
# #cache
# end

struct RecorderConfig
maintainerid # The one which signs the config file
membersca ### One needs to explicitly add the certifier server id here. That's because
serverid
registratorport ### The port to which the certificate of membership is delivered
votingport
proposalport
end

include("../debug.jl")

Expand Down
34 changes: 17 additions & 17 deletions src/BraidChains/analysis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,26 +77,26 @@ BraidChain(deme::Deme) = BraidChain(deme.ledger,deme.notary)

### I could make this outside. I could impplement a method rawrecords and records (makes sense because I have the recorder!)

import Base.count
function count(proposal::Proposal,messages::Vector) ### Here I could have
#voters = PeaceVote.voters(proposal,messages) # I need a index
# import Base.count
# function count(proposal::Proposal,messages::Vector) ### Here I could have
# #voters = PeaceVote.voters(proposal,messages) # I need a index

index = findfirst(item -> item==proposal,messages)
voters = Set()
voters!(voters,messages[1:index])
# index = findfirst(item -> item==proposal,messages)
# voters = Set()
# voters!(voters,messages[1:index])

ispvote(msg) = typeof(msg)==Vote && msg.id in voters && typeof(msg.msg)==Option && msg.msg.pid==proposal.uuid
# ispvote(msg) = typeof(msg)==Vote && msg.id in voters && typeof(msg.msg)==Option && msg.msg.pid==proposal.uuid

tally = zeros(Int,length(proposal.options))
# tally = zeros(Int,length(proposal.options))

for msg in messages[end:-1:index]
if ispvote(msg)
tally[msg.msg.vote] += 1
pop!(voters,msg.id)
end
end
# for msg in messages[end:-1:index]
# if ispvote(msg)
# tally[msg.msg.vote] += 1
# pop!(voters,msg.id)
# end
# end

return tally
end
# return tally
# end

count(proposal::Proposal,braidchain::BraidChain) = count(proposal,braidchain.records)
# count(proposal::Proposal,braidchain::BraidChain) = count(proposal,braidchain.records)
14 changes: 3 additions & 11 deletions src/Braiders/Braiders.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,7 @@ using SynchronicBallot: BallotBox, GateKeeper, SocketConfig
#using SecureIO
using PeaceVote: DemeSpec, Notary, Cypher, Signer, Deme


struct BraiderConfig
port # braiderport
ballotport # mixerport
N
gateid # braiderid
mixerid
end

using ..Types: BraiderConfig

#include("crypto.jl") ### I can make a module Utils so to import thoose things here

Expand Down Expand Up @@ -61,7 +53,7 @@ function Braider(braider::BraiderConfig,deme::ThisDeme,signer::Signer)
mixeruuid = braider.mixerid[1]

mixerdemespec = DemeSpec(mixeruuid)
mixerdeme = Deme(mixerdemespec,nothing)
mixerdeme = Deme(mixerdemespec,ledger=false)

Braider(braider,deme,mixerdeme,signer)
end
Expand All @@ -88,7 +80,7 @@ function braid!(config::BraiderConfig,deme::ThisDeme,voter::Signer,signer::Signe
mixeruuid = config.mixerid[1]

mixerdemespec = DemeSpec(mixeruuid)
mixerdeme = Deme(mixerdemespec,nothing)
mixerdeme = Deme(mixerdemespec,ledger=false)

braid!(config,deme,mixerdeme,voter,signer)
end
Expand Down
8 changes: 1 addition & 7 deletions src/Certifiers/Certifiers.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
### Could be part of PeaceVote
module Certifiers

using ..Types: CertifierConfig
using PeaceVote: Certificate, Signer, AbstractID, ID, Deme, Notary
using Sockets
using DiffieHellman
using Serialization

struct CertifierConfig
tookenca ### authorithies who can issue tookens. Server allows to add new tookens only from them.
serverid ### Server receiveing tookens and the member identities. Is also the one which signs and issues the certificates.
tookenport
#hmac for keeping the tooken secret
certifierport
end

const ThisDeme = Deme

Expand Down
33 changes: 33 additions & 0 deletions src/DataFormat/DataFormat.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
module DataFormat

using Base: UUID
using ..Types: SystemConfig
using PeaceVote: Notary, DemeSpec, Deme, datadir, Signer
using ..Crypto
#using ...PeaceFounder: SystemConfig

#const PeaceFounder = parentmodule(@__MODULE__)
#using PeaceFounder: SystemConfig

import Serialization

function binary(x)
Expand All @@ -10,10 +19,34 @@ end

loadbinary(data) = Serialization.deserialize(IOBuffer(data))


### Until the file fomrat is designed.
serialize(io::IO,x) = Serialization.serialize(io,x)
deserialize(io::IO) = Serialization.deserialize(io)

configfname(uuid::UUID) = datadir(uuid) * "/PeaceFounder" # In future could be PeaceFounder.toml

function deserialize(deme::Deme,::Type{SystemConfig})
fname = configfname(deme.spec.uuid)
@assert isfile(fname) "Config file not found!"
config, signature = Serialization.deserialize(fname)
id = verify(config,signature,deme.notary)
@assert id==deme.spec.maintainer
return config
end

### I could call this thing serialize
# serialize(fname::AbstractString,config::SystemConfig,signer::Signer)
# I could use AbstractLedger type with record! method. Passing deme would also be desirable as one could check that the signer of the config file is in the demespec.
function serialize(deme::Deme,config::SystemConfig,signer::Signer)
@assert deme.spec.maintainer==signer.id
fname = configfname(deme.spec.uuid)
mkpath(dirname(fname))
signature = sign(config,signer)
Serialization.serialize(fname,(config,signature))
end


export binary, loadbinary, serialize, deserialize

end
17 changes: 17 additions & 0 deletions src/Ledgers/Ledgers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ basename(record::Record) = basename(record.fname)

loadrecord(record::Record) = loadbinary(record.data)


serve(port,ledger::Ledger) = Synchronizers.serve(port,ledger.ledger)

# import Synchronizers.Ledger
# Ledger(serverid::BigInt) = Ledger(datadir() * "/$serverid/")

# sync!(ledger::Ledger,syncport) = sync(Synchronizer(syncport,ledger))

# function sync!(ledger::Ledger)
# serverid = parse(BigInt,basename(dirname(ledger.dir)))
# config = SystemConfig(serverid)
# sync!(ledger,config.syncport)
# end




export Ledger, record!, loadrecord, dirname, basename

end
Loading

2 comments on commit b1b5259

@JanisErdmanis
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/10701

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if Julia TagBot is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.0 -m "<description of version>" b1b5259ee18b3d2ec9393b26972e2f519e71cd6f
git push origin v0.2.0

Please sign in to comment.