Skip to content

Commit 908e2d1

Browse files
authored
Abandoned currencies are excluded from currency counts (#179)
1 parent 07f5405 commit 908e2d1

4 files changed

Lines changed: 22 additions & 3 deletions

File tree

ocp/data/currency/memory/store.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,14 @@ func (s *store) CountMints(ctx context.Context) (uint64, error) {
328328
s.mu.Lock()
329329
defer s.mu.Unlock()
330330

331-
return uint64(len(s.metadataRecords)), nil
331+
var count uint64
332+
for _, item := range s.metadataRecords {
333+
if item.State == currency.MetadataStateAbandoned {
334+
continue
335+
}
336+
count++
337+
}
338+
return count, nil
332339
}
333340

334341
func (s *store) IsNameAvailable(_ context.Context, name string) (bool, error) {

ocp/data/currency/postgres/model.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,10 @@ func dbGetAllMints(ctx context.Context, db *sqlx.DB) ([]string, error) {
532532

533533
func dbCountMints(ctx context.Context, db *sqlx.DB) (uint64, error) {
534534
var count uint64
535-
err := db.GetContext(ctx, &count, `SELECT COUNT(*) FROM `+metadataTableName)
535+
err := db.GetContext(ctx, &count,
536+
`SELECT COUNT(*) FROM `+metadataTableName+` WHERE state != $1`,
537+
currency.MetadataStateAbandoned,
538+
)
536539
if err != nil {
537540
return 0, err
538541
}

ocp/data/currency/store.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ type Store interface {
6969
// ErrNotFound is returned if no mints exist
7070
GetAllMints(ctx context.Context) ([]string, error)
7171

72-
// CountMints returns the total number of currency creator mints
72+
// CountMints returns the total number of currency creator mints,
73+
// excluding those in the abandoned state.
7374
CountMints(ctx context.Context) (uint64, error)
7475

7576
// IsNameAvailable checks whether a currency name is available for use.

ocp/data/currency/tests/tests.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,14 @@ func testCountMints(t *testing.T, s currency.Store) {
699699
count, err = s.CountMints(context.Background())
700700
require.NoError(t, err)
701701
assert.EqualValues(t, 2, count)
702+
703+
// Abandoned mints must not be counted
704+
record2.State = currency.MetadataStateAbandoned
705+
require.NoError(t, s.SaveMetadata(context.Background(), record2))
706+
707+
count, err = s.CountMints(context.Background())
708+
require.NoError(t, err)
709+
assert.EqualValues(t, 1, count)
702710
}
703711

704712
func testCountMetadataByState(t *testing.T, s currency.Store) {

0 commit comments

Comments
 (0)