Skip to content

Commit

Permalink
Migrated outbox enqueue action into database.go from internal/admin. …
Browse files Browse the repository at this point in the history
…Continuing DB layer clean-up.
  • Loading branch information
awgh committed Feb 20, 2018
1 parent 7cecc78 commit be9898f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 31 deletions.
9 changes: 4 additions & 5 deletions nodes/qldb/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,6 @@ func (node *Node) SendChannel(channelName string, data []byte, pubkey ...bc.PubK

func (node *Node) send(channelName string, destkey bc.PubKey, msg []byte, c *sql.DB) error {

//todo: is this passing msg by reference or not???
data, err := node.contentKey.EncryptMessage(msg, destkey)
if err != nil {
return err
Expand All @@ -272,10 +271,10 @@ func (node *Node) send(channelName string, destkey bc.PubKey, msg []byte, c *sql
data = append(rxsum, data...)

ts := time.Now().UnixNano()
//d := base64.StdEncoding.EncodeToString(data)
transactExec(c, "INSERT INTO outbox(channel, msg, timestamp) VALUES($1,$2, $3);",
channelName, data, ts)

err = node.qlOutboxEnqueue(channelName, data, ts, false) // todo: not checking if exists here?
if err != nil {
return err
}
return nil
}

Expand Down
24 changes: 24 additions & 0 deletions nodes/qldb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,30 @@ func (node *Node) qlGetPeers() ([]api.Peer, error) {
return peers, nil
}

func (node *Node) qlOutboxEnqueue(channelName string, msg []byte, ts int64, checkExists bool) error {
c := node.db()

doInsert := !checkExists

if checkExists {
// save message in my outbox, if not already present
r1 := c.QueryRow("SELECT channel FROM outbox WHERE channel==$1 AND msg==$2;", channelName, msg)
var rc string
err := r1.Scan(&rc)
if err == sql.ErrNoRows {
// we don't have this yet, so add it
doInsert = true
} else if err != nil {
return err
}
}
if doInsert {
transactExec(c, "INSERT INTO outbox(channel,msg,timestamp) VALUES($1,$2,$3);",
channelName, msg, ts)
}
return nil
}

func (node *Node) outboxBulkInsert(channelName string, timestamp int64, msgs [][]byte) {
c := node.db()
tx, err := c.Begin()
Expand Down
28 changes: 2 additions & 26 deletions nodes/qldb/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package qldb

import (
"bytes"
"database/sql"
"errors"
"fmt"
"os"
Expand All @@ -15,16 +14,7 @@ import (

// GetChannelPrivKey : Return the private key of a given channel
func (node *Node) GetChannelPrivKey(name string) (string, error) {
c := node.db()
r := transactQueryRow(c, "SELECT privkey FROM channels WHERE name==$1;", name)
var privkey string
if err := r.Scan(&privkey); err == sql.ErrNoRows {
return "", nil
} else if err != nil {
return "", err
} else {
return privkey, nil
}
return node.qlGetChannelPrivKey(name)
}

// Forward - Add an already-encrypted message to the outbound message queue (forward it along)
Expand All @@ -35,21 +25,7 @@ func (node *Node) Forward(channelName string, message []byte) error {
rxsum = append(rxsum, []byte(channelName)...)
message = append(rxsum, message...)

c := node.db()

// save message in my outbox, if not already present
// todo: do we really still need this check?
r1 := transactQueryRow(c, "SELECT channel FROM outbox WHERE channel==$1 AND msg==$2;", channelName, message)
var rc string
err := r1.Scan(&rc)
if err == sql.ErrNoRows {
// we don't have this yet, so add it
t := time.Now().UnixNano()
transactExec(c, "INSERT INTO outbox(channel,msg,timestamp) VALUES($1,$2,$3);",
channelName, message, t)
return nil
}
return err
return node.qlOutboxEnqueue(channelName, message, time.Now().UnixNano(), true)
}

// Handle - Decrypt and handle an encrypted message
Expand Down

0 comments on commit be9898f

Please sign in to comment.