2019-10-17 15:04:50 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package stripecoinpayments_test
|
|
|
|
|
|
|
|
import (
|
2019-10-23 13:04:54 +01:00
|
|
|
"encoding/base64"
|
2020-07-14 14:04:38 +01:00
|
|
|
"errors"
|
2020-02-13 18:08:45 +00:00
|
|
|
"sync"
|
2019-10-17 15:04:50 +01:00
|
|
|
"testing"
|
2019-10-23 13:04:54 +01:00
|
|
|
"time"
|
2019-10-17 15:04:50 +01:00
|
|
|
|
satellite/payments: specialized type for monetary amounts
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.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
"github.com/shopspring/decimal"
|
2019-10-17 15:04:50 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2021-06-22 01:09:56 +01:00
|
|
|
"github.com/stripe/stripe-go/v72"
|
2020-02-13 18:08:45 +00:00
|
|
|
"github.com/zeebo/errs"
|
2019-10-17 15:04:50 +01:00
|
|
|
|
2022-09-06 13:43:09 +01:00
|
|
|
"storj.io/common/currency"
|
2020-02-13 18:08:45 +00:00
|
|
|
"storj.io/common/errs2"
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/memory"
|
|
|
|
"storj.io/common/testcontext"
|
|
|
|
"storj.io/common/testrand"
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2020-07-17 16:17:21 +01:00
|
|
|
"storj.io/storj/private/testplanet"
|
2019-10-17 15:04:50 +01:00
|
|
|
"storj.io/storj/satellite"
|
|
|
|
"storj.io/storj/satellite/payments/coinpayments"
|
|
|
|
"storj.io/storj/satellite/payments/stripecoinpayments"
|
|
|
|
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
|
|
|
)
|
|
|
|
|
2019-11-05 13:16:02 +00:00
|
|
|
func TestTransactionsDB(t *testing.T) {
|
2020-01-19 16:29:15 +00:00
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
2019-11-05 13:16:02 +00:00
|
|
|
transactions := db.StripeCoinPayments().Transactions()
|
2019-10-17 15:04:50 +01:00
|
|
|
|
2022-09-06 13:43:09 +01:00
|
|
|
amount, err := currency.AmountFromString("2.0000000000000000005", currency.StorjToken)
|
satellite/payments: specialized type for monetary amounts
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.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
require.NoError(t, err)
|
2022-09-06 13:43:09 +01:00
|
|
|
received, err := currency.AmountFromString("1.0000000000000000003", currency.StorjToken)
|
satellite/payments: specialized type for monetary amounts
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.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
userID := testrand.UUID()
|
2019-10-23 13:04:54 +01:00
|
|
|
|
|
|
|
createTx := stripecoinpayments.Transaction{
|
|
|
|
ID: "testID",
|
satellite/payments: specialized type for monetary amounts
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.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
AccountID: userID,
|
2019-10-23 13:04:54 +01:00
|
|
|
Address: "testAddress",
|
satellite/payments: specialized type for monetary amounts
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.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
Amount: amount,
|
|
|
|
Received: received,
|
2020-01-14 13:38:32 +00:00
|
|
|
Status: coinpayments.StatusPending,
|
2019-10-23 13:04:54 +01:00
|
|
|
Key: "testKey",
|
2019-11-15 14:59:39 +00:00
|
|
|
Timeout: time.Second * 60,
|
2019-10-23 13:04:54 +01:00
|
|
|
}
|
|
|
|
|
2019-10-17 15:04:50 +01:00
|
|
|
t.Run("insert", func(t *testing.T) {
|
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
|
|
|
createdAt, err := transactions.Insert(ctx, createTx)
|
2019-10-23 13:04:54 +01:00
|
|
|
require.NoError(t, err)
|
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
|
|
|
requireSaneTimestamp(t, createdAt)
|
satellite/payments: specialized type for monetary amounts
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.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
txs, err := transactions.ListAccount(ctx, userID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, txs, 1)
|
|
|
|
compareTransactions(t, createTx, txs[0])
|
2019-10-23 13:04:54 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("update", func(t *testing.T) {
|
2022-09-06 13:43:09 +01:00
|
|
|
received, err := currency.AmountFromString("6.0000000000000000001", currency.StorjToken)
|
satellite/payments: specialized type for monetary amounts
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.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
require.NoError(t, err)
|
2019-10-17 15:04:50 +01:00
|
|
|
|
2019-10-23 13:04:54 +01:00
|
|
|
update := stripecoinpayments.TransactionUpdate{
|
|
|
|
TransactionID: createTx.ID,
|
2020-01-14 13:38:32 +00:00
|
|
|
Status: coinpayments.StatusReceived,
|
satellite/payments: specialized type for monetary amounts
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.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
Received: received,
|
2019-10-23 13:04:54 +01:00
|
|
|
}
|
|
|
|
|
satellite/payments: specialized type for monetary amounts
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.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
err = transactions.Update(ctx, []stripecoinpayments.TransactionUpdate{update}, nil)
|
2019-10-23 13:04:54 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
page, err := transactions.ListPending(ctx, 0, 1, time.Now())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
satellite/payments: specialized type for monetary amounts
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.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
require.Len(t, page.Transactions, 1)
|
2019-10-23 13:04:54 +01:00
|
|
|
assert.Equal(t, createTx.ID, page.Transactions[0].ID)
|
|
|
|
assert.Equal(t, update.Received, page.Transactions[0].Received)
|
|
|
|
assert.Equal(t, update.Status, page.Transactions[0].Status)
|
2019-10-29 16:04:34 +00:00
|
|
|
|
|
|
|
err = transactions.Update(ctx,
|
|
|
|
[]stripecoinpayments.TransactionUpdate{
|
|
|
|
{
|
|
|
|
TransactionID: createTx.ID,
|
2020-01-14 13:38:32 +00:00
|
|
|
Status: coinpayments.StatusCompleted,
|
satellite/payments: specialized type for monetary amounts
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.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
Received: received,
|
2019-10-29 16:04:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
coinpayments.TransactionIDList{
|
|
|
|
createTx.ID,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
page, err = transactions.ListUnapplied(ctx, 0, 1, time.Now())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, page.Transactions)
|
|
|
|
require.Equal(t, 1, len(page.Transactions))
|
|
|
|
|
|
|
|
assert.Equal(t, createTx.ID, page.Transactions[0].ID)
|
|
|
|
assert.Equal(t, update.Received, page.Transactions[0].Received)
|
2020-01-14 13:38:32 +00:00
|
|
|
assert.Equal(t, coinpayments.StatusCompleted, page.Transactions[0].Status)
|
2019-10-29 16:04:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("consume", func(t *testing.T) {
|
|
|
|
err := transactions.Consume(ctx, createTx.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
page, err := transactions.ListUnapplied(ctx, 0, 1, time.Now())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Nil(t, page.Transactions)
|
|
|
|
assert.Equal(t, 0, len(page.Transactions))
|
2019-10-23 13:04:54 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
satellite/payments: specialized type for monetary amounts
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.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
func requireSaneTimestamp(t *testing.T, when time.Time) {
|
|
|
|
// ensure time value is sane. I apologize to you people of the future when this starts breaking
|
|
|
|
require.Truef(t, when.After(time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC)),
|
|
|
|
"%s seems too small to be a valid creation timestamp", when)
|
|
|
|
require.Truef(t, when.Before(time.Date(2500, 1, 1, 0, 0, 0, 0, time.UTC)),
|
|
|
|
"%s seems too large to be a valid creation timestamp", when)
|
|
|
|
}
|
|
|
|
|
2020-02-13 18:08:45 +00:00
|
|
|
func TestConcurrentConsume(t *testing.T) {
|
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
|
|
|
transactions := db.StripeCoinPayments().Transactions()
|
|
|
|
|
|
|
|
const concurrentTries = 30
|
|
|
|
|
2022-09-06 13:43:09 +01:00
|
|
|
amount, err := currency.AmountFromString("2.0000000000000000005", currency.StorjToken)
|
satellite/payments: specialized type for monetary amounts
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.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
require.NoError(t, err)
|
2022-09-06 13:43:09 +01:00
|
|
|
received, err := currency.AmountFromString("1.0000000000000000003", currency.StorjToken)
|
satellite/payments: specialized type for monetary amounts
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.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
userID := testrand.UUID()
|
|
|
|
txID := coinpayments.TransactionID("testID")
|
2020-02-13 18:08:45 +00:00
|
|
|
|
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
|
|
|
_, err = transactions.Insert(ctx,
|
2020-02-13 18:08:45 +00:00
|
|
|
stripecoinpayments.Transaction{
|
satellite/payments: specialized type for monetary amounts
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.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
ID: txID,
|
|
|
|
AccountID: userID,
|
2020-02-13 18:08:45 +00:00
|
|
|
Address: "testAddress",
|
satellite/payments: specialized type for monetary amounts
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.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
Amount: amount,
|
|
|
|
Received: received,
|
2020-02-13 18:08:45 +00:00
|
|
|
Status: coinpayments.StatusPending,
|
|
|
|
Key: "testKey",
|
|
|
|
Timeout: time.Second * 60,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = transactions.Update(ctx,
|
|
|
|
[]stripecoinpayments.TransactionUpdate{{
|
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
|
|
|
TransactionID: txID,
|
2020-02-13 18:08:45 +00:00
|
|
|
Status: coinpayments.StatusCompleted,
|
satellite/payments: specialized type for monetary amounts
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.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
Received: received,
|
2020-02-13 18:08:45 +00:00
|
|
|
}},
|
|
|
|
coinpayments.TransactionIDList{
|
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
|
|
|
txID,
|
2020-02-13 18:08:45 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var errLock sync.Mutex
|
|
|
|
var alreadyConsumed []error
|
|
|
|
|
|
|
|
appendError := func(err error) {
|
|
|
|
defer errLock.Unlock()
|
|
|
|
errLock.Lock()
|
|
|
|
|
|
|
|
alreadyConsumed = append(alreadyConsumed, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var group errs2.Group
|
|
|
|
for i := 0; i < concurrentTries; i++ {
|
|
|
|
group.Go(func() error {
|
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
|
|
|
err := transactions.Consume(ctx, txID)
|
2020-02-13 18:08:45 +00:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-07-14 14:04:38 +01:00
|
|
|
if errors.Is(err, stripecoinpayments.ErrTransactionConsumed) {
|
2020-02-13 18:08:45 +00:00
|
|
|
appendError(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, errs.Combine(group.Wait()...))
|
|
|
|
require.Equal(t, concurrentTries-1, len(alreadyConsumed))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-11-05 13:16:02 +00:00
|
|
|
func TestTransactionsDBList(t *testing.T) {
|
2019-10-29 16:04:34 +00:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
const (
|
2020-02-11 13:11:14 +00:00
|
|
|
limit = 5
|
|
|
|
transactionCount = limit * 4
|
2019-10-29 16:04:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// create transactions
|
2022-09-06 13:43:09 +01:00
|
|
|
amount, err := currency.AmountFromString("4.0000000000000000005", currency.StorjToken)
|
satellite/payments: specialized type for monetary amounts
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.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
require.NoError(t, err)
|
2022-09-06 13:43:09 +01:00
|
|
|
received, err := currency.AmountFromString("5.0000000000000000003", currency.StorjToken)
|
satellite/payments: specialized type for monetary amounts
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.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
require.NoError(t, err)
|
2019-10-29 16:04:34 +00:00
|
|
|
|
|
|
|
var txs []stripecoinpayments.Transaction
|
|
|
|
for i := 0; i < transactionCount; i++ {
|
|
|
|
id := base64.StdEncoding.EncodeToString(testrand.Bytes(4 * memory.B))
|
|
|
|
addr := base64.StdEncoding.EncodeToString(testrand.Bytes(4 * memory.B))
|
|
|
|
key := base64.StdEncoding.EncodeToString(testrand.Bytes(4 * memory.B))
|
|
|
|
|
2020-01-14 13:38:32 +00:00
|
|
|
status := coinpayments.StatusPending
|
|
|
|
if i%2 == 0 {
|
|
|
|
status = coinpayments.StatusReceived
|
|
|
|
}
|
|
|
|
|
2019-10-29 16:04:34 +00:00
|
|
|
createTX := stripecoinpayments.Transaction{
|
|
|
|
ID: coinpayments.TransactionID(id),
|
|
|
|
AccountID: uuid.UUID{},
|
|
|
|
Address: addr,
|
satellite/payments: specialized type for monetary amounts
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.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
Amount: amount,
|
|
|
|
Received: received,
|
2020-01-14 13:38:32 +00:00
|
|
|
Status: status,
|
2019-10-29 16:04:34 +00:00
|
|
|
Key: key,
|
|
|
|
}
|
2019-10-23 13:04:54 +01:00
|
|
|
|
2019-10-29 16:04:34 +00:00
|
|
|
txs = append(txs, createTX)
|
|
|
|
}
|
2020-01-14 13:38:32 +00:00
|
|
|
|
2019-10-29 16:04:34 +00:00
|
|
|
t.Run("pending transactions", func(t *testing.T) {
|
2020-01-19 16:29:15 +00:00
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
2019-10-29 16:04:34 +00:00
|
|
|
for _, tx := range txs {
|
2019-11-05 13:16:02 +00:00
|
|
|
_, err := db.StripeCoinPayments().Transactions().Insert(ctx, tx)
|
2019-10-29 16:04:34 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2019-10-23 13:04:54 +01:00
|
|
|
|
2020-02-11 13:11:14 +00:00
|
|
|
page, err := db.StripeCoinPayments().Transactions().ListPending(ctx, 0, limit, time.Now())
|
2019-10-29 16:04:34 +00:00
|
|
|
require.NoError(t, err)
|
2020-02-11 13:11:14 +00:00
|
|
|
|
|
|
|
pendingTXs := page.Transactions
|
|
|
|
|
|
|
|
for page.Next {
|
|
|
|
page, err = db.StripeCoinPayments().Transactions().ListPending(ctx, page.NextOffset, limit, time.Now())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
pendingTXs = append(pendingTXs, page.Transactions...)
|
|
|
|
}
|
|
|
|
|
2020-01-14 13:38:32 +00:00
|
|
|
require.False(t, page.Next)
|
2020-02-11 13:11:14 +00:00
|
|
|
require.Equal(t, transactionCount, len(pendingTXs))
|
2019-10-23 13:04:54 +01:00
|
|
|
|
2019-10-29 16:04:34 +00:00
|
|
|
for _, act := range page.Transactions {
|
|
|
|
for _, exp := range txs {
|
|
|
|
if act.ID == exp.ID {
|
|
|
|
compareTransactions(t, exp, act)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2019-10-23 13:04:54 +01:00
|
|
|
|
2019-10-29 16:04:34 +00:00
|
|
|
t.Run("unapplied transaction", func(t *testing.T) {
|
2020-01-19 16:29:15 +00:00
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
2019-10-29 16:04:34 +00:00
|
|
|
var updatedTxs []stripecoinpayments.Transaction
|
|
|
|
var updates []stripecoinpayments.TransactionUpdate
|
|
|
|
var applies coinpayments.TransactionIDList
|
|
|
|
|
|
|
|
for _, tx := range txs {
|
2019-11-05 13:16:02 +00:00
|
|
|
_, err := db.StripeCoinPayments().Transactions().Insert(ctx, tx)
|
2019-10-29 16:04:34 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-01-14 13:38:32 +00:00
|
|
|
tx.Status = coinpayments.StatusCompleted
|
2019-10-29 16:04:34 +00:00
|
|
|
|
|
|
|
updates = append(updates,
|
|
|
|
stripecoinpayments.TransactionUpdate{
|
|
|
|
TransactionID: tx.ID,
|
|
|
|
Status: tx.Status,
|
|
|
|
Received: tx.Received,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
applies = append(applies, tx.ID)
|
|
|
|
updatedTxs = append(updatedTxs, tx)
|
2019-10-17 15:04:50 +01:00
|
|
|
}
|
|
|
|
|
2019-11-05 13:16:02 +00:00
|
|
|
err := db.StripeCoinPayments().Transactions().Update(ctx, updates, applies)
|
2019-10-17 15:04:50 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-02-11 13:11:14 +00:00
|
|
|
page, err := db.StripeCoinPayments().Transactions().ListUnapplied(ctx, 0, limit, time.Now())
|
2019-10-29 16:04:34 +00:00
|
|
|
require.NoError(t, err)
|
2020-02-11 13:11:14 +00:00
|
|
|
|
|
|
|
unappliedTXs := page.Transactions
|
|
|
|
|
|
|
|
for page.Next {
|
|
|
|
page, err = db.StripeCoinPayments().Transactions().ListUnapplied(ctx, page.NextOffset, limit, time.Now())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
unappliedTXs = append(unappliedTXs, page.Transactions...)
|
|
|
|
}
|
|
|
|
|
2020-01-14 13:38:32 +00:00
|
|
|
require.False(t, page.Next)
|
2020-02-11 13:11:14 +00:00
|
|
|
require.Equal(t, transactionCount, len(unappliedTXs))
|
2019-10-23 13:04:54 +01:00
|
|
|
|
2019-10-29 16:04:34 +00:00
|
|
|
for _, act := range page.Transactions {
|
|
|
|
for _, exp := range updatedTxs {
|
|
|
|
if act.ID == exp.ID {
|
|
|
|
compareTransactions(t, exp, act)
|
|
|
|
}
|
2019-10-23 13:04:54 +01:00
|
|
|
}
|
|
|
|
}
|
2019-10-29 16:04:34 +00:00
|
|
|
})
|
2019-10-17 15:04:50 +01:00
|
|
|
})
|
|
|
|
}
|
2019-10-23 13:04:54 +01:00
|
|
|
|
2019-11-15 14:59:39 +00:00
|
|
|
func TestTransactionsDBRates(t *testing.T) {
|
2020-01-19 16:29:15 +00:00
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
2019-11-15 14:59:39 +00:00
|
|
|
transactions := db.StripeCoinPayments().Transactions()
|
|
|
|
|
2021-07-29 02:33:05 +01:00
|
|
|
val, err := decimal.NewFromString("4.000000000000005")
|
satellite/payments: specialized type for monetary amounts
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.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
require.NoError(t, err)
|
2019-11-15 14:59:39 +00:00
|
|
|
|
|
|
|
const txID = "tx_id"
|
|
|
|
|
satellite/payments: specialized type for monetary amounts
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.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
err = transactions.LockRate(ctx, txID, val)
|
2019-11-15 14:59:39 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rate, err := transactions.GetLockedRate(ctx, txID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, val, rate)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-23 13:04:54 +01:00
|
|
|
// compareTransactions is a helper method to compare tx used to create db entry,
|
|
|
|
// with the tx returned from the db. Method doesn't compare created at field, but
|
|
|
|
// ensures that is not empty.
|
|
|
|
func compareTransactions(t *testing.T, exp, act stripecoinpayments.Transaction) {
|
|
|
|
assert.Equal(t, exp.ID, act.ID)
|
|
|
|
assert.Equal(t, exp.AccountID, act.AccountID)
|
|
|
|
assert.Equal(t, exp.Address, act.Address)
|
|
|
|
assert.Equal(t, exp.Amount, act.Amount)
|
|
|
|
assert.Equal(t, exp.Received, act.Received)
|
|
|
|
assert.Equal(t, exp.Status, act.Status)
|
|
|
|
assert.Equal(t, exp.Key, act.Key)
|
2019-11-15 14:59:39 +00:00
|
|
|
assert.Equal(t, exp.Timeout, act.Timeout)
|
2019-10-23 13:04:54 +01:00
|
|
|
assert.False(t, act.CreatedAt.IsZero())
|
|
|
|
}
|
2020-07-17 16:17:21 +01:00
|
|
|
|
|
|
|
func TestTransactions_ApplyTransactionBalance(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 0, UplinkCount: 1,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
satellite := planet.Satellites[0]
|
|
|
|
transactions := satellite.API.DB.StripeCoinPayments().Transactions()
|
|
|
|
userID := planet.Uplinks[0].Projects[0].Owner.ID
|
|
|
|
|
|
|
|
satellite.Core.Payments.Chore.TransactionCycle.Pause()
|
|
|
|
satellite.Core.Payments.Chore.AccountBalanceCycle.Pause()
|
|
|
|
|
|
|
|
// Emulate a deposit through CoinPayments.
|
|
|
|
txID := coinpayments.TransactionID("testID")
|
2022-09-06 13:43:09 +01:00
|
|
|
storjAmount, err := currency.AmountFromString("100", currency.StorjToken)
|
satellite/payments: specialized type for monetary amounts
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.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
storjUSDRate, err := decimal.NewFromString("0.2")
|
|
|
|
require.NoError(t, err)
|
2020-07-17 16:17:21 +01:00
|
|
|
|
|
|
|
createTx := stripecoinpayments.Transaction{
|
|
|
|
ID: txID,
|
|
|
|
AccountID: userID,
|
|
|
|
Address: "testAddress",
|
satellite/payments: specialized type for monetary amounts
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.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
Amount: storjAmount,
|
|
|
|
Received: storjAmount,
|
2020-07-17 16:17:21 +01:00
|
|
|
Status: coinpayments.StatusPending,
|
|
|
|
Key: "testKey",
|
|
|
|
Timeout: time.Second * 60,
|
|
|
|
}
|
|
|
|
|
|
|
|
tx, err := transactions.Insert(ctx, createTx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, tx)
|
|
|
|
|
|
|
|
update := stripecoinpayments.TransactionUpdate{
|
|
|
|
TransactionID: createTx.ID,
|
|
|
|
Status: coinpayments.StatusReceived,
|
satellite/payments: specialized type for monetary amounts
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.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
Received: storjAmount,
|
2020-07-17 16:17:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
err = transactions.Update(ctx, []stripecoinpayments.TransactionUpdate{update}, coinpayments.TransactionIDList{createTx.ID})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Check that the CoinPayments transaction is waiting to be applied to the Stripe customer balance.
|
|
|
|
page, err := transactions.ListUnapplied(ctx, 0, 1, time.Now())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, page.Transactions, 1)
|
|
|
|
|
|
|
|
err = transactions.LockRate(ctx, txID, storjUSDRate)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Trigger the AccountBalanceCycle. This calls Service.applyTransactionBalance()
|
|
|
|
satellite.Core.Payments.Chore.AccountBalanceCycle.TriggerWait()
|
|
|
|
|
|
|
|
cusID, err := satellite.API.DB.StripeCoinPayments().Customers().GetCustomerID(ctx, userID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Check that the CoinPayments deposit is reflected in the Stripe customer balance.
|
2022-04-28 03:54:56 +01:00
|
|
|
it := satellite.API.Payments.StripeClient.CustomerBalanceTransactions().List(&stripe.CustomerBalanceTransactionListParams{Customer: stripe.String(cusID)})
|
2020-07-17 16:17:21 +01:00
|
|
|
require.NoError(t, it.Err())
|
|
|
|
require.True(t, it.Next())
|
|
|
|
cbt := it.CustomerBalanceTransaction()
|
|
|
|
require.EqualValues(t, -2000, cbt.Amount)
|
|
|
|
require.EqualValues(t, txID, cbt.Metadata["txID"])
|
|
|
|
require.EqualValues(t, "100", cbt.Metadata["storj_amount"])
|
|
|
|
require.EqualValues(t, "0.2", cbt.Metadata["storj_usd_rate"])
|
|
|
|
})
|
|
|
|
}
|