Skip to content

Commit 5138612

Browse files
committed
Update swap transaction compute unit limits (#51)
1 parent b9b8fd3 commit 5138612

File tree

17 files changed

+176
-114
lines changed

17 files changed

+176
-114
lines changed

ocp/data/internal.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ type DatabaseData interface {
237237
FreeVmMemoryByIndex(ctx context.Context, memoryAccount string, index uint16) error
238238
FreeVmMemoryByAddress(ctx context.Context, address string) error
239239
ReserveVmMemory(ctx context.Context, vm string, accountType vm.VirtualAccountType, address string) (string, uint16, error)
240+
GetVmMemoryLocationByAddress(ctx context.Context, address string) (string, uint16, error)
240241

241242
// VM Storage
242243
// --------------------------------------------------------------------------------
@@ -876,6 +877,9 @@ func (dp *DatabaseProvider) FreeVmMemoryByAddress(ctx context.Context, address s
876877
func (dp *DatabaseProvider) ReserveVmMemory(ctx context.Context, vm string, accountType vm.VirtualAccountType, address string) (string, uint16, error) {
877878
return dp.vmRam.ReserveMemory(ctx, vm, accountType, address)
878879
}
880+
func (dp *DatabaseProvider) GetVmMemoryLocationByAddress(ctx context.Context, address string) (string, uint16, error) {
881+
return dp.vmRam.GetMemoryLocationByAddress(ctx, address)
882+
}
879883

880884
// VM Storage
881885
// --------------------------------------------------------------------------------

ocp/data/vm/ram/memory/store.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,20 @@ func (s *store) ReserveMemory(_ context.Context, vm string, accountType vm.Virtu
112112
return "", 0, ram.ErrNoFreeMemory
113113
}
114114

115+
// GetMemoryLocationByAddress implements vm.ram.Store.GetMemoryLocationByAddress
116+
func (s *store) GetMemoryLocationByAddress(_ context.Context, address string) (string, uint16, error) {
117+
s.mu.Lock()
118+
defer s.mu.Unlock()
119+
120+
reservationKey, ok := s.storedVirtualAccounts[address]
121+
if !ok {
122+
return "", 0, ram.ErrNotReserved
123+
}
124+
125+
memoryAccount, index := parseAccountIndexKey(reservationKey)
126+
return memoryAccount, index, nil
127+
}
128+
115129
func (s *store) find(data *ram.Record) *ram.Record {
116130
for _, item := range s.records {
117131
if item.Id == data.Id {
@@ -154,3 +168,18 @@ func (s *store) reset() {
154168
func getAccountIndexKey(memoryAccount string, index uint16) string {
155169
return fmt.Sprintf("%s:%d", memoryAccount, index)
156170
}
171+
172+
func parseAccountIndexKey(key string) (string, uint16) {
173+
var memoryAccount string
174+
var index uint16
175+
fmt.Sscanf(key, "%s", &memoryAccount)
176+
// Find the last colon and parse
177+
for i := len(key) - 1; i >= 0; i-- {
178+
if key[i] == ':' {
179+
memoryAccount = key[:i]
180+
fmt.Sscanf(key[i+1:], "%d", &index)
181+
break
182+
}
183+
}
184+
return memoryAccount, index
185+
}

ocp/data/vm/ram/postgres/model.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,18 @@ func dbReserveMemory(ctx context.Context, db *sqlx.DB, vm string, accountType vm
211211
})
212212
return memoryAccount, index, err
213213
}
214+
215+
func dbGetMemoryLocationByAddress(ctx context.Context, db *sqlx.DB, address string) (string, uint16, error) {
216+
var model allocatedMemoryModel
217+
218+
query := `SELECT id, vm, memory_account, index, is_allocated, stored_account_type, address, last_updated_at
219+
FROM ` + allocatedMemoryTableName + `
220+
WHERE address = $1 AND is_allocated`
221+
222+
err := db.QueryRowxContext(ctx, query, address).StructScan(&model)
223+
if err != nil {
224+
return "", 0, pgutil.CheckNoRows(err, ram.ErrNotReserved)
225+
}
226+
227+
return model.MemoryAccount, model.Index, nil
228+
}

ocp/data/vm/ram/postgres/store.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,8 @@ func (s *store) FreeMemoryByAddress(ctx context.Context, address string) error {
5353
func (s *store) ReserveMemory(ctx context.Context, vm string, accountType vm.VirtualAccountType, address string) (string, uint16, error) {
5454
return dbReserveMemory(ctx, s.db, vm, accountType, address)
5555
}
56+
57+
// GetMemoryLocationByAddress implements vm.ram.Store.GetMemoryLocationByAddress
58+
func (s *store) GetMemoryLocationByAddress(ctx context.Context, address string) (string, uint16, error) {
59+
return dbGetMemoryLocationByAddress(ctx, s.db, address)
60+
}

ocp/data/vm/ram/store.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,7 @@ type Store interface {
3232

3333
// ReserveMemory reserves a piece of memory in a VM for the virtual account address
3434
ReserveMemory(ctx context.Context, vm string, accountType vm.VirtualAccountType, address string) (string, uint16, error)
35+
36+
// GetMemoryLocationByAddress returns the memory account and index for a reserved virtual account address
37+
GetMemoryLocationByAddress(ctx context.Context, address string) (string, uint16, error)
3538
}

ocp/data/vm/ram/tests/tests.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,18 @@ func testHappyPath(t *testing.T, s ram.Store) {
107107

108108
_, _, err = s.ReserveMemory(ctx, "vm1", vm.VirtualAccountTypeTimelock, "newvirtualaccount3")
109109
assert.Equal(t, ram.ErrNoFreeMemory, err)
110+
111+
memoryAccount, index, err := s.GetMemoryLocationByAddress(ctx, "newvirtualaccount1")
112+
require.NoError(t, err)
113+
assert.Equal(t, "memoryaccount1", memoryAccount)
114+
assert.Equal(t, freedIndex1, index)
115+
116+
memoryAccount, index, err = s.GetMemoryLocationByAddress(ctx, "newvirtualaccount2")
117+
require.NoError(t, err)
118+
assert.Equal(t, "memoryaccount1", memoryAccount)
119+
assert.Equal(t, freedIndex2, index)
120+
121+
_, _, err = s.GetMemoryLocationByAddress(ctx, "nonexistent")
122+
assert.Equal(t, ram.ErrNotReserved, err)
110123
})
111124
}

ocp/rpc/transaction/server.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

88
"go.uber.org/zap"
99

10-
indexerpb "github.com/code-payments/code-vm-indexer/generated/indexer/v1"
1110
transactionpb "github.com/code-payments/ocp-protobuf-api/generated/go/transaction/v1"
1211

1312
"github.com/code-payments/ocp-server/ocp/aml"
@@ -24,8 +23,7 @@ type transactionServer struct {
2423

2524
log *zap.Logger
2625

27-
data ocp_data.Provider
28-
vmIndexerClient indexerpb.IndexerClient
26+
data ocp_data.Provider
2927

3028
auth *auth_util.RPCSignatureVerifier
3129

@@ -51,7 +49,6 @@ type transactionServer struct {
5149
func NewTransactionServer(
5250
log *zap.Logger,
5351
data ocp_data.Provider,
54-
vmIndexerClient indexerpb.IndexerClient,
5552
submitIntentIntegration SubmitIntentIntegration,
5653
airdropIntegration AirdropIntegration,
5754
antispamGuard *antispam.Guard,
@@ -92,8 +89,7 @@ func NewTransactionServer(
9289

9390
log: log,
9491

95-
data: data,
96-
vmIndexerClient: vmIndexerClient,
92+
data: data,
9793

9894
auth: auth_util.NewRPCSignatureVerifier(log, data),
9995

ocp/rpc/transaction/swap.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,9 @@ func (s *transactionServer) StatefulSwap(streamer transactionpb.Transaction_Stat
255255
// Section: On-demand account creation
256256
//
257257

258-
err = vm.EnsureVirtualTimelockAccountIsInitialized(ctx, s.data, s.vmIndexerClient, toMint, owner, true)
258+
err = vm.EnsureVirtualTimelockAccountIsInitialized(ctx, s.data, ownerDestinationTimelockVault, false)
259259
if err != nil {
260-
log.With(zap.Error(err)).Warn("timed out waiting for destination timelock account initialization")
260+
log.With(zap.Error(err)).Warn("error ensuring destination virtual timelock account is initialized")
261261
return handleStatefulSwapError(streamer, err)
262262
}
263263

@@ -288,7 +288,6 @@ func (s *transactionServer) StatefulSwap(streamer transactionpb.Transaction_Stat
288288
if common.IsCoreMint(fromMint) {
289289
swapHandler = NewCurrencyCreatorBuySwapHandler(
290290
s.data,
291-
s.vmIndexerClient,
292291
owner,
293292
swapAuthority,
294293
toMint,
@@ -298,7 +297,6 @@ func (s *transactionServer) StatefulSwap(streamer transactionpb.Transaction_Stat
298297
} else if common.IsCoreMint(toMint) {
299298
swapHandler = NewCurrencyCreatorSellSwapHandler(
300299
s.data,
301-
s.vmIndexerClient,
302300
owner,
303301
swapAuthority,
304302
fromMint,
@@ -308,7 +306,6 @@ func (s *transactionServer) StatefulSwap(streamer transactionpb.Transaction_Stat
308306
} else {
309307
swapHandler = NewCurrencyCreatorBuySellSwapHandler(
310308
s.data,
311-
s.vmIndexerClient,
312309
owner,
313310
swapAuthority,
314311
fromMint,

ocp/rpc/transaction/swap_handler.go

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package transaction
33
import (
44
"context"
55

6-
indexerpb "github.com/code-payments/code-vm-indexer/generated/indexer/v1"
7-
86
"github.com/code-payments/ocp-server/ocp/common"
97
ocp_data "github.com/code-payments/ocp-server/ocp/data"
108
vm_util "github.com/code-payments/ocp-server/ocp/vm"
@@ -36,8 +34,7 @@ type SwapHandler interface {
3634
}
3735

3836
type CurrencyCreatorBuySwapHandler struct {
39-
data ocp_data.Provider
40-
vmIndexerClient indexerpb.IndexerClient
37+
data ocp_data.Provider
4138

4239
buyer *common.Account
4340
temporaryHolder *common.Account
@@ -54,24 +51,22 @@ type CurrencyCreatorBuySwapHandler struct {
5451

5552
func NewCurrencyCreatorBuySwapHandler(
5653
data ocp_data.Provider,
57-
vmIndexerClient indexerpb.IndexerClient,
5854
buyer *common.Account,
5955
temporaryHolder *common.Account,
6056
mint *common.Account,
6157
amount uint64,
6258
nonce *common.Account,
6359
) SwapHandler {
6460
return &CurrencyCreatorBuySwapHandler{
65-
data: data,
66-
vmIndexerClient: vmIndexerClient,
61+
data: data,
6762

6863
buyer: buyer,
6964
temporaryHolder: temporaryHolder,
7065
mint: mint,
7166
amount: amount,
7267

7368
nonce: nonce,
74-
computeUnitLimit: 300_000,
69+
computeUnitLimit: 150_000,
7570
computeUnitPrice: 1_000,
7671
memoValue: "buy_v0",
7772
}
@@ -103,7 +98,12 @@ func (h *CurrencyCreatorBuySwapHandler) MakeInstructions(ctx context.Context) ([
10398
return nil, err
10499
}
105100

106-
h.memoryAccount, h.memoryIndex, err = vm_util.GetVirtualTimelockAccountLocationInMemory(ctx, h.vmIndexerClient, destinationVmConfig.Vm, h.buyer)
101+
destinationTimelockAccounts, err := h.buyer.GetTimelockAccounts(destinationVmConfig)
102+
if err != nil {
103+
return nil, err
104+
}
105+
106+
h.memoryAccount, h.memoryIndex, err = vm_util.GetVirtualTimelockAccountLocationInMemory(ctx, h.data, destinationTimelockAccounts.Vault)
107107
if err != nil {
108108
return nil, err
109109
}
@@ -203,8 +203,7 @@ func (h *CurrencyCreatorBuySwapHandler) MakeInstructions(ctx context.Context) ([
203203
}
204204

205205
type CurrencyCreatorSellSwapHandler struct {
206-
data ocp_data.Provider
207-
vmIndexerClient indexerpb.IndexerClient
206+
data ocp_data.Provider
208207

209208
seller *common.Account
210209
temporaryHolder *common.Account
@@ -221,24 +220,22 @@ type CurrencyCreatorSellSwapHandler struct {
221220

222221
func NewCurrencyCreatorSellSwapHandler(
223222
data ocp_data.Provider,
224-
vmIndexerClient indexerpb.IndexerClient,
225223
seller *common.Account,
226224
temporaryHolder *common.Account,
227225
mint *common.Account,
228226
amount uint64,
229227
nonce *common.Account,
230228
) SwapHandler {
231229
return &CurrencyCreatorSellSwapHandler{
232-
data: data,
233-
vmIndexerClient: vmIndexerClient,
230+
data: data,
234231

235232
seller: seller,
236233
temporaryHolder: temporaryHolder,
237234
mint: mint,
238235
amount: amount,
239236

240237
nonce: nonce,
241-
computeUnitLimit: 300_000,
238+
computeUnitLimit: 175_000,
242239
computeUnitPrice: 1_000,
243240
memoValue: "sell_v0",
244241
}
@@ -280,7 +277,12 @@ func (h *CurrencyCreatorSellSwapHandler) MakeInstructions(ctx context.Context) (
280277
return nil, err
281278
}
282279

283-
h.memoryAccount, h.memoryIndex, err = vm_util.GetVirtualTimelockAccountLocationInMemory(ctx, h.vmIndexerClient, destinationVmConfig.Vm, h.seller)
280+
destinationTimelockAccounts, err := h.seller.GetTimelockAccounts(destinationVmConfig)
281+
if err != nil {
282+
return nil, err
283+
}
284+
285+
h.memoryAccount, h.memoryIndex, err = vm_util.GetVirtualTimelockAccountLocationInMemory(ctx, h.data, destinationTimelockAccounts.Vault)
284286
if err != nil {
285287
return nil, err
286288
}
@@ -370,8 +372,7 @@ func (h *CurrencyCreatorSellSwapHandler) MakeInstructions(ctx context.Context) (
370372
}
371373

372374
type CurrencyCreatorBuySellSwapHandler struct {
373-
data ocp_data.Provider
374-
vmIndexerClient indexerpb.IndexerClient
375+
data ocp_data.Provider
375376

376377
swapper *common.Account
377378
temporaryHolder *common.Account
@@ -389,7 +390,6 @@ type CurrencyCreatorBuySellSwapHandler struct {
389390

390391
func NewCurrencyCreatorBuySellSwapHandler(
391392
data ocp_data.Provider,
392-
vmIndexerClient indexerpb.IndexerClient,
393393
swapper *common.Account,
394394
temporaryHolder *common.Account,
395395
fromMint *common.Account,
@@ -398,8 +398,7 @@ func NewCurrencyCreatorBuySellSwapHandler(
398398
nonce *common.Account,
399399
) SwapHandler {
400400
return &CurrencyCreatorBuySellSwapHandler{
401-
data: data,
402-
vmIndexerClient: vmIndexerClient,
401+
data: data,
403402

404403
swapper: swapper,
405404
temporaryHolder: temporaryHolder,
@@ -450,7 +449,12 @@ func (h *CurrencyCreatorBuySellSwapHandler) MakeInstructions(ctx context.Context
450449
return nil, err
451450
}
452451

453-
h.memoryAccount, h.memoryIndex, err = vm_util.GetVirtualTimelockAccountLocationInMemory(ctx, h.vmIndexerClient, destinationVmConfig.Vm, h.swapper)
452+
destinationTimelockAccounts, err := h.swapper.GetTimelockAccounts(destinationVmConfig)
453+
if err != nil {
454+
return nil, err
455+
}
456+
457+
h.memoryAccount, h.memoryIndex, err = vm_util.GetVirtualTimelockAccountLocationInMemory(ctx, h.data, destinationTimelockAccounts.Vault)
454458
if err != nil {
455459
return nil, err
456460
}

0 commit comments

Comments
 (0)