Skip to content

Commit

Permalink
Add election rule context when executing successful vote
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey committed May 16, 2019
1 parent 26050a6 commit 90db0e3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 161 deletions.
158 changes: 0 additions & 158 deletions x/gov/__demo_handler.go

This file was deleted.

56 changes: 56 additions & 0 deletions x/gov/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package gov

import (
"context"

"github.com/iov-one/weave"
"github.com/iov-one/weave/x"
)

type contextKey int // local to the gov module

const (
contextKeyGov contextKey = iota
)

// withElectionSuccess is a private method, as only this module
// can add a gov signer
func withElectionSuccess(ctx weave.Context, ruleID []byte) weave.Context {
val, _ := ctx.Value(contextKeyGov).([]weave.Condition)
if val == nil {
return context.WithValue(ctx, contextKeyGov, []weave.Condition{ElectionCondition(ruleID)})
}

return context.WithValue(ctx, contextKeyGov, append(val, ElectionCondition(ruleID)))
}

// ElectionCondition returns condition for an election rule ID
func ElectionCondition(ruleID []byte) weave.Condition {
return weave.NewCondition("gov", "rule", ruleID)
}

// Authenticate gets/sets permissions on the given context key
type Authenticate struct {
}

var _ x.Authenticator = Authenticate{}

// GetConditions returns permissions previously set on this context
func (a Authenticate) GetConditions(ctx weave.Context) []weave.Condition {
// (val, ok) form to return nil instead of panic if unset
val, _ := ctx.Value(contextKeyGov).([]weave.Condition)
if val == nil {
return nil
}
return val
}

// HasAddress returns true iff this address is in GetConditions
func (a Authenticate) HasAddress(ctx weave.Context, addr weave.Address) bool {
for _, s := range a.GetConditions(ctx) {
if addr.Equals(s.Address()) {
return true
}
}
return false
}
7 changes: 4 additions & 3 deletions x/gov/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,10 @@ func (h TallyHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx)
return nil, errors.Wrap(err, "options invalid")
}

// TODO: what do we use for ctx here???
// we need to add the election rules somehow
return h.executor(ctx, db, opts)
// we add the vote ctx here, to authenticate results in the executor
// ensure that the gov.Authenticator is used in those Handlers
voteCtx := withElectionSuccess(ctx, common.ElectionRuleRef.ID)
return h.executor(voteCtx, db, opts)
}

func (h TallyHandler) validate(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*TallyMsg, *Proposal, error) {
Expand Down

0 comments on commit 90db0e3

Please sign in to comment.