storj/satellite/payments/stripecoinpayments/transactions.go

101 lines
3.6 KiB
Go
Raw Normal View History

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package stripecoinpayments
import (
"context"
"time"
"github.com/shopspring/decimal"
"github.com/zeebo/errs"
"storj.io/common/currency"
"storj.io/common/uuid"
"storj.io/storj/satellite/payments/coinpayments"
)
// ErrTransactionConsumed is thrown when trying to consume already consumed transaction.
var ErrTransactionConsumed = errs.New("error transaction already consumed")
// TransactionsDB is an interface which defines functionality
// of DB which stores coinpayments transactions.
//
// architecture: Database
type TransactionsDB interface {
// Insert inserts new coinpayments transaction into DB.
satellite/satellitedb: prepare to remove big.Float from db Why: big.Float is not an ideal type for dealing with monetary amounts, because no matter how high the precision, some non-integer decimal values can not be represented exactly in base-2 floating point. Also, storing gob-encoded big.Float values in the database makes it very hard to use those values in meaningful queries, making it difficult to do any sort of analysis on billing. Now that we have amounts represented using monetary.Amount, we can simply store them in the database using integers (as given by the .BaseUnits() method on monetary.Amount). We should move toward storing the currency along with any monetary amount, wherever we are storing amounts, because satellites might want to deal with currencies other than STORJ and USD. Even better, it becomes much clearer what currency each monetary value is _supposed_ to be in (I had to dig through code to find that out for our current monetary columns). Deployment ---------- Getting rid of the big.Float columns will take multiple deployment steps. There does not seem to be any way to make the change in a way that lets existing queries continue to work on CockroachDB (it could be done with rules and triggers and a stored procedure that knows how to gob-decode big.Float objects, but CockroachDB doesn't have rules _or_ triggers _or_ stored procedures). Instead, in this first step, we make no changes to the database schema, but add code that knows how to deal with the planned changes to the schema when they are made in a future "step 2" deployment. All functions that deal with the coinbase_transactions table have been taught to recognize the "undefined column" error, and when it is seen, to call a separate "transition shim" function to accomplish the task. Once all the services are running this code, and the step 2 deployment makes breaking changes to the schema, any services that are still running and connected to the database will keep working correctly because of the fallback code included here. The step 2 deployment can be made without these transition shims included, because it will apply the database schema changes before any of its code runs. Step 1: No schema changes; just include code that recognizes the "undefined column" error when dealing with the coinbase_transactions or stripecoinpayments_tx_conversion_rates tables, and if found, assumes that the column changes from Step 2 have already been made. Step 2: In coinbase_transactions: * change the names of the 'amount' and 'received' columns to 'amount_gob' and 'received_gob' respectively * add new 'amount_numeric' and 'received_numeric' columns with INT8 type. In stripecoinpayments_tx_conversion_rates: * change the name of the 'rate' column to 'rate_gob' * add new 'rate_numeric' column with NUMERIC(8, 8) type Code reading from either of these tables must query both the X_gob and X_numeric columns. If X_numeric is not null, its value should be used; otherwise, the gob-encoded big.Float in X_gob should be used. A chore might be included in this step that transitions values from X_gob to X_numeric a few rows at a time. Step 3: Once all prod satellites have no values left in the _gob columns, we can drop those columns and add NOT NULL constraints to the _numeric columns. Change-Id: Id6db304b404e6fde44f5a8c23cdaeeaaa2324f20
2021-08-10 23:30:23 +01:00
Insert(ctx context.Context, tx Transaction) (time.Time, error)
// Update updates status and received for set of transactions.
Update(ctx context.Context, updates []TransactionUpdate, applies coinpayments.TransactionIDList) error
// Consume marks transaction as consumed, so it won't participate in apply account balance loop.
Consume(ctx context.Context, id coinpayments.TransactionID) error
// LockRate locks conversion rate for transaction.
LockRate(ctx context.Context, id coinpayments.TransactionID, rate decimal.Decimal) error
// GetLockedRate returns locked conversion rate for transaction or error if non exists.
GetLockedRate(ctx context.Context, id coinpayments.TransactionID) (decimal.Decimal, error)
// ListAccount returns all transaction for specific user.
ListAccount(ctx context.Context, userID uuid.UUID) ([]Transaction, error)
// ListPending returns TransactionsPage with pending transactions.
ListPending(ctx context.Context, offset int64, limit int, before time.Time) (TransactionsPage, error)
// ListUnapplied returns TransactionsPage with completed transaction that should be applied to account balance.
ListUnapplied(ctx context.Context, offset int64, limit int, before time.Time) (TransactionsPage, error)
}
// Transaction defines coinpayments transaction info that is stored in the DB.
type Transaction struct {
ID coinpayments.TransactionID
AccountID uuid.UUID
Address string
Amount currency.Amount
Received currency.Amount
Status coinpayments.Status
Key string
Timeout time.Duration
CreatedAt time.Time
}
// TransactionUpdate holds transaction update info.
type TransactionUpdate struct {
TransactionID coinpayments.TransactionID
Status coinpayments.Status
Received currency.Amount
}
// TransactionsPage holds set of transaction and indicates if
// there are more transactions to fetch.
type TransactionsPage struct {
Transactions []Transaction
Next bool
NextOffset int64
}
// IDList returns transaction id list of page's transactions.
func (page *TransactionsPage) IDList() TransactionAndUserList {
ids := make(TransactionAndUserList)
for _, tx := range page.Transactions {
ids[tx.ID] = tx.AccountID
}
return ids
}
// CreationTimes returns a map of creation times of page's transactions.
func (page *TransactionsPage) CreationTimes() map[coinpayments.TransactionID]time.Time {
creationTimes := make(map[coinpayments.TransactionID]time.Time)
for _, tx := range page.Transactions {
creationTimes[tx.ID] = tx.CreatedAt
}
return creationTimes
}
// TransactionAndUserList is a composite type for storing userID and txID.
type TransactionAndUserList map[coinpayments.TransactionID]uuid.UUID
// IDList returns transaction id list.
func (idMap TransactionAndUserList) IDList() coinpayments.TransactionIDList {
var list coinpayments.TransactionIDList
for transactionID := range idMap {
list = append(list, transactionID)
}
return list
}