Skip to content

Commit

Permalink
SPV-1108(feat) Bump-golangci-lint-version-in-spv-wallet (#746)
Browse files Browse the repository at this point in the history
Co-authored-by: Augustyn Chmiel <augustyn.chmiel@4chain.studio>
  • Loading branch information
ac4ch and ac4ch authored Oct 23, 2024
1 parent 1c50229 commit aa64c8d
Show file tree
Hide file tree
Showing 33 changed files with 273 additions and 61 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ jobs:
with:
version: ${{ github.ref_name }}
cgo_enabled: true
release_create: false # important! we don't want to create a release with this one, because we're doing it in release_default
# important! we don't want to create a release with this one, because we're doing it in release_default
release_create: false
docker_registry: public.ecr.aws
docker_org: ${{ vars.SPV_AWS_DOCKER_ORG }}
secrets:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.60.1
version: v1.61.0
args: --config=./.golangci-lint.yml
style-lint:
name: style-lint
Expand All @@ -64,7 +64,7 @@ jobs:
- name: golangci-style-lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.60.1
version: v1.61.0
args: --config=./.golangci-style.yml
test:
needs: [yamllint, asknancy]
Expand Down
2 changes: 1 addition & 1 deletion .golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ linters:
- unconvert
- ineffassign
- dogsled
- exportloopref
- copyloopvar
- sqlclosecheck
- nolintlint
- errcheck
Expand Down
9 changes: 7 additions & 2 deletions config/config_to_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
"net/url"
"time"

"github.com/bitcoin-sv/spv-wallet/conv"
"github.com/bitcoin-sv/spv-wallet/engine"
"github.com/bitcoin-sv/spv-wallet/engine/chain/models"
chainmodels "github.com/bitcoin-sv/spv-wallet/engine/chain/models"
"github.com/bitcoin-sv/spv-wallet/engine/cluster"
"github.com/bitcoin-sv/spv-wallet/engine/datastore"
"github.com/bitcoin-sv/spv-wallet/engine/spverrors"
Expand Down Expand Up @@ -70,8 +71,12 @@ func (c *AppConfig) addHttpClientOpts(options []engine.ClientOps) []engine.Clien

func (c *AppConfig) addCustomFeeUnit(options []engine.ClientOps) []engine.ClientOps {
if c.CustomFeeUnit != nil {
satoshis, err := conv.IntToUint64(c.CustomFeeUnit.Satoshis)
if err != nil {
panic(spverrors.Wrapf(err, "error converting custom fee unit satoshis"))
}
options = append(options, engine.WithCustomFeeUnit(bsv.FeeUnit{
Satoshis: bsv.Satoshis(c.CustomFeeUnit.Satoshis),
Satoshis: bsv.Satoshis(satoshis),
Bytes: c.CustomFeeUnit.Bytes,
}))
}
Expand Down
75 changes: 75 additions & 0 deletions conv/convert_primitives.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package conv

import (
"math"

sdk "github.com/bitcoin-sv/go-sdk/transaction"
"github.com/bitcoin-sv/spv-wallet/engine/spverrors"
)

// Int64ToUint32 will convert an int64 to a uint32, with range checks
func Int64ToUint32(value int64) (uint32, error) {
if value < 0 || value > math.MaxUint32 {
return 0, spverrors.ErrInvalidUint32
}
return uint32(value), nil
}

// Uint32ToInt64 will convert a uint32 to an int64 (safe as uint32 fits into int64)
func Uint32ToInt64(value uint32) int64 {
return int64(value)
}

// Uint64ToInt64 will convert a uint64 to an int64, with range checks
func Uint64ToInt64(value uint64) (int64, error) {
if value > math.MaxInt64 {
return 0, spverrors.ErrInvalidInt64
}
return int64(value), nil
}

// Int64ToUint64 will convert an int64 to a uint64, with range checks
func Int64ToUint64(value int64) (uint64, error) {
if value < 0 {
return 0, spverrors.ErrInvalidUint64
}
return uint64(value), nil
}

// Uint64ToInt will convert a uint64 to an int, with range checks
func Uint64ToInt(value uint64) (int, error) {
if value > math.MaxInt {
return 0, spverrors.ErrInvalidInt
}
return int(value), nil
}

// IntToUint64 will convert an int to a uint64, with range checks
func IntToUint64(value int) (uint64, error) {
if value < 0 {
return 0, spverrors.ErrInvalidUint64
}
return uint64(value), nil
}

// IntToUint32 will convert an int to a uint32, with range checks
func IntToUint32(value int) (uint32, error) {
if value < 0 || value > math.MaxUint32 {
return 0, spverrors.ErrInvalidUint32
}
return uint32(value), nil
}

// VarIntToInt will convert a VarInt to an int, with range checks
func VarIntToInt(varInt *sdk.VarInt) (int, error) {
if varInt == nil {
return 0, spverrors.ErrInvalidInt
}
i := uint64(*varInt)
// Ensure VarInt is not negative and within the range of int
if i > uint64(math.MaxInt) {
return 0, spverrors.ErrInvalidInt
}
// Convert the VarInt to an int
return int(i), nil
}
9 changes: 7 additions & 2 deletions engine/action_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"time"

trx "github.com/bitcoin-sv/go-sdk/transaction"
"github.com/bitcoin-sv/spv-wallet/engine/chain/models"
"github.com/bitcoin-sv/spv-wallet/conv"
chainmodels "github.com/bitcoin-sv/spv-wallet/engine/chain/models"
"github.com/bitcoin-sv/spv-wallet/engine/datastore"
"github.com/bitcoin-sv/spv-wallet/engine/spverrors"
"github.com/bitcoin-sv/spv-wallet/engine/utils"
Expand Down Expand Up @@ -376,7 +377,11 @@ func (c *Client) HandleTxCallback(ctx context.Context, callbackResp *chainmodels
}

tx.BlockHash = callbackResp.BlockHash
tx.BlockHeight = uint64(callbackResp.BlockHeight)
blockHeight, err := conv.Int64ToUint64(callbackResp.BlockHeight)
if err != nil {
return spverrors.Wrapf(err, "failed to convert block height to uint64 - tx: %v", callbackResp.BlockHeight)
}
tx.BlockHeight = blockHeight
tx.SetBUMP(bump)
tx.UpdateFromBroadcastStatus(callbackResp.TXStatus)

Expand Down
7 changes: 6 additions & 1 deletion engine/beef_tx_bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package engine
import (
trx "github.com/bitcoin-sv/go-sdk/transaction"
"github.com/bitcoin-sv/go-sdk/util"
"github.com/bitcoin-sv/spv-wallet/conv"
"github.com/bitcoin-sv/spv-wallet/engine/spverrors"
)

Expand Down Expand Up @@ -62,7 +63,11 @@ func toBeefBytes(tx *trx.Transaction, bumps BUMPs) []byte {
bumpIdx := getBumpPathIndex(tx, bumps)
if bumpIdx > -1 {
txBeefBytes = append(txBeefBytes, hasBUMP)
txBeefBytes = append(txBeefBytes, trx.VarInt(bumpIdx).Bytes()...)
idx, err := conv.IntToUint64(bumpIdx)
if err != nil {
panic(spverrors.Wrapf(err, "error converting bump index"))
}
txBeefBytes = append(txBeefBytes, trx.VarInt(idx).Bytes()...)
} else {
txBeefBytes = append(txBeefBytes, hasNoBUMP)
}
Expand Down
2 changes: 1 addition & 1 deletion engine/chain/internal/arc/broadcast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package arc_test

import (
"context"
"iter"
"testing"
"time"

Expand All @@ -11,7 +12,6 @@ import (
"github.com/bitcoin-sv/spv-wallet/engine/chain/models"
"github.com/bitcoin-sv/spv-wallet/engine/tester"
"github.com/stretchr/testify/require"
"iter"
)

func TestBroadcastTransaction(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion engine/chain/internal/combined_txs_getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package internal

import (
"context"
"iter"
"maps"
"slices"

sdk "github.com/bitcoin-sv/go-sdk/transaction"
"github.com/bitcoin-sv/spv-wallet/engine/chain/errors"
"github.com/bitcoin-sv/spv-wallet/engine/chain/models"
"github.com/bitcoin-sv/spv-wallet/engine/spverrors"
"iter"
)

// CombineTxsGetters creates a new CombinedTxsGetter
Expand Down
2 changes: 1 addition & 1 deletion engine/chain/internal/combined_txs_getter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package internal_test
import (
"context"
"errors"
"iter"
"testing"
"time"

sdk "github.com/bitcoin-sv/go-sdk/transaction"
"github.com/bitcoin-sv/spv-wallet/engine/chain/internal"
"github.com/bitcoin-sv/spv-wallet/engine/chain/models"
"github.com/stretchr/testify/require"
"iter"
)

type mockTxsGetter struct {
Expand Down
2 changes: 1 addition & 1 deletion engine/chain/internal/ef/mock_txgetter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package ef_test

import (
"context"
"iter"
"slices"
"testing"

sdk "github.com/bitcoin-sv/go-sdk/transaction"
"github.com/bitcoin-sv/spv-wallet/engine/spverrors"
"iter"
)

type onMissingTxBehavior int
Expand Down
2 changes: 1 addition & 1 deletion engine/chain/internal/ef/unsourced_inputs.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package ef

import (
"iter"
"maps"

sdk "github.com/bitcoin-sv/go-sdk/transaction"
"github.com/bitcoin-sv/spv-wallet/engine/spverrors"
"iter"
)

type unsourcedInputs struct {
Expand Down
2 changes: 1 addition & 1 deletion engine/chain/internal/junglebus/txs_getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package junglebus
import (
"context"
"errors"
"iter"

sdk "github.com/bitcoin-sv/go-sdk/transaction"
chainerrors "github.com/bitcoin-sv/spv-wallet/engine/chain/errors"
"github.com/bitcoin-sv/spv-wallet/engine/spverrors"
"iter"
)

// GetTransactions implements chainmodels.TransactionsGetter interface to allow fetching transactions from Junglebus
Expand Down
2 changes: 1 addition & 1 deletion engine/chain/models/arc_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package chainmodels

import (
"context"
"iter"

sdk "github.com/bitcoin-sv/go-sdk/transaction"
"iter"
)

// TransactionsGetter is an interface for getting transactions by their IDs
Expand Down
11 changes: 9 additions & 2 deletions engine/datastore/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"math"
"reflect"
"strings"
"time"
Expand Down Expand Up @@ -124,10 +125,16 @@ func convertToInt64(i interface{}) int64 {
case uint32:
return int64(v)
case uint64:
// Clamp values that are larger than MaxInt64
if v > math.MaxInt64 {
return math.MaxInt64
}
return int64(v)
case int64:
return v
default:
return 0
}

return i.(int64)
}

// GetModel will get a model from the datastore
Expand Down
29 changes: 25 additions & 4 deletions engine/model_draft_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/bitcoin-sv/go-sdk/script"
trx "github.com/bitcoin-sv/go-sdk/transaction"
"github.com/bitcoin-sv/go-sdk/transaction/template/p2pkh"
"github.com/bitcoin-sv/spv-wallet/conv"
"github.com/bitcoin-sv/spv-wallet/engine/datastore"
"github.com/bitcoin-sv/spv-wallet/engine/spverrors"
"github.com/bitcoin-sv/spv-wallet/engine/utils"
Expand Down Expand Up @@ -499,15 +500,25 @@ func (m *DraftTransaction) estimateSize() uint64 {
size := defaultOverheadSize // version + nLockTime

inputSize := trx.VarInt(len(m.Configuration.Inputs))
size += uint64(inputSize.Length())

value, err := conv.IntToUint64(inputSize.Length())
if err != nil {
m.client.Logger().Error().Msg(err.Error())
return 0
}
size += value

for _, input := range m.Configuration.Inputs {
size += utils.GetInputSizeForType(input.Type)
}

outputSize := trx.VarInt(len(m.Configuration.Outputs))
size += uint64(outputSize.Length())

value, err = conv.IntToUint64(outputSize.Length())
if err != nil {
m.client.Logger().Error().Msg(err.Error())
return 0
}
size += value
for _, output := range m.Configuration.Outputs {
for _, s := range output.Scripts {
size += utils.GetOutputSize(s.Script)
Expand Down Expand Up @@ -612,6 +623,11 @@ func (m *DraftTransaction) setChangeDestination(ctx context.Context, satoshisCha
numberOfDestinations = 1
}

// Check if numberOfDestinations is negative or too large before conversion
if numberOfDestinations < 0 {
return fee, fmt.Errorf("invalid number of destinations: %d", numberOfDestinations)
}

newFee = m.estimateFee(m.Configuration.FeeUnit, uint64(numberOfDestinations)*changeOutputSize)
satoshisChange -= newFee - fee
m.Configuration.ChangeSatoshis = satoshisChange
Expand Down Expand Up @@ -881,9 +897,14 @@ func (m *DraftTransaction) SignInputs(xPriv *compat.ExtendedKey) (signedHex stri
); err != nil {
return
}

idx32, conversionError := conv.IntToUint32(index)
if err != nil {
return "", spverrors.Wrapf(conversionError, "failed to convert index %d to uint32", index)
}
var s *p2pkh.P2PKH
if s, err = utils.GetUnlockingScript(
txDraft, uint32(index), privateKey,
txDraft, idx32, privateKey,
); err != nil {
return
}
Expand Down
7 changes: 6 additions & 1 deletion engine/model_paymail_addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/bitcoin-sv/go-paymail"
compat "github.com/bitcoin-sv/go-sdk/compat/bip32"
"github.com/bitcoin-sv/spv-wallet/conv"
"github.com/bitcoin-sv/spv-wallet/engine/datastore"
"github.com/bitcoin-sv/spv-wallet/engine/spverrors"
"github.com/bitcoin-sv/spv-wallet/engine/utils"
Expand Down Expand Up @@ -265,7 +266,11 @@ func (m *PaymailAddress) incrementExternalXpubDerivationSeq(ctx context.Context)
return err
}

m.XpubDerivationSeq = uint32(newNum)
newNumU32, err := conv.Int64ToUint32(newNum)
if err != nil {
return spverrors.Wrapf(err, "failed to convert int64 to uint32")
}
m.XpubDerivationSeq = newNumU32
return nil
}

Expand Down
Loading

0 comments on commit aa64c8d

Please sign in to comment.