From 6c1c3d5f8f693bf5158bcb182dcb7068ce0df94f Mon Sep 17 00:00:00 2001 From: Prad Nukala Date: Sun, 7 Jul 2024 17:45:08 -0400 Subject: [PATCH] Refactored the SonrApp struct by reorganizing and grouping the keepers and other fields. --- app/app.go | 106 ++++++++++++--------------- {internal/env => app}/context.go | 2 +- {internal/env => app}/local.go | 3 +- cmd/sonrd/root.go | 5 +- pkg/vfs/file.go | 96 ------------------------- pkg/vfs/folder.go | 120 ------------------------------- pkg/vfs/ipfs.go | 93 ------------------------ pkg/vfs/utils.go | 58 --------------- 8 files changed, 50 insertions(+), 433 deletions(-) rename {internal/env => app}/context.go (98%) rename {internal/env => app}/local.go (95%) delete mode 100644 pkg/vfs/file.go delete mode 100644 pkg/vfs/folder.go delete mode 100644 pkg/vfs/ipfs.go delete mode 100644 pkg/vfs/utils.go diff --git a/app/app.go b/app/app.go index 8a4a12b1d..461d57bf0 100644 --- a/app/app.go +++ b/app/app.go @@ -211,67 +211,53 @@ var ( // SonrApp extended ABCI application type SonrApp struct { - *baseapp.BaseApp - legacyAmino *codec.LegacyAmino - appCodec codec.Codec - txConfig client.TxConfig - interfaceRegistry types.InterfaceRegistry - - // keys to access the substores - keys map[string]*storetypes.KVStoreKey - tkeys map[string]*storetypes.TransientStoreKey - memKeys map[string]*storetypes.MemoryStoreKey - - // keepers - AccountKeeper authkeeper.AccountKeeper - BankKeeper bankkeeper.BaseKeeper - CapabilityKeeper *capabilitykeeper.Keeper - StakingKeeper *stakingkeeper.Keeper - SlashingKeeper slashingkeeper.Keeper - MintKeeper mintkeeper.Keeper - DistrKeeper distrkeeper.Keeper - GovKeeper govkeeper.Keeper - CrisisKeeper *crisiskeeper.Keeper - UpgradeKeeper *upgradekeeper.Keeper - ParamsKeeper paramskeeper.Keeper - AuthzKeeper authzkeeper.Keeper - EvidenceKeeper evidencekeeper.Keeper - FeeGrantKeeper feegrantkeeper.Keeper - GroupKeeper groupkeeper.Keeper - NFTKeeper nftkeeper.Keeper - ConsensusParamsKeeper consensusparamkeeper.Keeper - CircuitKeeper circuitkeeper.Keeper - - IBCKeeper *ibckeeper.Keeper // IBC Keeper must be a pointer in the app, so we can SetRouter on it correctly - IBCFeeKeeper ibcfeekeeper.Keeper - ICAControllerKeeper icacontrollerkeeper.Keeper - ICAHostKeeper icahostkeeper.Keeper - TransferKeeper ibctransferkeeper.Keeper - - // Custom - TokenFactoryKeeper tokenfactorykeeper.Keeper - POAKeeper poakeeper.Keeper - GlobalFeeKeeper globalfeekeeper.Keeper - PacketForwardKeeper *packetforwardkeeper.Keeper - - ScopedIBCKeeper capabilitykeeper.ScopedKeeper - ScopedICAHostKeeper capabilitykeeper.ScopedKeeper - ScopedICAControllerKeeper capabilitykeeper.ScopedKeeper - ScopedTransferKeeper capabilitykeeper.ScopedKeeper - ScopedIBCFeeKeeper capabilitykeeper.ScopedKeeper - OracleKeeper oraclekeeper.Keeper - DidKeeper didkeeper.Keeper - - // the module manager - ModuleManager *module.Manager + BankKeeper bankkeeper.BaseKeeper + AccountKeeper authkeeper.AccountKeeper + GroupKeeper groupkeeper.Keeper + TokenFactoryKeeper tokenfactorykeeper.Keeper + IBCFeeKeeper ibcfeekeeper.Keeper + ParamsKeeper paramskeeper.Keeper + NFTKeeper nftkeeper.Keeper + AuthzKeeper authzkeeper.Keeper + FeeGrantKeeper feegrantkeeper.Keeper + OracleKeeper oraclekeeper.Keeper + interfaceRegistry types.InterfaceRegistry + txConfig client.TxConfig + appCodec codec.Codec + configurator module.Configurator + StakingKeeper *stakingkeeper.Keeper + tkeys map[string]*storetypes.TransientStoreKey + CrisisKeeper *crisiskeeper.Keeper + UpgradeKeeper *upgradekeeper.Keeper + legacyAmino *codec.LegacyAmino + sm *module.SimulationManager BasicModuleManager module.BasicManager - - // simulation manager - sm *module.SimulationManager - - // module configurator - configurator module.Configurator - once sync.Once + ModuleManager *module.Manager + *baseapp.BaseApp + CapabilityKeeper *capabilitykeeper.Keeper + keys map[string]*storetypes.KVStoreKey + PacketForwardKeeper *packetforwardkeeper.Keeper + IBCKeeper *ibckeeper.Keeper + memKeys map[string]*storetypes.MemoryStoreKey + GovKeeper govkeeper.Keeper + DidKeeper didkeeper.Keeper + POAKeeper poakeeper.Keeper + DistrKeeper distrkeeper.Keeper + MintKeeper mintkeeper.Keeper + CircuitKeeper circuitkeeper.Keeper + EvidenceKeeper evidencekeeper.Keeper + ICAHostKeeper icahostkeeper.Keeper + TransferKeeper ibctransferkeeper.Keeper + ICAControllerKeeper icacontrollerkeeper.Keeper + ConsensusParamsKeeper consensusparamkeeper.Keeper + ScopedIBCFeeKeeper capabilitykeeper.ScopedKeeper + ScopedTransferKeeper capabilitykeeper.ScopedKeeper + ScopedICAControllerKeeper capabilitykeeper.ScopedKeeper + SlashingKeeper slashingkeeper.Keeper + ScopedICAHostKeeper capabilitykeeper.ScopedKeeper + ScopedIBCKeeper capabilitykeeper.ScopedKeeper + GlobalFeeKeeper globalfeekeeper.Keeper + once sync.Once } // NewChainApp returns a reference to an initialized ChainApp. diff --git a/internal/env/context.go b/app/context.go similarity index 98% rename from internal/env/context.go rename to app/context.go index 97a232c54..79de9d673 100644 --- a/internal/env/context.go +++ b/app/context.go @@ -1,4 +1,4 @@ -package env +package app import ( "context" diff --git a/internal/env/local.go b/app/local.go similarity index 95% rename from internal/env/local.go rename to app/local.go index e449e9d0a..522e95760 100644 --- a/internal/env/local.go +++ b/app/local.go @@ -1,4 +1,4 @@ -package env +package app import ( "github.com/ipfs/kubo/client/rpc" @@ -7,7 +7,6 @@ import ( var ( ChainID = "testnet" ValAddr = "val1" - NodeDir = ".sonr" ) // Initialize initializes the local configuration values diff --git a/cmd/sonrd/root.go b/cmd/sonrd/root.go index e456a28fd..981eec5ae 100644 --- a/cmd/sonrd/root.go +++ b/cmd/sonrd/root.go @@ -22,7 +22,6 @@ import ( "github.com/onsonr/hway/app" "github.com/onsonr/hway/app/params" - "github.com/onsonr/hway/internal/env" // NewRootCmd creates a new root command for chain app. It is called once in the // main function. ) @@ -100,8 +99,8 @@ func NewRootCmd() *cobra.Command { } // Set the context chain ID and validator address - env.SetLocalChainID(initClientCtx.ChainID) - env.SetLocalValidatorAddress(initClientCtx.FromAddress.String()) + app.SetLocalChainID(initClientCtx.ChainID) + app.SetLocalValidatorAddress(initClientCtx.FromAddress.String()) customAppTemplate, customAppConfig := initAppConfig() customCMTConfig := initCometBFTConfig() diff --git a/pkg/vfs/file.go b/pkg/vfs/file.go deleted file mode 100644 index 7d0e96635..000000000 --- a/pkg/vfs/file.go +++ /dev/null @@ -1,96 +0,0 @@ -package vfs - -import ( - "fmt" - "io/fs" - "os" - "path/filepath" - - "github.com/ipfs/boxo/files" -) - -// File represents a file in the filesystem -type File string - -// NewFile creates a new File instance -func NewFile(path string) File { - return File(path) -} - -// Name returns the name of the file -func (f File) Name() string { - return filepath.Base(string(f)) -} - -// Path returns the path of the file -func (f File) Path() string { - return string(f) -} - -// Read reads the contents of the file -func (f File) Read() ([]byte, error) { - return os.ReadFile(string(f)) -} - -// Write writes data to the file, but throws an error if the file already exists -func (f File) Write(data []byte) error { - if f.Exists() { - return fmt.Errorf("file already exists: %s", f) - } - return os.WriteFile(string(f), data, 0644) -} - -// Append appends data to the file -func (f File) Append(data []byte) error { - file, err := os.OpenFile(string(f), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644) - if err != nil { - return err - } - defer file.Close() - - _, err = file.Write(data) - return err -} - -// Overwrite deletes all data for the file at path if it exists and writes the new data -func (f File) Overwrite(data []byte) error { - // If the file exists, remove it first - if f.Exists() { - if err := f.Remove(); err != nil { - return fmt.Errorf("failed to remove existing file: %w", err) - } - } - - // Write the new data - return os.WriteFile(string(f), data, 0644) -} - -// Exists checks if the file exists -func (f File) Exists() bool { - _, err := os.Stat(string(f)) - return !os.IsNotExist(err) -} - -// Remove removes the file -func (f File) Remove() error { - return os.Remove(string(f)) -} - -// Stat returns the FileInfo for the file -func (f File) Stat() (os.FileInfo, error) { - return os.Stat(string(f)) -} - -// Open opens the file and returns an fs.File -func (f File) Open() (fs.File, error) { - return os.Open(string(f)) -} - -// Node returns a files.Node representation of the file -func (f File) Node() (files.Node, error) { - file, err := os.Open(string(f)) - if err != nil { - return nil, err - } - return files.NewReaderFile(file), nil -} diff --git a/pkg/vfs/folder.go b/pkg/vfs/folder.go deleted file mode 100644 index 49c4020c9..000000000 --- a/pkg/vfs/folder.go +++ /dev/null @@ -1,120 +0,0 @@ -package vfs - -import ( - "io/fs" - "os" - "path/filepath" - - "github.com/ipfs/boxo/files" -) - -// Folder represents a folder in the filesystem -type Folder string - -// NewFolder creates a new Folder instance -func NewFolder(path string) Folder { - return Folder(path) -} - -// Name returns the name of the folder -func (f Folder) Name() string { - return filepath.Base(string(f)) -} - -// Path returns the path of the folder -func (f Folder) Path() string { - return string(f) -} - -// Create creates the folder if it doesn't exist -func (f Folder) Create() error { - return os.MkdirAll(string(f), os.ModePerm) -} - -// Exists checks if the folder exists -func (f Folder) Exists() bool { - info, err := os.Stat(string(f)) - return err == nil && info.IsDir() -} - -// Ls lists the contents of the folder -func (f Folder) Ls() ([]fs.DirEntry, error) { - return os.ReadDir(string(f)) -} - -// WriteFile writes data to a file in the folder -func (f Folder) WriteFile(name string, data []byte, perm os.FileMode) (File, error) { - err := os.WriteFile(filepath.Join(string(f), name), data, perm) - return File(filepath.Join(string(f), name)), err -} - -// ReadFile reads the contents of a file in the folder -func (f Folder) ReadFile(name string) ([]byte, error) { - return os.ReadFile(filepath.Join(string(f), name)) -} - -// DeleteFile deletes a file from the folder -func (f Folder) DeleteFile(name string) error { - return os.Remove(filepath.Join(string(f), name)) -} - -// Remove removes the folder and its contents -func (f Folder) Remove() error { - return os.RemoveAll(string(f)) -} - -// Join joins the folder path with the given elements -func (f Folder) Join(elem ...string) Folder { - return Folder(filepath.Join(append([]string{string(f)}, elem...)...)) -} - -// IsDir checks if the folder is a directory -func (f Folder) IsDir() bool { - info, err := os.Stat(string(f)) - return err == nil && info.IsDir() -} - -// Touch creates an empty file if it doesn't exist, or updates its access and modification times if it does -func (f Folder) Touch(name string) (File, error) { - var err error - filePath := filepath.Join(string(f), name) - _, err = os.Stat(filePath) - if os.IsNotExist(err) { - file, err := os.Create(filePath) - if err != nil { - return File(filePath), err - } - if err := file.Close(); err != nil { - return File(filePath), err - } - return File(filePath), nil - } - return File(filePath), err -} - -// Node returns a files.Node representation of the folder -func (f Folder) Node() (files.Node, error) { - entries, err := f.Ls() - if err != nil { - return nil, err - } - dirEntries := make([]files.DirEntry, 0, len(entries)) - for _, entry := range entries { - if entry.IsDir() { - subFolder := f.Join(entry.Name()) - subNode, err := subFolder.Node() - if err != nil { - return nil, err - } - dirEntries = append(dirEntries, files.FileEntry(entry.Name(), subNode)) - } else { - file := File(filepath.Join(string(f), entry.Name())) - fileNode, err := file.Node() - if err != nil { - return nil, err - } - dirEntries = append(dirEntries, files.FileEntry(entry.Name(), fileNode)) - } - } - return files.NewSliceDirectory(dirEntries), nil -} diff --git a/pkg/vfs/ipfs.go b/pkg/vfs/ipfs.go deleted file mode 100644 index 6dc5fcf45..000000000 --- a/pkg/vfs/ipfs.go +++ /dev/null @@ -1,93 +0,0 @@ -package vfs - -import ( - "context" - "os" - "path/filepath" - - "github.com/ipfs/boxo/path" - "github.com/ipfs/kubo/core/coreiface/options" - "github.com/onsonr/hway/internal/env" -) - -// Constant for the name of the folder where the vaults are stored -const kVaultsFolderName = ".sonr-vaults" - -// enclaveRoot is the folder where the vaults are stored -var enclaveRoot Folder - -// Package initializes the VaultsFolder -func init() { - // Initialize VaultsFolder - homeDir, err := os.UserHomeDir() - if err != nil { - panic(err) - } - - // Create the folder if it does not exist - enclaveRoot = NewFolder(filepath.Join(homeDir, kVaultsFolderName)) - if !enclaveRoot.Exists() { - if err := enclaveRoot.Create(); err != nil { - panic(err) - } - } -} - -// NewVaultFolder creates a new folder under the VaultsFolder directory -func NewVaultFolder(name string) (Folder, error) { - vaultFolder := enclaveRoot.Join(name) - err := vaultFolder.Create() - if err != nil { - return "", err - } - return vaultFolder, nil -} - -// SaveToIPFS saves the Folder to IPFS and returns the IPFS path -func SyncFolderToIPFS(ctx context.Context, f Folder) (path.Path, error) { - node, err := f.Node() - if err != nil { - return nil, err - } - c, err := env.GetIPFSClient() - if err != nil { - return nil, err - } - - path, err := c.Unixfs().Add(ctx, node) - if err != nil { - return nil, err - } - - return path, nil -} - -// PublishToIPNS publishes the Folder to IPNS -func PublishToIPNS(ctx context.Context, ipfsPath path.Path, f Folder) error { - c, err := env.GetIPFSClient() - if err != nil { - return err - } - _, err = c.Name().Publish(ctx, ipfsPath, options.Name.Key(f.Name())) - if err != nil { - return err - } - return nil -} - -// LoadFromIPFS loads a Folder from IPFS -func LoadFromIPFS(ctx context.Context, path string) (Folder, error) { - c, err := env.GetIPFSClient() - if err != nil { - return "", err - } - cid, err := ParsePath(path) - if err != nil { - return "", err - } - node, err := c.Unixfs().Get(ctx, cid) - if err != nil { - return "", err - } - return LoadNodeInFolder(path, node) -} diff --git a/pkg/vfs/utils.go b/pkg/vfs/utils.go deleted file mode 100644 index 0fd5deb21..000000000 --- a/pkg/vfs/utils.go +++ /dev/null @@ -1,58 +0,0 @@ -package vfs - -import ( - "fmt" - "io" - "os" - "path/filepath" - - "github.com/ipfs/boxo/files" - "github.com/ipfs/boxo/path" -) - -// Helper function to parse IPFS path -func ParsePath(p string) (path.Path, error) { - return path.NewPath(p) -} - -// FetchVaultPath returns the path to the vault with the given name -func FetchVaultPath(name string) string { - return enclaveRoot.Join(name).Path() -} - -// LoadNodeInFolder loads an IPFS node into a Folder -func LoadNodeInFolder(path string, node files.Node) (Folder, error) { - folder := NewFolder(path) - if err := folder.Create(); err != nil { - return "", err - } - - switch n := node.(type) { - case files.File: - f, err := os.Create(folder.Path()) - if err != nil { - return "", err - } - defer f.Close() - if _, err := io.Copy(f, n); err != nil { - return "", err - } - case files.Directory: - entries := n.Entries() - for entries.Next() { - name := entries.Name() - childNode := entries.Node() - childPath := filepath.Join(folder.Path(), name) - if _, err := LoadNodeInFolder(childPath, childNode); err != nil { - return "", err - } - } - if entries.Err() != nil { - return "", entries.Err() - } - default: - return "", fmt.Errorf("unsupported node type: %T", n) - } - - return folder, nil -}