Skip to content
Snippets Groups Projects
Commit 7c524f97 authored by Richard T. Carback III's avatar Richard T. Carback III
Browse files

Add helper to initialize filesystem based local KV, replace all init and load...

Add helper to initialize filesystem based local KV, replace all init and load cMix code with the helper, move kv initialization to be inside the top level new/load functions, remove it from the storage setup
parent 05479784
Branches
Tags
5 merge requests!620Account Sychronization First Pass,!618Make it so that transaction log can run without a remote to write to, and add...,!617Project/haven beta,!594Add helper to initialize filesystem based local KV, replace all init and load...,!523Draft: Account Synchronization Over Remote Storage
......@@ -56,10 +56,14 @@ func NewCmixFromBackup(ndfJSON, storageDir, backupPassphrase string,
cmixGrp, e2eGrp := xxdk.DecodeGroups(def)
kv, err := xxdk.LocalKV(storageDir, sessionPassword, rngStreamGen)
if err != nil {
return nil, "", err
}
// Note we do not need registration here
storageSess, err := xxdk.CheckVersionAndSetupStorage(def, storageDir,
sessionPassword, userInfo, cmixGrp, e2eGrp,
backUp.RegistrationCode, rngStreamGen)
storageSess, err := xxdk.CheckVersionAndSetupStorage(def, kv, userInfo,
cmixGrp, e2eGrp, backUp.RegistrationCode, rngStreamGen)
if err != nil {
return nil, "", err
}
......
......@@ -39,7 +39,11 @@ var initCmd = &cobra.Command{
jww.FATAL.Panicf("%+v", err)
}
err = xxdk.NewCmix(string(ndfJson), storeDir, storePassword, regCode)
err = xxdk.NewCmix(string(ndfJson), storeDir, storePassword,
regCode)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
net, err := xxdk.OpenCmix(storeDir, storePassword)
if err != nil {
jww.FATAL.Panicf("%+v", err)
......
......@@ -9,7 +9,6 @@ package xxdk
import (
"math"
"path/filepath"
"time"
"gitlab.com/xx_network/primitives/netTime"
......@@ -23,11 +22,10 @@ import (
"gitlab.com/elixxir/client/v4/stoppable"
"gitlab.com/elixxir/client/v4/storage"
"gitlab.com/elixxir/client/v4/storage/user"
"gitlab.com/elixxir/client/v4/sync"
"gitlab.com/elixxir/client/v4/storage/versioned"
"gitlab.com/elixxir/comms/client"
"gitlab.com/elixxir/crypto/cyclic"
"gitlab.com/elixxir/crypto/fastRNG"
"gitlab.com/elixxir/ekv"
"gitlab.com/elixxir/primitives/version"
"gitlab.com/xx_network/comms/connect"
"gitlab.com/xx_network/crypto/csprng"
......@@ -81,13 +79,18 @@ func NewCmix(
return err
}
kv, err := LocalKV(storageDir, password, rngStreamGen)
if err != nil {
return err
}
cmixGrp, e2eGrp := DecodeGroups(def)
start := netTime.Now()
userInfo := createNewUser(rngStreamGen, e2eGrp)
jww.DEBUG.Printf(
"PortableUserInfo generation took: %s", netTime.Now().Sub(start))
_, err = CheckVersionAndSetupStorage(def, storageDir, password,
_, err = CheckVersionAndSetupStorage(def, kv,
userInfo, cmixGrp, e2eGrp, registrationCode, rngStreamGen)
return err
}
......@@ -113,7 +116,12 @@ func NewVanityCmix(ndfJSON, storageDir string, password []byte,
userInfo := createNewVanityUser(rngStream, e2eGrp, userIdPrefix)
_, err = CheckVersionAndSetupStorage(def, storageDir, password,
kv, err := LocalKV(storageDir, password, rngStreamGen)
if err != nil {
return err
}
_, err = CheckVersionAndSetupStorage(def, kv,
userInfo, cmixGrp, e2eGrp, registrationCode, rngStreamGen)
if err != nil {
return err
......@@ -122,8 +130,6 @@ func NewVanityCmix(ndfJSON, storageDir string, password []byte,
return nil
}
// func OpenSynchronizedCmix(
// OpenCmix creates client storage but does not connect to the network or login.
// Note that this is a helper function that, in most applications, should not be
// used on its own. Consider using LoadCmix instead, which calls this function
......@@ -132,27 +138,21 @@ func OpenCmix(storageDir string, password []byte) (*Cmix, error) {
jww.INFO.Printf("OpenCmix()")
rngStreamGen := fastRNG.NewStreamGenerator(12, 1024, csprng.NewSystemRNG)
storageKV, err := LocalKV(storageDir, password, rngStreamGen)
if err != nil {
return nil, err
}
return openCmix(storageKV, rngStreamGen)
}
func openCmix(storageKV versioned.KV, rngStreamGen *fastRNG.StreamGenerator) (
*Cmix, error) {
currentVersion, err := version.ParseVersion(SEMVER)
if err != nil {
return nil, errors.WithMessage(err, "Could not parse version string.")
}
// OpenCmix never connects to a remote.
passwordStr := string(password)
localKV, err := ekv.NewFilestore(storageDir, passwordStr)
if err != nil {
return nil, errors.WithMessage(err,
"failed to create storage session")
}
localFS := sync.NewFileSystemRemoteStorage(filepath.Join(storageDir,
localTxLogPath))
storageKV, err := sync.LocalKV(storageDir, password,
localFS, localKV, rngStreamGen)
if err != nil {
return nil, err
}
storageSess, err := storage.Load(storageKV, currentVersion)
if err != nil {
return nil, err
......@@ -192,12 +192,14 @@ func NewProtoCmix_Unsafe(ndfJSON, storageDir string, password []byte,
rngStreamGen := fastRNG.NewStreamGenerator(12, 1024,
csprng.NewSystemRNG)
rngStream := rngStreamGen.GetStream()
defer rngStream.Close()
kv, err := LocalKV(storageDir, password, rngStreamGen)
if err != nil {
return err
}
cmixGrp, e2eGrp := DecodeGroups(def)
storageSess, err := CheckVersionAndSetupStorage(
def, storageDir, password, usr, cmixGrp, e2eGrp,
def, kv, usr, cmixGrp, e2eGrp,
protoUser.RegCode, rngStreamGen)
if err != nil {
return err
......@@ -627,8 +629,9 @@ func DecodeGroups(ndf *ndf.NetworkDefinition) (cmixGrp, e2eGrp *cyclic.Group) {
// CheckVersionAndSetupStorage checks the client version and creates a new
// storage for user data. This function is common code shared by NewCmix,
// NewPrecannedCmix and NewVanityCmix.
func CheckVersionAndSetupStorage(def *ndf.NetworkDefinition, storageDir string,
password []byte, userInfo user.Info, cmixGrp, e2eGrp *cyclic.Group,
func CheckVersionAndSetupStorage(def *ndf.NetworkDefinition,
storageKV versioned.KV, userInfo user.Info,
cmixGrp, e2eGrp *cyclic.Group,
registrationCode string, rng *fastRNG.StreamGenerator) (storage.Session,
error) {
// Get current client version
......@@ -638,19 +641,6 @@ func CheckVersionAndSetupStorage(def *ndf.NetworkDefinition, storageDir string,
}
// Create storage
passwordStr := string(password)
localKV, err := ekv.NewFilestore(storageDir, passwordStr)
if err != nil {
return nil, errors.WithMessage(err,
"failed to create storage session")
}
localFS := sync.NewFileSystemRemoteStorage(filepath.Join(storageDir,
localTxLogPath))
storageKV, err := sync.LocalKV(storageDir, password,
localFS, localKV, rng)
if err != nil {
return nil, err
}
storageSess, err := storage.New(storageKV, userInfo,
currentVersion, cmixGrp, e2eGrp)
if err != nil {
......
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2022 xx foundation //
// //
// Use of this source code is governed by a license that can be found in the //
// LICENSE file. //
////////////////////////////////////////////////////////////////////////////////
package xxdk
import (
"path/filepath"
"github.com/pkg/errors"
"gitlab.com/elixxir/client/v4/sync"
"gitlab.com/elixxir/crypto/fastRNG"
"gitlab.com/elixxir/ekv"
)
// LocalKV creates a filesystem based KV that doesn't
// synchronize with a remote storage system.
func LocalKV(storageDir string, password []byte,
rng *fastRNG.StreamGenerator) (*sync.VersionedKV, error) {
passwordStr := string(password)
localKV, err := ekv.NewFilestore(storageDir, passwordStr)
if err != nil {
return nil, errors.WithMessage(err,
"failed to create storage session")
}
localFS := sync.NewFileSystemRemoteStorage(filepath.Join(storageDir,
localTxLogPath))
return sync.LocalKV(storageDir, password, localFS, localKV, rng)
}
......@@ -41,8 +41,13 @@ func NewPrecannedCmix(precannedID uint, defJSON, storageDir string,
}
cmixGrp, e2eGrp := DecodeGroups(def)
kv, err := LocalKV(storageDir, password, rngStreamGen)
if err != nil {
return err
}
userInfo := createPrecannedUser(precannedID, rngStream, e2eGrp)
store, err := CheckVersionAndSetupStorage(def, storageDir, password,
store, err := CheckVersionAndSetupStorage(def, kv,
userInfo, cmixGrp, e2eGrp, "", rngStreamGen)
if err != nil {
return err
......
......@@ -43,7 +43,6 @@ func newTestingClient(face interface{}) (*Cmix, error) {
return nil, errors.Errorf(
"Could not construct a mock client: %v", err)
}
c, err := OpenCmix(storageDir, password)
if err != nil {
return nil, errors.Errorf("Could not open a mock client: %v",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment