diff --git a/batch/batch.go b/batch/batch.go new file mode 100644 index 0000000..8fe10b2 --- /dev/null +++ b/batch/batch.go @@ -0,0 +1,35 @@ +package batch + +import ( + "github.com/syndtr/goleveldb/leveldb" +// "encoding/json" + "fmt" + "github.com/vocdoni/dvote-relay/types" +) + +//add (queue for counting) +func Add(p types.Packet, bdb *leveldb.DB) error { + err := bdb.Put([]byte(fmt.Sprintf("%v", p.Nullifier)),[]byte(fmt.Sprintf("%v", p)), nil) + if err != nil { + return err + } + return nil +} + +//create (return batch) +func Create(bdb *leveldb.DB) []byte { + var b []byte + + iter := bdb.NewIterator(nil, nil) + for iter.Next() { + err := iter.Error() + if err != nil { + panic(err) + } + //put p in batch + //db.Put(iter.Key(), iter.Val(), nil) + //bdb.Delete(iter.Key(), nil) + } + iter.Release() + return b +} diff --git a/batch/batch_test.go b/batch/batch_test.go new file mode 100644 index 0000000..389c4ab --- /dev/null +++ b/batch/batch_test.go @@ -0,0 +1 @@ +package batch diff --git a/data/data.go b/data/data.go index ca1d5bb..30a4441 100644 --- a/data/data.go +++ b/data/data.go @@ -8,13 +8,6 @@ import ( shell "github.com/ipfs/go-ipfs-api" ) -type votePacket struct { - PID uint - Nullifier string - Vote string - Franchise string -} - func publish(object []byte) (string) { sh := shell.NewShell("localhost:5001") cid, err := sh.Add(bytes.NewBuffer(object)) diff --git a/main.go b/main.go new file mode 100644 index 0000000..cdb8590 --- /dev/null +++ b/main.go @@ -0,0 +1,54 @@ +package main + +import ( + "fmt" + "github.com/syndtr/goleveldb/leveldb" + "time" + "github.com/vocdoni/dvote-relay/batch" +) + +var dbPath = "$HOME/.dvote/relay.db" +var bdbPath = "$HOME/.dvote/batch.db" +var batchSeconds = 3 //seconds + +var err error +var db *leveldb.DB +var bdb *leveldb.DB +var batchTimer *time.Ticker +var batchSignal chan int + + +func setup() { + db, err = leveldb.OpenFile(dbPath, nil) + if err != nil { + panic(err) + } + defer db.Close() + + bdb, err = leveldb.OpenFile(bdbPath, nil) + if err != nil { + panic(err) + } + defer bdb.Close() + + batchTimer = time.NewTicker(time.Second * time.Duration(batchSeconds)) + //create batch signal channel +} + +func main() { + setup() + fmt.Println("Entering main loop") + for { + select { + case <- batchTimer.C: + fmt.Println("Timer triggered") + fmt.Println(batch.Create(bdb)) + //replace with chain link + case <- batchSignal: + fmt.Println("Signal triggered") + fmt.Println(batch.Create(bdb)) +// case <- stopSignal: +// return + } + } +} diff --git a/net/net.go b/net/net.go index a6e3482..e9db0a1 100644 --- a/net/net.go +++ b/net/net.go @@ -5,22 +5,15 @@ import ( "fmt" "net/http" "time" - "strconv" "io" + "github.com/vocdoni/dvote-relay/batch" + "github.com/vocdoni/dvote-relay/types" ) -type submission struct { - Type string - Nonce []byte - Key []byte - Package []byte - Expiry time.Time -} - -func parseSubmission(rw http.ResponseWriter, request *http.Request) { +func parse(rw http.ResponseWriter, request *http.Request) { decoder := json.NewDecoder(request.Body) - var s submission + var s Submission err := decoder.Decode(&s) if err != nil { @@ -28,20 +21,24 @@ func parseSubmission(rw http.ResponseWriter, request *http.Request) { } //check PoW - //check discriminator + //check key //decrypt //check franchise - //add to leveldb + err = batch.add(p) + if err != nil { + return err + } + j, err := json.Marshal(s) - //io.WriteString(rw, string(j)) + io.WriteString(rw, string(j)) } -func listen(port int) { - http.HandleFunc("/submit", parseSubmission) - portstr := strconv.Itoa(port) +func listen(port string) { + http.HandleFunc("/submit", parse) + //add waitgroup go func() { - fmt.Println("serving on " + portstr) - err := http.ListenAndServe(":" + portstr, nil) + fmt.Println("serving on " + port) + err := http.ListenAndServe(":" + port, nil) if err != nil { panic("ListenAndServe: " + err.Error()) } diff --git a/net/net_test.go b/net/net_test.go index 013146b..add58a7 100644 --- a/net/net_test.go +++ b/net/net_test.go @@ -10,6 +10,10 @@ import ( "io/ioutil" ) +//func generateSubmission() submission { + +//} + func TestListen(t *testing.T) { t.Log("Testing listener") @@ -21,15 +25,14 @@ func TestListen(t *testing.T) { time.Now(), } - go listen(8080) + listen("8080") url := "http://localhost:8080/submit" fmt.Println("URL:>", url) j, err := json.Marshal(testSubmission) if err != nil { - fmt.Println(err) - return + t.Errorf("Bad test JSON: %s", err) } req, err := http.NewRequest("POST", url, bytes.NewBuffer(j)) @@ -38,7 +41,7 @@ func TestListen(t *testing.T) { client := &http.Client{} resp, err := client.Do(req) if err != nil { - panic(err) + t.Errorf("Error in client: %s", err) } defer resp.Body.Close() diff --git a/types/types.go b/types/types.go new file mode 100644 index 0000000..7c2c56d --- /dev/null +++ b/types/types.go @@ -0,0 +1,30 @@ +package types + +import ( + "time" +) + +type Submission struct { + Type string + Nonce []byte + KeyProof []byte + Package []byte + Timestamp time.Time +} + +type Batch struct { + Type string + Nullifiers [][]byte + URL string + TXID string + Nonce []byte + Signature string +} + +type Packet struct { + PID uint + Nullifier string + Vote string + Franchise string +} +