From 484c17ebb12dd4884cbf233946b8266df8ea51e4 Mon Sep 17 00:00:00 2001 From: Ori Newman Date: Mon, 16 Sep 2024 13:27:34 +0300 Subject: [PATCH] Add validations to CLI flags --- .github/workflows/deploy.yaml | 12 +++---- cmd/kaspawallet/config.go | 63 +++++++++++++++++++++++++++++++---- 2 files changed, 62 insertions(+), 13 deletions(-) diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index 1525aae980..e9188f4080 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -1,7 +1,7 @@ name: Build and upload assets on: release: - types: [ published ] + types: [published] jobs: build: @@ -9,7 +9,7 @@ jobs: strategy: fail-fast: false matrix: - os: [ ubuntu-latest, windows-latest, macos-latest ] + os: [ubuntu-latest, windows-latest, macos-latest] name: Building, ${{ matrix.os }} steps: - name: Fix CRLF on Windows @@ -19,7 +19,6 @@ jobs: - name: Check out code into the Go module directory uses: actions/checkout@v2 - - name: Setup Go uses: actions/setup-go@v2 with: @@ -31,7 +30,7 @@ jobs: # `-tags netgo,osusergo` means use pure go replacements for "os/user" and "net" # `-s -w` strips the binary to produce smaller size binaries run: | - go build -v -ldflags="-s -w -extldflags=-static" -tags netgo,osusergo -o ./bin/ . ./cmd/... + go build -v -ldflags="-s -w -extldflags=-static" -tags netgo,osusergo -o ./bin/ ./cmd/... archive="bin/kaspad-${{ github.event.release.tag_name }}-linux.zip" asset_name="kaspad-${{ github.event.release.tag_name }}-linux.zip" zip -r "${archive}" ./bin/* @@ -42,7 +41,7 @@ jobs: if: runner.os == 'Windows' shell: bash run: | - go build -v -ldflags="-s -w" -o bin/ . ./cmd/... + go build -v -ldflags="-s -w" -o bin/ ./cmd/... archive="bin/kaspad-${{ github.event.release.tag_name }}-win64.zip" asset_name="kaspad-${{ github.event.release.tag_name }}-win64.zip" powershell "Compress-Archive bin/* \"${archive}\"" @@ -52,14 +51,13 @@ jobs: - name: Build on MacOS if: runner.os == 'macOS' run: | - go build -v -ldflags="-s -w" -o ./bin/ . ./cmd/... + go build -v -ldflags="-s -w" -o ./bin/ ./cmd/... archive="bin/kaspad-${{ github.event.release.tag_name }}-osx.zip" asset_name="kaspad-${{ github.event.release.tag_name }}-osx.zip" zip -r "${archive}" ./bin/* echo "archive=${archive}" >> $GITHUB_ENV echo "asset_name=${asset_name}" >> $GITHUB_ENV - - name: Upload release asset uses: actions/upload-release-asset@v1 env: diff --git a/cmd/kaspawallet/config.go b/cmd/kaspawallet/config.go index 64656e2e5c..8d96d30584 100644 --- a/cmd/kaspawallet/config.go +++ b/cmd/kaspawallet/config.go @@ -360,13 +360,25 @@ func parseCommandLine() (subCommand string, config interface{}) { if err != nil { printErrorAndExit(err) } + + err = validateBumpFeeConfig(bumpFeeConf) + if err != nil { + printErrorAndExit(err) + } + config = bumpFeeConf case bumpFeeUnsignedSubCmd: - combineNetworkFlags(&bumpFeeConf.NetworkFlags, &cfg.NetworkFlags) - err := bumpFeeConf.ResolveNetwork(parser) + combineNetworkFlags(&bumpFeeUnsignedConf.NetworkFlags, &cfg.NetworkFlags) + err := bumpFeeUnsignedConf.ResolveNetwork(parser) + if err != nil { + printErrorAndExit(err) + } + + err = validateBumpFeeUnsignedConfig(bumpFeeUnsignedConf) if err != nil { printErrorAndExit(err) } + config = bumpFeeUnsignedConf } @@ -388,8 +400,8 @@ func validateCreateUnsignedTransactionConf(conf *createUnsignedTransactionConfig return errors.New("--fee-rate must be a positive number") } - if conf.MaxFeeRate > 0 && conf.FeeRate > 0 { - return errors.New("at most one of '--max-fee-rate' or '--fee-rate' can be specified") + if boolToUint8(conf.MaxFeeRate > 0)+boolToUint8(conf.FeeRate > 0)+boolToUint8(conf.MaxFee > 0) > 1 { + return errors.New("at most one of '--max-fee-rate', '--fee-rate' or '--max-fee' can be specified") } return nil @@ -410,13 +422,52 @@ func validateSendConfig(conf *sendConfig) error { return errors.New("--fee-rate must be a positive number") } - if conf.MaxFeeRate > 0 && conf.FeeRate > 0 { - return errors.New("at most one of '--max-fee-rate' or '--fee-rate' can be specified") + if boolToUint8(conf.MaxFeeRate > 0)+boolToUint8(conf.FeeRate > 0)+boolToUint8(conf.MaxFee > 0) > 1 { + return errors.New("at most one of '--max-fee-rate', '--fee-rate' or '--max-fee' can be specified") } return nil } +func validateBumpFeeConfig(conf *bumpFeeConfig) error { + if conf.MaxFeeRate < 0 { + return errors.New("--max-fee-rate must be a positive number") + } + + if conf.FeeRate < 0 { + return errors.New("--fee-rate must be a positive number") + } + + if boolToUint8(conf.MaxFeeRate > 0)+boolToUint8(conf.FeeRate > 0)+boolToUint8(conf.MaxFee > 0) > 1 { + return errors.New("at most one of '--max-fee-rate', '--fee-rate' or '--max-fee' can be specified") + } + + return nil +} + +func validateBumpFeeUnsignedConfig(conf *bumpFeeUnsignedConfig) error { + if conf.MaxFeeRate < 0 { + return errors.New("--max-fee-rate must be a positive number") + } + + if conf.FeeRate < 0 { + return errors.New("--fee-rate must be a positive number") + } + + if boolToUint8(conf.MaxFeeRate > 0)+boolToUint8(conf.FeeRate > 0)+boolToUint8(conf.MaxFee > 0) > 1 { + return errors.New("at most one of '--max-fee-rate', '--fee-rate' or '--max-fee' can be specified") + } + + return nil +} + +func boolToUint8(b bool) uint8 { + if b { + return 1 + } + return 0 +} + func combineNetworkFlags(dst, src *config.NetworkFlags) { dst.Testnet = dst.Testnet || src.Testnet dst.Simnet = dst.Simnet || src.Simnet