Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions ads/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/iotaledger/hive.go/ads
go 1.22

require (
fortio.org/safecast v1.0.0
github.com/iotaledger/hive.go/ds v0.0.0-20240517131232-748f1ce3a2d2
github.com/iotaledger/hive.go/ierrors v0.0.0-20240517131232-748f1ce3a2d2
github.com/iotaledger/hive.go/kvstore v0.0.0-20240517131232-748f1ce3a2d2
Expand Down
2 changes: 2 additions & 0 deletions ads/go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
fortio.org/safecast v1.0.0 h1:dr3131WPX8iS1pTf76+39WeXbTrerDYLvi9s7Oi3wiY=
fortio.org/safecast v1.0.0/go.mod h1:xZmcPk3vi4kuUFf+tq4SvnlVdwViqf6ZSZl91Jr9Jdg=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
23 changes: 21 additions & 2 deletions ads/map_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"crypto/sha256"
"sync"

"fortio.org/safecast"

"github.com/pokt-network/smt"

"github.com/iotaledger/hive.go/ds/types"
Expand Down Expand Up @@ -124,7 +126,12 @@ func (m *authenticatedMap[IdentifierType, K, V]) Size() int {
return 0
}

return int(size)
v, err := safecast.Convert[int](size)
if err != nil {
return 0
}

return v
}

// Commit persists the current state of the map to the storage.
Expand Down Expand Up @@ -277,7 +284,19 @@ func (m *authenticatedMap[IdentifierType, K, V]) addSize(delta int) error {
return ierrors.Wrap(err, "failed to get size")
}

if err := m.size.Set(uint64(int(size) + delta)); err != nil {
sizeInt, err := safecast.Convert[int](size)
if err != nil {
return ierrors.Wrap(err, "failed to convert size to int")
}

newSize := sizeInt + delta

updatedSize, err := safecast.Convert[uint64](newSize)
if err != nil {
return ierrors.Wrap(err, "failed to convert new size to uint64")
}

if err := m.size.Set(updatedSize); err != nil {
return ierrors.Wrap(err, "failed to set size")
}

Expand Down
4 changes: 3 additions & 1 deletion app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ func loadConfigurations(configFilesFlagSet *flag.FlagSet, configurationSets []*C

for _, config := range configurationSets {
// propagate values in the config back to bound parameters
config.config.UpdateBoundParameters()
if err := config.config.UpdateBoundParameters(); err != nil {
return err
}
}

return nil
Expand Down
54 changes: 45 additions & 9 deletions app/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strings"
"time"

"fortio.org/safecast"

"github.com/knadh/koanf"
"github.com/knadh/koanf/providers/env"
"github.com/knadh/koanf/providers/file"
Expand Down Expand Up @@ -616,7 +618,7 @@ func (c *Configuration) BindParameters(flagset *flag.FlagSet, namespace string,

// UpdateBoundParameters updates parameters that were bound using the BindParameters method with the current values in
// the configuration.
func (c *Configuration) UpdateBoundParameters() {
func (c *Configuration) UpdateBoundParameters() error {
for _, boundParameter := range c.boundParameters {
parameterName := boundParameter.Name

Expand All @@ -633,25 +635,57 @@ func (c *Configuration) UpdateBoundParameters() {
case reflectutils.IntType:
*(boundParameter.BoundPointer.(*int)) = c.Int(parameterName)
case reflectutils.Int8Type:
*(boundParameter.BoundPointer.(*int8)) = int8(c.Int(parameterName))
v, err := safecast.Convert[int8](c.Int(parameterName))
if err != nil {
return err
}
*(boundParameter.BoundPointer.(*int8)) = v
case reflectutils.Int16Type:
*(boundParameter.BoundPointer.(*int16)) = int16(c.Int(parameterName))
v, err := safecast.Convert[int16](c.Int(parameterName))
if err != nil {
return err
}
*(boundParameter.BoundPointer.(*int16)) = v
case reflectutils.Int32Type:
*(boundParameter.BoundPointer.(*int32)) = int32(c.Int(parameterName))
v, err := safecast.Convert[int32](c.Int(parameterName))
if err != nil {
return err
}
*(boundParameter.BoundPointer.(*int32)) = v
case reflectutils.Int64Type:
*(boundParameter.BoundPointer.(*int64)) = c.Int64(parameterName)
case reflectutils.StringType:
*(boundParameter.BoundPointer.(*string)) = c.String(parameterName)
case reflectutils.UintType:
*(boundParameter.BoundPointer.(*uint)) = uint(c.Int(parameterName))
v, err := safecast.Convert[uint](c.Int(parameterName))
if err != nil {
return err
}
*(boundParameter.BoundPointer.(*uint)) = v
case reflectutils.Uint8Type:
*(boundParameter.BoundPointer.(*uint8)) = uint8(c.Int(parameterName))
v, err := safecast.Convert[uint8](c.Int(parameterName))
if err != nil {
return err
}
*(boundParameter.BoundPointer.(*uint8)) = v
case reflectutils.Uint16Type:
*(boundParameter.BoundPointer.(*uint16)) = uint16(c.Int(parameterName))
v, err := safecast.Convert[uint16](c.Int(parameterName))
if err != nil {
return err
}
*(boundParameter.BoundPointer.(*uint16)) = v
case reflectutils.Uint32Type:
*(boundParameter.BoundPointer.(*uint32)) = uint32(c.Int(parameterName))
v, err := safecast.Convert[uint32](c.Int(parameterName))
if err != nil {
return err
}
*(boundParameter.BoundPointer.(*uint32)) = v
case reflectutils.Uint64Type:
*(boundParameter.BoundPointer.(*uint64)) = uint64(c.Int64(parameterName))
v, err := safecast.Convert[uint64](c.Int64(parameterName))
if err != nil {
return err
}
*(boundParameter.BoundPointer.(*uint64)) = v
case reflectutils.StringSliceType:
*(boundParameter.BoundPointer.(*[]string)) = c.Strings(parameterName)
case reflectutils.StringMapType:
Expand All @@ -672,6 +706,8 @@ func (c *Configuration) UpdateBoundParameters() {
reflect.ValueOf(boundParameter.BoundPointer).Elem().Set(newBoundParameterPointer.Elem())
}
}

return nil
}

// GetParameterPath returns the path to the parameter with the given name.
Expand Down
3 changes: 2 additions & 1 deletion app/configuration/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ func TestBindAndUpdateParameters(t *testing.T) {
err = config.LoadFlagSet(flagset)
assert.NoError(t, err)

config.UpdateBoundParameters()
err = config.UpdateBoundParameters()
assert.NoError(t, err)

assertFlag(t, flagset, config, &parameters.TestField,
"configuration.testField",
Expand Down
1 change: 1 addition & 0 deletions app/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/iotaledger/hive.go/app
go 1.22

require (
fortio.org/safecast v1.0.0
github.com/felixge/fgprof v0.9.4
github.com/hashicorp/go-version v1.6.0
github.com/iotaledger/hive.go/ierrors v0.0.0-20240517131232-748f1ce3a2d2
Expand Down
2 changes: 2 additions & 0 deletions app/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
fortio.org/safecast v1.0.0 h1:dr3131WPX8iS1pTf76+39WeXbTrerDYLvi9s7Oi3wiY=
fortio.org/safecast v1.0.0/go.mod h1:xZmcPk3vi4kuUFf+tq4SvnlVdwViqf6ZSZl91Jr9Jdg=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
Expand Down
2 changes: 1 addition & 1 deletion app/shutdown/shutdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (gs *ShutdownHandler) writeSelfShutdownLogFile(msg string, critical bool) {
message += " (CRITICAL)"
}

if _, err := f.WriteString(fmt.Sprintf("%s: %s\n", time.Now().Format(time.RFC3339), message)); err != nil {
if _, err := fmt.Fprintf(f, "%s: %s\n", time.Now().Format(time.RFC3339), message); err != nil {
gs.LogWarnf("self-shutdown log can't be written, error: %s", err.Error())
}
}
Expand Down
2 changes: 2 additions & 0 deletions core/safemath/safe_math.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ func SafeMulUint64(x, y uint64) (uint64, error) {
// Returns x * y or an error if that computation would under- or overflow.
//
// According to benchmarks, this function is about 27% faster than SafeMul.
//
//nolint:gosec // disable G115 - integer overlfows are checked manually
func SafeMulInt64(x, y int64) (int64, error) {
// This function stores the sign of the resulting int64 multiplication
// and then executes the multiplication with two uint64s, in 128-bit space.
Expand Down
2 changes: 1 addition & 1 deletion crypto/randomness.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (s RandomnessSource) Seed(int64) {}

// Int63 returns a non-negative random 63-bit integer as an int64.
func (s RandomnessSource) Int63() int64 {
return int64(s.Uint64() & ^uint64(1<<63))
return int64(s.Uint64() & ^uint64(1<<63)) //nolint:gosec // ignore integer overflow error
}

// Uint64 returns a random 64-bit value as a uint64.
Expand Down
1 change: 1 addition & 0 deletions ds/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/iotaledger/hive.go/ds
go 1.22

require (
fortio.org/safecast v1.0.0
github.com/iotaledger/hive.go/constraints v0.0.0-20240517131232-748f1ce3a2d2
github.com/iotaledger/hive.go/ierrors v0.0.0-20240517131232-748f1ce3a2d2
github.com/iotaledger/hive.go/lo v0.0.0-20240517131232-748f1ce3a2d2
Expand Down
2 changes: 2 additions & 0 deletions ds/go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
fortio.org/safecast v1.0.0 h1:dr3131WPX8iS1pTf76+39WeXbTrerDYLvi9s7Oi3wiY=
fortio.org/safecast v1.0.0/go.mod h1:xZmcPk3vi4kuUFf+tq4SvnlVdwViqf6ZSZl91Jr9Jdg=
github.com/btcsuite/btcd/btcec/v2 v2.2.1 h1:xP60mv8fvp+0khmrN0zTdPC3cNm24rfeE6lh2R/Yv3E=
github.com/btcsuite/btcd/btcec/v2 v2.2.1/go.mod h1:9/CSmJxmuvqzX9Wh2fXMWToLOHhPd11lSPuIupwTkI8=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
Expand Down
6 changes: 3 additions & 3 deletions ds/reactive/clock_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@ func newClock(granularity time.Duration) *clock {
}

// set the initial value.
c.variable.Set(time.Now())
c.Set(time.Now())

go func() {
// align the ticker to the given granularity.
time.Sleep(time.Duration(int64(granularity) - (time.Now().UnixNano() % int64(granularity))))
ticker := time.NewTicker(granularity)

// first tick after the initial value.
c.variable.Set(time.Now().Truncate(granularity))
c.Set(time.Now().Truncate(granularity))

for {
select {
case <-c.shutdown:
return
case t := <-ticker.C:
c.variable.Set(t.Truncate(granularity))
c.Set(t.Truncate(granularity))
}
}
}()
Expand Down
5 changes: 4 additions & 1 deletion ds/reactive/wait_group_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"sync"
"sync/atomic"

"fortio.org/safecast"

"github.com/iotaledger/hive.go/ds"
"github.com/iotaledger/hive.go/lo"
)
Expand Down Expand Up @@ -37,7 +39,8 @@ func newWaitGroup[T comparable](elements ...T) *waitGroup[T] {
// Add adds the given elements to the wait group.
func (w *waitGroup[T]) Add(elements ...T) {
// first increase the counter so that the trigger is not executed before all elements are added
w.pendingElementsCounter.Add(int32(len(elements)))
v := safecast.MustConvert[int32](len(elements))
w.pendingElementsCounter.Add(v)

// then add the elements (and correct the counter if the elements are already present)
for _, element := range elements {
Expand Down
9 changes: 8 additions & 1 deletion ds/serializableorderedmap/serializable_orderedmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package serializableorderedmap
import (
"context"

"fortio.org/safecast"

"github.com/iotaledger/hive.go/ds/orderedmap"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/serializer/v2"
Expand All @@ -23,9 +25,14 @@ func New[K comparable, V any]() *SerializableOrderedMap[K, V] {

// Encode returns a serialized byte slice of the object.
func (o *SerializableOrderedMap[K, V]) Encode(api *serix.API) ([]byte, error) {
v, err := safecast.Convert[uint32](o.Size())
if err != nil {
return nil, err
}

seri := serializer.NewSerializer()

seri.WriteNum(uint32(o.Size()), func(err error) error {
seri.WriteNum(v, func(err error) error {
return ierrors.Wrap(err, "failed to write SerializableOrderedMap size to serializer")
})

Expand Down
2 changes: 1 addition & 1 deletion ds/set_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func newReadableSet[T comparable](elements ...T) *readableSet[T] {
}

for _, element := range elements {
r.OrderedMap.Set(element, types.Void)
r.Set(element, types.Void)
}

return r
Expand Down
2 changes: 1 addition & 1 deletion ierrors/ierrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// It enhances error handling by adding additional error creation and manipulation functions.
// This package also supports stacktraces when the "stacktrace" build tag is added.
//
//nolint:goerr113
//nolint:err113
package ierrors

import (
Expand Down
2 changes: 1 addition & 1 deletion ierrors/ierrors_no_stacktrace.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//go:build !stacktrace

//nolint:goerr113
//nolint:err113
package ierrors

import (
Expand Down
1 change: 1 addition & 0 deletions kvstore/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/iotaledger/hive.go/kvstore
go 1.22

require (
fortio.org/safecast v1.0.0
github.com/iotaledger/grocksdb v1.7.5-0.20230220105546-5162e18885c7
github.com/iotaledger/hive.go/ds v0.0.0-20240517131232-748f1ce3a2d2
github.com/iotaledger/hive.go/ierrors v0.0.0-20240517131232-748f1ce3a2d2
Expand Down
2 changes: 2 additions & 0 deletions kvstore/go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
fortio.org/safecast v1.0.0 h1:dr3131WPX8iS1pTf76+39WeXbTrerDYLvi9s7Oi3wiY=
fortio.org/safecast v1.0.0/go.mod h1:xZmcPk3vi4kuUFf+tq4SvnlVdwViqf6ZSZl91Jr9Jdg=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down
2 changes: 2 additions & 0 deletions kvstore/mapdb/mapdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func (s *mapDB) Set(key kvstore.Key, value kvstore.Value) error {
return s.set(key, value)
}

//nolint:unparam // error is always nil
func (s *mapDB) set(key kvstore.Key, value kvstore.Value) error {
s.m.set(byteutils.ConcatBytes(s.realm, key), value)

Expand Down Expand Up @@ -142,6 +143,7 @@ func (s *mapDB) Delete(key kvstore.Key) error {
return s.delete(key)
}

//nolint:unparam // error is always nil
func (s *mapDB) delete(key kvstore.Key) error {
s.m.delete(byteutils.ConcatBytes(s.realm, key))

Expand Down
20 changes: 10 additions & 10 deletions kvstore/testutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,28 +74,28 @@ func RandString(length int) string {
}

// RandUint8 returns a random uint8.
func RandUint8(max uint8) uint8 {
return uint8(RandomInt31n(int32(max)))
func RandUint8(maximum uint8) uint8 {
return uint8(RandomInt31n(int32(maximum))) //nolint:gosec
}

// RandUint16 returns a random uint16.
func RandUint16(max uint16) uint16 {
return uint16(RandomInt31n(int32(max)))
func RandUint16(maximum uint16) uint16 {
return uint16(RandomInt31n(int32(maximum))) //nolint:gosec
}

// RandUint32 returns a random uint32.
func RandUint32(max uint32) uint32 {
return uint32(RandomInt63n(int64(max)))
func RandUint32(maximum uint32) uint32 {
return uint32(RandomInt63n(int64(maximum))) //nolint:gosec
}

// RandUint64 returns a random uint64.
func RandUint64(max uint64) uint64 {
return uint64(RandomInt63n(int64(uint32(max))))
func RandUint64(maximum uint64) uint64 {
return uint64(RandomInt63n(int64(uint32(maximum)))) //nolint:gosec
}

// RandFloat64 returns a random float64.
func RandFloat64(max float64) float64 {
return RandomFloat64() * max
func RandFloat64(maximum float64) float64 {
return RandomFloat64() * maximum
}

// Rand32ByteArray returns an array with 32 random bytes.
Expand Down
Loading
Loading