Browse Source

added main loop and config, added naive batch logic

batching_initial_approach
imw 6 years ago
parent
commit
335c7b3217
7 changed files with 143 additions and 30 deletions
  1. +35
    -0
      batch/batch.go
  2. +1
    -0
      batch/batch_test.go
  3. +0
    -7
      data/data.go
  4. +54
    -0
      main.go
  5. +16
    -19
      net/net.go
  6. +7
    -4
      net/net_test.go
  7. +30
    -0
      types/types.go

+ 35
- 0
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
}

+ 1
- 0
batch/batch_test.go

@ -0,0 +1 @@
package batch

+ 0
- 7
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))

+ 54
- 0
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
}
}
}

+ 16
- 19
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())
}

+ 7
- 4
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()

+ 30
- 0
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
}

Loading…
Cancel
Save