Skip to content

Commit

Permalink
Merge pull request #3 from dedis/purb-controller
Browse files Browse the repository at this point in the history
add purb compatible controller
  • Loading branch information
jbsv authored Nov 29, 2023
2 parents 62b8a65 + 49c1f03 commit 4be5872
Show file tree
Hide file tree
Showing 12 changed files with 347 additions and 108 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ require (
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/urfave/cli/v2 v2.2.0 // indirect
go.dedis.ch/fixbuf v1.0.3 // indirect
go.etcd.io/bbolt v1.3.5 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/sys v0.14.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
47 changes: 27 additions & 20 deletions store/kv/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@ import (
"go.dedis.ch/kyber/v3/util/key"
"go.dedis.ch/libpurb/libpurb"
"golang.org/x/xerrors"
"path/filepath"

"go.dedis.ch/kyber/v3/util/random"
)

const numberOfRecipients = 1

// NewBlob creates a new blob
func NewBlob(keypair []key.Pair) *libpurb.Purb {
func NewBlob(path string) *libpurb.Purb {
p := libpurb.NewPurb(
getSuiteInfo(),
false,
random.New(),
)
p.Recipients = createRecipients(keypair)
p.Recipients = createRecipients(path)

return p
}
Expand Down Expand Up @@ -64,27 +65,33 @@ func getSuiteInfo() libpurb.SuiteInfoMap {
}

// see example in libpurb
func createRecipients(keypair []key.Pair) []libpurb.Recipient {
func createRecipients(path string) []libpurb.Recipient {
r := make([]libpurb.Recipient, 0)
suites := []libpurb.Suite{curve25519.NewBlakeSHA256Curve25519(true)}

if len(keypair) < numberOfRecipients {
keypair = make([]key.Pair, 0)
suite := []libpurb.Suite{curve25519.NewBlakeSHA256Curve25519(true)}

keysPath := filepath.Join(path, "purb.keys")
loader := NewKeysLoader(keysPath)
keypair := make([]key.Pair, numberOfRecipients)
err := loader.Load(&keypair)
if err != nil {
// cannot load keys, create new ones
for i := range keypair {
keypair[i] = *key.NewKeyPair(suite[0])
}
err := loader.Save(&keypair)
if err != nil {
panic(err)
}
}

for _, suite := range suites {
for i := 0; i < numberOfRecipients; i++ {
if len(keypair) < numberOfRecipients {
keypair = append(keypair, *key.NewKeyPair(suite))
}

r = append(r, libpurb.Recipient{
SuiteName: suite.String(),
Suite: suite,
PublicKey: keypair[i].Public,
PrivateKey: keypair[i].Private,
})
}
for i := 0; i < numberOfRecipients; i++ {
r = append(r, libpurb.Recipient{
SuiteName: suite[0].String(),
Suite: suite[0],
PublicKey: keypair[i].Public,
PrivateKey: keypair[i].Private,
})
}

return r
}
26 changes: 18 additions & 8 deletions store/kv/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,33 @@
package controller

import (
"path/filepath"

"go.dedis.ch/dela/cli"
"go.dedis.ch/dela/cli/node"
"go.dedis.ch/dela/core/store/kv"
"go.dedis.ch/purb-db/store/kv"
"golang.org/x/xerrors"
)

// MinimalController is a CLI controller to inject a key/value database.
//
// - implements node.Initializer
type minimalController struct{}
type minimalController struct {
purbIsOn bool
}

// NewController returns a minimal controller that will inject a key/value
// database.
// NewController returns a minimal controller
// that will inject a key/value database.
func NewController() node.Initializer {
return minimalController{}
return minimalController{
purbIsOn: true,
}
}

// NewControllerWithoutPurb returns a minimal controller without PURB
// that will inject a key/value database.
func NewControllerWithoutPurb() node.Initializer {
return minimalController{
purbIsOn: false,
}
}

// SetCommands implements node.Initializer. It does not register any command.
Expand All @@ -29,7 +39,7 @@ func (m minimalController) SetCommands(builder node.Builder) {}
// OnStart implements node.Initializer. It opens the database in a file using
// the config path as the base.
func (m minimalController) OnStart(flags cli.Flags, inj node.Injector) error {
db, err := kv.New(filepath.Join(flags.String("config"), "dela.db"))
db, err := kv.NewDB(flags.String("config"), m.purbIsOn)
if err != nil {
return xerrors.Errorf("db: %v", err)
}
Expand Down
46 changes: 46 additions & 0 deletions store/kv/controller/controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package controller

import (
"github.com/stretchr/testify/require"
"go.dedis.ch/dela/cli/node"
"testing"
)

func TestNewController(t *testing.T) {
c := NewController()
require.NotNil(t, c)

require.Equal(t, minimalController{true}, c)
}

func TestNewControllerWithoutPurb(t *testing.T) {
c := NewControllerWithoutPurb()
require.NotNil(t, c)

require.Equal(t, minimalController{false}, c)
}

func TestOnStart(t *testing.T) {
c := NewController()

err := c.OnStart(node.FlagSet{}, node.NewInjector())
require.NoError(t, err)
}

func TestOnStop(t *testing.T) {
c := NewController()

inj := node.NewInjector()

err := c.OnStart(node.FlagSet{}, inj)
require.NoError(t, err)

err = c.OnStop(inj)
require.NoError(t, err)
}

func TestSetCommands(t *testing.T) {
c := NewController()

c.SetCommands(nil)
}
72 changes: 26 additions & 46 deletions store/kv/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"errors"
"io"
"os"
"path/filepath"
"sync"

"go.dedis.ch/kyber/v3/util/key"
"go.dedis.ch/libpurb/libpurb"
"golang.org/x/xerrors"
)
Expand Down Expand Up @@ -37,74 +37,51 @@ type purbDB struct {
}

// NewDB opens a new database to the given file.
func NewDB(path string, purbIsOn bool) (DB, []key.Pair, error) {
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
return nil, nil, xerrors.Errorf("failed to open DB file: %v", err)
}
defer f.Close()

stats, _ := f.Stat()
s := stats.Size()
if s > 0 {
return nil, nil, xerrors.New("failed to create DB file: file already exists")
}

var data = make([]byte, s)
l, err := f.Read(data)
if int64(l) != 0 || err != nil {
return nil, nil, xerrors.Errorf("failed to read DB file: %v", err)
}

var b *libpurb.Purb = nil
keypair := make([]key.Pair, 0)
func NewDB(path string, purbIsOn bool) (DB, error) {
var filePath string
if purbIsOn {
b = NewBlob(nil)
pair := key.Pair{
Public: b.Recipients[0].PublicKey,
Private: b.Recipients[0].PrivateKey,
}
keypair = append(keypair, pair)
filePath = filepath.Join(path, "purb.db")
} else {
filePath = filepath.Join(path, "kv.db")
}

p := &purbDB{
dbFile: path,
bucketDb: newBucketDb(),
purbIsOn: purbIsOn,
blob: b,
f, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
return nil, xerrors.Errorf("failed to open DB file: %v", err)
}
defer f.Close()

return p, keypair, nil
}

// LoadDB opens a database from a given file.
func LoadDB(path string, purbIsOn bool, keypair []key.Pair) (DB, error) {
var b *libpurb.Purb = nil
if purbIsOn {
b = NewBlob(keypair)
b = NewBlob(path)
}

p := &purbDB{
dbFile: path,
dbFile: filePath,
bucketDb: newBucketDb(),
purbIsOn: purbIsOn,
blob: b,
}

err := p.load()
if err != nil {
return nil, err
stats, _ := f.Stat()
s := stats.Size()
if s > 0 {
err = p.load()
if err != nil {
return nil, xerrors.Errorf("failed to load DB file: %v", err)
}
}

return p, nil
}

// View implements kv.DB. It executes the read-only transaction in the context
// of the database.
func (p *purbDB) View(fn func(ReadableTx) error) error {
tx := &dpTx{db: p.bucketDb, new: newBucketDb()}
tx := &dpTx{db: &p.bucketDb, new: newBucketDb()}

tx.db.RLock()
err := fn(tx)
tx.db.RUnlock()

if err != nil {
return err
Expand All @@ -116,9 +93,12 @@ func (p *purbDB) View(fn func(ReadableTx) error) error {
// Update implements kv.DB. It executes the writable transaction in the context
// of the database.
func (p *purbDB) Update(fn func(WritableTx) error) error {
tx := &dpTx{db: p.bucketDb, new: newBucketDb()}
tx := &dpTx{db: &p.bucketDb, new: newBucketDb()}

tx.db.RLock()
err := fn(tx)
tx.db.RUnlock()

if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 4be5872

Please sign in to comment.