Skip to content

Commit c6dda79

Browse files
committed
Nonce service now allocates nonces across different mainnet use cases
1 parent 0efb905 commit c6dda79

File tree

4 files changed

+21
-17
lines changed

4 files changed

+21
-17
lines changed

pkg/code/async/nonce/allocator.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ import (
1616
// - Need some level of memory account management with the ability to find a free index
1717
// - Does not require a vault key record
1818

19-
// todo: Split out Solana mainnet nonce allocation based on use case
20-
21-
func (p *service) generateNonceAccountsOnSolanaMainnet(serviceCtx context.Context) error {
19+
func (p *service) generateNonceAccountsOnSolanaMainnet(serviceCtx context.Context, purpose nonce.Purpose, desiredPoolSize uint64) error {
2220

2321
hasWarnedUser := false
2422
err := retry.Loop(
@@ -30,7 +28,7 @@ func (p *service) generateNonceAccountsOnSolanaMainnet(serviceCtx context.Contex
3028
defer m.End()
3129
tracedCtx := newrelic.NewContext(serviceCtx, m)
3230

33-
num_invalid, err := p.data.GetNonceCountByState(tracedCtx, nonce.EnvironmentSolana, nonce.EnvironmentInstanceSolanaMainnet, nonce.StateInvalid)
31+
num_invalid, err := p.data.GetNonceCountByStateAndPurpose(tracedCtx, nonce.EnvironmentSolana, nonce.EnvironmentInstanceSolanaMainnet, nonce.StateInvalid, purpose)
3432
if err != nil {
3533
return err
3634
}
@@ -40,30 +38,30 @@ func (p *service) generateNonceAccountsOnSolanaMainnet(serviceCtx context.Contex
4038
return ErrInvalidNonceLimitExceeded
4139
}
4240

43-
num_available, err := p.data.GetNonceCountByState(tracedCtx, nonce.EnvironmentSolana, nonce.EnvironmentInstanceSolanaMainnet, nonce.StateAvailable)
41+
num_available, err := p.data.GetNonceCountByStateAndPurpose(tracedCtx, nonce.EnvironmentSolana, nonce.EnvironmentInstanceSolanaMainnet, nonce.StateAvailable, purpose)
4442
if err != nil {
4543
return err
4644
}
4745

48-
num_claimed, err := p.data.GetNonceCountByState(tracedCtx, nonce.EnvironmentSolana, nonce.EnvironmentInstanceSolanaMainnet, nonce.StateClaimed)
46+
num_claimed, err := p.data.GetNonceCountByStateAndPurpose(tracedCtx, nonce.EnvironmentSolana, nonce.EnvironmentInstanceSolanaMainnet, nonce.StateClaimed, purpose)
4947
if err != nil {
5048
return err
5149
}
5250

53-
num_released, err := p.data.GetNonceCountByState(tracedCtx, nonce.EnvironmentSolana, nonce.EnvironmentInstanceSolanaMainnet, nonce.StateReleased)
51+
num_released, err := p.data.GetNonceCountByStateAndPurpose(tracedCtx, nonce.EnvironmentSolana, nonce.EnvironmentInstanceSolanaMainnet, nonce.StateReleased, purpose)
5452
if err != nil {
5553
return err
5654
}
5755

58-
num_unknown, err := p.data.GetNonceCountByState(tracedCtx, nonce.EnvironmentSolana, nonce.EnvironmentInstanceSolanaMainnet, nonce.StateUnknown)
56+
num_unknown, err := p.data.GetNonceCountByStateAndPurpose(tracedCtx, nonce.EnvironmentSolana, nonce.EnvironmentInstanceSolanaMainnet, nonce.StateUnknown, purpose)
5957
if err != nil {
6058
return err
6159
}
6260

6361
// Get a count of nonces that are available or potentially available
6462
// within a short amount of time.
6563
num_potentially_available := num_available + num_claimed + num_released + num_unknown
66-
if num_potentially_available >= p.conf.solanaMainnetNoncePoolSize.Get(serviceCtx) {
64+
if num_potentially_available >= desiredPoolSize {
6765
if hasWarnedUser {
6866
p.log.Warn("The nonce pool size is reached.")
6967
hasWarnedUser = false

pkg/code/async/nonce/config.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@ const (
1111
solanaMainnetNoncePubkeyPrefixConfigEnvName = envConfigPrefix + "SOLANA_MAINNET_NONCE_PUBKEY_PREFIX"
1212
defaultSolanaMainnetNoncePubkeyPrefix = "non"
1313

14-
solanaMainnetNoncePoolSizeConfigEnvName = envConfigPrefix + "SOLANA_MAINNET_NONCE_POOL_SIZE"
15-
defaultSolanaMainnetNoncePoolSize = 1000
14+
onDemandTransactiontNoncePoolSizeConfigEnvName = envConfigPrefix + "ON_DEMAND_TRANSACTION_NONCE_POOL_SIZE"
15+
defaultOnDemandTransactionNoncePoolSize = 1000
16+
17+
clientSwapNoncePoolSizeConfigEnvName = envConfigPrefix + "CLIENT_SWAP_NONCE_POOL_SIZE"
18+
defaultClientSwapNoncePoolSize = 1000
1619
)
1720

1821
type conf struct {
19-
solanaMainnetNoncePubkeyPrefix config.String
20-
solanaMainnetNoncePoolSize config.Uint64
22+
solanaMainnetNoncePubkeyPrefix config.String
23+
onDemandTransactionNoncePoolSize config.Uint64
24+
clientSwapNoncePoolSize config.Uint64
2125
}
2226

2327
// ConfigProvider defines how config values are pulled
@@ -27,8 +31,9 @@ type ConfigProvider func() *conf
2731
func WithEnvConfigs() ConfigProvider {
2832
return func() *conf {
2933
return &conf{
30-
solanaMainnetNoncePubkeyPrefix: env.NewStringConfig(solanaMainnetNoncePubkeyPrefixConfigEnvName, defaultSolanaMainnetNoncePubkeyPrefix),
31-
solanaMainnetNoncePoolSize: env.NewUint64Config(solanaMainnetNoncePoolSizeConfigEnvName, defaultSolanaMainnetNoncePoolSize),
34+
solanaMainnetNoncePubkeyPrefix: env.NewStringConfig(solanaMainnetNoncePubkeyPrefixConfigEnvName, defaultSolanaMainnetNoncePubkeyPrefix),
35+
onDemandTransactionNoncePoolSize: env.NewUint64Config(onDemandTransactiontNoncePoolSizeConfigEnvName, defaultOnDemandTransactionNoncePoolSize),
36+
clientSwapNoncePoolSize: env.NewUint64Config(clientSwapNoncePoolSizeConfigEnvName, defaultClientSwapNoncePoolSize),
3237
}
3338
}
3439
}

pkg/code/async/nonce/keys.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (p *service) generateKeys(ctx context.Context) error {
4949
return err
5050
}
5151

52-
reserveSize := (p.conf.solanaMainnetNoncePoolSize.Get(ctx) * 2)
52+
reserveSize := ((p.conf.onDemandTransactionNoncePoolSize.Get(ctx) + p.conf.clientSwapNoncePoolSize.Get(ctx)) * 2)
5353

5454
// If we have sufficient keys, don't generate any more.
5555
if res >= reserveSize {

pkg/code/async/nonce/service.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ func (p *service) Start(ctx context.Context, interval time.Duration) error {
4545
go p.generateKeys(ctx)
4646

4747
// Watch the size of the Solana mainnet nonce pool and create accounts if necessary
48-
go p.generateNonceAccountsOnSolanaMainnet(ctx)
48+
go p.generateNonceAccountsOnSolanaMainnet(ctx, nonce.PurposeOnDemandTransaction, p.conf.onDemandTransactionNoncePoolSize.Get(ctx))
49+
go p.generateNonceAccountsOnSolanaMainnet(ctx, nonce.PurposeClientSwap, p.conf.clientSwapNoncePoolSize.Get(ctx))
4950

5051
// Setup workers to watch for nonce state changes on the Solana side
5152
for _, state := range []nonce.State{

0 commit comments

Comments
 (0)