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
47 changes: 47 additions & 0 deletions pkg/code/data/balance/checkpoint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package balance

import (
"errors"
"time"
)

// Note: Only supports external balances
type Record struct {
Id uint64

TokenAccount string
Quarks uint64
SlotCheckpoint uint64

LastUpdatedAt time.Time
}

func (r *Record) Validate() error {
if len(r.TokenAccount) == 0 {
return errors.New("token account is required")
}

return nil
}

func (r *Record) Clone() Record {
return Record{
Id: r.Id,

TokenAccount: r.TokenAccount,
Quarks: r.Quarks,
SlotCheckpoint: r.SlotCheckpoint,

LastUpdatedAt: r.LastUpdatedAt,
}
}

func (r *Record) CopyTo(dst *Record) {
dst.Id = r.Id

dst.TokenAccount = r.TokenAccount
dst.Quarks = r.Quarks
dst.SlotCheckpoint = r.SlotCheckpoint

dst.LastUpdatedAt = r.LastUpdatedAt
}
92 changes: 92 additions & 0 deletions pkg/code/data/balance/memory/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package memory

import (
"context"
"sync"
"time"

"github.com/code-payments/code-server/pkg/code/data/balance"
)

type store struct {
mu sync.Mutex
records []*balance.Record
last uint64
}

// New returns a new in memory balance.Store
func New() balance.Store {
return &store{}
}

// SaveCheckpoint implements balance.Store.SaveCheckpoint
func (s *store) SaveCheckpoint(_ context.Context, data *balance.Record) error {
if err := data.Validate(); err != nil {
return err
}

s.mu.Lock()
defer s.mu.Unlock()

s.last++
if item := s.find(data); item != nil {
if data.SlotCheckpoint <= item.SlotCheckpoint {
return balance.ErrStaleCheckpoint
}

item.SlotCheckpoint = data.SlotCheckpoint
item.Quarks = data.Quarks
item.LastUpdatedAt = time.Now()
item.CopyTo(data)
} else {
if data.Id == 0 {
data.Id = s.last
}
data.LastUpdatedAt = time.Now()
c := data.Clone()
s.records = append(s.records, &c)
}

return nil
}

// GetCheckpoint implements balance.Store.GetCheckpoint
func (s *store) GetCheckpoint(_ context.Context, account string) (*balance.Record, error) {
s.mu.Lock()
defer s.mu.Unlock()

if item := s.findByTokenAccount(account); item != nil {
cloned := item.Clone()
return &cloned, nil
}
return nil, balance.ErrCheckpointNotFound
}

func (s *store) find(data *balance.Record) *balance.Record {
for _, item := range s.records {
if item.Id == data.Id {
return item
}
if data.TokenAccount == item.TokenAccount {
return item
}
}
return nil
}

func (s *store) findByTokenAccount(account string) *balance.Record {
for _, item := range s.records {
if account == item.TokenAccount {
return item
}
}
return nil
}

func (s *store) reset() {
s.mu.Lock()
defer s.mu.Unlock()

s.records = nil
s.last = 0
}
15 changes: 15 additions & 0 deletions pkg/code/data/balance/memory/store_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package memory

import (
"testing"

"github.com/code-payments/code-server/pkg/code/data/balance/tests"
)

func TestBalanceMemoryStore(t *testing.T) {
testStore := New()
teardown := func() {
testStore.(*store).reset()
}
tests.RunTests(t, testStore, teardown)
}
94 changes: 94 additions & 0 deletions pkg/code/data/balance/postgres/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package postgres

import (
"context"
"database/sql"
"time"

"github.com/jmoiron/sqlx"

"github.com/code-payments/code-server/pkg/code/data/balance"
pgutil "github.com/code-payments/code-server/pkg/database/postgres"
)

const (
tableName = "codewallet__core_balancecheckpoint"
)

type model struct {
Id sql.NullInt64 `db:"id"`

TokenAccount string `db:"token_account"`
Quarks uint64 `db:"quarks"`
SlotCheckpoint uint64 `db:"slot_checkpoint"`

LastUpdatedAt time.Time `db:"last_updated_at"`
}

func toModel(obj *balance.Record) (*model, error) {
if err := obj.Validate(); err != nil {
return nil, err
}

return &model{
TokenAccount: obj.TokenAccount,
Quarks: obj.Quarks,
SlotCheckpoint: obj.SlotCheckpoint,
LastUpdatedAt: obj.LastUpdatedAt,
}, nil
}

func fromModel(obj *model) *balance.Record {
return &balance.Record{
Id: uint64(obj.Id.Int64),
TokenAccount: obj.TokenAccount,
Quarks: obj.Quarks,
SlotCheckpoint: obj.SlotCheckpoint,
LastUpdatedAt: obj.LastUpdatedAt,
}
}

func (m *model) dbSave(ctx context.Context, db *sqlx.DB) error {
return pgutil.ExecuteInTx(ctx, db, sql.LevelDefault, func(tx *sqlx.Tx) error {
query := `INSERT INTO ` + tableName + `
(token_account, quarks, slot_checkpoint, last_updated_at)
VALUES ($1, $2, $3, $4)

ON CONFLICT (token_account)
DO UPDATE
SET quarks = $2, slot_checkpoint = $3, last_updated_at = $4
WHERE ` + tableName + `.token_account = $1 AND ` + tableName + `.slot_checkpoint < $3

RETURNING
id, token_account, quarks, slot_checkpoint, last_updated_at`

m.LastUpdatedAt = time.Now()

err := tx.QueryRowxContext(
ctx,
query,
m.TokenAccount,
m.Quarks,
m.SlotCheckpoint,
m.LastUpdatedAt.UTC(),
).StructScan(m)

return pgutil.CheckNoRows(err, balance.ErrStaleCheckpoint)
})
}

func dbGetCheckpoint(ctx context.Context, db *sqlx.DB, account string) (*model, error) {
res := &model{}

query := `SELECT
id, token_account, quarks, slot_checkpoint, last_updated_at
FROM ` + tableName + `
WHERE token_account = $1
LIMIT 1`

err := db.GetContext(ctx, res, query, account)
if err != nil {
return nil, pgutil.CheckNoRows(err, balance.ErrCheckpointNotFound)
}
return res, nil
}
47 changes: 47 additions & 0 deletions pkg/code/data/balance/postgres/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package postgres

import (
"context"
"database/sql"

"github.com/jmoiron/sqlx"

"github.com/code-payments/code-server/pkg/code/data/balance"
)

type store struct {
db *sqlx.DB
}

// New returns a new postgres balance.Store
func New(db *sql.DB) balance.Store {
return &store{
db: sqlx.NewDb(db, "pgx"),
}
}

// SaveCheckpoint implements balance.Store.SaveCheckpoint
func (s *store) SaveCheckpoint(ctx context.Context, record *balance.Record) error {
model, err := toModel(record)
if err != nil {
return err
}

if err := model.dbSave(ctx, s.db); err != nil {
return err
}

res := fromModel(model)
res.CopyTo(record)

return nil
}

// GetCheckpoint implements balance.Store.GetCheckpoint
func (s *store) GetCheckpoint(ctx context.Context, account string) (*balance.Record, error) {
model, err := dbGetCheckpoint(ctx, s.db, account)
if err != nil {
return nil, err
}
return fromModel(model), nil
}
Loading