-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
alok
committed
Dec 1, 2021
1 parent
fbc0370
commit a9ba8c8
Showing
5 changed files
with
154 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package inmem | ||
|
||
import ( | ||
"encoding/base64" | ||
|
||
"github.com/ipfs/go-datastore" | ||
"github.com/ipfs/go-datastore/namespace" | ||
"github.com/libp2p/go-libp2p-core/crypto" | ||
"github.com/libp2p/go-libp2p-core/peer" | ||
"github.com/plexsysio/gkvstore" | ||
ipfsdsStore "github.com/plexsysio/gkvstore-ipfsds" | ||
"github.com/plexsysio/go-msuite/modules/config" | ||
"github.com/plexsysio/go-msuite/modules/repo" | ||
) | ||
|
||
type inmemRepo struct { | ||
c config.Config | ||
ds datastore.Batching | ||
st gkvstore.Store | ||
} | ||
|
||
func initIdentity(c config.Config) error { | ||
if c.Get("Identity", make(map[string]interface{})) { | ||
return nil | ||
} | ||
sk, pk, err := crypto.GenerateKeyPair(crypto.Ed25519, 2048) | ||
if err != nil { | ||
return err | ||
} | ||
skbytes, err := crypto.MarshalPrivateKey(sk) | ||
if err != nil { | ||
return err | ||
} | ||
ident := map[string]interface{}{} | ||
ident["PrivKey"] = base64.StdEncoding.EncodeToString(skbytes) | ||
|
||
id, err := peer.IDFromPublicKey(pk) | ||
if err != nil { | ||
return err | ||
} | ||
ident["ID"] = id.Pretty() | ||
c.Set("Identity", ident) | ||
return nil | ||
} | ||
|
||
func CreateOrOpen(c config.Config) (repo.Repo, error) { | ||
err := initIdentity(c) | ||
if err != nil { | ||
return nil, err | ||
} | ||
ds := datastore.NewMapDatastore() | ||
st := ipfsdsStore.New(namespace.Wrap(ds, datastore.NewKey("/kv"))) | ||
return &inmemRepo{ | ||
c: c, | ||
ds: ds, | ||
st: st, | ||
}, nil | ||
} | ||
|
||
func (i *inmemRepo) Config() config.Config { | ||
return i.c | ||
} | ||
|
||
func (i *inmemRepo) SetConfig(c config.Config) error { | ||
i.c = c | ||
return nil | ||
} | ||
|
||
func (i *inmemRepo) Datastore() datastore.Batching { | ||
return i.ds | ||
} | ||
|
||
func (i *inmemRepo) Store() gkvstore.Store { | ||
return i.st | ||
} | ||
|
||
func (i *inmemRepo) Close() error { | ||
i.ds.Close() | ||
i.st.Close() | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package inmem_test | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
jsonConf "github.com/plexsysio/go-msuite/modules/config/json" | ||
"github.com/plexsysio/go-msuite/modules/repo/inmem" | ||
) | ||
|
||
func TestInmemRepo(t *testing.T) { | ||
t.Run("create", func(t *testing.T) { | ||
r, err := inmem.CreateOrOpen(jsonConf.DefaultConfig()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
err = r.Close() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
}) | ||
t.Run("create with config", func(t *testing.T) { | ||
cfg := jsonConf.DefaultConfig() | ||
cfg.Set("Dummy1", "Val1") | ||
cfg.Set("Dummy2", "Val2") | ||
|
||
r, err := inmem.CreateOrOpen(cfg) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
cfg2 := r.Config() | ||
if !reflect.DeepEqual(cfg, cfg2) { | ||
t.Fatal("expected config to be the same") | ||
} | ||
|
||
cfg3 := jsonConf.DefaultConfig() | ||
err = r.SetConfig(cfg3) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
cfg4 := r.Config() | ||
if !reflect.DeepEqual(cfg4, cfg3) { | ||
t.Fatal("expected config to be the same") | ||
} | ||
|
||
if reflect.DeepEqual(cfg4, cfg) { | ||
t.Fatal("expected config to be different") | ||
} | ||
|
||
err = r.Close() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters