Skip to content

Commit

Permalink
add purb compatible controller
Browse files Browse the repository at this point in the history
  • Loading branch information
jbsv committed Nov 21, 2023
1 parent 16370da commit 28c635d
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 18 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 @@ -129,7 +129,6 @@ golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
11 changes: 4 additions & 7 deletions store/kv/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
const numberOfRecipients = 1

// NewBlob creates a new blob
func NewBlob(keypair []key.Pair) *libpurb.Purb {
func NewBlob(keypair []*key.Pair) *libpurb.Purb {
p := libpurb.NewPurb(
getSuiteInfo(),
false,
Expand Down Expand Up @@ -64,18 +64,15 @@ func getSuiteInfo() libpurb.SuiteInfoMap {
}

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

if len(keypair) < numberOfRecipients {
keypair = make([]key.Pair, 0)
}

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

r = append(r, libpurb.Recipient{
Expand Down
20 changes: 15 additions & 5 deletions store/kv/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,30 @@
package controller

import (
"go.dedis.ch/kyber/v3/util/key"
"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 {
isPurbOn bool
keys []*key.Pair
}

// NewController returns a minimal controller that will inject a key/value
// database.
func NewController() node.Initializer {
return minimalController{}
func NewController(isPurbOn bool) node.Initializer {
return minimalController{
isPurbOn,
nil,
}
}

// SetCommands implements node.Initializer. It does not register any command.
Expand All @@ -29,10 +36,13 @@ 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, keys, err := kv.NewDB(filepath.Join(flags.String("config"), "dela.db"), m.isPurbOn)
if err != nil {
return xerrors.Errorf("db: %v", err)
}
if len(keys) == 1 {
copy(m.keys, keys)
}

inj.Inject(db)

Expand Down
124 changes: 124 additions & 0 deletions store/kv/controller/controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package controller

import (
"go.dedis.ch/dela/cli"
"go.dedis.ch/dela/cli/node"
"go.dedis.ch/kyber/v3/util/key"
"reflect"
"testing"
)

func TestNewController(t *testing.T) {
type args struct {
isPurbOn bool
}
tests := []struct {
name string
args args
want node.Initializer
}{
// Test cases.
{"DB with purb off", args{false}, minimalController{false, nil}},
{"DB with purb on", args{true}, minimalController{true, nil}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewController(tt.args.isPurbOn); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewController() = %v, want %v", got, tt.want)
}
})
}
}

func Test_minimalController_OnStart(t *testing.T) {
type fields struct {
isPurbOn bool
keys []*key.Pair
}
type args struct {
flags cli.Flags
inj node.Injector
}
inj := node.NewInjector()
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
// Test cases.
{"DB with purb off", fields{false, nil}, args{node.FlagSet{}, inj}, false},
{"DB with purb on", fields{true, nil}, args{node.FlagSet{}, inj}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := minimalController{
isPurbOn: tt.fields.isPurbOn,
keys: tt.fields.keys,
}
if err := m.OnStart(tt.args.flags, tt.args.inj); (err != nil) != tt.wantErr {
t.Errorf("OnStart() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func Test_minimalController_OnStop(t *testing.T) {
type fields struct {
isPurbOn bool
keys []*key.Pair
}
type args struct {
inj node.Injector
}
inj := node.NewInjector()
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
// Test cases.
{"DB with purb off", fields{false, nil}, args{inj}, false},
{"DB with purb on", fields{true, nil}, args{inj}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := minimalController{
isPurbOn: tt.fields.isPurbOn,
keys: tt.fields.keys,
}
if err := m.OnStop(tt.args.inj); (err != nil) != tt.wantErr {
t.Errorf("OnStop() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func Test_minimalController_SetCommands(t *testing.T) {
type fields struct {
isPurbOn bool
keys []*key.Pair
}
type args struct {
builder node.Builder
}
tests := []struct {
name string
fields fields
args args
}{
// Test cases.
{"DB with purb off", fields{false, nil}, args{nil}},
{"DB with purb on", fields{true, nil}, args{nil}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := minimalController{
isPurbOn: tt.fields.isPurbOn,
keys: tt.fields.keys,
}
m.SetCommands(tt.args.builder)
})
}
}
8 changes: 4 additions & 4 deletions store/kv/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type purbDB struct {
}

// NewDB opens a new database to the given file.
func NewDB(path string, purbIsOn bool) (DB, []key.Pair, error) {
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)
Expand All @@ -57,14 +57,14 @@ func NewDB(path string, purbIsOn bool) (DB, []key.Pair, error) {
}

var b *libpurb.Purb = nil
keypair := make([]key.Pair, 0)
keypair := make([]*key.Pair, 0)
if purbIsOn {
b = NewBlob(nil)
pair := key.Pair{
Public: b.Recipients[0].PublicKey,
Private: b.Recipients[0].PrivateKey,
}
keypair = append(keypair, pair)
keypair = append(keypair, &pair)
}

p := &purbDB{
Expand All @@ -78,7 +78,7 @@ func NewDB(path string, purbIsOn bool) (DB, []key.Pair, error) {
}

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

0 comments on commit 28c635d

Please sign in to comment.