From b27defb84425b1f18473eaa128b86c48820bbc85 Mon Sep 17 00:00:00 2001 From: jeffyanta Date: Wed, 10 Dec 2025 10:22:21 -0500 Subject: [PATCH] Cleanup unused data methods --- ocp/data/fulfillment/memory/store.go | 180 +-------------------- ocp/data/fulfillment/postgres/model.go | 110 +------------ ocp/data/fulfillment/postgres/store.go | 52 +----- ocp/data/fulfillment/store.go | 28 ---- ocp/data/fulfillment/tests/tests.go | 115 +------------ ocp/data/internal.go | 39 ----- ocp/data/vm/storage/account.go | 10 +- ocp/data/vm/storage/postgres/model.go | 23 ++- ocp/data/vm/storage/postgres/store_test.go | 2 +- ocp/data/vm/storage/tests/tests.go | 2 - 10 files changed, 20 insertions(+), 541 deletions(-) diff --git a/ocp/data/fulfillment/memory/store.go b/ocp/data/fulfillment/memory/store.go index 46dd59f..a83edb6 100644 --- a/ocp/data/fulfillment/memory/store.go +++ b/ocp/data/fulfillment/memory/store.go @@ -7,9 +7,9 @@ import ( "sync" "time" + "github.com/code-payments/ocp-server/database/query" "github.com/code-payments/ocp-server/ocp/data/fulfillment" "github.com/code-payments/ocp-server/ocp/data/intent" - "github.com/code-payments/ocp-server/database/query" "github.com/code-payments/ocp-server/pointer" ) @@ -130,88 +130,6 @@ func (s *store) findByStateAndAddress(state fulfillment.State, address string) [ return res } -func (s *store) findByStateAndAddressAsSource(state fulfillment.State, address string) []*fulfillment.Record { - res := make([]*fulfillment.Record, 0) - for _, item := range s.records { - if item.State != state { - continue - } - - if item.Source == address { - res = append(res, item) - continue - } - } - return res -} - -func (s *store) findByTypeStateAndAddress(fulfillmentType fulfillment.Type, state fulfillment.State, address string) []*fulfillment.Record { - res := make([]*fulfillment.Record, 0) - for _, item := range s.records { - if item.FulfillmentType != fulfillmentType { - continue - } - - if item.State != state { - continue - } - - if item.Source == address { - res = append(res, item) - continue - } - - if item.Destination != nil && *item.Destination == address { - res = append(res, item) - continue - } - } - return res -} - -func (s *store) findByTypeStateAndAddressAsSource(fulfillmentType fulfillment.Type, state fulfillment.State, address string) []*fulfillment.Record { - res := make([]*fulfillment.Record, 0) - for _, item := range s.records { - if item.FulfillmentType != fulfillmentType { - continue - } - - if item.State != state { - continue - } - - if item.Source == address { - res = append(res, item) - continue - } - } - return res -} - -func (s *store) findByTypeActionAndState(intentId string, actionId uint32, fulfillmentType fulfillment.Type, state fulfillment.State) []*fulfillment.Record { - res := make([]*fulfillment.Record, 0) - for _, item := range s.records { - if item.Intent != intentId { - continue - } - - if item.ActionId != actionId { - continue - } - - if item.FulfillmentType != fulfillmentType { - continue - } - - if item.State != state { - continue - } - - res = append(res, item) - } - return res -} - func (s *store) findScheduableByAddress(address string) []*fulfillment.Record { res := make([]*fulfillment.Record, 0) for _, item := range s.records { @@ -257,21 +175,6 @@ func (s *store) findScheduableByDestination(destinaion string) []*fulfillment.Re return res } -func (s *store) findScheduableByType(fulfillmentType fulfillment.Type) []*fulfillment.Record { - res := make([]*fulfillment.Record, 0) - for _, item := range s.records { - if item.State != fulfillment.StateUnknown && item.State != fulfillment.StatePending { - continue - } - - if item.FulfillmentType == fulfillmentType { - res = append(res, item) - continue - } - } - return res -} - func (s *store) filter(items []*fulfillment.Record, cursor query.Cursor, limit uint64, direction query.Ordering) []*fulfillment.Record { var start uint64 @@ -304,16 +207,6 @@ func (s *store) filter(items []*fulfillment.Record, cursor query.Cursor, limit u return res } -func (s *store) filterByType(items []*fulfillment.Record, fulfillmentType fulfillment.Type) []*fulfillment.Record { - var res []*fulfillment.Record - for _, item := range items { - if item.FulfillmentType == fulfillmentType { - res = append(res, item) - } - } - return res -} - func (s *store) filterDisabledActiveScheduling(items []*fulfillment.Record) []*fulfillment.Record { var res []*fulfillment.Record for _, item := range items { @@ -345,13 +238,6 @@ func (s *store) filterScheduledAfter(items []*fulfillment.Record, intentOrdering return res } -func (s *store) Count(ctx context.Context) (uint64, error) { - s.mu.Lock() - defer s.mu.Unlock() - - return uint64(len(s.records)), nil -} - func (s *store) CountByState(ctx context.Context, state fulfillment.State) (uint64, error) { s.mu.Lock() defer s.mu.Unlock() @@ -360,19 +246,6 @@ func (s *store) CountByState(ctx context.Context, state fulfillment.State) (uint return uint64(len(res)), nil } -func (s *store) CountByStateGroupedByType(ctx context.Context, state fulfillment.State) (map[fulfillment.Type]uint64, error) { - s.mu.Lock() - defer s.mu.Unlock() - - items := s.findByState(state) - - res := make(map[fulfillment.Type]uint64) - for _, item := range items { - res[item.FulfillmentType] += 1 - } - return res, nil -} - func (s *store) CountForMetrics(ctx context.Context, state fulfillment.State) (map[fulfillment.Type]uint64, error) { return nil, errors.New("not implemented") } @@ -385,22 +258,6 @@ func (s *store) CountByStateAndAddress(ctx context.Context, state fulfillment.St return uint64(len(res)), nil } -func (s *store) CountByTypeStateAndAddressAsSource(ctx context.Context, fulfillmentType fulfillment.Type, state fulfillment.State, address string) (uint64, error) { - s.mu.Lock() - defer s.mu.Unlock() - - res := s.findByTypeStateAndAddressAsSource(fulfillmentType, state, address) - return uint64(len(res)), nil -} - -func (s *store) CountByTypeStateAndAddress(ctx context.Context, fulfillmentType fulfillment.Type, state fulfillment.State, address string) (uint64, error) { - s.mu.Lock() - defer s.mu.Unlock() - - res := s.findByTypeStateAndAddress(fulfillmentType, state, address) - return uint64(len(res)), nil -} - func (s *store) CountByIntentAndState(ctx context.Context, intent string, state fulfillment.State) (uint64, error) { s.mu.Lock() defer s.mu.Unlock() @@ -409,14 +266,6 @@ func (s *store) CountByIntentAndState(ctx context.Context, intent string, state return uint64(len(res)), nil } -func (s *store) CountByTypeActionAndState(ctx context.Context, intentId string, actionId uint32, fulfillmentType fulfillment.Type, state fulfillment.State) (uint64, error) { - s.mu.Lock() - defer s.mu.Unlock() - - res := s.findByTypeActionAndState(intentId, actionId, fulfillmentType, state) - return uint64(len(res)), nil -} - func (s *store) CountByIntent(ctx context.Context, intent string) (uint64, error) { s.mu.Lock() defer s.mu.Unlock() @@ -628,19 +477,6 @@ func (s *store) GetAllByAction(ctx context.Context, intentId string, actionId ui return cloneAll(res), nil } -func (s *store) GetAllByTypeAndAction(ctx context.Context, fulfillmentType fulfillment.Type, intentId string, actionId uint32) ([]*fulfillment.Record, error) { - s.mu.Lock() - defer s.mu.Unlock() - - res := s.findByAction(intentId, actionId) - res = s.filterByType(res, fulfillmentType) - if len(res) == 0 { - return nil, fulfillment.ErrFulfillmentNotFound - } - - return cloneAll(res), nil -} - func (s *store) GetFirstSchedulableByAddressAsSource(ctx context.Context, address string) (*fulfillment.Record, error) { s.mu.Lock() defer s.mu.Unlock() @@ -668,20 +504,6 @@ func (s *store) GetFirstSchedulableByAddressAsDestination(ctx context.Context, a return nil, fulfillment.ErrFulfillmentNotFound } -func (s *store) GetFirstSchedulableByType(ctx context.Context, fulfillmentType fulfillment.Type) (*fulfillment.Record, error) { - s.mu.Lock() - defer s.mu.Unlock() - - if items := s.findScheduableByType(fulfillmentType); len(items) > 0 { - sorted := fulfillment.BySchedulingOrder(items) - sort.Sort(sorted) - - cloned := sorted[0].Clone() - return &cloned, nil - } - return nil, fulfillment.ErrFulfillmentNotFound -} - func (s *store) GetNextSchedulableByAddress(ctx context.Context, address string, intentOrderingIndex uint64, actionOrderingIndex, fulfillmentOrderingIndex uint32) (*fulfillment.Record, error) { s.mu.Lock() defer s.mu.Unlock() diff --git a/ocp/data/fulfillment/postgres/model.go b/ocp/data/fulfillment/postgres/model.go index cd30a49..05490ec 100644 --- a/ocp/data/fulfillment/postgres/model.go +++ b/ocp/data/fulfillment/postgres/model.go @@ -151,18 +151,6 @@ func fromFulfillmentModel(obj *fulfillmentModel) *fulfillment.Record { } } -func dbGetCount(ctx context.Context, db *sqlx.DB) (uint64, error) { - var res uint64 - - query := `SELECT COUNT(*) FROM ` + fulfillmentTableName - err := db.GetContext(ctx, &res, query) - if err != nil { - return 0, err - } - - return res, nil -} - func dbGetCountByState(ctx context.Context, db *sqlx.DB, state fulfillment.State) (uint64, error) { var res uint64 @@ -175,29 +163,6 @@ func dbGetCountByState(ctx context.Context, db *sqlx.DB, state fulfillment.State return res, nil } -func dbGetCountByStateGroupedByType(ctx context.Context, db *sqlx.DB, state fulfillment.State) (map[fulfillment.Type]uint64, error) { - type countedType struct { - Type fulfillment.Type `db:"fulfillment_type"` - Count uint64 `db:"count"` - } - - var countedTypes []countedType - query := `SELECT fulfillment_type, COUNT(*) as count FROM ` + fulfillmentTableName + ` - WHERE state = $1 - GROUP BY fulfillment_type - ` - err := db.SelectContext(ctx, &countedTypes, query, state) - if err != nil { - return nil, err - } - - res := make(map[fulfillment.Type]uint64) - for _, countedType := range countedTypes { - res[countedType.Type] = countedType.Count - } - return res, nil -} - func dbGetCountForMetrics(ctx context.Context, db *sqlx.DB, state fulfillment.State) (map[fulfillment.Type]uint64, error) { type countedType struct { Type fulfillment.Type `db:"fulfillment_type"` @@ -213,7 +178,7 @@ func dbGetCountForMetrics(ctx context.Context, db *sqlx.DB, state fulfillment.St var countedTypes []countedType query := `SELECT fulfillment_type, COUNT(*) as count FROM ` + fulfillmentTableName + ` - WHERE fulfillment_type != $1 AND state = $2 + WHERE state = $1 AND fulfillment_type != $2 GROUP BY fulfillment_type ` err := db.SelectContext(ctx, &countedTypes, query, exclusion, state) @@ -240,30 +205,6 @@ func dbGetCountByStateAndAddress(ctx context.Context, db *sqlx.DB, state fulfill return res, nil } -func dbGetCountByTypeStateAndAddress(ctx context.Context, db *sqlx.DB, fulfillmentType fulfillment.Type, state fulfillment.State, address string) (uint64, error) { - var res uint64 - - query := `SELECT COUNT(*) FROM ` + fulfillmentTableName + ` WHERE ((source = $1 OR destination = $1) AND state = $2 AND fulfillment_type = $3)` - err := db.GetContext(ctx, &res, query, address, state, fulfillmentType) - if err != nil { - return 0, err - } - - return res, nil -} - -func dbGetCountByTypeStateAndAddressAsSource(ctx context.Context, db *sqlx.DB, fulfillmentType fulfillment.Type, state fulfillment.State, address string) (uint64, error) { - var res uint64 - - query := `SELECT COUNT(*) FROM ` + fulfillmentTableName + ` WHERE (source = $1 AND state = $2 AND fulfillment_type = $3)` - err := db.GetContext(ctx, &res, query, address, state, fulfillmentType) - if err != nil { - return 0, err - } - - return res, nil -} - func dbGetCountByIntentAndState(ctx context.Context, db *sqlx.DB, intent string, state fulfillment.State) (uint64, error) { var res uint64 @@ -288,19 +229,6 @@ func dbGetCountByIntent(ctx context.Context, db *sqlx.DB, intent string) (uint64 return res, nil } -func dbGetCountByTypeActionAndState(ctx context.Context, db *sqlx.DB, intentId string, actionId uint32, fulfillmentType fulfillment.Type, state fulfillment.State) (uint64, error) { - var res uint64 - - query := `SELECT COUNT(*) FROM ` + fulfillmentTableName + ` - WHERE intent = $1 AND action_id = $2 AND fulfillment_type = $3 AND state = $4` - err := db.GetContext(ctx, &res, query, intentId, actionId, fulfillmentType, state) - if err != nil { - return 0, err - } - - return res, nil -} - func dbGetCountPendingByType(ctx context.Context, db *sqlx.DB) (map[fulfillment.Type]uint64, error) { type countedType struct { Type fulfillment.Type `db:"fulfillment_type"` @@ -578,26 +506,6 @@ func dbGetAllByAction(ctx context.Context, db *sqlx.DB, intentId string, actionI return res, nil } -func dbGetAllByTypeAndAction(ctx context.Context, db *sqlx.DB, fulfillmentType fulfillment.Type, intentId string, actionId uint32) ([]*fulfillmentModel, error) { - res := []*fulfillmentModel{} - - query := `SELECT id, intent, intent_type, action_id, action_type, fulfillment_type, data, signature, nonce, blockhash, virtual_signature, virtual_nonce, virtual_blockhash, source, destination, intent_ordering_index, action_ordering_index, fulfillment_ordering_index, disable_active_scheduling, state, version, created_at - FROM ` + fulfillmentTableName + ` - WHERE intent = $1 AND action_id = $2 AND fulfillment_type = $3 - ` - - err := db.SelectContext(ctx, &res, query, intentId, actionId, fulfillmentType) - if err != nil { - return nil, pgutil.CheckNoRows(err, fulfillment.ErrFulfillmentNotFound) - } - - if len(res) == 0 { - return nil, fulfillment.ErrFulfillmentNotFound - } - - return res, nil -} - func dbGetFirstSchedulableByAddressAsSource(ctx context.Context, db *sqlx.DB, address string) (*fulfillmentModel, error) { res := &fulfillmentModel{} @@ -630,22 +538,6 @@ func dbGetFirstSchedulableByAddressAsDestination(ctx context.Context, db *sqlx.D return res, nil } -func dbGetFirstSchedulableByType(ctx context.Context, db *sqlx.DB, fulfillmentType fulfillment.Type) (*fulfillmentModel, error) { - res := &fulfillmentModel{} - - query := `SELECT id, intent, intent_type, action_id, action_type, fulfillment_type, data, signature, nonce, blockhash, virtual_signature, virtual_nonce, virtual_blockhash, source, destination, intent_ordering_index, action_ordering_index, fulfillment_ordering_index, disable_active_scheduling, state, version, created_at - FROM ` + fulfillmentTableName + ` - WHERE (fulfillment_type = $1 AND (state = $2 OR state = $3)) - ORDER BY intent_ordering_index ASC, action_ordering_index ASC, fulfillment_ordering_index ASC - LIMIT 1` - - err := db.GetContext(ctx, res, query, fulfillmentType, fulfillment.StateUnknown, fulfillment.StatePending) - if err != nil { - return nil, pgutil.CheckNoRows(err, fulfillment.ErrFulfillmentNotFound) - } - return res, nil -} - func dbGetNextSchedulableByAddress(ctx context.Context, db *sqlx.DB, address string, intentOrderingIndex uint64, actionOrderingIndex, fulfillmentOrderingIndex uint32) (*fulfillmentModel, error) { res := &fulfillmentModel{} diff --git a/ocp/data/fulfillment/postgres/store.go b/ocp/data/fulfillment/postgres/store.go index b7c0d98..853dedf 100644 --- a/ocp/data/fulfillment/postgres/store.go +++ b/ocp/data/fulfillment/postgres/store.go @@ -7,9 +7,9 @@ import ( "github.com/jmoiron/sqlx" - "github.com/code-payments/ocp-server/ocp/data/fulfillment" pgutil "github.com/code-payments/ocp-server/database/postgres" "github.com/code-payments/ocp-server/database/query" + "github.com/code-payments/ocp-server/ocp/data/fulfillment" ) type store struct { @@ -22,21 +22,11 @@ func New(db *sql.DB) fulfillment.Store { } } -// Count implements fulfillment.Store.Count -func (s *store) Count(ctx context.Context) (uint64, error) { - return dbGetCount(ctx, s.db) -} - // CountByState implements fulfillment.Store.CountByState func (s *store) CountByState(ctx context.Context, state fulfillment.State) (uint64, error) { return dbGetCountByState(ctx, s.db, state) } -// CountByStateGroupedByType implements fulfillment.Store.CountByStateGroupedByType -func (s *store) CountByStateGroupedByType(ctx context.Context, state fulfillment.State) (map[fulfillment.Type]uint64, error) { - return dbGetCountByStateGroupedByType(ctx, s.db, state) -} - // CountForMetrics implements fulfillment.Store.CountForMetrics func (s *store) CountForMetrics(ctx context.Context, state fulfillment.State) (map[fulfillment.Type]uint64, error) { return dbGetCountForMetrics(ctx, s.db, state) @@ -47,16 +37,6 @@ func (s *store) CountByStateAndAddress(ctx context.Context, state fulfillment.St return dbGetCountByStateAndAddress(ctx, s.db, state, address) } -// CountByTypeStateAndAddress implements fulfillment.Store.CountByTypeStateAndAddress -func (s *store) CountByTypeStateAndAddress(ctx context.Context, fulfillmentType fulfillment.Type, state fulfillment.State, address string) (uint64, error) { - return dbGetCountByTypeStateAndAddress(ctx, s.db, fulfillmentType, state, address) -} - -// CountByTypeStateAndAddressAsSource implements fulfillment.Store.CountByTypeStateAndAddressAsSource -func (s *store) CountByTypeStateAndAddressAsSource(ctx context.Context, fulfillmentType fulfillment.Type, state fulfillment.State, address string) (uint64, error) { - return dbGetCountByTypeStateAndAddressAsSource(ctx, s.db, fulfillmentType, state, address) -} - // CountByIntentAndState implements fulfillment.Store.CountByIntentAndState func (s *store) CountByIntentAndState(ctx context.Context, intent string, state fulfillment.State) (uint64, error) { return dbGetCountByIntentAndState(ctx, s.db, intent, state) @@ -67,11 +47,6 @@ func (s *store) CountByIntent(ctx context.Context, intent string) (uint64, error return dbGetCountByIntent(ctx, s.db, intent) } -// CountByTypeActionAndState implements fulfillment.Store.CountByTypeActionAndState -func (s *store) CountByTypeActionAndState(ctx context.Context, intentId string, actionId uint32, fulfillmentType fulfillment.Type, state fulfillment.State) (uint64, error) { - return dbGetCountByTypeActionAndState(ctx, s.db, intentId, actionId, fulfillmentType, state) -} - // CountPendingByType implements fulfillment.Store.CountPendingByType func (s *store) CountPendingByType(ctx context.Context) (map[fulfillment.Type]uint64, error) { return dbGetCountPendingByType(ctx, s.db) @@ -201,21 +176,6 @@ func (s *store) GetAllByAction(ctx context.Context, intent string, actionId uint return fulfillments, nil } -// GetAllByTypeAndAction implements fulfillment.Store.GetAllByTypeAndAction -func (s *store) GetAllByTypeAndAction(ctx context.Context, fulfillmentType fulfillment.Type, intentId string, actionId uint32) ([]*fulfillment.Record, error) { - models, err := dbGetAllByTypeAndAction(ctx, s.db, fulfillmentType, intentId, actionId) - if err != nil { - return nil, err - } - - fulfillments := make([]*fulfillment.Record, len(models)) - for i, model := range models { - fulfillments[i] = fromFulfillmentModel(model) - } - - return fulfillments, nil -} - // GetFirstSchedulableByAddressAsSource implements fulfillment.Store.GetFirstSchedulableByAddressAsSource func (s *store) GetFirstSchedulableByAddressAsSource(ctx context.Context, address string) (*fulfillment.Record, error) { model, err := dbGetFirstSchedulableByAddressAsSource(ctx, s.db, address) @@ -236,16 +196,6 @@ func (s *store) GetFirstSchedulableByAddressAsDestination(ctx context.Context, a return fromFulfillmentModel(model), nil } -// GetFirstSchedulableByType implements fulfillment.Store.GetFirstSchedulableByType -func (s *store) GetFirstSchedulableByType(ctx context.Context, fulfillmentType fulfillment.Type) (*fulfillment.Record, error) { - model, err := dbGetFirstSchedulableByType(ctx, s.db, fulfillmentType) - if err != nil { - return nil, err - } - - return fromFulfillmentModel(model), nil -} - // GetNextSchedulableByAddress implements fulfillment.Store.GetNextSchedulableByAddress func (s *store) GetNextSchedulableByAddress(ctx context.Context, address string, intentOrderingIndex uint64, actionOrderingIndex, fulfillmentOrderingIndex uint32) (*fulfillment.Record, error) { model, err := dbGetNextSchedulableByAddress(ctx, s.db, address, intentOrderingIndex, actionOrderingIndex, fulfillmentOrderingIndex) diff --git a/ocp/data/fulfillment/store.go b/ocp/data/fulfillment/store.go index 19e9c74..b53da59 100644 --- a/ocp/data/fulfillment/store.go +++ b/ocp/data/fulfillment/store.go @@ -15,38 +15,21 @@ var ( ) type Store interface { - // Count returns the total count of fulfillment records. - Count(ctx context.Context) (uint64, error) - // Count returns the total count of fulfillments in the provided state. CountByState(ctx context.Context, state State) (uint64, error) - // CountByStateGroupedByType returns the total count of fulfillments, grouped - // by type, in the provided state. - CountByStateGroupedByType(ctx context.Context, state State) (map[Type]uint64, error) - // CountForMetrics is like CountByStateGroupedByType for metrics. Partial data may be provided. CountForMetrics(ctx context.Context, state State) (map[Type]uint64, error) // CountByStateAndAddress returns the total count of fulfillments for the provided account and state. CountByStateAndAddress(ctx context.Context, state State, address string) (uint64, error) - // CountByStateAndAddress returns the total count of fulfillments for the provided type, state and account (as a source an destination). - CountByTypeStateAndAddress(ctx context.Context, fulfillmentType Type, state State, address string) (uint64, error) - - // CountByStateAndAddress returns the total count of fulfillments for the provided type, state and account as a source. - CountByTypeStateAndAddressAsSource(ctx context.Context, fulfillmentType Type, state State, address string) (uint64, error) - // Count returns the total count of fulfillments for the provided intent and state. CountByIntentAndState(ctx context.Context, intent string, state State) (uint64, error) // Count returns the total count of fulfillments for the provided intent. CountByIntent(ctx context.Context, intent string) (uint64, error) - // CountByTypeActionAndState returns the total count of fulfillments with a - // given type, action and state. - CountByTypeActionAndState(ctx context.Context, intentId string, actionId uint32, fulfillmentType Type, state State) (uint64, error) - // CountPendingByType gets the count of pending transactions by type. // This is particularly useful for estimating fees that will be consumed // by our subsidizer. @@ -84,11 +67,6 @@ type Store interface { // Returns ErrNotFound if no records are found. GetAllByAction(ctx context.Context, intentId string, actionId uint32) ([]*Record, error) - // GetAllByTypeAndAction returns all fulfillment records for a given type and action - // - // Returns ErrNotFound if no records are found. - GetAllByTypeAndAction(ctx context.Context, fulfillmentType Type, intentId string, actionId uint32) ([]*Record, error) - // GetFirstSchedulableByAddressAsSource returns the earliest fulfillment // that can be scheduled for an account as a source given the total ordering // of all fulfillments. @@ -103,12 +81,6 @@ type Store interface { // Returns ErrNotFound if no records are found. GetFirstSchedulableByAddressAsDestination(ctx context.Context, address string) (*Record, error) - // GetFirstSchedulableByType returns the earliest fulfillment that can be scheduled - // for fulfillments of the provided type. - // - // Returns ErrNotFound if no records are found. - GetFirstSchedulableByType(ctx context.Context, fulfillmentType Type) (*Record, error) - // GetNextSchedulableByAddress gets the next schedulable fulfillment for an account after // a point in time defined by ordering indices. // diff --git a/ocp/data/fulfillment/tests/tests.go b/ocp/data/fulfillment/tests/tests.go index a567671..15e4292 100644 --- a/ocp/data/fulfillment/tests/tests.go +++ b/ocp/data/fulfillment/tests/tests.go @@ -10,10 +10,10 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/code-payments/ocp-server/database/query" "github.com/code-payments/ocp-server/ocp/data/action" "github.com/code-payments/ocp-server/ocp/data/fulfillment" "github.com/code-payments/ocp-server/ocp/data/intent" - "github.com/code-payments/ocp-server/database/query" "github.com/code-payments/ocp-server/pointer" ) @@ -26,7 +26,6 @@ func RunTests(t *testing.T, s fulfillment.Store, teardown func()) { testGetAllByState, testGetAllByIntent, testGetAllByAction, - testGetAllByTypeAndAction, testGetCount, testSchedulingQueries, testSubsidizerQueries, @@ -659,48 +658,6 @@ func testGetAllByAction(t *testing.T, s fulfillment.Store) { }) } -func testGetAllByTypeAndAction(t *testing.T, s fulfillment.Store) { - t.Run("testGetAllByTypeAndAction", func(t *testing.T) { - ctx := context.Background() - - expected := []*fulfillment.Record{ - {Signature: pointer.String("t1"), Intent: "i1", ActionId: 0, FulfillmentType: fulfillment.NoPrivacyTransferWithAuthority}, - {Signature: pointer.String("t2"), Intent: "i1", ActionId: 0, FulfillmentType: fulfillment.NoPrivacyWithdraw}, - {Signature: pointer.String("t3"), Intent: "i1", ActionId: 1, FulfillmentType: fulfillment.NoPrivacyTransferWithAuthority}, - {Signature: pointer.String("t4"), Intent: "i1", ActionId: 2, FulfillmentType: fulfillment.NoPrivacyTransferWithAuthority}, - {Signature: pointer.String("t5"), Intent: "i1", ActionId: 2, FulfillmentType: fulfillment.NoPrivacyTransferWithAuthority}, - } - - // Fill in required fields that have no relevancy to this test - for i, record := range expected { - record.IntentType = intent.SendPublicPayment - record.ActionType = action.NoPrivacyTransfer - record.Data = []byte(fmt.Sprintf("d%d", i+1)) - record.Nonce = pointer.String(fmt.Sprintf("n%d", i+1)) - record.Blockhash = pointer.String(fmt.Sprintf("bh%d", i+1)) - record.Source = "test_source" - record.Destination = pointer.String("test_destination") - } - - err := s.PutAll(ctx, expected...) - require.NoError(t, err) - - actual, err := s.GetAllByTypeAndAction(ctx, fulfillment.NoPrivacyTransferWithAuthority, "i1", 0) - require.NoError(t, err) - require.Len(t, actual, 1) - assert.Equal(t, "t1", *actual[0].Signature) - - actual, err = s.GetAllByTypeAndAction(ctx, fulfillment.NoPrivacyTransferWithAuthority, "i1", 2) - require.NoError(t, err) - require.Len(t, actual, 2) - assert.Equal(t, "t4", *actual[0].Signature) - assert.Equal(t, "t5", *actual[1].Signature) - - _, err = s.GetAllByTypeAndAction(ctx, fulfillment.NoPrivacyWithdraw, "i1", 2) - assert.Equal(t, fulfillment.ErrFulfillmentNotFound, err) - }) -} - func testGetCount(t *testing.T, s fulfillment.Store) { t.Run("testGetCount", func(t *testing.T) { ctx := context.Background() @@ -725,13 +682,8 @@ func testGetCount(t *testing.T, s fulfillment.Store) { record.Blockhash = pointer.String(fmt.Sprintf("bh%d", i+1)) } - for index, item := range expected { - count, err := s.Count(ctx) - require.NoError(t, err) - assert.EqualValues(t, index, count) - - err = s.PutAll(ctx, item) - require.NoError(t, err) + for _, item := range expected { + require.NoError(t, s.PutAll(ctx, item)) } count, err := s.CountByState(ctx, fulfillment.StateConfirmed) @@ -762,34 +714,6 @@ func testGetCount(t *testing.T, s fulfillment.Store) { require.NoError(t, err) assert.EqualValues(t, 0, count) - count, err = s.CountByTypeStateAndAddress(ctx, fulfillment.NoPrivacyTransferWithAuthority, fulfillment.StateUnknown, "s2") - require.NoError(t, err) - assert.EqualValues(t, 1, count) - - count, err = s.CountByTypeStateAndAddress(ctx, fulfillment.NoPrivacyTransferWithAuthority, fulfillment.StateUnknown, "destination") - require.NoError(t, err) - assert.EqualValues(t, 3, count) - - count, err = s.CountByTypeStateAndAddress(ctx, fulfillment.NoPrivacyTransferWithAuthority, fulfillment.StatePending, "s2") - require.NoError(t, err) - assert.EqualValues(t, 0, count) - - count, err = s.CountByTypeStateAndAddress(ctx, fulfillment.NoPrivacyWithdraw, fulfillment.StateUnknown, "s2") - require.NoError(t, err) - assert.EqualValues(t, 0, count) - - count, err = s.CountByTypeStateAndAddress(ctx, fulfillment.NoPrivacyTransferWithAuthority, fulfillment.StateUnknown, "unknown") - require.NoError(t, err) - assert.EqualValues(t, 0, count) - - count, err = s.CountByTypeStateAndAddressAsSource(ctx, fulfillment.NoPrivacyTransferWithAuthority, fulfillment.StateUnknown, "s2") - require.NoError(t, err) - assert.EqualValues(t, 1, count) - - count, err = s.CountByTypeStateAndAddressAsSource(ctx, fulfillment.NoPrivacyTransferWithAuthority, fulfillment.StateUnknown, "destination") - require.NoError(t, err) - assert.EqualValues(t, 0, count) - count, err = s.CountByIntentAndState(ctx, "i0", fulfillment.StateConfirmed) require.NoError(t, err) assert.EqualValues(t, 0, count) @@ -821,31 +745,6 @@ func testGetCount(t *testing.T, s fulfillment.Store) { count, err = s.CountByIntent(ctx, "i0") require.NoError(t, err) assert.EqualValues(t, 3, count) - - count, err = s.CountByTypeActionAndState(ctx, "i0", 0, fulfillment.NoPrivacyTransferWithAuthority, fulfillment.StateUnknown) - require.NoError(t, err) - assert.EqualValues(t, 2, count) - - count, err = s.CountByTypeActionAndState(ctx, "i0", 1, fulfillment.NoPrivacyTransferWithAuthority, fulfillment.StateUnknown) - require.NoError(t, err) - assert.EqualValues(t, 1, count) - - count, err = s.CountByTypeActionAndState(ctx, "i2", 0, fulfillment.NoPrivacyTransferWithAuthority, fulfillment.StateRevoked) - require.NoError(t, err) - assert.EqualValues(t, 2, count) - - count, err = s.CountByTypeActionAndState(ctx, "i2", 0, fulfillment.NoPrivacyWithdraw, fulfillment.StateRevoked) - require.NoError(t, err) - assert.EqualValues(t, 0, count) - - count, err = s.CountByTypeActionAndState(ctx, "i2", 0, fulfillment.NoPrivacyTransferWithAuthority, fulfillment.StatePending) - require.NoError(t, err) - assert.EqualValues(t, 0, count) - - countByType, err := s.CountByStateGroupedByType(ctx, fulfillment.StateUnknown) - require.NoError(t, err) - assert.Len(t, countByType, 1) - assert.EqualValues(t, 3, countByType[fulfillment.NoPrivacyTransferWithAuthority]) }) } @@ -928,14 +827,6 @@ func testSchedulingQueries(t *testing.T, s fulfillment.Store) { require.NoError(t, err) assert.Equal(t, "t000", *actual.Signature) - actual, err = s.GetFirstSchedulableByType(ctx, fulfillment.NoPrivacyTransferWithAuthority) - require.NoError(t, err) - assert.Equal(t, "t000", *actual.Signature) - - actual, err = s.GetFirstSchedulableByType(ctx, fulfillment.NoPrivacyWithdraw) - require.NoError(t, err) - assert.Equal(t, "t100", *actual.Signature) - for i, record := range records { for _, account := range []string{account1, account2} { actual, err = s.GetNextSchedulableByAddress(ctx, account, record.IntentOrderingIndex, record.ActionOrderingIndex, record.FulfillmentOrderingIndex) diff --git a/ocp/data/internal.go b/ocp/data/internal.go index a93085a..6bc6a62 100644 --- a/ocp/data/internal.go +++ b/ocp/data/internal.go @@ -150,24 +150,17 @@ type DatabaseData interface { GetFulfillmentById(ctx context.Context, id uint64) (*fulfillment.Record, error) GetFulfillmentBySignature(ctx context.Context, signature string) (*fulfillment.Record, error) GetFulfillmentByVirtualSignature(ctx context.Context, signature string) (*fulfillment.Record, error) - GetFulfillmentCount(ctx context.Context) (uint64, error) GetFulfillmentCountByState(ctx context.Context, state fulfillment.State) (uint64, error) - GetFulfillmentCountByStateGroupedByType(ctx context.Context, state fulfillment.State) (map[fulfillment.Type]uint64, error) GetFulfillmentCountForMetrics(ctx context.Context, state fulfillment.State) (map[fulfillment.Type]uint64, error) GetFulfillmentCountByStateAndAddress(ctx context.Context, state fulfillment.State, address string) (uint64, error) - GetFulfillmentCountByTypeStateAndAddress(ctx context.Context, fulfillmentType fulfillment.Type, state fulfillment.State, address string) (uint64, error) - GetFulfillmentCountByTypeStateAndAddressAsSource(ctx context.Context, fulfillmentType fulfillment.Type, state fulfillment.State, address string) (uint64, error) GetFulfillmentCountByIntentAndState(ctx context.Context, intent string, state fulfillment.State) (uint64, error) GetFulfillmentCountByIntent(ctx context.Context, intent string) (uint64, error) - GetFulfillmentCountByTypeActionAndState(ctx context.Context, intentId string, actionId uint32, fulfillmentType fulfillment.Type, state fulfillment.State) (uint64, error) GetPendingFulfillmentCountByType(ctx context.Context) (map[fulfillment.Type]uint64, error) GetAllFulfillmentsByState(ctx context.Context, state fulfillment.State, includeDisabledActiveScheduling bool, opts ...query.Option) ([]*fulfillment.Record, error) GetAllFulfillmentsByIntent(ctx context.Context, intent string, opts ...query.Option) ([]*fulfillment.Record, error) GetAllFulfillmentsByAction(ctx context.Context, intentId string, actionId uint32) ([]*fulfillment.Record, error) - GetAllFulfillmentsByTypeAndAction(ctx context.Context, fulfillmentType fulfillment.Type, intentId string, actionId uint32) ([]*fulfillment.Record, error) GetFirstSchedulableFulfillmentByAddressAsSource(ctx context.Context, address string) (*fulfillment.Record, error) GetFirstSchedulableFulfillmentByAddressAsDestination(ctx context.Context, address string) (*fulfillment.Record, error) - GetFirstSchedulableFulfillmentByType(ctx context.Context, fulfillmentType fulfillment.Type) (*fulfillment.Record, error) GetNextSchedulableFulfillmentByAddress(ctx context.Context, address string, intentOrderingIndex uint64, actionOrderingIndex, fulfillmentOrderingIndex uint32) (*fulfillment.Record, error) PutAllFulfillments(ctx context.Context, records ...*fulfillment.Record) error UpdateFulfillment(ctx context.Context, record *fulfillment.Record) error @@ -176,7 +169,6 @@ type DatabaseData interface { // -------------------------------------------------------------------------------- SaveIntent(ctx context.Context, record *intent.Record) error GetIntent(ctx context.Context, intentID string) (*intent.Record, error) - GetIntentBySignature(ctx context.Context, signature string) (*intent.Record, error) GetAllIntentsByOwner(ctx context.Context, owner string, opts ...query.Option) ([]*intent.Record, error) GetOriginalGiftCardIssuedIntent(ctx context.Context, giftCardVault string) (*intent.Record, error) GetGiftCardClaimedIntent(ctx context.Context, giftCardVault string) (*intent.Record, error) @@ -553,36 +545,21 @@ func (dp *DatabaseProvider) GetFulfillmentBySignature(ctx context.Context, signa func (dp *DatabaseProvider) GetFulfillmentByVirtualSignature(ctx context.Context, signature string) (*fulfillment.Record, error) { return dp.fulfillments.GetByVirtualSignature(ctx, signature) } -func (dp *DatabaseProvider) GetFulfillmentCount(ctx context.Context) (uint64, error) { - return dp.fulfillments.Count(ctx) -} func (dp *DatabaseProvider) GetFulfillmentCountByState(ctx context.Context, state fulfillment.State) (uint64, error) { return dp.fulfillments.CountByState(ctx, state) } -func (dp *DatabaseProvider) GetFulfillmentCountByStateGroupedByType(ctx context.Context, state fulfillment.State) (map[fulfillment.Type]uint64, error) { - return dp.fulfillments.CountByStateGroupedByType(ctx, state) -} func (dp *DatabaseProvider) GetFulfillmentCountForMetrics(ctx context.Context, state fulfillment.State) (map[fulfillment.Type]uint64, error) { return dp.fulfillments.CountForMetrics(ctx, state) } func (dp *DatabaseProvider) GetFulfillmentCountByStateAndAddress(ctx context.Context, state fulfillment.State, address string) (uint64, error) { return dp.fulfillments.CountByStateAndAddress(ctx, state, address) } -func (dp *DatabaseProvider) GetFulfillmentCountByTypeStateAndAddress(ctx context.Context, fulfillmentType fulfillment.Type, state fulfillment.State, address string) (uint64, error) { - return dp.fulfillments.CountByTypeStateAndAddress(ctx, fulfillmentType, state, address) -} -func (dp *DatabaseProvider) GetFulfillmentCountByTypeStateAndAddressAsSource(ctx context.Context, fulfillmentType fulfillment.Type, state fulfillment.State, address string) (uint64, error) { - return dp.fulfillments.CountByTypeStateAndAddressAsSource(ctx, fulfillmentType, state, address) -} func (dp *DatabaseProvider) GetFulfillmentCountByIntentAndState(ctx context.Context, intent string, state fulfillment.State) (uint64, error) { return dp.fulfillments.CountByIntentAndState(ctx, intent, state) } func (dp *DatabaseProvider) GetFulfillmentCountByIntent(ctx context.Context, intent string) (uint64, error) { return dp.fulfillments.CountByIntent(ctx, intent) } -func (dp *DatabaseProvider) GetFulfillmentCountByTypeActionAndState(ctx context.Context, intentId string, actionId uint32, fulfillmentType fulfillment.Type, state fulfillment.State) (uint64, error) { - return dp.fulfillments.CountByTypeActionAndState(ctx, intentId, actionId, fulfillmentType, state) -} func (dp *DatabaseProvider) GetPendingFulfillmentCountByType(ctx context.Context) (map[fulfillment.Type]uint64, error) { return dp.fulfillments.CountPendingByType(ctx) } @@ -605,18 +582,12 @@ func (dp *DatabaseProvider) GetAllFulfillmentsByIntent(ctx context.Context, inte func (dp *DatabaseProvider) GetAllFulfillmentsByAction(ctx context.Context, intentId string, actionId uint32) ([]*fulfillment.Record, error) { return dp.fulfillments.GetAllByAction(ctx, intentId, actionId) } -func (dp *DatabaseProvider) GetAllFulfillmentsByTypeAndAction(ctx context.Context, fulfillmentType fulfillment.Type, intentId string, actionId uint32) ([]*fulfillment.Record, error) { - return dp.fulfillments.GetAllByTypeAndAction(ctx, fulfillmentType, intentId, actionId) -} func (dp *DatabaseProvider) GetFirstSchedulableFulfillmentByAddressAsSource(ctx context.Context, address string) (*fulfillment.Record, error) { return dp.fulfillments.GetFirstSchedulableByAddressAsSource(ctx, address) } func (dp *DatabaseProvider) GetFirstSchedulableFulfillmentByAddressAsDestination(ctx context.Context, address string) (*fulfillment.Record, error) { return dp.fulfillments.GetFirstSchedulableByAddressAsDestination(ctx, address) } -func (dp *DatabaseProvider) GetFirstSchedulableFulfillmentByType(ctx context.Context, fulfillmentType fulfillment.Type) (*fulfillment.Record, error) { - return dp.fulfillments.GetFirstSchedulableByType(ctx, fulfillmentType) -} func (dp *DatabaseProvider) GetNextSchedulableFulfillmentByAddress(ctx context.Context, address string, intentOrderingIndex uint64, actionOrderingIndex, fulfillmentOrderingIndex uint32) (*fulfillment.Record, error) { return dp.fulfillments.GetNextSchedulableByAddress(ctx, address, intentOrderingIndex, actionOrderingIndex, fulfillmentOrderingIndex) } @@ -632,16 +603,6 @@ func (dp *DatabaseProvider) UpdateFulfillment(ctx context.Context, record *fulfi func (dp *DatabaseProvider) GetIntent(ctx context.Context, intentID string) (*intent.Record, error) { return dp.intents.Get(ctx, intentID) } -func (dp *DatabaseProvider) GetIntentBySignature(ctx context.Context, signature string) (*intent.Record, error) { - fulfillmentRecord, err := dp.fulfillments.GetBySignature(ctx, signature) - if err == fulfillment.ErrFulfillmentNotFound { - return nil, intent.ErrIntentNotFound - } else if err != nil { - return nil, err - } - - return dp.intents.Get(ctx, fulfillmentRecord.Intent) -} func (dp *DatabaseProvider) GetAllIntentsByOwner(ctx context.Context, owner string, opts ...query.Option) ([]*intent.Record, error) { req, err := query.DefaultPaginationHandler(opts...) if err != nil { diff --git a/ocp/data/vm/storage/account.go b/ocp/data/vm/storage/account.go index 5ccbe4f..54d5431 100644 --- a/ocp/data/vm/storage/account.go +++ b/ocp/data/vm/storage/account.go @@ -18,8 +18,8 @@ type Record struct { Vm string - Name string - Address string + Address string + Levels uint8 AvailableCapacity uint64 Purpose Purpose @@ -36,10 +36,6 @@ func (r *Record) Validate() error { return errors.New("address is required") } - if len(r.Name) == 0 { - return errors.New("name is required") - } - if r.Levels == 0 { return errors.New("levels is required") } @@ -61,7 +57,6 @@ func (r *Record) Clone() Record { Vm: r.Vm, - Name: r.Name, Address: r.Address, Levels: r.Levels, AvailableCapacity: r.AvailableCapacity, @@ -76,7 +71,6 @@ func (r *Record) CopyTo(dst *Record) { dst.Vm = r.Vm - dst.Name = r.Name dst.Address = r.Address dst.Levels = r.Levels dst.AvailableCapacity = r.AvailableCapacity diff --git a/ocp/data/vm/storage/postgres/model.go b/ocp/data/vm/storage/postgres/model.go index 31c1e76..e4d7ecc 100644 --- a/ocp/data/vm/storage/postgres/model.go +++ b/ocp/data/vm/storage/postgres/model.go @@ -21,8 +21,8 @@ type accountModel struct { Vm string `db:"vm"` - Name string `db:"name"` - Address string `db:"address"` + Address string `db:"address"` + Levels uint8 `db:"levels"` AvailableCapacity uint64 `db:"available_capacity"` Purpose uint8 `db:"purpose"` @@ -49,8 +49,8 @@ func toAccountModel(obj *storage.Record) (*accountModel, error) { return &accountModel{ Vm: obj.Vm, - Name: obj.Name, - Address: obj.Address, + Address: obj.Address, + Levels: obj.Levels, AvailableCapacity: obj.AvailableCapacity, Purpose: uint8(obj.Purpose), @@ -65,8 +65,8 @@ func fromAccountModel(obj *accountModel) *storage.Record { Vm: obj.Vm, - Name: obj.Name, - Address: obj.Address, + Address: obj.Address, + Levels: obj.Levels, AvailableCapacity: obj.AvailableCapacity, Purpose: storage.Purpose(obj.Purpose), @@ -78,9 +78,9 @@ func fromAccountModel(obj *accountModel) *storage.Record { func (m *accountModel) dbInitialize(ctx context.Context, db *sqlx.DB) error { return pgutil.ExecuteInTx(ctx, db, sql.LevelDefault, func(tx *sqlx.Tx) error { query := `INSERT INTO ` + accountTableName + ` - (vm, name, address, levels, available_capacity, purpose, created_at) - VALUES ($1, $2, $3, $4, $5, $6, $7) - RETURNING id, vm, name, address, levels, available_capacity, purpose, created_at` + (vm, address, levels, available_capacity, purpose, created_at) + VALUES ($1, $2, $3, $4, $5, $6) + RETURNING id, vm, address, levels, available_capacity, purpose, created_at` if m.CreatedAt.IsZero() { m.CreatedAt = time.Now() @@ -90,7 +90,6 @@ func (m *accountModel) dbInitialize(ctx context.Context, db *sqlx.DB) error { ctx, query, m.Vm, - m.Name, m.Address, m.Levels, m.AvailableCapacity, @@ -106,7 +105,7 @@ func dbFindAnyWithAvailableCapacity(ctx context.Context, db *sqlx.DB, vm string, res := &accountModel{} query := `SELECT - id, vm, name, address, levels, available_capacity, purpose, created_at + id, vm, address, levels, available_capacity, purpose, created_at FROM ` + accountTableName + ` WHERE vm = $1 AND purpose = $2 AND available_capacity >= $3 LIMIT 1` @@ -149,7 +148,7 @@ func dbReserveStorage(ctx context.Context, db *sqlx.DB, vm string, purpose stora query2 := `UPDATE ` + accountTableName + ` SET available_capacity = available_capacity - 1 WHERE vm = $1 AND purpose = $2 and available_capacity > 0 - RETURNING id, vm, name, address, levels, available_capacity, purpose, created_at` + RETURNING id, vm, address, levels, available_capacity, purpose, created_at` err = tx.QueryRowxContext( ctx, diff --git a/ocp/data/vm/storage/postgres/store_test.go b/ocp/data/vm/storage/postgres/store_test.go index b828de1..d57027b 100644 --- a/ocp/data/vm/storage/postgres/store_test.go +++ b/ocp/data/vm/storage/postgres/store_test.go @@ -29,8 +29,8 @@ const ( vm TEXT NOT NULL, - name TEXT NOT NULL, address TEXT NOT NULL, + levels INTEGER NOT NULL, available_capacity BIGINT NOT NULL CHECK(available_capacity >= 0), purpose INTEGER NOT NULL, diff --git a/ocp/data/vm/storage/tests/tests.go b/ocp/data/vm/storage/tests/tests.go index 01e8c9e..cb51cb0 100644 --- a/ocp/data/vm/storage/tests/tests.go +++ b/ocp/data/vm/storage/tests/tests.go @@ -27,7 +27,6 @@ func testHappyPath(t *testing.T, s storage.Store) { record := &storage.Record{ Vm: "vm1", - Name: "name1", Address: "storageaccount1", Levels: 4, AvailableCapacity: storage.GetMaxCapacity(4) - 1, @@ -83,7 +82,6 @@ func testHappyPath(t *testing.T, s storage.Store) { func assertEquivalentRecords(t *testing.T, obj1, obj2 *storage.Record) { assert.Equal(t, obj1.Vm, obj2.Vm) - assert.Equal(t, obj1.Name, obj2.Name) assert.Equal(t, obj1.Address, obj2.Address) assert.Equal(t, obj1.Levels, obj2.Levels) assert.Equal(t, obj1.AvailableCapacity, obj2.AvailableCapacity)