Skip to content

Commit 24d0d61

Browse files
committed
Fixes for stateful swap system
1 parent 50844ee commit 24d0d61

File tree

11 files changed

+27
-64
lines changed

11 files changed

+27
-64
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
filippo.io/edwards25519 v1.1.0
77
github.com/aws/aws-sdk-go-v2 v0.17.0
88
github.com/bits-and-blooms/bloom/v3 v3.1.0
9-
github.com/code-payments/code-protobuf-api v1.19.1-0.20251124021538-11fc87a9e233
9+
github.com/code-payments/code-protobuf-api v1.19.1-0.20251126151344-3fd1dad96a6f
1010
github.com/code-payments/code-vm-indexer v0.1.11-0.20241028132209-23031e814fba
1111
github.com/emirpasic/gods v1.12.0
1212
github.com/envoyproxy/protoc-gen-validate v1.2.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnht
8080
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
8181
github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I=
8282
github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ=
83-
github.com/code-payments/code-protobuf-api v1.19.1-0.20251124021538-11fc87a9e233 h1:JTrB4gOeGjaguLD00bBWdIiO7fdQmvXkyWyOV9ZNSnk=
84-
github.com/code-payments/code-protobuf-api v1.19.1-0.20251124021538-11fc87a9e233/go.mod h1:fl4xu32MeNpGZR3wFhwEeKO0qVDuxYBNkqnvuADt6cA=
83+
github.com/code-payments/code-protobuf-api v1.19.1-0.20251126151344-3fd1dad96a6f h1:iqZ/lwIKAyfbyV1LKfoDfevdOcDu5QxkyeHd8rrH5KQ=
84+
github.com/code-payments/code-protobuf-api v1.19.1-0.20251126151344-3fd1dad96a6f/go.mod h1:fl4xu32MeNpGZR3wFhwEeKO0qVDuxYBNkqnvuADt6cA=
8585
github.com/code-payments/code-vm-indexer v0.1.11-0.20241028132209-23031e814fba h1:Bkp+gmeb6Y2PWXfkSCTMBGWkb2P1BujRDSjWeI+0j5I=
8686
github.com/code-payments/code-vm-indexer v0.1.11-0.20241028132209-23031e814fba/go.mod h1:jSiifpiBpyBQ8q2R0MGEbkSgWC6sbdRTyDBntmW+j1E=
8787
github.com/containerd/continuity v0.0.0-20190827140505-75bee3e2ccb6 h1:NmTXa/uVnDyp0TY5MKi197+3HWcnYWfnHGyaFthlnGw=

pkg/code/async/geyser/external_deposit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func initiateExternalDepositIntoVm(ctx context.Context, data code_data.Provider,
9999
return errors.Wrap(err, "error getting timelock accounts")
100100
}
101101

102-
err = common.EnsureVirtualTimelockAccountIsInitialized(ctx, data, vmIndexerClient, vmConfig.Vm, userAuthority, true)
102+
err = common.EnsureVirtualTimelockAccountIsInitialized(ctx, data, vmIndexerClient, mint, userAuthority, true)
103103
if err != nil {
104104
return errors.Wrap(err, "error ensuring vta is initialized")
105105
}

pkg/code/async/nonce/pool.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func (p *service) handleReleased(ctx context.Context, record *nonce.Record) erro
199199
// Nonces that exist but we don't yet know their stored blockhash.
200200

201201
// Fetch the Solana transaction where the nonce would be consumed
202-
var txn *transaction.Record
202+
var txn *solana.ConfirmedTransaction
203203
var err error
204204
switch record.Environment {
205205
case nonce.EnvironmentSolana:
@@ -220,7 +220,7 @@ func (p *service) handleReleased(ctx context.Context, record *nonce.Record) erro
220220
}
221221

222222
// Sanity check the Solana transaction is in a finalized or failed state
223-
if txn.ConfirmationState != transaction.ConfirmationFinalized && txn.ConfirmationState != transaction.ConfirmationFailed {
223+
if txn == nil {
224224
return nil
225225
}
226226

pkg/code/async/nonce/util.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,19 @@ func (p *service) getLatestBlockhash(ctx context.Context) (*solana.Blockhash, er
6666
return &bh, nil
6767
}
6868

69-
func (p *service) getTransaction(ctx context.Context, signature string) (*transaction.Record, error) {
69+
func (p *service) getTransaction(ctx context.Context, signature string) (*solana.ConfirmedTransaction, error) {
7070
return p.getTransactionFromBlockchain(ctx, signature)
7171
}
7272

73-
func (p *service) getTransactionFromBlockchain(ctx context.Context, signature string) (*transaction.Record, error) {
74-
stx, err := p.data.GetBlockchainTransaction(ctx, signature, solana.CommitmentFinalized)
73+
func (p *service) getTransactionFromBlockchain(ctx context.Context, signature string) (*solana.ConfirmedTransaction, error) {
74+
txn, err := p.data.GetBlockchainTransaction(ctx, signature, solana.CommitmentFinalized)
7575
if err == solana.ErrSignatureNotFound {
7676
return nil, transaction.ErrNotFound
7777
}
7878
if err != nil {
7979
return nil, err
8080
}
81-
82-
tx, err := transaction.FromConfirmedTransaction(stx)
83-
if err != nil {
84-
return nil, err
85-
}
86-
return tx, nil
81+
return txn, nil
8782
}
8883

8984
func (p *service) getRentAmount(ctx context.Context) (uint64, error) {

pkg/code/async/swap/config.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package async_swap
33
import (
44
"github.com/code-payments/code-server/pkg/config"
55
"github.com/code-payments/code-server/pkg/config/env"
6-
"github.com/code-payments/code-server/pkg/config/memory"
7-
"github.com/code-payments/code-server/pkg/config/wrapper"
86
)
97

108
const (
@@ -15,8 +13,7 @@ const (
1513
)
1614

1715
type conf struct {
18-
batchSize config.Uint64
19-
enableCachedTransactionLookup config.Bool
16+
batchSize config.Uint64
2017
}
2118

2219
// ConfigProvider defines how config values are pulled
@@ -26,8 +23,7 @@ type ConfigProvider func() *conf
2623
func WithEnvConfigs() ConfigProvider {
2724
return func() *conf {
2825
return &conf{
29-
batchSize: env.NewUint64Config(BatchSizeConfigEnvName, defaultFulfillmentBatchSize),
30-
enableCachedTransactionLookup: wrapper.NewBoolConfig(memory.NewConfig(false), false),
26+
batchSize: env.NewUint64Config(BatchSizeConfigEnvName, defaultFulfillmentBatchSize),
3127
}
3228
}
3329
}

pkg/code/async/swap/util.go

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -179,35 +179,6 @@ func (p *service) markNonceReleasedDueToSubmittedTransaction(ctx context.Context
179179
return p.data.SaveNonce(ctx, nonceRecord)
180180
}
181181

182-
func (p *service) getTransaction(ctx context.Context, record *swap.Record) (*transaction.Record, error) {
183-
if record.TransactionSignature == nil || len(*record.TransactionSignature) == 0 {
184-
return nil, transaction.ErrNotFound
185-
}
186-
187-
if p.conf.enableCachedTransactionLookup.Get(ctx) {
188-
return p.data.GetTransaction(ctx, *record.TransactionSignature)
189-
}
190-
191-
return p.getTransactionFromBlockchain(ctx, record)
192-
}
193-
194-
func (p *service) getTransactionFromBlockchain(ctx context.Context, record *swap.Record) (*transaction.Record, error) {
195-
stx, err := p.data.GetBlockchainTransaction(ctx, *record.TransactionSignature, solana.CommitmentFinalized)
196-
if err == solana.ErrSignatureNotFound {
197-
return nil, transaction.ErrNotFound
198-
}
199-
if err != nil {
200-
return nil, err
201-
}
202-
203-
tx, err := transaction.FromConfirmedTransaction(stx)
204-
if err != nil {
205-
return nil, err
206-
}
207-
208-
return tx, nil
209-
}
210-
211182
func getDeltaQuarksFromTokenBalances(tokenAccount *common.Account, tokenBalances *solana.TransactionTokenBalances) (int64, error) {
212183
var preQuarkBalance, postQuarkBalance int64
213184
var err error

pkg/code/async/swap/worker.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
"github.com/code-payments/code-server/pkg/code/data/intent"
1313
"github.com/code-payments/code-server/pkg/code/data/swap"
14-
"github.com/code-payments/code-server/pkg/code/data/transaction"
1514
"github.com/code-payments/code-server/pkg/database/query"
1615
"github.com/code-payments/code-server/pkg/metrics"
1716
"github.com/code-payments/code-server/pkg/retry"
@@ -106,17 +105,15 @@ func (p *service) handleStateSubmitting(ctx context.Context, record *swap.Record
106105
return err
107106
}
108107

109-
finalizedTxn, err := p.getTransaction(ctx, record)
108+
finalizedTxn, err := p.data.GetBlockchainTransaction(ctx, *record.TransactionSignature, solana.CommitmentFinalized)
110109
if err != nil && err != solana.ErrSignatureNotFound {
111110
return errors.Wrap(err, "error getting finalized transaction")
112111
}
113112

114113
if finalizedTxn != nil {
115-
if finalizedTxn.HasErrors || finalizedTxn.ConfirmationState == transaction.ConfirmationFailed {
114+
if finalizedTxn.Err != nil || finalizedTxn.Meta.Err != nil {
116115
return p.markSwapFailed(ctx, record)
117-
}
118-
119-
if finalizedTxn.ConfirmationState == transaction.ConfirmationFinalized {
116+
} else {
120117
err = p.updateBalances(ctx, record)
121118
if err != nil {
122119
return errors.Wrap(err, "error updating balances")

pkg/code/common/vm.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ func GetVmConfigForMint(ctx context.Context, data code_data.Provider, mint *Acco
110110
}
111111
}
112112

113-
func EnsureVirtualTimelockAccountIsInitialized(ctx context.Context, data code_data.Provider, vmIndexerClient indexerpb.IndexerClient, vm, owner *Account, waitForInitialization bool) error {
114-
vmConfig, err := GetVmConfigForMint(ctx, data, vm)
113+
func EnsureVirtualTimelockAccountIsInitialized(ctx context.Context, data code_data.Provider, vmIndexerClient indexerpb.IndexerClient, mint, owner *Account, waitForInitialization bool) error {
114+
vmConfig, err := GetVmConfigForMint(ctx, data, mint)
115115
if err != nil {
116116
return err
117117
}
@@ -147,12 +147,12 @@ func EnsureVirtualTimelockAccountIsInitialized(ctx context.Context, data code_da
147147
}
148148

149149
for range 60 {
150-
time.Sleep(time.Second)
151-
152-
_, _, err := GetVirtualTimelockAccountLocationInMemory(ctx, vmIndexerClient, vm, owner)
150+
_, _, err := GetVirtualTimelockAccountLocationInMemory(ctx, vmIndexerClient, vmConfig.Vm, owner)
153151
if err == nil {
154152
return nil
155153
}
154+
155+
time.Sleep(time.Second)
156156
}
157157

158158
return errors.New("timed out waiting for initialization")

pkg/code/server/transaction/errors.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,8 @@ func handleStartSwapError(streamer transactionpb.Transaction_StartSwapServer, er
326326
return status.Error(codes.DeadlineExceeded, err.Error())
327327
case context.Canceled:
328328
return status.Error(codes.Canceled, err.Error())
329+
case transaction.ErrNoAvailableNonces, transaction.ErrNoncePoolNotFound:
330+
return status.Error(codes.Unavailable, "")
329331
}
330332
return status.Error(codes.Internal, "rpc server failure")
331333
}

0 commit comments

Comments
 (0)