Browse Source

added primitive ballot/envelope generator, changed type naming

batching_initial_approach
imw 6 years ago
parent
commit
6cce8cb242
5 changed files with 148 additions and 30 deletions
  1. +14
    -9
      batch/batch.go
  2. +104
    -0
      generator/generator.go
  3. +2
    -2
      main.go
  4. +17
    -9
      net/net.go
  5. +11
    -10
      types/types.go

+ 14
- 9
batch/batch.go

@ -14,22 +14,21 @@ var currentBatchSize int
var err error
func Setup(l *db.LevelDbStorage) {
rdb = l.WithPrefix([]byte("relay"))
bdb = l.WithPrefix([]byte("batch"))
fmt.Println("Batch storage:")
fmt.Println(bdb)
rdb = l.WithPrefix([]byte("relay_"))
bdb = l.WithPrefix([]byte("batch_"))
}
//add (queue for counting)
func Add(p types.Packet) error {
fmt.Println(bdb)
func Add(p types.Ballot) error {
err := bdb.Put([]byte(fmt.Sprintf("%v", p.Nullifier)),[]byte(fmt.Sprintf("%v", p)))
if err != nil {
return err
}
//this actually needs to see if it was added
currentBatchSize++
fmt.Println(bdb.Info())
if currentBatchSize >= BatchSize {
BatchSignal <- true
}
@ -37,17 +36,23 @@ func Add(p types.Packet) error {
}
//create (return batch)
//k is []byte nullifier
//v is []byte package
func Create() []byte {
var b []byte
var i int
iter := bdb.Iter()
for iter.Next() {
//k := iter.Key()
//v := iter.Value()
k := iter.Key()
v := iter.Value()
err := iter.Error()
if err != nil {
panic(err)
}
fmt.Println(i)
fmt.Println(string(k))
fmt.Println(string(v))
i++
//put p in batch
//rdb.Put(iter.Key(), iter.Val(), nil)
//bdb.Delete(iter.Key(), nil)

+ 104
- 0
generator/generator.go

@ -0,0 +1,104 @@
package main
import (
"os"
"strconv"
"time"
"fmt"
"math/rand"
"encoding/json"
"encoding/base64"
"net/http"
"bytes"
"io/ioutil"
"github.com/vocdoni/dvote-relay/types"
)
func makeBallot() string {
var bal types.Ballot
bal.Type = "ballot0"
pidBytes := make([]byte, 32)
rand.Read(pidBytes)
bal.PID = base64.StdEncoding.EncodeToString(pidBytes)
nullifier := make([]byte, 32)
rand.Read(nullifier)
bal.Nullifier = nullifier
vote := make([]byte, 32)
rand.Read(vote)
bal.Vote = vote
franchise := make([]byte, 32)
rand.Read(franchise)
bal.Franchise = franchise
b, err := json.Marshal(bal)
if err != nil {
fmt.Println(err)
return "error"
}
//todo: add encryption, pow
return string(b)
}
func makeEnvelope(ballot string) string {
var env types.Envelope
env.Type = "envelope0"
env.Nonce = rand.Uint64()
kp := make([]byte, 4)
rand.Read(kp)
env.KeyProof = kp
env.Ballot = []byte(ballot)
env.Timestamp = time.Now()
e, err := json.Marshal(env)
if err != nil {
fmt.Println(err)
return "error"
}
//todo: add encryption, pow
return string(e)
}
func main() {
interval := os.Args[1]
i, _ := strconv.Atoi(interval)
timer := time.NewTicker(time.Millisecond * time.Duration(i))
rand.Seed(time.Now().UnixNano())
url := "http://localhost:8080/submit"
fmt.Println("URL:>", url)
for {
select {
case <- timer.C:
fmt.Println(makeEnvelope(makeBallot()))
var jsonStr = []byte(makeEnvelope(makeBallot()))
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
req.Header.Set("X-Custom-Header", "myvalue")
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Println("response Status:", resp.Status)
fmt.Println("response Headers:", resp.Header)
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("response Body:", string(body))
default:
continue
}
}
}

+ 2
- 2
main.go

@ -40,12 +40,12 @@ func main() {
select {
case <- batchTimer.C:
fmt.Println("Timer triggered")
fmt.Println(batch.Create())
// fmt.Println(batch.Create())
//replace with chain link
case signal := <-batchSignal:
if signal == true {
fmt.Println("Signal triggered")
fmt.Println(batch.Create())
batch.Create()
}
default:
continue

+ 17
- 9
net/net.go

@ -13,30 +13,38 @@ import (
func parse(rw http.ResponseWriter, request *http.Request) {
decoder := json.NewDecoder(request.Body)
var s types.Submission
var p types.Packet
err := decoder.Decode(&s)
var e types.Envelope
var b types.Ballot
err := decoder.Decode(&e)
if err != nil {
panic(err)
}
err = json.Unmarshal(e.Ballot, &b)
if err != nil {
panic(err)
}
//check PoW
//check key
//decrypt
//check franchise
//construct packet
p.PID = "1"
p.Nullifier = []byte{1,2,3}
p.Vote = []byte{4,5,6}
p.Franchise = []byte{7,8,9}
err = batch.Add(p)
//this should should be randomized, or actually taken from input
//b.PID = "1"
//b.Nullifier = []byte{1,2,3}
//b.Vote = []byte{4,5,6}
//b.Franchise = []byte{7,8,9}
err = batch.Add(b)
if err != nil {
panic(err)
}
j, err := json.Marshal(s)
j, err := json.Marshal(e)
io.WriteString(rw, string(j))
}

+ 11
- 10
types/types.go

@ -4,11 +4,19 @@ import (
"time"
)
type Submission struct {
type Ballot struct {
Type string
Nonce []byte
PID string
Nullifier []byte
Vote []byte
Franchise []byte
}
type Envelope struct {
Type string
Nonce uint64
KeyProof []byte
Package []byte
Ballot []byte
Timestamp time.Time
}
@ -21,10 +29,3 @@ type Batch struct {
Signature string
}
type Packet struct {
PID string
Nullifier []byte
Vote []byte
Franchise []byte
}

Loading…
Cancel
Save