Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/delete_temp_siks_when_election_ends #1080

Merged
merged 15 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/api_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ type ElectionDescription struct {
ElectionType ElectionType `json:"electionType"`
Questions []Question `json:"questions"`
Census CensusTypeDescription `json:"census"`
TempSIKs bool `json:"tempSIKs"`
}

type ElectionFilter struct {
Expand Down
2 changes: 1 addition & 1 deletion apiclient/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ func (c *HTTPclient) RegisterSIKForVote(electionId types.HexBytes, proof *Census
},
})
if err != nil {
return nil, fmt.Errorf("error enconsing RegisterSIKTx: %w", err)
return nil, fmt.Errorf("error enconding RegisterSIKTx: %w", err)
}
// sign it and send it
hash, _, err := c.SignAndSendTx(stx)
Expand Down
1 change: 1 addition & 0 deletions apiclient/election.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ func (c *HTTPclient) NewElection(description *api.ElectionDescription) (types.He
CensusOrigin: censusOrigin,
Metadata: &metadataURI,
MaxCensusSize: description.Census.Size,
TempSIKs: &description.TempSIKs,
}
log.Debugf("election transaction: %+v", log.FormatProto(process))

Expand Down
13 changes: 13 additions & 0 deletions apiclient/sik.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ import (

type SIKRoots []string

// ValidSIK checks if the current client account has a valid SIK registered in
// the vochain
func (c *HTTPclient) ValidSIK() (bool, error) {
resp, code, err := c.Request(HTTPGET, nil, "siks", c.account.AddressString())
if err != nil {
return false, err
}
if code != apirest.HTTPstatusOK {
return false, fmt.Errorf("%s: %d (%s)", errCodeNot200, code, resp)
}
return true, nil
}

// ValidSIKRoots returns the currently valid roots of SIK merkle tree from the
// API.
func (c *HTTPclient) ValidSIKRoots() (SIKRoots, error) {
Expand Down
3 changes: 2 additions & 1 deletion cmd/cli/vocdonicli.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func (c *Config) Load(filepath string) error {
data, err := os.ReadFile(filepath)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
c.LastAccountUsed = -1
return nil
}
return err
Expand Down Expand Up @@ -94,7 +95,7 @@ func NewVocdoniCLI(configFile, host string) (*vocdoniCLI, error) {
if err != nil {
return nil, err
}
if len(cfg.Accounts)-1 >= cfg.LastAccountUsed {
if len(cfg.Accounts)-1 >= cfg.LastAccountUsed && cfg.LastAccountUsed >= 0 {
log.Infof("using account %d", cfg.LastAccountUsed)
if err := api.SetAccount(cfg.Accounts[cfg.LastAccountUsed].PrivKey.String()); err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions cmd/end2endtest/dynamicensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (t *E2EDynamicensusElection) Setup(api *apiclient.HTTPclient, c *config) er

// create a census with 2 voterAccounts less than the nvotes passed, that will allow to create another
// census with the missing nvotes
censusRoot, censusURI, err := t.elections[i].setupCensus(vapi.CensusTypeWeighted, t.elections[i].config.nvotes-2)
censusRoot, censusURI, err := t.elections[i].setupCensus(vapi.CensusTypeWeighted, t.elections[i].config.nvotes-2, false)
if err != nil {
return err
}
Expand Down Expand Up @@ -84,7 +84,7 @@ func (t *E2EDynamicensusElection) Run() error {
api := election.api
nvotes := election.config.nvotes

censusRoot2, censusURI2, err := election.setupCensus(vapi.CensusTypeWeighted, 2)
censusRoot2, censusURI2, err := election.setupCensus(vapi.CensusTypeWeighted, 2, false)
if err != nil {
return nil, apiclient.VoteData{}, err
}
Expand Down
86 changes: 70 additions & 16 deletions cmd/end2endtest/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,21 +167,18 @@ func (t *e2eElection) waitUntilElectionStarts(electionID types.HexBytes) (*vapi.
return election, nil
}

func (t *e2eElection) generateProofs(root types.HexBytes, isAnonymousVoting bool, csp *ethereum.SignKeys) (map[string]*apiclient.CensusProof, map[string]*apiclient.CensusProof) {
func (t *e2eElection) generateSIKProofs(root types.HexBytes) map[string]*apiclient.CensusProof {
type voterProof struct {
proof *apiclient.CensusProof
sikproof *apiclient.CensusProof
address string
}
proofs := make(map[string]*apiclient.CensusProof, len(t.voterAccounts))
sikProofs := make(map[string]*apiclient.CensusProof, len(t.voterAccounts))
proofCh := make(chan *voterProof)
stopProofs := make(chan bool)
go func() {
for {
select {
case p := <-proofCh:
proofs[p.address] = p.proof
sikProofs[p.address] = p.sikproof
case <-stopProofs:
return
Expand All @@ -191,7 +188,61 @@ func (t *e2eElection) generateProofs(root types.HexBytes, isAnonymousVoting bool

addNaccounts := func(accounts []*ethereum.SignKeys, wg *sync.WaitGroup) {
defer wg.Done()
log.Infof("generating %d voting proofs", len(accounts))
log.Infof("generating %d sik proofs", len(accounts))
for _, acc := range accounts {
voterProof := &voterProof{address: acc.Address().Hex()}

var err error
voterPrivKey := acc.PrivateKey()
voterApi := t.api.Clone(voterPrivKey.String())
voterProof.sikproof, err = voterApi.GenSIKProof()
if err != nil {
log.Warn(err)
}
proofCh <- voterProof
}
}

pcount := t.config.nvotes / t.config.parallelCount
var wg sync.WaitGroup
for i := 0; i < len(t.voterAccounts); i += pcount {
end := i + pcount
if end > len(t.voterAccounts) {
end = len(t.voterAccounts)
}
wg.Add(1)
go addNaccounts(t.voterAccounts[i:end], &wg)
}

wg.Wait()
log.Debugf("%d/%d sik proofs generated successfully", len(sikProofs), len(t.voterAccounts))
stopProofs <- true

return sikProofs
}

func (t *e2eElection) generateProofs(root types.HexBytes, isAnonymousVoting bool, csp *ethereum.SignKeys) map[string]*apiclient.CensusProof {
type voterProof struct {
proof *apiclient.CensusProof
address string
}
proofs := make(map[string]*apiclient.CensusProof, len(t.voterAccounts))
proofCh := make(chan *voterProof)
stopProofs := make(chan bool)
go func() {
for {
select {
case p := <-proofCh:
proofs[p.address] = p.proof
case <-stopProofs:
return
}
}
}()

addNaccounts := func(accounts []*ethereum.SignKeys, wg *sync.WaitGroup) {
defer wg.Done()
log.Infof("generating %d census proofs", len(accounts))
for _, acc := range accounts {
voterProof := &voterProof{address: acc.Address().Hex()}

Expand All @@ -205,9 +256,6 @@ func (t *e2eElection) generateProofs(root types.HexBytes, isAnonymousVoting bool
if err != nil {
log.Warn(err)
}
if isAnonymousVoting {
voterProof.sikproof, err = voterApi.GenSIKProof()
}
}
if err != nil {
log.Warn(err)
Expand All @@ -232,10 +280,10 @@ func (t *e2eElection) generateProofs(root types.HexBytes, isAnonymousVoting bool
}

wg.Wait()
log.Debugf("%d/%d voting proofs generated successfully", len(proofs), len(t.voterAccounts))
log.Debugf("%d/%d census proofs generated successfully", len(proofs), len(t.voterAccounts))
stopProofs <- true

return proofs, sikProofs
return proofs
}

func (t *e2eElection) setupAccount() error {
Expand All @@ -249,7 +297,7 @@ func (t *e2eElection) setupAccount() error {
}

// setupCensus create a new census that will have nAcct voterAccounts and participants
func (t *e2eElection) setupCensus(censusType string, nAcct int) (types.HexBytes, string, error) {
func (t *e2eElection) setupCensus(censusType string, nAcct int, createAccounts bool) (types.HexBytes, string, error) {
// Create a new census
censusID, err := t.api.NewCensus(censusType)
if err != nil {
Expand All @@ -261,7 +309,7 @@ func (t *e2eElection) setupCensus(censusType string, nAcct int) (types.HexBytes,
t.voterAccounts = append(t.voterAccounts, ethereum.NewSignKeysBatch(nAcct)...)

// Register the accounts in the vochain if is required
if censusType == vapi.CensusTypeZKWeighted {
if censusType == vapi.CensusTypeZKWeighted && createAccounts {
for _, acc := range t.voterAccounts {
pKey := acc.PrivateKey()
if _, _, err := t.createAccount(pKey.String()); err != nil &&
Expand Down Expand Up @@ -308,7 +356,7 @@ func (t *e2eElection) setupElection(ed *vapi.ElectionDescription) error {
// if the census is not defined yet, set up a new census that will have
// nvotes voterAccounts and participants
if ed.Census.RootHash == nil {
censusRoot, censusURI, err := t.setupCensus(ed.Census.Type, t.config.nvotes)
censusRoot, censusURI, err := t.setupCensus(ed.Census.Type, t.config.nvotes, true)
if err != nil {
return err
}
Expand Down Expand Up @@ -341,7 +389,10 @@ func (t *e2eElection) setupElection(ed *vapi.ElectionDescription) error {
return err
}
t.election = election
t.proofs, t.sikproofs = t.generateProofs(ed.Census.RootHash, ed.ElectionType.Anonymous, nil)
t.proofs = t.generateProofs(ed.Census.RootHash, ed.ElectionType.Anonymous, nil)
if ed.ElectionType.Anonymous && !ed.TempSIKs {
t.sikproofs = t.generateSIKProofs(ed.Census.RootHash)
}

return nil
}
Expand All @@ -366,7 +417,7 @@ func (t *e2eElection) setupElectionRaw(prc *models.Process) error {

default:
censusType := vapi.CensusTypeWeighted
censusRoot, censusURI, err := t.setupCensus(censusType, t.config.nvotes)
censusRoot, censusURI, err := t.setupCensus(censusType, t.config.nvotes, false)
if err != nil {
return err
}
Expand All @@ -388,7 +439,10 @@ func (t *e2eElection) setupElectionRaw(prc *models.Process) error {
t.election = election
prc.ProcessId = t.election.ElectionID

t.proofs, t.sikproofs = t.generateProofs(prc.CensusRoot, prc.EnvelopeType.Anonymous, csp)
t.proofs = t.generateProofs(prc.CensusRoot, prc.EnvelopeType.Anonymous, csp)
if prc.EnvelopeType.Anonymous {
t.sikproofs = t.generateSIKProofs(prc.CensusRoot)
}

return nil
}
Expand Down
Loading