Skip to content

Commit

Permalink
Merge pull request #13 from rarimo/feat/nullifier-migrate
Browse files Browse the repository at this point in the history
Feat/nullifier migrate
  • Loading branch information
Zaptoss authored May 23, 2024
2 parents 3da2db4 + 809ea3d commit 71b64f9
Show file tree
Hide file tree
Showing 57 changed files with 318 additions and 303 deletions.
7 changes: 0 additions & 7 deletions docs/spec/components/parameters/filterDID.yaml

This file was deleted.

8 changes: 8 additions & 0 deletions docs/spec/components/parameters/filterNullifier.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
in: query
name: 'filter[nullifier]'
required: true
description: Filter by single nullifier. Auth is required for this filter.
schema:
type: string
example: "0x123...abc"
pattern: '^0x[0-9a-fA-F]{64}$'
6 changes: 0 additions & 6 deletions docs/spec/components/parameters/pathDID.yaml

This file was deleted.

7 changes: 7 additions & 0 deletions docs/spec/components/parameters/pathNullifier.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
in: path
name: 'nullifier'
required: true
schema:
type: string
example: "0x123...abc"
pattern: '^0x[0-9a-fA-F]{64}$'
5 changes: 3 additions & 2 deletions docs/spec/components/schemas/BalanceKey.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ required:
properties:
id:
type: string
description: DID of the points owner
example: "did:iden3:readonly:tUDjWxnVJNi7t3FudukqrUcNwF5KVGoWgim5pp2jV"
description: Nullifier of the points owner
example: "0x123...abc"
pattern: '^0x[0-9a-fA-F]{64}$'
type:
type: string
enum: [ balance ]
5 changes: 3 additions & 2 deletions docs/spec/components/schemas/CreateBalanceKey.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ required:
properties:
id:
type: string
description: DID of the points owner
example: "did:iden3:readonly:tUDjWxnVJNi7t3FudukqrUcNwF5KVGoWgim5pp2jV"
description: Nullifier of the points owner
example: "0x123...abc"
pattern: '^0x[0-9a-fA-F]{64}$'
type:
type: string
enum: [ create_balance, update_balance ]
5 changes: 3 additions & 2 deletions docs/spec/components/schemas/WithdrawKey.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ required:
properties:
id:
type: string
description: DID of the points owner
example: "did:iden3:readonly:tUDjWxnVJNi7t3FudukqrUcNwF5KVGoWgim5pp2jV"
description: Nullifier of the points owner
example: "0x123...abc"
pattern: '^0x[0-9a-fA-F]{64}$'
type:
type: string
enum: [ withdraw ]
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ post:
the new account synchronously (to display them right after the request).
If balance already exists, but it is disabled (it was not referred by another user,
but has fulfilled some event), you should use PATCH balances/{did} endpoint as well.
but has fulfilled some event), you should use PATCH balances/{nullifier} endpoint as well.
operationId: createPointsBalance
requestBody:
content:
Expand Down Expand Up @@ -39,7 +39,7 @@ post:
404:
$ref: '#/components/responses/notFound'
409:
description: Balance already exists for provided DID
description: Balance already exists for provided nullifier
content:
application/vnd.api+json:
schema:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ get:
You should create new balance for the new user by making POST request.
operationId: getPointsBalance
parameters:
- $ref: '#/components/parameters/pathDID'
- $ref: '#/components/parameters/pathNullifier'
- in: query
name: 'rank'
description: 'Specifies whether to return the rank'
Expand Down Expand Up @@ -47,10 +47,10 @@ patch:
summary: Activate points balance
description: |
Activate inactive balance. Balance is inactive when referred_by field is null.
did in query must match with id in request body
nullifier in query must match with id in request body
operationId: activatePointsBalance
parameters:
- $ref: '#/components/parameters/pathDID'
- $ref: '#/components/parameters/pathNullifier'
requestBody:
content:
application/vnd.api+json:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ get:
description: Points withdrawal history of the user
operationId: getWithdrawalHistory
parameters:
- $ref: '#/components/parameters/pathDID'
- $ref: '#/components/parameters/pathNullifier'
- $ref: '#/components/parameters/pageCursor'
- $ref: '#/components/parameters/pageLimit'
- $ref: '#/components/parameters/pageOrder'
Expand Down Expand Up @@ -39,7 +39,7 @@ post:
Updated balance with new rank is returned.
operationId: withdrawPoints
parameters:
- $ref: '#/components/parameters/pathDID'
- $ref: '#/components/parameters/pathNullifier'
requestBody:
required: true
content:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ get:
description: Returns events for a single user.
operationId: getEvents
parameters:
- $ref: '#/components/parameters/filterDID'
- $ref: '#/components/parameters/filterNullifier'
- in: query
name: 'filter[status]'
description: |
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/go-ozzo/ozzo-validation/v4 v4.3.0
github.com/google/jsonapi v1.0.0
github.com/iden3/go-iden3-core/v2 v2.0.4
github.com/rarimo/auth-svc v1.0.0-rc2.0.20240311143312-de1e2258f175
github.com/rarimo/decentralized-auth-svc v0.0.0-20240522134350-2694eafa9509
github.com/rarimo/saver-grpc-lib v1.0.0
github.com/rubenv/sql-migrate v1.6.1
gitlab.com/distributed_lab/ape v1.7.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1217,12 +1217,12 @@ github.com/prometheus/procfs v0.9.0 h1:wzCHvIvM5SxWqYvwgVL7yJY8Lz3PKn49KQtpgMYJf
github.com/prometheus/procfs v0.9.0/go.mod h1:+pB4zwohETzFnmlpe6yd2lSc+0/46IYZRB/chUwxUZY=
github.com/rakyll/statik v0.1.7 h1:OF3QCZUuyPxuGEP7B4ypUa7sB/iHtqOTDYZXGM8KOdQ=
github.com/rakyll/statik v0.1.7/go.mod h1:AlZONWzMtEnMs7W4e/1LURLiI49pIMmp6V9Unghqrcc=
github.com/rarimo/auth-svc v1.0.0-rc2.0.20240311143312-de1e2258f175 h1:LWfw+633hf9J0unWMA528CEf+taYdzhhoqJVFj3po6Y=
github.com/rarimo/auth-svc v1.0.0-rc2.0.20240311143312-de1e2258f175/go.mod h1:XtPIuJABJ3QNmhcpmJRlQrxuagPfpIWwFbY0j5SakFg=
github.com/rarimo/broadcaster-svc v1.0.2 h1:ExQcjjWCRP5+POLDlZHrTD1ffUsBH+Dgv5FAgcP3BXc=
github.com/rarimo/broadcaster-svc v1.0.2/go.mod h1:lYIHy+X4IqQt4eBdtMN/V352H3EV0/gO8G+32SFwUWI=
github.com/rarimo/cosmos-sdk v0.46.7 h1:jU2PiWzc+19SF02cXM0O0puKPeH1C6Q6t2lzJ9s1ejc=
github.com/rarimo/cosmos-sdk v0.46.7/go.mod h1:fqKqz39U5IlEFb4nbQ72951myztsDzFKKDtffYJ63nk=
github.com/rarimo/decentralized-auth-svc v0.0.0-20240522134350-2694eafa9509 h1:U3gu/Z61tVIVEVoWL1YdwiNzkaXlkgd7cSeEslVfsLI=
github.com/rarimo/decentralized-auth-svc v0.0.0-20240522134350-2694eafa9509/go.mod h1:V9XSqZSBN/YmLdI6PW6GL2xNeJ94IXAnhcuvyQfVBL8=
github.com/rarimo/saver-grpc-lib v1.0.0 h1:MGUVjYg7unmodYczVsLqlqZNkT4CIgKqdo6aQtL1qdE=
github.com/rarimo/saver-grpc-lib v1.0.0/go.mod h1:DpugWK5B7Hi0bdC3MPe/9FD2zCxaRwsyykdwxtF1Zgg=
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:N/ElC8H3+5XpJzTSTfLsJV/mx9Q9g7kxmchpfZyxgzM=
Expand Down
16 changes: 8 additions & 8 deletions internal/assets/migrations/001_initial.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ AS $$ BEGIN NEW.updated_at = EXTRACT('EPOCH' FROM NOW()); RETURN NEW; END; $$;

CREATE TABLE IF NOT EXISTS balances
(
did text PRIMARY KEY,
nullifier TEXT PRIMARY KEY,
amount bigint NOT NULL default 0,
created_at integer NOT NULL default EXTRACT('EPOCH' FROM NOW()),
updated_at integer NOT NULL default EXTRACT('EPOCH' FROM NOW()),
Expand All @@ -26,30 +26,30 @@ EXECUTE FUNCTION trigger_set_updated_at();
CREATE TABLE IF NOT EXISTS referrals
(
id text PRIMARY KEY,
user_did text NOT NULL REFERENCES balances (did),
nullifier TEXT NOT NULL REFERENCES balances (nullifier),
is_consumed boolean NOT NULL default false
);

ALTER TABLE balances ADD CONSTRAINT referred_by_fk FOREIGN KEY (referred_by) REFERENCES referrals (id);
CREATE INDEX IF NOT EXISTS referrals_user_did_index ON referrals (user_did);
CREATE INDEX IF NOT EXISTS referrals_nullifier_index ON referrals (nullifier);

CREATE TYPE event_status AS ENUM ('open', 'fulfilled', 'claimed');

CREATE TABLE IF NOT EXISTS events
(
id uuid PRIMARY KEY NOT NULL default gen_random_uuid(),
user_did text NOT NULL REFERENCES balances (did),
nullifier TEXT NOT NULL REFERENCES balances (nullifier),
type text NOT NULL,
status event_status NOT NULL,
created_at integer NOT NULL default EXTRACT('EPOCH' FROM NOW()),
updated_at integer NOT NULL default EXTRACT('EPOCH' FROM NOW()),
meta jsonb,
points_amount integer,
external_id text,
CONSTRAINT unique_external_id UNIQUE (user_did, type, external_id)
CONSTRAINT unique_external_id UNIQUE (nullifier, type, external_id)
);

CREATE INDEX IF NOT EXISTS events_page_index ON events (user_did, updated_at);
CREATE INDEX IF NOT EXISTS events_page_index ON events (nullifier, updated_at);

CREATE TRIGGER set_updated_at
BEFORE UPDATE
Expand All @@ -60,13 +60,13 @@ EXECUTE FUNCTION trigger_set_updated_at();
CREATE TABLE IF NOT EXISTS withdrawals
(
id uuid PRIMARY KEY default gen_random_uuid(),
user_did text NOT NULL REFERENCES balances (did),
nullifier TEXT NOT NULL REFERENCES balances (nullifier),
amount integer NOT NULL,
address text NOT NULL,
created_at integer NOT NULL default EXTRACT('EPOCH' FROM NOW())
);

CREATE INDEX IF NOT EXISTS withdrawals_page_index ON withdrawals (user_did, created_at);
CREATE INDEX IF NOT EXISTS withdrawals_page_index ON withdrawals (nullifier, created_at);

-- +migrate Down
DROP TABLE IF EXISTS withdrawals;
Expand Down
2 changes: 0 additions & 2 deletions internal/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/rarimo/rarime-points-svc/internal/service"
"github.com/rarimo/rarime-points-svc/internal/service/workers/expirywatch"
"github.com/rarimo/rarime-points-svc/internal/service/workers/reopener"
"github.com/rarimo/rarime-points-svc/internal/service/workers/sbtcheck"
"gitlab.com/distributed_lab/kit/kv"
"gitlab.com/distributed_lab/logan/v3"
)
Expand Down Expand Up @@ -55,7 +54,6 @@ func Run(args []string) bool {

switch cmd {
case serviceCmd.FullCommand():
run(func(context.Context, config.Config) { sbtcheck.Run(ctx, cfg) })
run(service.Run)
run(reopener.Run)
run(expirywatch.Run)
Expand Down
4 changes: 2 additions & 2 deletions internal/config/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package config

import (
"github.com/rarimo/auth-svc/pkg/auth"
"github.com/rarimo/decentralized-auth-svc/pkg/auth"
"github.com/rarimo/rarime-points-svc/internal/data/evtypes"
"github.com/rarimo/rarime-points-svc/internal/service/workers/sbtcheck"
"github.com/rarimo/saver-grpc-lib/broadcaster"
Expand All @@ -19,7 +19,7 @@ type Config interface {
evtypes.EventTypeser
sbtcheck.SbtChecker

PointPrice() int64
PointPrice() PointsPrice
}

type config struct {
Expand Down
15 changes: 9 additions & 6 deletions internal/config/point_price.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import (
"gitlab.com/distributed_lab/kit/kv"
)

func (c *config) PointPrice() int64 {
type PointsPrice struct {
PointPriceURMO int64 `fig:"point_price_urmo,required"`
Disabled bool `fig:"disabled"`
}

func (c *config) PointPrice() PointsPrice {
return c.pointPrice.Do(func() interface{} {
var cfg struct {
PointPriceURMO int64 `fig:"point_price_urmo,required"`
}
var cfg PointsPrice

err := figure.Out(&cfg).
From(kv.MustGetStringMap(c.getter, "withdrawal")).
Expand All @@ -20,6 +23,6 @@ func (c *config) PointPrice() int64 {
panic(fmt.Errorf("failed to figure out withdrawal point price: %w", err))
}

return cfg.PointPriceURMO
}).(int64)
return cfg
}).(PointsPrice)
}
8 changes: 4 additions & 4 deletions internal/data/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

type Balance struct {
DID string `db:"did"`
Nullifier string `db:"nullifier"`
Amount int64 `db:"amount"`
CreatedAt int32 `db:"created_at"`
UpdatedAt int32 `db:"updated_at"`
Expand All @@ -29,10 +29,10 @@ type BalancesQ interface {
Page(*pgdb.OffsetPageParams) BalancesQ
Select() ([]Balance, error)
Get() (*Balance, error)
// GetWithRank returns balance with rank, filtered by DID. No other filters can be applied.
GetWithRank(did string) (*Balance, error)
// GetWithRank returns balance with rank, filtered by nullifier. No other filters can be applied.
GetWithRank(nullifier string) (*Balance, error)

FilterByDID(string) BalancesQ
FilterByNullifier(string) BalancesQ
FilterByPassportHash(string) BalancesQ
FilterDisabled() BalancesQ
}
8 changes: 4 additions & 4 deletions internal/data/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (s EventStatus) String() string {

type Event struct {
ID string `db:"id"`
UserDID string `db:"user_did"`
Nullifier string `db:"nullifier"`
Type string `db:"type"`
Status EventStatus `db:"status"`
CreatedAt int32 `db:"created_at"`
Expand All @@ -33,8 +33,8 @@ type Event struct {

// ReopenableEvent is a pair that is sufficient to build a new open event with a specific type for a user
type ReopenableEvent struct {
UserDID string `db:"user_did"`
Type string `db:"type"`
Nullifier string `db:"nullifier"`
Type string `db:"type"`
}

type EventsQ interface {
Expand All @@ -58,7 +58,7 @@ type EventsQ interface {
SelectAbsentTypes(allTypes ...string) ([]ReopenableEvent, error)

FilterByID(string) EventsQ
FilterByUserDID(string) EventsQ
FilterByNullifier(string) EventsQ
FilterByStatus(...EventStatus) EventsQ
FilterByType(...string) EventsQ
FilterByUpdatedAtBefore(int64) EventsQ
Expand Down
8 changes: 4 additions & 4 deletions internal/data/evtypes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (t Types) Names(filters ...filter) []string {
return res
}

func (t Types) PrepareEvents(userDID string, filters ...filter) []data.Event {
func (t Types) PrepareEvents(nullifier string, filters ...filter) []data.Event {
t.ensureInitialized()
const extraCap = 1 // in case we append to the resulting slice outside the function
events := make([]data.Event, 0, len(t.list)+extraCap)
Expand All @@ -128,9 +128,9 @@ func (t Types) PrepareEvents(userDID string, filters ...filter) []data.Event {
}

events = append(events, data.Event{
UserDID: userDID,
Type: et.Name,
Status: status,
Nullifier: nullifier,
Type: et.Name,
Status: status,
})
}

Expand Down
Loading

0 comments on commit 71b64f9

Please sign in to comment.