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
2 changes: 1 addition & 1 deletion app/parse_addr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestParseAddress(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
requireT := require.New(t)
prefix, addr, err := parseAddress(tc.address)
requireT.EqualValues(tc.expectedPrefix, prefix)
requireT.Equal(tc.expectedPrefix, prefix)
if !tc.verifyError {
requireT.NoError(err)
requireT.NotNil(addr)
Expand Down
4 changes: 2 additions & 2 deletions client/coreum/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestBatchSend(t *testing.T) {
assertT := assert.New(t)
requireT := require.New(t)

ctx := logger.WithLogger(context.Background(), zaptest.NewLogger(t))
ctx := logger.WithLogger(t.Context(), zaptest.NewLogger(t))
ctx, cancel := context.WithCancel(ctx)
t.Cleanup(cancel)
amount := sdk.NewCoin("test-denom", sdkmath.NewInt(13))
Expand Down Expand Up @@ -88,5 +88,5 @@ func TestBatchSend(t *testing.T) {
totalAddressesCount += len(call.requests)
}

assertT.EqualValues(requestCount, totalAddressesCount)
assertT.Equal(requestCount, totalAddressesCount)
}
80 changes: 40 additions & 40 deletions client/coreum/batcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ import (
"github.com/CoreumFoundation/coreum-tools/pkg/parallel"
)

// Batcher exposes functionality to batch many transfer requests.
type Batcher struct {
requestBuffer chan request
client coreumClient
fundingAddresses []sdk.AccAddress
batchSize int
batchChan chan batch

mu sync.RWMutex
stopped bool
}

// NewBatcher returns new instance of Batcher type.
func NewBatcher(
client coreumClient,
Expand Down Expand Up @@ -40,18 +52,6 @@ type coreumClient interface {
) (string, error)
}

// Batcher exposes functionality to batch many transfer requests.
type Batcher struct {
requestBuffer chan request
client coreumClient
fundingAddresses []sdk.AccAddress
batchSize int
batchChan chan batch

mu sync.RWMutex
stopped bool
}

type result struct {
txHash string
err error
Expand All @@ -76,6 +76,34 @@ func (b *Batcher) SendToken(ctx context.Context, destAddress sdk.AccAddress, amo
}
}

// Run starts goroutines for batch processing requests.
func (b *Batcher) Run(ctx context.Context) error {
return parallel.Run(ctx, func(ctx context.Context, spawn parallel.SpawnFn) error {
spawn("closer", parallel.Fail, func(ctx context.Context) error {
<-ctx.Done()
b.close()
return errors.WithStack(ctx.Err())
})
spawn("createBatches", parallel.Fail, func(ctx context.Context) error {
b.createBatches()
return errors.WithStack(ctx.Err())
})
spawn("processBatches", parallel.Fail, func(ctx context.Context) error {
_ = parallel.Run(ctx, func(ctx context.Context, spawn parallel.SpawnFn) error {
for _, fundingAddress := range b.fundingAddresses {
spawn(fundingAddress.String(), parallel.Continue, func(ctx context.Context) error {
b.processBatches(ctx, fundingAddress)
return nil
})
}
return nil
})
return errors.WithStack(ctx.Err())
})
return nil
})
}

func (b *Batcher) close() {
b.mu.Lock()
defer b.mu.Unlock()
Expand Down Expand Up @@ -107,34 +135,6 @@ func (b *Batcher) requestFund(address sdk.AccAddress, amount sdk.Coin) (<-chan r
return req.responseChan, nil
}

// Run starts goroutines for batch processing requests.
func (b *Batcher) Run(ctx context.Context) error {
return parallel.Run(ctx, func(ctx context.Context, spawn parallel.SpawnFn) error {
spawn("closer", parallel.Fail, func(ctx context.Context) error {
<-ctx.Done()
b.close()
return errors.WithStack(ctx.Err())
})
spawn("createBatches", parallel.Fail, func(ctx context.Context) error {
b.createBatches()
return errors.WithStack(ctx.Err())
})
spawn("processBatches", parallel.Fail, func(ctx context.Context) error {
_ = parallel.Run(ctx, func(ctx context.Context, spawn parallel.SpawnFn) error {
for _, fundingAddress := range b.fundingAddresses {
spawn(fundingAddress.String(), parallel.Continue, func(ctx context.Context) error {
b.processBatches(ctx, fundingAddress)
return nil
})
}
return nil
})
return errors.WithStack(ctx.Err())
})
return nil
})
}

type batch []request

func (b *Batcher) processBatches(ctx context.Context, fromAddress sdk.AccAddress) {
Expand Down
8 changes: 6 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/CoreumFoundation/faucet

go 1.23.3
go 1.24

toolchain go1.24.3

// CosmosSDK replacements
replace (
Expand Down Expand Up @@ -28,7 +30,7 @@ require (
github.com/samber/lo v1.49.1
github.com/spf13/pflag v1.0.6
github.com/stretchr/testify v1.10.0
go.uber.org/zap v1.23.0
go.uber.org/zap v1.27.0
google.golang.org/grpc v1.70.0
)

Expand All @@ -45,6 +47,8 @@ require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
github.com/99designs/keyring v1.2.2 // indirect
github.com/CoreumFoundation/crust v0.0.0-20250517130213-2f1a1d58aebc // indirect
github.com/CoreumFoundation/crust/znet v0.0.0-20250517130213-2f1a1d58aebc // indirect
github.com/CosmWasm/wasmd v0.54.0 // indirect
github.com/CosmWasm/wasmvm/v2 v2.2.2 // indirect
github.com/DataDog/datadog-go v4.8.3+incompatible // indirect
Expand Down
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ github.com/CoreumFoundation/coreum-tools v0.4.1-0.20241202115740-dbc6962a4d0a h1
github.com/CoreumFoundation/coreum-tools v0.4.1-0.20241202115740-dbc6962a4d0a/go.mod h1:VD93vCHkxYaT/RhOesXTFgd/GQDW54tr0BqGi5JU1c0=
github.com/CoreumFoundation/coreum/v5 v5.0.0-20250414180032-219788281a9a h1:Vlj88d6MrwunZIqR/q1k0jerWi1MJojBUGXgSZkI6Oc=
github.com/CoreumFoundation/coreum/v5 v5.0.0-20250414180032-219788281a9a/go.mod h1:yYMKci1gJ3M/IA2qeg6ek0tske0xe7eMlOasFt6Zxvk=
github.com/CoreumFoundation/crust v0.0.0-20250517130213-2f1a1d58aebc h1:hZ5O7tmGEjWhxMxU5Ofu3aW9uJJIY16y61lwxuAgp3A=
github.com/CoreumFoundation/crust v0.0.0-20250517130213-2f1a1d58aebc/go.mod h1:nTVhm7aaOrQSWIswfIdDrxV4BNI2vgwFSPaO+0G+cgM=
github.com/CoreumFoundation/crust v0.0.0-20250519091438-152b10021791 h1:25Kn1mMzUe88dZ9U6fDpwYI6hxaaZM1K2dTBGxQzjn8=
github.com/CoreumFoundation/crust v0.0.0-20250519091438-152b10021791/go.mod h1:nTVhm7aaOrQSWIswfIdDrxV4BNI2vgwFSPaO+0G+cgM=
github.com/CoreumFoundation/crust/znet v0.0.0-20250517130213-2f1a1d58aebc h1:apEr45SsfQSWs3pwbS+iQDvKnWDhG5HUEev2w4eS67g=
github.com/CoreumFoundation/crust/znet v0.0.0-20250517130213-2f1a1d58aebc/go.mod h1:mGXKXTmOMlK0EPDZXkyo9h5xWiGgDKBbi3EVN5hY1dU=
github.com/CoreumFoundation/crust/znet v0.0.0-20250519091438-152b10021791 h1:cW9H1wa0OIRRNqa5BbGaEZABRHj3nae9utJES2hJSGE=
github.com/CoreumFoundation/crust/znet v0.0.0-20250519091438-152b10021791/go.mod h1:mGXKXTmOMlK0EPDZXkyo9h5xWiGgDKBbi3EVN5hY1dU=
github.com/CosmWasm/wasmd v0.54.0 h1:/txsBehV1xnAi46H1xwuuY6D4NySujBy+wa5+ryItS8=
github.com/CosmWasm/wasmd v0.54.0/go.mod h1:8Zu/rj6RHbJ8Gx0WdqsGeHvgnEQb0rqchpqhgMxASRU=
github.com/CosmWasm/wasmvm/v2 v2.2.2 h1:MaQMtaZN8L08N0uAlBlOICP+GWolibJsajHGo3fQ03w=
Expand Down Expand Up @@ -848,6 +856,7 @@ go.uber.org/zap v1.18.1/go.mod h1:xg/QME4nWcxGxrpdeYfq7UvYrLh66cuVKdrbD1XF/NI=
go.uber.org/zap v1.21.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw=
go.uber.org/zap v1.23.0 h1:OjGQ5KQDEUawVHxNwQgPpiypGHOxo2mNZsOqTak4fFY=
go.uber.org/zap v1.23.0/go.mod h1:D+nX8jyLsMHMYrln8A0rJjFt/T/9/bGgIhAqxv5URuY=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k=
golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
Expand Down
4 changes: 3 additions & 1 deletion go.work
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
go 1.23.3
go 1.24

toolchain go1.24.3

use .

Expand Down
5 changes: 2 additions & 3 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ cloud.google.com/go/compute v1.19.0/go.mod h1:rikpw2y+UMidAe9tISo04EHNOIf42RLYF/
cloud.google.com/go/compute v1.20.1/go.mod h1:4tCnrn48xsqlwSAiLf1HXMQk8CONslYbdiEZc9FEIbM=
cloud.google.com/go/compute v1.23.1/go.mod h1:CqB3xpmPKKt3OJpW2ndFIXnA9A4xAy/F3Xp1ixncW78=
cloud.google.com/go/compute v1.23.3/go.mod h1:VCgBUoMnIVIR0CscqQiPJLAG25E3ZRZMzcFZeQ+h8CI=
cloud.google.com/go/compute v1.29.0 h1:Lph6d8oPi38NHkOr6S55Nus/Pbbcp37m/J0ohgKAefs=
cloud.google.com/go/compute v1.29.0/go.mod h1:HFlsDurE5DpQZClAGf/cYh+gxssMhBxBovZDYkEn/Og=
cloud.google.com/go/compute/metadata v0.1.0/go.mod h1:Z1VN+bulIf6bt4P/C37K4DyZYZEXYonfTBHHFPO/4UU=
cloud.google.com/go/compute/metadata v0.2.0/go.mod h1:zFmK7XCadkQkj6TtorcaGlCW1hT1fIilQDwofLpJ20k=
Expand Down Expand Up @@ -990,8 +989,6 @@ github.com/CoreumFoundation/coreum-tools v0.4.1-0.20230627094203-821c6a4eebab/go
github.com/CoreumFoundation/coreum-tools v0.4.1-0.20240321120602-0a9c50facc68/go.mod h1:VD93vCHkxYaT/RhOesXTFgd/GQDW54tr0BqGi5JU1c0=
github.com/CoreumFoundation/coreum/build v0.0.0-20240612092619-8d09603e839d h1:/n0Mw0BjsipOn8dkMJsx4YurrPmFLp8vZzWhugUWL2c=
github.com/CoreumFoundation/coreum/build v0.0.0-20240612092619-8d09603e839d/go.mod h1:OTX5LdcocDzWHklOuMJKGn9xNkPn0WB9UPXIBVyOIJo=
github.com/CoreumFoundation/coreum/build v0.0.0-20250417103402-9a7f9035f445 h1:bK3mk6AfdpLeTg1ECdeMoOaKn+4JSphRVre3lVQ7qMQ=
github.com/CoreumFoundation/coreum/build v0.0.0-20250417103402-9a7f9035f445/go.mod h1:9LO1Sy+zjLtcPw6p0p12/ExWpUeZs8oa/v9rGst+NVI=
github.com/CoreumFoundation/coreum/v4 v4.0.0-20240213123712-d7d6a45ddb8f h1:a+lBcN3rWvoa/5mvdOQMaUU604/J3h/0rumGnISRglo=
github.com/CoreumFoundation/coreum/v4 v4.0.0-20240213123712-d7d6a45ddb8f/go.mod h1:m17GEOObKO0uMNRSeitPFjjne55MSbzFeTlAONaMGkI=
github.com/CoreumFoundation/coreum/v5 v5.0.0 h1:pW+3ydeSlpBfvKCmoJ1/I9At0u/ZDsQl3DebkicQI3c=
Expand All @@ -1003,6 +1000,8 @@ github.com/CoreumFoundation/crust v0.0.0-20240612073237-afb494aa7ae2 h1:LyJ72/OL
github.com/CoreumFoundation/crust v0.0.0-20240612073237-afb494aa7ae2/go.mod h1:+tc+D5f9K7oNNtv8UazzNy8LheAhBG7UDc25JwY803Y=
github.com/CoreumFoundation/crust v0.0.0-20250325111106-1ceef3a161fe h1:X4l6o15vSyKwcof6Juqa1Sjyi95+79Os6oJABx3W4C0=
github.com/CoreumFoundation/crust v0.0.0-20250325111106-1ceef3a161fe/go.mod h1:6upahk1765UAryMA55OskmSDVoP98MMEAF4A+TFDyJI=
github.com/CoreumFoundation/crust v0.0.0-20250422105139-051d68f6bb18 h1:Dakkd1sHgL4RB+hcXJC815EyCfH45McG19zKVTPI5YY=
github.com/CoreumFoundation/crust v0.0.0-20250422105139-051d68f6bb18/go.mod h1:6upahk1765UAryMA55OskmSDVoP98MMEAF4A+TFDyJI=
github.com/CoreumFoundation/crust v0.0.0-20250428150205-f8b385a8d919 h1:FBknw0Rx4zgX8r01B0oFyuVAe6WBpQ6T4/3QlXkGzec=
github.com/CoreumFoundation/crust v0.0.0-20250428150205-f8b385a8d919/go.mod h1:6upahk1765UAryMA55OskmSDVoP98MMEAF4A+TFDyJI=
github.com/CoreumFoundation/crust/znet v0.0.0-20250325111106-1ceef3a161fe h1:otll6VYj1juN6W0x7/iJFc8h16QwXMobCZgpuvljW6o=
Expand Down
10 changes: 5 additions & 5 deletions integration-tests/transfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func init() {
func TestTransferRequest(t *testing.T) {
t.Parallel()

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
ctx, cancel := context.WithTimeout(t.Context(), 30*time.Second)
t.Cleanup(cancel)
address := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()).String()

Expand All @@ -93,13 +93,13 @@ func TestTransferRequest(t *testing.T) {
require.NoError(t, err)

// make assertions
assert.EqualValues(t, cfg.transferAmount, resp.Balances.AmountOf(cfg.network.Denom()).String())
assert.Equal(t, cfg.transferAmount, resp.Balances.AmountOf(cfg.network.Denom()).String())
}

func TestTransferRequestWithGenPrivkey(t *testing.T) {
t.Parallel()

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
ctx, cancel := context.WithTimeout(t.Context(), 30*time.Second)
t.Cleanup(cancel)

// request fund
Expand All @@ -117,13 +117,13 @@ func TestTransferRequestWithGenPrivkey(t *testing.T) {
require.NoError(t, err)

// make assertions
assert.EqualValues(t, cfg.transferAmount, resp.Balances.AmountOf(cfg.network.Denom()).String())
assert.Equal(t, cfg.transferAmount, resp.Balances.AmountOf(cfg.network.Denom()).String())
}

func TestTransferRequest_WrongAddress(t *testing.T) {
t.Parallel()

ctx := context.Background()
ctx := t.Context()
address := "core1hrlnys435ph2gehthddlg2g2s246my30q0gfs2"

// request fund
Expand Down
12 changes: 6 additions & 6 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestWithEnv_WithPrefix(t *testing.T) {
err := WithEnv(flagSet, "pfx")
require.NoError(t, err)

assert.EqualValues(t, 12, port)
assert.Equal(t, 12, port)
}

func TestWithEnv_WithoutPrefix(t *testing.T) {
Expand All @@ -32,7 +32,7 @@ func TestWithEnv_WithoutPrefix(t *testing.T) {
err := WithEnv(flagSet, "some_prefix")
require.NoError(t, err)

assert.EqualValues(t, 1, port)
assert.Equal(t, 1, port)
}

func TestWithEnv_OnlySetEnv(t *testing.T) {
Expand All @@ -44,7 +44,7 @@ func TestWithEnv_OnlySetEnv(t *testing.T) {
err := WithEnv(flagSet, "")
require.NoError(t, err)

assert.EqualValues(t, 12, port)
assert.Equal(t, 12, port)
}

func TestWithEnv_OnlySetFlag(t *testing.T) {
Expand All @@ -57,7 +57,7 @@ func TestWithEnv_OnlySetFlag(t *testing.T) {
err := WithEnv(flagSet, "")
require.NoError(t, err)

assert.EqualValues(t, 20, port)
assert.Equal(t, 20, port)
}

func TestWithEnv_FlagPrecedesEnv_SenEnvBeforeParse(t *testing.T) {
Expand All @@ -72,7 +72,7 @@ func TestWithEnv_FlagPrecedesEnv_SenEnvBeforeParse(t *testing.T) {
err := WithEnv(flagSet, "")
require.NoError(t, err)

assert.EqualValues(t, 183, port)
assert.Equal(t, 183, port)
}

func TestWithEnv_FlagPrecedesEnv_SenEnvAfterParse(t *testing.T) {
Expand All @@ -86,7 +86,7 @@ func TestWithEnv_FlagPrecedesEnv_SenEnvAfterParse(t *testing.T) {
err := WithEnv(flagSet, "")
require.NoError(t, err)

assert.EqualValues(t, 183, port)
assert.Equal(t, 183, port)
}

func setArgAndRevert(args []string) func() {
Expand Down
18 changes: 9 additions & 9 deletions pkg/limiter/limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,6 @@ func (p period) Increment(ip net.IP) {
p.counters[string(ip)]++
}

// NewWeightedWindowLimiter returns new limiter implementing weighted window algorithm.
func NewWeightedWindowLimiter(limit uint64, duration time.Duration) *WeightedWindowLimiter {
return &WeightedWindowLimiter{
limit: limit,
duration: duration,
current: newPeriod(duration),
}
}

// WeightedWindowLimiter imlements rate limiting using weighted window algorithm.
type WeightedWindowLimiter struct {
limit uint64
Expand All @@ -61,6 +52,15 @@ type WeightedWindowLimiter struct {
current period
}

// NewWeightedWindowLimiter returns new limiter implementing weighted window algorithm.
func NewWeightedWindowLimiter(limit uint64, duration time.Duration) *WeightedWindowLimiter {
return &WeightedWindowLimiter{
limit: limit,
duration: duration,
current: newPeriod(duration),
}
}

// IsRequestAllowed tells if request should be handled or rejected due to exhausted rate limit.
func (l *WeightedWindowLimiter) IsRequestAllowed(ip net.IP) bool {
l.mu.Lock()
Expand Down