2020-03-15 13:06:01 +00:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2021-01-14 16:41:36 +00:00
|
|
|
package payouts_test
|
2020-03-15 13:06:01 +00:00
|
|
|
|
|
|
|
import (
|
2020-03-16 04:28:03 +00:00
|
|
|
"fmt"
|
2020-03-15 13:06:01 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-03-16 04:28:03 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-03-15 13:06:01 +00:00
|
|
|
|
|
|
|
"storj.io/common/testcontext"
|
2021-05-14 23:03:38 +01:00
|
|
|
"storj.io/common/testrand"
|
2020-03-15 13:06:01 +00:00
|
|
|
"storj.io/storj/storagenode"
|
2021-01-14 16:41:36 +00:00
|
|
|
"storj.io/storj/storagenode/payouts"
|
2020-03-15 13:06:01 +00:00
|
|
|
"storj.io/storj/storagenode/storagenodedb/storagenodedbtest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestHeldAmountDB(t *testing.T) {
|
|
|
|
storagenodedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db storagenode.DB) {
|
2021-01-14 16:41:36 +00:00
|
|
|
payout := db.Payout()
|
2021-05-14 23:03:38 +01:00
|
|
|
satelliteID := testrand.NodeID()
|
2020-03-15 13:06:01 +00:00
|
|
|
period := "2020-01"
|
2021-01-14 16:41:36 +00:00
|
|
|
paystub := payouts.PayStub{
|
2020-03-15 13:06:01 +00:00
|
|
|
SatelliteID: satelliteID,
|
|
|
|
Period: "2020-01",
|
|
|
|
Created: time.Now().UTC(),
|
|
|
|
Codes: "1",
|
|
|
|
UsageAtRest: 1,
|
|
|
|
UsageGet: 2,
|
|
|
|
UsagePut: 3,
|
|
|
|
UsageGetRepair: 4,
|
|
|
|
UsagePutRepair: 5,
|
|
|
|
UsageGetAudit: 6,
|
|
|
|
CompAtRest: 7,
|
|
|
|
CompGet: 8,
|
|
|
|
CompPut: 9,
|
|
|
|
CompGetRepair: 10,
|
|
|
|
CompPutRepair: 11,
|
|
|
|
CompGetAudit: 12,
|
|
|
|
SurgePercent: 13,
|
|
|
|
Held: 14,
|
|
|
|
Owed: 15,
|
|
|
|
Disposed: 16,
|
|
|
|
}
|
2020-05-18 19:37:16 +01:00
|
|
|
paystub2 := paystub
|
|
|
|
paystub2.Period = "2020-02"
|
|
|
|
paystub2.Created = paystub.Created.Add(time.Hour * 24 * 30)
|
2020-03-15 13:06:01 +00:00
|
|
|
|
2022-07-01 10:32:17 +01:00
|
|
|
t.Run("StorePayStub", func(t *testing.T) {
|
2021-01-14 16:41:36 +00:00
|
|
|
err := payout.StorePayStub(ctx, paystub)
|
2020-03-15 13:06:01 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
2021-01-14 16:41:36 +00:00
|
|
|
payment := payouts.Payment{
|
2020-09-08 16:15:08 +01:00
|
|
|
SatelliteID: satelliteID,
|
|
|
|
Period: period,
|
|
|
|
Receipt: "test",
|
|
|
|
}
|
|
|
|
|
2022-07-01 10:32:17 +01:00
|
|
|
t.Run("GetPayStub", func(t *testing.T) {
|
2021-01-14 16:41:36 +00:00
|
|
|
err := payout.StorePayment(ctx, payment)
|
2020-07-16 12:22:37 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-01-14 16:41:36 +00:00
|
|
|
stub, err := payout.GetPayStub(ctx, satelliteID, period)
|
2020-03-15 13:06:01 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-14 16:41:36 +00:00
|
|
|
receipt, err := payout.GetReceipt(ctx, satelliteID, period)
|
2020-09-08 16:15:08 +01:00
|
|
|
assert.NoError(t, err)
|
2020-03-15 13:06:01 +00:00
|
|
|
assert.Equal(t, stub.Period, paystub.Period)
|
|
|
|
assert.Equal(t, stub.Created, paystub.Created)
|
|
|
|
assert.Equal(t, stub.Codes, paystub.Codes)
|
|
|
|
assert.Equal(t, stub.CompAtRest, paystub.CompAtRest)
|
|
|
|
assert.Equal(t, stub.CompGet, paystub.CompGet)
|
|
|
|
assert.Equal(t, stub.CompGetAudit, paystub.CompGetAudit)
|
|
|
|
assert.Equal(t, stub.CompGetRepair, paystub.CompGetRepair)
|
|
|
|
assert.Equal(t, stub.CompPut, paystub.CompPut)
|
|
|
|
assert.Equal(t, stub.CompPutRepair, paystub.CompPutRepair)
|
|
|
|
assert.Equal(t, stub.Disposed, paystub.Disposed)
|
|
|
|
assert.Equal(t, stub.Held, paystub.Held)
|
|
|
|
assert.Equal(t, stub.Owed, paystub.Owed)
|
|
|
|
assert.Equal(t, stub.Paid, paystub.Paid)
|
|
|
|
assert.Equal(t, stub.SatelliteID, paystub.SatelliteID)
|
|
|
|
assert.Equal(t, stub.SurgePercent, paystub.SurgePercent)
|
|
|
|
assert.Equal(t, stub.UsageAtRest, paystub.UsageAtRest)
|
|
|
|
assert.Equal(t, stub.UsageGet, paystub.UsageGet)
|
|
|
|
assert.Equal(t, stub.UsageGetAudit, paystub.UsageGetAudit)
|
|
|
|
assert.Equal(t, stub.UsageGetRepair, paystub.UsageGetRepair)
|
|
|
|
assert.Equal(t, stub.UsagePut, paystub.UsagePut)
|
|
|
|
assert.Equal(t, stub.UsagePutRepair, paystub.UsagePutRepair)
|
2020-09-08 16:15:08 +01:00
|
|
|
assert.Equal(t, receipt, payment.Receipt)
|
2020-03-15 13:06:01 +00:00
|
|
|
|
2021-01-14 16:41:36 +00:00
|
|
|
stub, err = payout.GetPayStub(ctx, satelliteID, "")
|
2020-03-15 13:06:01 +00:00
|
|
|
assert.Error(t, err)
|
2021-01-14 16:41:36 +00:00
|
|
|
assert.Equal(t, true, payouts.ErrNoPayStubForPeriod.Has(err))
|
2020-03-15 13:06:01 +00:00
|
|
|
assert.Nil(t, stub)
|
2020-09-08 16:15:08 +01:00
|
|
|
assert.NotNil(t, receipt)
|
2021-01-14 16:41:36 +00:00
|
|
|
receipt, err = payout.GetReceipt(ctx, satelliteID, "")
|
2020-09-08 16:15:08 +01:00
|
|
|
assert.Error(t, err)
|
2021-01-14 16:41:36 +00:00
|
|
|
assert.Equal(t, true, payouts.ErrNoPayStubForPeriod.Has(err))
|
2020-03-15 13:06:01 +00:00
|
|
|
|
2021-05-14 23:03:38 +01:00
|
|
|
stub, err = payout.GetPayStub(ctx, testrand.NodeID(), period)
|
2020-03-15 13:06:01 +00:00
|
|
|
assert.Error(t, err)
|
2021-01-14 16:41:36 +00:00
|
|
|
assert.Equal(t, true, payouts.ErrNoPayStubForPeriod.Has(err))
|
2020-03-15 13:06:01 +00:00
|
|
|
assert.Nil(t, stub)
|
2020-09-08 16:15:08 +01:00
|
|
|
assert.NotNil(t, receipt)
|
2020-03-15 13:06:01 +00:00
|
|
|
})
|
|
|
|
|
2022-07-01 10:32:17 +01:00
|
|
|
t.Run("AllPayStubs", func(t *testing.T) {
|
2021-01-14 16:41:36 +00:00
|
|
|
stubs, err := payout.AllPayStubs(ctx, period)
|
2020-03-15 13:06:01 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, stubs)
|
|
|
|
assert.Equal(t, 1, len(stubs))
|
|
|
|
assert.Equal(t, stubs[0].Period, paystub.Period)
|
|
|
|
assert.Equal(t, stubs[0].Created, paystub.Created)
|
|
|
|
assert.Equal(t, stubs[0].Codes, paystub.Codes)
|
|
|
|
assert.Equal(t, stubs[0].CompAtRest, paystub.CompAtRest)
|
|
|
|
assert.Equal(t, stubs[0].CompGet, paystub.CompGet)
|
|
|
|
assert.Equal(t, stubs[0].CompGetAudit, paystub.CompGetAudit)
|
|
|
|
assert.Equal(t, stubs[0].CompGetRepair, paystub.CompGetRepair)
|
|
|
|
assert.Equal(t, stubs[0].CompPut, paystub.CompPut)
|
|
|
|
assert.Equal(t, stubs[0].CompPutRepair, paystub.CompPutRepair)
|
|
|
|
assert.Equal(t, stubs[0].Disposed, paystub.Disposed)
|
|
|
|
assert.Equal(t, stubs[0].Held, paystub.Held)
|
|
|
|
assert.Equal(t, stubs[0].Owed, paystub.Owed)
|
|
|
|
assert.Equal(t, stubs[0].Paid, paystub.Paid)
|
|
|
|
assert.Equal(t, stubs[0].SatelliteID, paystub.SatelliteID)
|
|
|
|
assert.Equal(t, stubs[0].SurgePercent, paystub.SurgePercent)
|
|
|
|
assert.Equal(t, stubs[0].UsageAtRest, paystub.UsageAtRest)
|
|
|
|
assert.Equal(t, stubs[0].UsageGet, paystub.UsageGet)
|
|
|
|
assert.Equal(t, stubs[0].UsageGetAudit, paystub.UsageGetAudit)
|
|
|
|
assert.Equal(t, stubs[0].UsageGetRepair, paystub.UsageGetRepair)
|
|
|
|
assert.Equal(t, stubs[0].UsagePut, paystub.UsagePut)
|
|
|
|
assert.Equal(t, stubs[0].UsagePutRepair, paystub.UsagePutRepair)
|
|
|
|
|
2021-01-14 16:41:36 +00:00
|
|
|
stubs, err = payout.AllPayStubs(ctx, "")
|
2020-03-15 13:06:01 +00:00
|
|
|
assert.Equal(t, len(stubs), 0)
|
2020-03-16 04:28:03 +00:00
|
|
|
assert.NoError(t, err)
|
2020-03-15 13:06:01 +00:00
|
|
|
})
|
2020-04-28 14:07:50 +01:00
|
|
|
|
2021-01-14 16:41:36 +00:00
|
|
|
payment = payouts.Payment{
|
2020-06-24 18:05:43 +01:00
|
|
|
ID: 1,
|
|
|
|
Created: time.Now().UTC(),
|
|
|
|
SatelliteID: satelliteID,
|
|
|
|
Period: period,
|
|
|
|
Amount: 228,
|
|
|
|
Receipt: "receipt",
|
|
|
|
Notes: "notes",
|
|
|
|
}
|
|
|
|
|
2022-07-01 10:32:17 +01:00
|
|
|
t.Run("StorePayment", func(t *testing.T) {
|
2021-01-14 16:41:36 +00:00
|
|
|
err := payout.StorePayment(ctx, payment)
|
2020-06-24 18:05:43 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
2022-07-01 10:32:17 +01:00
|
|
|
t.Run("SatellitesHeldbackHistory", func(t *testing.T) {
|
2021-01-14 16:41:36 +00:00
|
|
|
heldback, err := payout.SatellitesHeldbackHistory(ctx, satelliteID)
|
2020-04-28 14:07:50 +01:00
|
|
|
assert.NoError(t, err)
|
2020-09-10 15:08:25 +01:00
|
|
|
assert.Equal(t, heldback[0].Amount, paystub.Held)
|
2020-04-28 14:07:50 +01:00
|
|
|
assert.Equal(t, heldback[0].Period, paystub.Period)
|
|
|
|
})
|
2020-05-18 19:37:16 +01:00
|
|
|
|
2022-07-01 10:32:17 +01:00
|
|
|
t.Run("SatellitePeriods", func(t *testing.T) {
|
2021-01-14 16:41:36 +00:00
|
|
|
periods, err := payout.SatellitePeriods(ctx, paystub.SatelliteID)
|
2020-05-18 19:37:16 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, periods)
|
|
|
|
assert.Equal(t, 1, len(periods))
|
|
|
|
assert.Equal(t, paystub.Period, periods[0])
|
|
|
|
|
2021-01-14 16:41:36 +00:00
|
|
|
err = payout.StorePayStub(ctx, paystub2)
|
2020-05-18 19:37:16 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-01-14 16:41:36 +00:00
|
|
|
periods, err = payout.SatellitePeriods(ctx, paystub.SatelliteID)
|
2020-05-18 19:37:16 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, periods)
|
|
|
|
assert.Equal(t, 2, len(periods))
|
|
|
|
assert.Equal(t, paystub.Period, periods[0])
|
|
|
|
assert.Equal(t, paystub2.Period, periods[1])
|
|
|
|
})
|
|
|
|
|
2022-07-01 10:32:17 +01:00
|
|
|
t.Run("AllPeriods", func(t *testing.T) {
|
2021-01-14 16:41:36 +00:00
|
|
|
periods, err := payout.AllPeriods(ctx)
|
2020-05-18 19:37:16 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, periods)
|
|
|
|
assert.Equal(t, 2, len(periods))
|
|
|
|
assert.Equal(t, paystub.Period, periods[0])
|
|
|
|
assert.Equal(t, paystub2.Period, periods[1])
|
|
|
|
|
|
|
|
paystub3 := paystub2
|
2021-05-14 23:03:38 +01:00
|
|
|
paystub3.SatelliteID = testrand.NodeID()
|
2020-05-18 19:37:16 +01:00
|
|
|
paystub3.Period = "2020-03"
|
|
|
|
paystub3.Created = paystub2.Created.Add(time.Hour * 24 * 30)
|
|
|
|
|
2021-01-14 16:41:36 +00:00
|
|
|
err = payout.StorePayStub(ctx, paystub3)
|
2020-05-18 19:37:16 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-01-14 16:41:36 +00:00
|
|
|
periods, err = payout.AllPeriods(ctx)
|
2020-05-18 19:37:16 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, periods)
|
|
|
|
assert.Equal(t, 3, len(periods))
|
|
|
|
assert.Equal(t, paystub.Period, periods[0])
|
|
|
|
assert.Equal(t, paystub2.Period, periods[1])
|
|
|
|
assert.Equal(t, paystub3.Period, periods[2])
|
|
|
|
})
|
2020-03-18 11:27:42 +00:00
|
|
|
})
|
|
|
|
}
|
2020-03-16 04:28:03 +00:00
|
|
|
|
|
|
|
func TestSatellitePayStubPeriodCached(t *testing.T) {
|
|
|
|
storagenodedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db storagenode.DB) {
|
2020-09-10 15:08:25 +01:00
|
|
|
heldAmountDB := db.Payout()
|
2020-05-27 17:27:28 +01:00
|
|
|
reputationDB := db.Reputation()
|
2020-07-09 18:43:06 +01:00
|
|
|
satellitesDB := db.Satellites()
|
2021-07-29 15:30:57 +01:00
|
|
|
service, err := payouts.NewService(nil, heldAmountDB, reputationDB, satellitesDB, nil)
|
2020-11-04 14:38:15 +00:00
|
|
|
require.NoError(t, err)
|
2020-03-16 04:28:03 +00:00
|
|
|
|
2021-01-14 16:41:36 +00:00
|
|
|
payStub := payouts.PayStub{
|
2021-05-14 23:03:38 +01:00
|
|
|
SatelliteID: testrand.NodeID(),
|
2020-03-16 04:28:03 +00:00
|
|
|
Created: time.Now().UTC(),
|
|
|
|
Codes: "code",
|
|
|
|
UsageAtRest: 1,
|
|
|
|
UsageGet: 2,
|
|
|
|
UsagePut: 3,
|
|
|
|
UsageGetRepair: 4,
|
|
|
|
UsagePutRepair: 5,
|
|
|
|
UsageGetAudit: 6,
|
|
|
|
CompAtRest: 7,
|
|
|
|
CompGet: 8,
|
|
|
|
CompPut: 9,
|
|
|
|
CompGetRepair: 10,
|
|
|
|
CompPutRepair: 11,
|
|
|
|
CompGetAudit: 12,
|
|
|
|
SurgePercent: 13,
|
|
|
|
Held: 14,
|
|
|
|
Owed: 15,
|
|
|
|
Disposed: 16,
|
|
|
|
Paid: 17,
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 1; i < 4; i++ {
|
|
|
|
payStub.Period = fmt.Sprintf("2020-0%d", i)
|
|
|
|
err := heldAmountDB.StorePayStub(ctx, payStub)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2020-07-02 14:54:32 +01:00
|
|
|
payStubs, err := service.SatellitePayStubPeriod(ctx, payStub.SatelliteID, "2020-01", "2020-03")
|
2020-03-16 04:28:03 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 3, len(payStubs))
|
|
|
|
|
2020-07-02 14:54:32 +01:00
|
|
|
payStubs, err = service.SatellitePayStubPeriod(ctx, payStub.SatelliteID, "2019-01", "2021-03")
|
2020-03-16 04:28:03 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 3, len(payStubs))
|
|
|
|
|
2020-07-02 14:54:32 +01:00
|
|
|
payStubs, err = service.SatellitePayStubPeriod(ctx, payStub.SatelliteID, "2019-01", "2020-01")
|
2020-03-16 04:28:03 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(payStubs))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAllPayStubPeriodCached(t *testing.T) {
|
|
|
|
storagenodedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db storagenode.DB) {
|
2020-09-10 15:08:25 +01:00
|
|
|
heldAmountDB := db.Payout()
|
2020-05-27 17:27:28 +01:00
|
|
|
reputationDB := db.Reputation()
|
2020-07-09 18:43:06 +01:00
|
|
|
satellitesDB := db.Satellites()
|
2021-07-29 15:30:57 +01:00
|
|
|
service, err := payouts.NewService(nil, heldAmountDB, reputationDB, satellitesDB, nil)
|
2020-11-04 14:38:15 +00:00
|
|
|
require.NoError(t, err)
|
2020-03-16 04:28:03 +00:00
|
|
|
|
2021-01-14 16:41:36 +00:00
|
|
|
payStub := payouts.PayStub{
|
2021-05-14 23:03:38 +01:00
|
|
|
SatelliteID: testrand.NodeID(),
|
2020-03-16 04:28:03 +00:00
|
|
|
Created: time.Now().UTC(),
|
|
|
|
Codes: "code",
|
|
|
|
UsageAtRest: 1,
|
|
|
|
UsageGet: 2,
|
|
|
|
UsagePut: 3,
|
|
|
|
UsageGetRepair: 4,
|
|
|
|
UsagePutRepair: 5,
|
|
|
|
UsageGetAudit: 6,
|
|
|
|
CompAtRest: 7,
|
|
|
|
CompGet: 8,
|
|
|
|
CompPut: 9,
|
|
|
|
CompGetRepair: 10,
|
|
|
|
CompPutRepair: 11,
|
|
|
|
CompGetAudit: 12,
|
|
|
|
SurgePercent: 13,
|
|
|
|
Held: 14,
|
|
|
|
Owed: 15,
|
|
|
|
Disposed: 16,
|
|
|
|
Paid: 17,
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 1; i < 4; i++ {
|
|
|
|
payStub.SatelliteID[0] += byte(i)
|
|
|
|
for j := 1; j < 4; j++ {
|
|
|
|
payStub.Period = fmt.Sprintf("2020-0%d", j)
|
|
|
|
err := heldAmountDB.StorePayStub(ctx, payStub)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-02 14:54:32 +01:00
|
|
|
payStubs, err := service.AllPayStubsPeriod(ctx, "2020-01", "2020-03")
|
2020-03-16 04:28:03 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 9, len(payStubs))
|
|
|
|
|
2020-07-02 14:54:32 +01:00
|
|
|
payStubs, err = service.AllPayStubsPeriod(ctx, "2019-01", "2021-03")
|
2020-03-16 04:28:03 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 9, len(payStubs))
|
|
|
|
|
2020-07-02 14:54:32 +01:00
|
|
|
payStubs, err = service.AllPayStubsPeriod(ctx, "2019-01", "2020-01")
|
2020-03-16 04:28:03 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 3, len(payStubs))
|
|
|
|
|
2020-07-02 14:54:32 +01:00
|
|
|
payStubs, err = service.AllPayStubsPeriod(ctx, "2019-01", "2019-01")
|
2020-03-16 04:28:03 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, len(payStubs))
|
|
|
|
})
|
|
|
|
}
|
2021-02-02 17:37:24 +00:00
|
|
|
|
|
|
|
func TestPayouts(t *testing.T) {
|
|
|
|
storagenodedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db storagenode.DB) {
|
|
|
|
payout := db.Payout()
|
2022-07-01 10:32:17 +01:00
|
|
|
t.Run("SatelliteIDs", func(t *testing.T) {
|
2021-05-14 23:03:38 +01:00
|
|
|
id1 := testrand.NodeID()
|
|
|
|
id2 := testrand.NodeID()
|
|
|
|
id3 := testrand.NodeID()
|
2021-02-02 17:37:24 +00:00
|
|
|
err := payout.StorePayStub(ctx, payouts.PayStub{
|
|
|
|
SatelliteID: id1,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = payout.StorePayStub(ctx, payouts.PayStub{
|
|
|
|
SatelliteID: id1,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = payout.StorePayStub(ctx, payouts.PayStub{
|
|
|
|
SatelliteID: id2,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = payout.StorePayStub(ctx, payouts.PayStub{
|
|
|
|
SatelliteID: id3,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = payout.StorePayStub(ctx, payouts.PayStub{
|
|
|
|
SatelliteID: id3,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = payout.StorePayStub(ctx, payouts.PayStub{
|
|
|
|
SatelliteID: id2,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
listIDs, err := payout.GetPayingSatellitesIDs(ctx)
|
|
|
|
require.Equal(t, len(listIDs), 3)
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
2022-07-01 10:32:17 +01:00
|
|
|
t.Run("GetSatelliteEarned", func(t *testing.T) {
|
2021-05-14 23:03:38 +01:00
|
|
|
id1 := testrand.NodeID()
|
|
|
|
id2 := testrand.NodeID()
|
|
|
|
id3 := testrand.NodeID()
|
2021-02-02 17:37:24 +00:00
|
|
|
err := payout.StorePayStub(ctx, payouts.PayStub{
|
|
|
|
Period: "2020-11",
|
|
|
|
SatelliteID: id1,
|
|
|
|
CompGet: 11,
|
|
|
|
CompAtRest: 11,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = payout.StorePayStub(ctx, payouts.PayStub{
|
|
|
|
Period: "2020-12",
|
|
|
|
SatelliteID: id1,
|
|
|
|
CompGet: 22,
|
|
|
|
CompAtRest: 22,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = payout.StorePayStub(ctx, payouts.PayStub{
|
|
|
|
Period: "2020-11",
|
|
|
|
SatelliteID: id2,
|
|
|
|
CompGet: 33,
|
|
|
|
CompAtRest: 33,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = payout.StorePayStub(ctx, payouts.PayStub{
|
|
|
|
Period: "2020-10",
|
|
|
|
SatelliteID: id3,
|
|
|
|
CompGet: 44,
|
|
|
|
CompAtRest: 44,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = payout.StorePayStub(ctx, payouts.PayStub{
|
|
|
|
Period: "2020-11",
|
|
|
|
SatelliteID: id3,
|
|
|
|
CompGet: 55,
|
|
|
|
CompAtRest: 55,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = payout.StorePayStub(ctx, payouts.PayStub{
|
|
|
|
Period: "2020-10",
|
|
|
|
SatelliteID: id2,
|
|
|
|
CompGet: 66,
|
|
|
|
CompAtRest: 66,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
satellite1Earned, err := payout.GetEarnedAtSatellite(ctx, id1)
|
|
|
|
require.Equal(t, int(satellite1Earned), 66)
|
|
|
|
require.NoError(t, err)
|
|
|
|
satellite2Earned, err := payout.GetEarnedAtSatellite(ctx, id2)
|
|
|
|
require.Equal(t, int(satellite2Earned), 198)
|
|
|
|
require.NoError(t, err)
|
|
|
|
satellite3Earned, err := payout.GetEarnedAtSatellite(ctx, id3)
|
|
|
|
require.Equal(t, int(satellite3Earned), 198)
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
2021-05-05 18:34:10 +01:00
|
|
|
|
2022-07-01 10:32:17 +01:00
|
|
|
t.Run("GetSatelliteSummary", func(t *testing.T) {
|
2021-05-14 23:03:38 +01:00
|
|
|
id1 := testrand.NodeID()
|
|
|
|
id2 := testrand.NodeID()
|
2021-05-05 18:34:10 +01:00
|
|
|
|
|
|
|
err := payout.StorePayStub(ctx, payouts.PayStub{
|
|
|
|
Period: "2020-11",
|
|
|
|
SatelliteID: id1,
|
|
|
|
Paid: 11,
|
|
|
|
Held: 11,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = payout.StorePayStub(ctx, payouts.PayStub{
|
|
|
|
Period: "2020-10",
|
|
|
|
SatelliteID: id1,
|
|
|
|
Paid: 22,
|
|
|
|
Held: 22,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = payout.StorePayStub(ctx, payouts.PayStub{
|
|
|
|
Period: "2020-11",
|
|
|
|
SatelliteID: id2,
|
|
|
|
Paid: 33,
|
|
|
|
Held: 33,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = payout.StorePayStub(ctx, payouts.PayStub{
|
|
|
|
Period: "2020-10",
|
|
|
|
SatelliteID: id2,
|
|
|
|
Paid: 66,
|
|
|
|
Held: 66,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
paid, held, err := payout.GetSatellitePeriodSummary(ctx, id1, "2020-10")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, paid, int64(22))
|
|
|
|
require.Equal(t, held, int64(22))
|
|
|
|
|
|
|
|
paid2, held2, err := payout.GetSatelliteSummary(ctx, id2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, paid2, int64(99))
|
|
|
|
require.Equal(t, held2, int64(99))
|
|
|
|
})
|
2021-02-02 17:37:24 +00:00
|
|
|
})
|
|
|
|
}
|
2021-05-14 23:03:38 +01:00
|
|
|
|
2021-05-24 18:13:47 +01:00
|
|
|
func TestUndistributed(t *testing.T) {
|
2021-05-14 23:03:38 +01:00
|
|
|
storagenodedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db storagenode.DB) {
|
|
|
|
payoutdb := db.Payout()
|
|
|
|
satelliteID1 := testrand.NodeID()
|
|
|
|
satelliteID2 := testrand.NodeID()
|
|
|
|
|
2021-05-24 18:13:47 +01:00
|
|
|
t.Run("empty db no error", func(t *testing.T) {
|
|
|
|
undistributed, err := payoutdb.GetUndistributed(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, undistributed, 0)
|
2021-05-14 23:03:38 +01:00
|
|
|
})
|
|
|
|
|
2021-05-24 18:13:47 +01:00
|
|
|
t.Run("few paystubs with different satellites", func(t *testing.T) {
|
|
|
|
err := payoutdb.StorePayStub(ctx, payouts.PayStub{
|
|
|
|
SatelliteID: satelliteID2,
|
|
|
|
Period: "2020-01",
|
|
|
|
Distributed: 150,
|
|
|
|
Paid: 250,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2021-05-14 23:03:38 +01:00
|
|
|
|
2021-05-24 18:13:47 +01:00
|
|
|
err = payoutdb.StorePayStub(ctx, payouts.PayStub{
|
|
|
|
SatelliteID: satelliteID2,
|
|
|
|
Period: "2020-02",
|
|
|
|
Distributed: 250,
|
|
|
|
Paid: 350,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2021-05-14 23:03:38 +01:00
|
|
|
|
2021-05-24 18:13:47 +01:00
|
|
|
err = payoutdb.StorePayStub(ctx, payouts.PayStub{
|
|
|
|
SatelliteID: satelliteID1,
|
|
|
|
Period: "2020-01",
|
|
|
|
Distributed: 100,
|
|
|
|
Paid: 300,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2021-05-14 23:03:38 +01:00
|
|
|
|
2021-05-24 18:13:47 +01:00
|
|
|
err = payoutdb.StorePayStub(ctx, payouts.PayStub{
|
|
|
|
SatelliteID: satelliteID1,
|
|
|
|
Period: "2020-02",
|
|
|
|
Distributed: 400,
|
|
|
|
Paid: 500,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
undistributed, err := payoutdb.GetUndistributed(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, undistributed, 500)
|
|
|
|
})
|
2021-05-14 23:03:38 +01:00
|
|
|
})
|
|
|
|
}
|
2021-05-25 13:02:20 +01:00
|
|
|
|
|
|
|
func TestSummedPaystubs(t *testing.T) {
|
|
|
|
storagenodedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db storagenode.DB) {
|
|
|
|
payoutdb := db.Payout()
|
|
|
|
satelliteID1 := testrand.NodeID()
|
|
|
|
satelliteID2 := testrand.NodeID()
|
|
|
|
|
|
|
|
err := payoutdb.StorePayStub(ctx, payouts.PayStub{
|
|
|
|
SatelliteID: satelliteID2,
|
|
|
|
Period: "2020-01",
|
|
|
|
Distributed: 150,
|
|
|
|
Paid: 250,
|
2021-06-01 16:02:50 +01:00
|
|
|
Disposed: 100,
|
2021-05-25 13:02:20 +01:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = payoutdb.StorePayStub(ctx, payouts.PayStub{
|
|
|
|
SatelliteID: satelliteID2,
|
|
|
|
Period: "2020-02",
|
|
|
|
Distributed: 250,
|
|
|
|
Paid: 350,
|
2021-06-01 16:02:50 +01:00
|
|
|
Disposed: 200,
|
2021-05-25 13:02:20 +01:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = payoutdb.StorePayStub(ctx, payouts.PayStub{
|
|
|
|
SatelliteID: satelliteID1,
|
|
|
|
Period: "2020-01",
|
|
|
|
Distributed: 100,
|
|
|
|
Paid: 300,
|
2021-06-01 16:02:50 +01:00
|
|
|
Disposed: 300,
|
2021-05-25 13:02:20 +01:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = payoutdb.StorePayStub(ctx, payouts.PayStub{
|
|
|
|
SatelliteID: satelliteID1,
|
|
|
|
Period: "2020-02",
|
|
|
|
Distributed: 400,
|
|
|
|
Paid: 500,
|
2021-06-01 16:02:50 +01:00
|
|
|
Disposed: 400,
|
2021-05-25 13:02:20 +01:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
t.Run("all satellites period", func(t *testing.T) {
|
|
|
|
paystub, err := payoutdb.GetSatellitePaystubs(ctx, satelliteID1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, paystub.Distributed, 500)
|
|
|
|
require.EqualValues(t, paystub.Paid, 800)
|
2021-06-01 16:02:50 +01:00
|
|
|
require.EqualValues(t, paystub.Disposed, 700)
|
2021-05-25 13:02:20 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("satellites period", func(t *testing.T) {
|
|
|
|
paystub, err := payoutdb.GetPaystubs(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, paystub.Distributed, 900)
|
|
|
|
require.EqualValues(t, paystub.Paid, 1400)
|
2021-06-01 16:02:50 +01:00
|
|
|
require.EqualValues(t, paystub.Disposed, 1000)
|
2021-05-25 13:02:20 +01:00
|
|
|
})
|
2021-05-24 21:40:31 +01:00
|
|
|
|
|
|
|
t.Run("all satellites period", func(t *testing.T) {
|
|
|
|
paystub, err := payoutdb.GetPeriodPaystubs(ctx, "2020-01")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, paystub.Distributed, 250)
|
|
|
|
require.EqualValues(t, paystub.Paid, 550)
|
2021-06-01 16:02:50 +01:00
|
|
|
require.EqualValues(t, paystub.Disposed, 400)
|
2021-05-24 21:40:31 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("satellites period", func(t *testing.T) {
|
|
|
|
paystub, err := payoutdb.GetSatellitePeriodPaystubs(ctx, "2020-02", satelliteID2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, paystub.Distributed, 250)
|
|
|
|
require.EqualValues(t, paystub.Paid, 350)
|
2021-06-01 16:02:50 +01:00
|
|
|
require.EqualValues(t, paystub.Disposed, 200)
|
2021-05-24 21:40:31 +01:00
|
|
|
})
|
2021-05-25 13:02:20 +01:00
|
|
|
})
|
|
|
|
}
|
2021-04-22 19:50:42 +01:00
|
|
|
|
|
|
|
func TestDBHeldAmountHistory(t *testing.T) {
|
|
|
|
storagenodedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db storagenode.DB) {
|
|
|
|
payoutsDB := db.Payout()
|
|
|
|
satelliteID1 := testrand.NodeID()
|
|
|
|
satelliteID2 := testrand.NodeID()
|
|
|
|
|
|
|
|
paystubs := []payouts.PayStub{
|
|
|
|
{
|
|
|
|
SatelliteID: satelliteID1,
|
|
|
|
Period: "2021-01",
|
|
|
|
Held: 10,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SatelliteID: satelliteID1,
|
|
|
|
Period: "2021-02",
|
|
|
|
Held: 10,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SatelliteID: satelliteID1,
|
|
|
|
Period: "2021-03",
|
|
|
|
Held: 10,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SatelliteID: satelliteID1,
|
|
|
|
Period: "2021-04",
|
|
|
|
Held: 10,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SatelliteID: satelliteID2,
|
|
|
|
Period: "2021-01",
|
|
|
|
Held: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SatelliteID: satelliteID2,
|
|
|
|
Period: "2021-02",
|
|
|
|
Held: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SatelliteID: satelliteID2,
|
|
|
|
Period: "2021-03",
|
|
|
|
Held: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SatelliteID: satelliteID2,
|
|
|
|
Period: "2021-04",
|
|
|
|
Held: 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := []payouts.HeldAmountHistory{
|
|
|
|
{
|
|
|
|
SatelliteID: satelliteID1,
|
|
|
|
HeldAmounts: []payouts.HeldForPeriod{
|
|
|
|
{
|
|
|
|
Period: "2021-01",
|
|
|
|
Amount: 10,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Period: "2021-02",
|
|
|
|
Amount: 10,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Period: "2021-03",
|
|
|
|
Amount: 10,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Period: "2021-04",
|
|
|
|
Amount: 10,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SatelliteID: satelliteID2,
|
|
|
|
HeldAmounts: []payouts.HeldForPeriod{
|
|
|
|
{
|
|
|
|
Period: "2021-01",
|
|
|
|
Amount: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Period: "2021-02",
|
|
|
|
Amount: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Period: "2021-03",
|
|
|
|
Amount: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Period: "2021-04",
|
|
|
|
Amount: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, paystub := range paystubs {
|
|
|
|
err := payoutsDB.StorePayStub(ctx, paystub)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
history, err := payoutsDB.HeldAmountHistory(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.ElementsMatch(t, expected, history)
|
|
|
|
})
|
|
|
|
}
|