Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require -numUnits in postcli #241

Merged
merged 2 commits into from
Sep 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions cmd/postcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"os"
"os/signal"
"path/filepath"
"strconv"

"github.com/davecgh/go-spew/spew"
"go.uber.org/zap"
Expand All @@ -28,6 +29,27 @@ import (

const edKeyFileName = "key.bin"

type numUnitsFlag struct {
set bool
value uint32
}

func (nu *numUnitsFlag) Set(s string) error {
val, err := strconv.ParseUint(s, 10, 32)
if err != nil {
return err
}
*nu = numUnitsFlag{
set: true,
value: uint32(val),
}
return nil
}

func (nu *numUnitsFlag) String() string {
return fmt.Sprintf("%d", nu.value)
}

var (
cfg = config.MainnetConfig()
opts = config.MainnetInitOpts()
Expand All @@ -46,6 +68,7 @@ var (
commitmentAtxIdHex string
commitmentAtxId []byte
reset bool
numUnits numUnitsFlag

logLevel zapcore.Level

Expand All @@ -72,7 +95,7 @@ func parseFlags() {
flag.BoolVar(&reset, "reset", false, "whether to reset the datadir before starting")
flag.StringVar(&idHex, "id", "", "miner's id (public key), in hex (will be auto-generated if not provided)")
flag.StringVar(&commitmentAtxIdHex, "commitmentAtxId", "", "commitment atx id, in hex (required)")
numUnits := flag.Uint64("numUnits", uint64(opts.NumUnits), "number of units")
flag.Var(&numUnits, "numUnits", "number of units")

flag.IntVar(&opts.FromFileIdx, "fromFile", 0, "index of the first file to init (inclusive)")
var to int
Expand All @@ -89,10 +112,19 @@ func parseFlags() {
*opts.ProviderID = uint32(providerID)
}

opts.NumUnits = uint32(*numUnits) // workaround the missing type support for uint32
if numUnits.set {
opts.NumUnits = numUnits.value
}
}

func processFlags() error {
// we require the user to explicitly pass numunits to avoid erasing existing data
if !numUnits.set {
return fmt.Errorf("-numUnits must be specified to perform initialization. to use the default value, "+
"run with -numUnits %d. note: if there's more than this amount of data on disk, "+
"THIS WILL ERASE EXISTING DATA. MAKE ABSOLUTELY SURE YOU SPECIFY THE CORRECT VALUE", opts.NumUnits)
}

if opts.ProviderID == nil {
return errors.New("-provider flag is required")
}
Expand Down Expand Up @@ -206,7 +238,7 @@ func main() {
case errors.Is(err, ErrKeyFileExists):
log.Fatalln("cli: key file already exists. This appears to be a mistake. If you're trying to initialize a new identity delete key.bin and try again otherwise specify identity with `-id` flag")
case err != nil:
log.Fatalln("failed to process flags", err)
log.Fatalln("failed to process flags:", err)
}

init, err := initialization.NewInitializer(
Expand Down