forked from decred/vspd
-
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.
vspadmin: New binary to create empty databases.
vspadmin is a new binary which implements one-off admin tasks which are necessarily interactive and thus do not fit neatly into the long-lived vspd binary. The first behaviour added to vspadmin is the ability to create a new vspd database. This behaviour is removed from vspd with the associated config option deprecated. Documentation and scripts updated to reflect the change.
- Loading branch information
1 parent
0633260
commit 997205e
Showing
6 changed files
with
177 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# vspadmin | ||
|
||
vspadmin is a tool to perform various VSP administration tasks. | ||
|
||
## Usage | ||
|
||
```no-highlight | ||
vspadmin [OPTIONS] COMMAND | ||
``` | ||
|
||
## Options | ||
|
||
```no-highlight | ||
--homedir= Path to application home directory. (default: /home/user/.vspd) | ||
--network=[mainnet|testnet|simnet] Decred network to use. (default: mainnet) | ||
-h, --help Show help message | ||
``` | ||
|
||
## Commands | ||
|
||
### `createdatabase` | ||
|
||
Creates a new database for a new deployment of vspd. Accepts the xpub key to be | ||
used for collecting fees as a parameter. | ||
|
||
Example: | ||
|
||
```no-highlight | ||
go run ./cmd/vspadmin createdatabase <xpub> | ||
``` |
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,130 @@ | ||
// Copyright (c) 2024 The Decred developers | ||
// Use of this source code is governed by an ISC | ||
// license that can be found in the LICENSE file. | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/decred/dcrd/dcrutil/v4" | ||
"github.com/decred/dcrd/hdkeychain/v3" | ||
"github.com/decred/vspd/database" | ||
"github.com/decred/vspd/internal/config" | ||
"github.com/jessevdk/go-flags" | ||
) | ||
|
||
const ( | ||
dbFilename = "vspd.db" | ||
) | ||
|
||
type conf struct { | ||
HomeDir string `long:"homedir" description:"Path to application home directory."` | ||
Network string `long:"network" description:"Decred network to use." choice:"mainnet" choice:"testnet" choice:"simnet"` | ||
} | ||
|
||
var defaultConf = conf{ | ||
HomeDir: dcrutil.AppDataDir("vspd", false), | ||
Network: "mainnet", | ||
} | ||
|
||
func log(format string, a ...any) { | ||
fmt.Printf(format+"\n", a...) | ||
} | ||
|
||
// fileExists reports whether the named file or directory exists. | ||
func fileExists(name string) bool { | ||
if _, err := os.Stat(name); os.IsNotExist(err) { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func createDatabase(homeDir string, feeXPub string, network *config.Network) error { | ||
dataDir := filepath.Join(homeDir, "data", network.Name) | ||
dbFile := filepath.Join(dataDir, dbFilename) | ||
|
||
// Return error if database already exists. | ||
if fileExists(dbFile) { | ||
return fmt.Errorf("%s database already exists in %s", network.Name, dataDir) | ||
} | ||
|
||
// Ensure provided xpub is a valid key for the selected network. | ||
_, err := hdkeychain.NewKeyFromString(feeXPub, network.Params) | ||
if err != nil { | ||
return fmt.Errorf("failed to parse feexpub: %w", err) | ||
} | ||
|
||
// Ensure the data directory exists. | ||
err = os.MkdirAll(dataDir, 0700) | ||
if err != nil { | ||
return fmt.Errorf("failed to create data directory: %w", err) | ||
} | ||
|
||
// Create new database. | ||
err = database.CreateNew(dbFile, feeXPub) | ||
if err != nil { | ||
return fmt.Errorf("error creating db file %s: %w", dbFile, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// run is the real main function for vspadmin. It is necessary to work around | ||
// the fact that deferred functions do not run when os.Exit() is called. | ||
func run() int { | ||
cfg := defaultConf | ||
|
||
// If command line options are requesting help, write it to stdout and exit. | ||
if config.WriteHelp(&cfg) { | ||
return 0 | ||
} | ||
|
||
// Parse command line options. | ||
remainingArgs, err := flags.Parse(&cfg) | ||
if err != nil { | ||
// Don't need to log the error, flags lib has already done it. | ||
return 1 | ||
} | ||
|
||
network, err := config.NetworkFromName(cfg.Network) | ||
if err != nil { | ||
log("%v", err) | ||
return 1 | ||
} | ||
|
||
if len(remainingArgs) < 1 { | ||
log("No command specified") | ||
return 1 | ||
} | ||
|
||
switch remainingArgs[0] { | ||
case "createdatabase": | ||
if len(remainingArgs) != 2 { | ||
log("createdatabase has one required argument, fee xpub") | ||
return 1 | ||
} | ||
|
||
feeXPub := remainingArgs[1] | ||
|
||
err = createDatabase(cfg.HomeDir, feeXPub, network) | ||
if err != nil { | ||
log("createdatabase failed: %v", err) | ||
return 1 | ||
} | ||
|
||
log("New %s vspd database created in %s", network.Name, cfg.HomeDir) | ||
|
||
default: | ||
log("%q is not a valid command", remainingArgs[0]) | ||
return 1 | ||
} | ||
|
||
return 0 | ||
} | ||
|
||
func main() { | ||
os.Exit(run()) | ||
} |
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
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