Skip to content

Commit c0dd0fe

Browse files
authored
Fixes and improvements to nonce service (#115)
1 parent 5184c8e commit c0dd0fe

File tree

4 files changed

+31
-21
lines changed

4 files changed

+31
-21
lines changed

pkg/code/async/nonce/allocator.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import (
44
"context"
55
"time"
66

7+
"github.com/newrelic/go-agent/v3/newrelic"
8+
9+
"github.com/code-payments/code-server/pkg/code/data/nonce"
710
"github.com/code-payments/code-server/pkg/metrics"
811
"github.com/code-payments/code-server/pkg/retry"
9-
"github.com/code-payments/code-server/pkg/code/data/nonce"
10-
"github.com/newrelic/go-agent/v3/newrelic"
1112
)
1213

1314
func (p *service) generateNonceAccounts(serviceCtx context.Context) error {

pkg/code/async/nonce/keys.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import (
44
"context"
55
"time"
66

7+
"github.com/newrelic/go-agent/v3/newrelic"
8+
9+
"github.com/code-payments/code-server/pkg/code/data/vault"
710
"github.com/code-payments/code-server/pkg/database/query"
811
"github.com/code-payments/code-server/pkg/metrics"
912
"github.com/code-payments/code-server/pkg/retry"
10-
"github.com/code-payments/code-server/pkg/code/data/vault"
11-
"github.com/newrelic/go-agent/v3/newrelic"
1213
)
1314

1415
func (p *service) generateKey(ctx context.Context) (*vault.Record, error) {

pkg/code/async/nonce/pool.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import (
88
"github.com/mr-tron/base58/base58"
99
"github.com/newrelic/go-agent/v3/newrelic"
1010

11+
"github.com/code-payments/code-server/pkg/code/data/nonce"
12+
"github.com/code-payments/code-server/pkg/code/data/transaction"
1113
"github.com/code-payments/code-server/pkg/database/query"
1214
"github.com/code-payments/code-server/pkg/metrics"
1315
"github.com/code-payments/code-server/pkg/retry"
1416
"github.com/code-payments/code-server/pkg/solana"
1517
"github.com/code-payments/code-server/pkg/solana/system"
16-
"github.com/code-payments/code-server/pkg/code/data/nonce"
17-
"github.com/code-payments/code-server/pkg/code/data/transaction"
1818
)
1919

2020
func (p *service) worker(serviceCtx context.Context, state nonce.State, interval time.Duration) error {
@@ -131,19 +131,21 @@ func (p *service) handle(ctx context.Context, record *nonce.Record) error {
131131
func (p *service) handleUnknown(ctx context.Context, record *nonce.Record) error {
132132
// Newly created nonces.
133133

134-
// Check the signature for a potential timeout (e.g. if the nonce account
135-
// was never created because the blockchain never saw the init/create
136-
// transaction)
137-
err := p.checkForMissingTx(ctx, record)
138-
if err != nil {
139-
return p.markInvalid(ctx, record)
140-
}
141-
142134
// We're going to the blockchain directly here (super slow btw)
143135
// because we don't capture the transaction through history yet (it only
144136
// grabs transfer style transactions for KIN accounts).
145137
stx, err := p.data.GetBlockchainTransaction(ctx, record.Signature, solana.CommitmentFinalized)
146-
if err != nil {
138+
if err == solana.ErrSignatureNotFound {
139+
// Check the signature for a potential timeout (e.g. if the nonce account
140+
// was never created because the blockchain never saw the init/create
141+
// transaction)
142+
err := p.checkForMissingTx(ctx, record)
143+
if err != nil {
144+
return p.markInvalid(ctx, record)
145+
}
146+
147+
return nil
148+
} else if err != nil {
147149
return err
148150
}
149151

pkg/code/async/nonce/util.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,24 @@ package async_nonce
33
import (
44
"context"
55
"errors"
6-
"fmt"
6+
"sync"
77
"time"
88

9-
"github.com/code-payments/code-server/pkg/solana"
10-
"github.com/code-payments/code-server/pkg/solana/memo"
11-
"github.com/code-payments/code-server/pkg/solana/system"
9+
"github.com/mr-tron/base58/base58"
10+
1211
"github.com/code-payments/code-server/pkg/code/common"
1312
"github.com/code-payments/code-server/pkg/code/data/nonce"
1413
"github.com/code-payments/code-server/pkg/code/data/transaction"
1514
"github.com/code-payments/code-server/pkg/code/data/vault"
16-
"github.com/mr-tron/base58/base58"
15+
"github.com/code-payments/code-server/pkg/solana"
16+
compute_budget "github.com/code-payments/code-server/pkg/solana/computebudget"
17+
"github.com/code-payments/code-server/pkg/solana/system"
1718
)
1819

1920
var (
2021
sigTimeout = time.Minute * 5
2122
sigTimeoutCache = make(map[string]time.Time) // temporary hack
23+
sigCacheMu sync.Mutex
2224
)
2325

2426
func (p *service) markReleased(ctx context.Context, record *nonce.Record) error {
@@ -148,7 +150,8 @@ func (p *service) createNonceAccountTx(ctx context.Context, nonce *nonce.Record)
148150
}
149151

150152
instructions := []solana.Instruction{
151-
memo.Instruction(fmt.Sprintf("nonce:%d", nonce.Id)),
153+
compute_budget.SetComputeUnitLimit(10_000),
154+
compute_budget.SetComputeUnitPrice(10_000),
152155
system.CreateAccount(
153156
subPub,
154157
noncePub,
@@ -174,6 +177,9 @@ func (p *service) createNonceAccountTx(ctx context.Context, nonce *nonce.Record)
174177
}
175178

176179
func (p *service) checkForMissingTx(ctx context.Context, nonce *nonce.Record) error {
180+
sigCacheMu.Lock()
181+
defer sigCacheMu.Unlock()
182+
177183
// todo: use the DB to store the createdAt time of the nonce account
178184

179185
t := sigTimeoutCache[nonce.Signature]

0 commit comments

Comments
 (0)