Skip to content

Commit

Permalink
Add validations to CLI flags
Browse files Browse the repository at this point in the history
  • Loading branch information
someone235 committed Sep 16, 2024
1 parent 8e8f7c9 commit 484c17e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 13 deletions.
12 changes: 5 additions & 7 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
name: Build and upload assets
on:
release:
types: [ published ]
types: [published]

jobs:
build:
runs-on: ${{ matrix.os }}
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
Expand All @@ -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:
Expand All @@ -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/*
Expand All @@ -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}\""
Expand All @@ -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:
Expand Down
63 changes: 57 additions & 6 deletions cmd/kaspawallet/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 484c17e

Please sign in to comment.