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 go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/aws/aws-sdk-go-v2/feature/rds/auth v1.6.25
github.com/aws/aws-sdk-go-v2/service/s3 v1.102.2
github.com/code-payments/code-vm-indexer v1.2.0
github.com/code-payments/ocp-protobuf-api v1.12.0
github.com/code-payments/ocp-protobuf-api v1.13.0
github.com/emirpasic/gods v1.12.0
github.com/envoyproxy/protoc-gen-validate v1.2.1
github.com/golang/protobuf v1.5.4
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I
github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ=
github.com/code-payments/code-vm-indexer v1.2.0 h1:rSHpBMiT9BKgmKcXg/VIoi/h0t7jNxGx07Qz59m+6Q0=
github.com/code-payments/code-vm-indexer v1.2.0/go.mod h1:vn91YN2qNqb+gGJeZe2+l+TNxVmEEiRHXXnIn2Y40h8=
github.com/code-payments/ocp-protobuf-api v1.12.0 h1:Gm+DMGJXvV4PIdM609BfcxoRrAwJHDAC12mRc6SL+Jc=
github.com/code-payments/ocp-protobuf-api v1.12.0/go.mod h1:tw6BooY5a8l6CtSZnKOruyKII0W04n89pcM4BizrgG8=
github.com/code-payments/ocp-protobuf-api v1.13.0 h1:Skzi+h0wqAu1e8zMNN7jCeQoJB3t1BVAVpnv0JuRDIU=
github.com/code-payments/ocp-protobuf-api v1.13.0/go.mod h1:tw6BooY5a8l6CtSZnKOruyKII0W04n89pcM4BizrgG8=
github.com/containerd/continuity v0.0.0-20190827140505-75bee3e2ccb6 h1:NmTXa/uVnDyp0TY5MKi197+3HWcnYWfnHGyaFthlnGw=
github.com/containerd/continuity v0.0.0-20190827140505-75bee3e2ccb6/go.mod h1:GL3xCUCBDV3CZiTSEKksMWbLE66hEyuu9qyDOOqM47Y=
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
Expand Down
12 changes: 12 additions & 0 deletions ocp/data/intent/intent.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type Record struct {
ReceivePaymentsPubliclyMetadata *ReceivePaymentsPubliclyMetadata
PublicDistributionMetadata *PublicDistributionMetadata

AppMetadata []byte

State State

Version uint64
Expand Down Expand Up @@ -146,6 +148,12 @@ func (r *Record) Clone() Record {
publicDistributionMetadata = &cloned
}

var appMetadata []byte
if r.AppMetadata != nil {
appMetadata = make([]byte, len(r.AppMetadata))
copy(appMetadata, r.AppMetadata)
}

return Record{
Id: r.Id,

Expand All @@ -162,6 +170,8 @@ func (r *Record) Clone() Record {
ReceivePaymentsPubliclyMetadata: receivePaymentsPubliclyMetadata,
PublicDistributionMetadata: publicDistributionMetadata,

AppMetadata: appMetadata,

State: r.State,

Version: r.Version,
Expand All @@ -186,6 +196,8 @@ func (r *Record) CopyTo(dst *Record) {
dst.ReceivePaymentsPubliclyMetadata = r.ReceivePaymentsPubliclyMetadata
dst.PublicDistributionMetadata = r.PublicDistributionMetadata

dst.AppMetadata = r.AppMetadata

dst.State = r.State

dst.Version = r.Version
Expand Down
24 changes: 14 additions & 10 deletions ocp/data/intent/postgres/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type intentModel struct {
IsReturned bool `db:"is_returned"`
IsIssuerVoidingGiftCard bool `db:"is_issuer_voiding_gift_card"`
IsSwap bool `db:"is_swap"`
AppMetadata []byte `db:"app_metadata"`
State uint `db:"state"`
Version int64 `db:"version"`
CreatedAt time.Time `db:"created_at"`
Expand Down Expand Up @@ -73,6 +74,7 @@ func toIntentModel(obj *intent.Record) (*intentModel, error) {
IntentType: uint(obj.IntentType),
Mint: mint,
InitiatorOwner: obj.InitiatorOwnerAccount,
AppMetadata: obj.AppMetadata,
State: uint(obj.State),
CreatedAt: obj.CreatedAt,
Version: int64(obj.Version),
Expand Down Expand Up @@ -142,6 +144,7 @@ func fromIntentModel(obj *intentModel) *intent.Record {
IntentId: obj.IntentId,
IntentType: intent.Type(obj.IntentType),
InitiatorOwnerAccount: obj.InitiatorOwner,
AppMetadata: obj.AppMetadata,
State: intent.State(obj.State),
Version: uint64(obj.Version),
CreatedAt: obj.CreatedAt.UTC(),
Expand Down Expand Up @@ -225,16 +228,16 @@ func (m *intentModel) dbSave(ctx context.Context, db *sqlx.DB) error {

return pgutil.ExecuteInTx(ctx, db, sql.LevelDefault, func(tx *sqlx.Tx) error {
query := `INSERT INTO ` + intentTableName + `
(intent_id, intent_type, mint, owner, source, destination_owner, destination, quantity, exchange_currency, exchange_rate, native_amount, usd_market_value, original_usd_market_value, is_withdraw, is_remote_send, is_returned, is_issuer_voiding_gift_card, is_swap, state, version, created_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20 + 1, $21)
(intent_id, intent_type, mint, owner, source, destination_owner, destination, quantity, exchange_currency, exchange_rate, native_amount, usd_market_value, original_usd_market_value, is_withdraw, is_remote_send, is_returned, is_issuer_voiding_gift_card, is_swap, app_metadata, state, version, created_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21 + 1, $22)

ON CONFLICT (intent_id)
DO UPDATE
SET usd_market_value = $12, state = $19, version = ` + intentTableName + `.version + 1
WHERE ` + intentTableName + `.intent_id = $1 AND ` + intentTableName + `.version = $20
SET usd_market_value = $12, state = $20, version = ` + intentTableName + `.version + 1
WHERE ` + intentTableName + `.intent_id = $1 AND ` + intentTableName + `.version = $21

RETURNING
id, intent_id, intent_type, mint, owner, source, destination_owner, destination, quantity, exchange_currency, exchange_rate, native_amount, usd_market_value, original_usd_market_value, is_withdraw, is_remote_send, is_returned, is_issuer_voiding_gift_card, is_swap, state, version, created_at`
id, intent_id, intent_type, mint, owner, source, destination_owner, destination, quantity, exchange_currency, exchange_rate, native_amount, usd_market_value, original_usd_market_value, is_withdraw, is_remote_send, is_returned, is_issuer_voiding_gift_card, is_swap, app_metadata, state, version, created_at`

err := tx.QueryRowxContext(
ctx,
Expand All @@ -257,6 +260,7 @@ func (m *intentModel) dbSave(ctx context.Context, db *sqlx.DB) error {
m.IsReturned,
m.IsIssuerVoidingGiftCard,
m.IsSwap,
m.AppMetadata,
m.State,
m.Version,
m.CreatedAt,
Expand Down Expand Up @@ -382,7 +386,7 @@ func dbGetAccounts(ctx context.Context, db *sqlx.DB, intentType intent.Type, pag
func dbGetIntentByIntentID(ctx context.Context, db *sqlx.DB, intentID string) (*intentModel, error) {
res := &intentModel{}

query := `SELECT id, intent_id, intent_type, mint, owner, source, destination_owner, destination, quantity, exchange_currency, exchange_rate, native_amount, usd_market_value, original_usd_market_value, is_withdraw, is_remote_send, is_returned, is_issuer_voiding_gift_card, is_swap, state, version, created_at
query := `SELECT id, intent_id, intent_type, mint, owner, source, destination_owner, destination, quantity, exchange_currency, exchange_rate, native_amount, usd_market_value, original_usd_market_value, is_withdraw, is_remote_send, is_returned, is_issuer_voiding_gift_card, is_swap, app_metadata, state, version, created_at
FROM ` + intentTableName + `
WHERE intent_id = $1
LIMIT 1`
Expand All @@ -402,7 +406,7 @@ func dbGetIntentByIntentID(ctx context.Context, db *sqlx.DB, intentID string) (*
func dbGetIntentByID(ctx context.Context, db *sqlx.DB, id int64) (*intentModel, error) {
res := &intentModel{}

query := `SELECT id, intent_id, intent_type, mint, owner, source, destination_owner, destination, quantity, exchange_currency, exchange_rate, native_amount, usd_market_value, original_usd_market_value, is_withdraw, is_remote_send, is_returned, is_issuer_voiding_gift_card, is_swap, state, version, created_at
query := `SELECT id, intent_id, intent_type, mint, owner, source, destination_owner, destination, quantity, exchange_currency, exchange_rate, native_amount, usd_market_value, original_usd_market_value, is_withdraw, is_remote_send, is_returned, is_issuer_voiding_gift_card, is_swap, app_metadata, state, version, created_at
FROM ` + intentTableName + `
WHERE id = $1
LIMIT 1`
Expand All @@ -425,7 +429,7 @@ func dbGetAllByOwner(ctx context.Context, db *sqlx.DB, owner string, cursor q.Cu
models := []*intentModel{}

opts := []any{owner}
query1 := `SELECT id, intent_id, intent_type, mint, owner, source, destination_owner, destination, quantity, exchange_currency, exchange_rate, native_amount, usd_market_value, original_usd_market_value, is_withdraw, is_remote_send, is_returned, is_issuer_voiding_gift_card, is_swap, state, version, created_at
query1 := `SELECT id, intent_id, intent_type, mint, owner, source, destination_owner, destination, quantity, exchange_currency, exchange_rate, native_amount, usd_market_value, original_usd_market_value, is_withdraw, is_remote_send, is_returned, is_issuer_voiding_gift_card, is_swap, app_metadata, state, version, created_at
FROM ` + intentTableName + `
WHERE (owner = $1 OR destination_owner = $1)
`
Expand Down Expand Up @@ -494,7 +498,7 @@ func dbGetAllByOwner(ctx context.Context, db *sqlx.DB, owner string, cursor q.Cu
func dbGetOriginalGiftCardIssuedIntent(ctx context.Context, db *sqlx.DB, giftCardVault string) (*intentModel, error) {
res := []*intentModel{}

query := `SELECT id, intent_id, intent_type, mint, owner, source, destination_owner, destination, quantity, exchange_currency, exchange_rate, native_amount, usd_market_value, original_usd_market_value, is_withdraw, is_remote_send, is_returned, is_issuer_voiding_gift_card, is_swap, state, version, created_at
query := `SELECT id, intent_id, intent_type, mint, owner, source, destination_owner, destination, quantity, exchange_currency, exchange_rate, native_amount, usd_market_value, original_usd_market_value, is_withdraw, is_remote_send, is_returned, is_issuer_voiding_gift_card, is_swap, app_metadata, state, version, created_at
FROM ` + intentTableName + `
WHERE destination = $1 and intent_type = $2 AND state != $3 AND is_remote_send IS TRUE
LIMIT 2
Expand Down Expand Up @@ -526,7 +530,7 @@ func dbGetOriginalGiftCardIssuedIntent(ctx context.Context, db *sqlx.DB, giftCar
func dbGetGiftCardClaimedIntent(ctx context.Context, db *sqlx.DB, giftCardVault string) (*intentModel, error) {
res := []*intentModel{}

query := `SELECT id, intent_id, intent_type, mint, owner, source, destination_owner, destination, quantity, exchange_currency, exchange_rate, native_amount, usd_market_value, original_usd_market_value, is_withdraw, is_remote_send, is_returned, is_issuer_voiding_gift_card, is_swap, state, version, created_at
query := `SELECT id, intent_id, intent_type, mint, owner, source, destination_owner, destination, quantity, exchange_currency, exchange_rate, native_amount, usd_market_value, original_usd_market_value, is_withdraw, is_remote_send, is_returned, is_issuer_voiding_gift_card, is_swap, app_metadata, state, version, created_at
FROM ` + intentTableName + `
WHERE source = $1 and intent_type = $2 AND state != $3 AND is_remote_send IS TRUE
LIMIT 2
Expand Down
2 changes: 2 additions & 0 deletions ocp/data/intent/postgres/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ const (
is_issuer_voiding_gift_card BOOL NOT NULL,
is_swap BOOL NOT NULL,

app_metadata BYTEA NULL,

state INTEGER NOT NULL,

version INTEGER NOT NULL,
Expand Down
6 changes: 4 additions & 2 deletions ocp/data/intent/tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,9 @@ func testSendPublicPaymentRoundTrip(t *testing.T, s intent.Store) {
IsRemoteSend: true,
IsSwapSell: true,
},
State: intent.StateUnknown,
CreatedAt: time.Now(),
AppMetadata: []byte("test_app_metadata"),
State: intent.StateUnknown,
CreatedAt: time.Now(),
}
cloned := expected.Clone()
err = s.Save(ctx, &expected)
Expand All @@ -181,6 +182,7 @@ func testSendPublicPaymentRoundTrip(t *testing.T, s intent.Store) {
assert.Equal(t, cloned.SendPublicPaymentMetadata.IsWithdrawal, actual.SendPublicPaymentMetadata.IsWithdrawal)
assert.Equal(t, cloned.SendPublicPaymentMetadata.IsRemoteSend, actual.SendPublicPaymentMetadata.IsRemoteSend)
assert.Equal(t, cloned.SendPublicPaymentMetadata.IsSwapSell, actual.SendPublicPaymentMetadata.IsSwapSell)
assert.Equal(t, cloned.AppMetadata, actual.AppMetadata)
assert.Equal(t, cloned.State, actual.State)
assert.Equal(t, cloned.CreatedAt.Unix(), actual.CreatedAt.Unix())
assert.EqualValues(t, 1, actual.Id)
Expand Down
9 changes: 9 additions & 0 deletions ocp/rpc/transaction/intent.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ func (s *transactionServer) SubmitIntent(streamer transactionpb.Transaction_Subm
return handleSubmitIntentError(ctx, streamer, intentRecord, err)
}

// Store any optional app-level metadata provided alongside the intent
if appMetadata := submitActionsReq.Metadata.GetAppMetadata(); appMetadata != nil {
intentRecord.AppMetadata = appMetadata.Value
}

// Check whether the intent is a no-op
isNoop, err := intentHandler.IsNoop(ctx, intentRecord, submitActionsReq.Metadata, submitActionsReq.Actions)
if err != nil {
Expand Down Expand Up @@ -890,6 +895,10 @@ func (s *transactionServer) GetIntentMetadata(ctx context.Context, req *transact
}, nil
}

if len(intentRecord.AppMetadata) > 0 {
metadata.AppMetadata = &transactionpb.AppMetadata{Value: intentRecord.AppMetadata}
}

return &transactionpb.GetIntentMetadataResponse{
Result: transactionpb.GetIntentMetadataResponse_OK,
Metadata: metadata,
Expand Down
Loading