Skip to content

Commit

Permalink
chore: payment refactor to use global store prices for billing
Browse files Browse the repository at this point in the history
  • Loading branch information
forcodedancing committed Aug 9, 2023
1 parent e207200 commit 8bf45ff
Show file tree
Hide file tree
Showing 29 changed files with 896 additions and 988 deletions.
3 changes: 0 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,11 +455,8 @@ func New(
app.PaymentKeeper = *paymentmodulekeeper.NewKeeper(
appCodec,
keys[paymentmoduletypes.StoreKey],

app.BankKeeper,
app.AccountKeeper,
app.SpKeeper,

authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
paymentModule := paymentmodule.NewAppModule(appCodec, app.PaymentKeeper, app.AccountKeeper, app.BankKeeper)
Expand Down
70 changes: 30 additions & 40 deletions e2e/tests/payment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,14 +302,14 @@ func (s *PaymentTestSuite) TestDeposit_ActiveAccount() {

params := s.queryParams()
reserveTime := params.VersionedParams.ReserveTime
queryGetSpStoragePriceByTimeResp, err := s.Client.QueryGetSpStoragePriceByTime(ctx, &sptypes.QueryGetSpStoragePriceByTimeRequest{
SpAddr: sp.OperatorKey.GetAddr().String(),
queryGetSpStoragePriceByTimeResp, err := s.Client.QueryGlobalSpStorePriceByTime(ctx, &sptypes.QueryGlobalSpStorePriceByTimeRequest{
Timestamp: 0,
})
s.T().Logf("queryGetSpStoragePriceByTimeResp %s, err: %v", queryGetSpStoragePriceByTimeResp, err)
s.Require().NoError(err)

bucketChargedReadQuota := uint64(1000)
readPrice := queryGetSpStoragePriceByTimeResp.SpStoragePrice.ReadPrice
readPrice := queryGetSpStoragePriceByTimeResp.GlobalSpStorePrice.ReadPrice
totalUserRate := readPrice.MulInt(sdkmath.NewIntFromUint64(bucketChargedReadQuota)).TruncateInt()
taxRateParam := params.VersionedParams.ValidatorTaxRate
taxStreamRate := taxRateParam.MulInt(totalUserRate).TruncateInt()
Expand Down Expand Up @@ -388,14 +388,14 @@ func (s *PaymentTestSuite) TestDeposit_ResumeInOneBlock() {

params := s.queryParams()
reserveTime := params.VersionedParams.ReserveTime
queryGetSpStoragePriceByTimeResp, err := s.Client.QueryGetSpStoragePriceByTime(ctx, &sptypes.QueryGetSpStoragePriceByTimeRequest{
SpAddr: sp.OperatorKey.GetAddr().String(),
queryGetSpStoragePriceByTimeResp, err := s.Client.QueryGlobalSpStorePriceByTime(ctx, &sptypes.QueryGlobalSpStorePriceByTimeRequest{
Timestamp: 0,
})
s.T().Logf("queryGetSpStoragePriceByTimeResp %s, err: %v", queryGetSpStoragePriceByTimeResp, err)
s.Require().NoError(err)

bucketChargedReadQuota := uint64(1000)
readPrice := queryGetSpStoragePriceByTimeResp.SpStoragePrice.ReadPrice
readPrice := queryGetSpStoragePriceByTimeResp.GlobalSpStorePrice.ReadPrice
totalUserRate := readPrice.MulInt(sdkmath.NewIntFromUint64(bucketChargedReadQuota)).TruncateInt()
taxRateParam := params.VersionedParams.ValidatorTaxRate
taxStreamRate := taxRateParam.MulInt(totalUserRate).TruncateInt()
Expand Down Expand Up @@ -505,14 +505,14 @@ func (s *PaymentTestSuite) TestDeposit_ResumeInBlocks() {
userAddr := user.GetAddr().String()

reserveTime := params.VersionedParams.ReserveTime
queryGetSpStoragePriceByTimeResp, err := s.Client.QueryGetSpStoragePriceByTime(ctx, &sptypes.QueryGetSpStoragePriceByTimeRequest{
SpAddr: sp.OperatorKey.GetAddr().String(),
queryGetSpStoragePriceByTimeResp, err := s.Client.QueryGlobalSpStorePriceByTime(ctx, &sptypes.QueryGlobalSpStorePriceByTimeRequest{
Timestamp: 0,
})
s.T().Logf("queryGetSpStoragePriceByTimeResp %s, err: %v", queryGetSpStoragePriceByTimeResp, err)
s.Require().NoError(err)

bucketChargedReadQuota := uint64(1000)
readPrice := queryGetSpStoragePriceByTimeResp.SpStoragePrice.ReadPrice
readPrice := queryGetSpStoragePriceByTimeResp.GlobalSpStorePrice.ReadPrice
totalUserRate := readPrice.MulInt(sdkmath.NewIntFromUint64(bucketChargedReadQuota)).TruncateInt()
taxRateParam := params.VersionedParams.ValidatorTaxRate
taxStreamRate := taxRateParam.MulInt(totalUserRate).TruncateInt()
Expand Down Expand Up @@ -642,12 +642,13 @@ func (s *PaymentTestSuite) TestAutoSettle_InOneBlock() {
bucketChargedReadQuota := uint64(1000)
params := s.queryParams()
reserveTime := params.VersionedParams.ReserveTime
queryGetSpStoragePriceByTimeResp, err := s.Client.QueryGetSpStoragePriceByTime(ctx, &sptypes.QueryGetSpStoragePriceByTimeRequest{
SpAddr: sp.OperatorKey.GetAddr().String(),
queryGetSpStoragePriceByTimeResp, err := s.Client.QueryGlobalSpStorePriceByTime(ctx, &sptypes.QueryGlobalSpStorePriceByTimeRequest{
Timestamp: 0,
})
s.T().Logf("queryGetSpStoragePriceByTimeResp %s, err: %v", queryGetSpStoragePriceByTimeResp, err)
s.Require().NoError(err)
readPrice := queryGetSpStoragePriceByTimeResp.SpStoragePrice.ReadPrice

readPrice := queryGetSpStoragePriceByTimeResp.GlobalSpStorePrice.ReadPrice
totalUserRate := readPrice.MulInt(sdkmath.NewIntFromUint64(bucketChargedReadQuota)).TruncateInt()
taxRateParam := params.VersionedParams.ValidatorTaxRate
taxStreamRate := taxRateParam.MulInt(totalUserRate).TruncateInt()
Expand Down Expand Up @@ -779,14 +780,14 @@ func (s *PaymentTestSuite) TestAutoSettle_InBlocks() {
userAddr := user.GetAddr().String()

reserveTime := params.VersionedParams.ReserveTime
queryGetSpStoragePriceByTimeResp, err := s.Client.QueryGetSpStoragePriceByTime(ctx, &sptypes.QueryGetSpStoragePriceByTimeRequest{
SpAddr: sp.OperatorKey.GetAddr().String(),
queryGetSpStoragePriceByTimeResp, err := s.Client.QueryGlobalSpStorePriceByTime(ctx, &sptypes.QueryGlobalSpStorePriceByTimeRequest{
Timestamp: 0,
})
s.T().Logf("queryGetSpStoragePriceByTimeResp %s, err: %v", queryGetSpStoragePriceByTimeResp, err)
s.Require().NoError(err)

bucketChargedReadQuota := uint64(1000)
readPrice := queryGetSpStoragePriceByTimeResp.SpStoragePrice.ReadPrice
readPrice := queryGetSpStoragePriceByTimeResp.GlobalSpStorePrice.ReadPrice
totalUserRate := readPrice.MulInt(sdkmath.NewIntFromUint64(bucketChargedReadQuota)).TruncateInt()
taxRateParam := params.VersionedParams.ValidatorTaxRate
taxStreamRate := taxRateParam.MulInt(totalUserRate).TruncateInt()
Expand Down Expand Up @@ -875,14 +876,14 @@ func (s *PaymentTestSuite) TestWithdraw() {

params := s.queryParams()
reserveTime := params.VersionedParams.ReserveTime
queryGetSpStoragePriceByTimeResp, err := s.Client.QueryGetSpStoragePriceByTime(ctx, &sptypes.QueryGetSpStoragePriceByTimeRequest{
SpAddr: sp.OperatorKey.GetAddr().String(),
queryGetSpStoragePriceByTimeResp, err := s.Client.QueryGlobalSpStorePriceByTime(ctx, &sptypes.QueryGlobalSpStorePriceByTimeRequest{
Timestamp: 0,
})
s.T().Logf("queryGetSpStoragePriceByTimeResp %s, err: %v", queryGetSpStoragePriceByTimeResp, err)
s.Require().NoError(err)

bucketChargedReadQuota := uint64(100000)
readPrice := queryGetSpStoragePriceByTimeResp.SpStoragePrice.ReadPrice
readPrice := queryGetSpStoragePriceByTimeResp.GlobalSpStorePrice.ReadPrice
totalUserRate := readPrice.MulInt(sdkmath.NewIntFromUint64(bucketChargedReadQuota)).TruncateInt()
taxRateParam := params.VersionedParams.ValidatorTaxRate
taxStreamRate := taxRateParam.MulInt(totalUserRate).TruncateInt()
Expand Down Expand Up @@ -1218,9 +1219,8 @@ func (s *PaymentTestSuite) TestDiscontinue_InOneBlock_WithPriceChange() {
streamRecordsBefore := s.getStreamRecords(streamAddresses)

// query storage price
priceRes, err := s.Client.QueryGetSpStoragePriceByTime(ctx, &sptypes.QueryGetSpStoragePriceByTimeRequest{
SpAddr: sp.OperatorKey.GetAddr().String(),
Timestamp: 0,
priceRes, err := s.Client.QuerySpStoragePrice(ctx, &sptypes.QuerySpStoragePriceRequest{
SpAddr: sp.OperatorKey.GetAddr().String(),
})
s.Require().NoError(err)
s.T().Log("price", priceRes.SpStoragePrice)
Expand Down Expand Up @@ -1421,9 +1421,8 @@ func (s *PaymentTestSuite) TestDiscontinue_InBlocks_WithPriceChange() {
streamRecordsBefore := s.getStreamRecords(streamAddresses)

// query storage price
priceRes, err := s.Client.QueryGetSpStoragePriceByTime(ctx, &sptypes.QueryGetSpStoragePriceByTimeRequest{
SpAddr: sp.OperatorKey.GetAddr().String(),
Timestamp: 0,
priceRes, err := s.Client.QuerySpStoragePrice(ctx, &sptypes.QuerySpStoragePriceRequest{
SpAddr: sp.OperatorKey.GetAddr().String(),
})
s.Require().NoError(err)
s.T().Log("price", priceRes.SpStoragePrice)
Expand Down Expand Up @@ -1559,9 +1558,8 @@ func (s *PaymentTestSuite) TestDiscontinue_InBlocks_WithPriceChangeReserveTimeCh
streamRecordsBefore := s.getStreamRecords(streamAddresses)

// query storage price
priceRes, err := s.Client.QueryGetSpStoragePriceByTime(ctx, &sptypes.QueryGetSpStoragePriceByTimeRequest{
SpAddr: sp.OperatorKey.GetAddr().String(),
Timestamp: 0,
priceRes, err := s.Client.QuerySpStoragePrice(ctx, &sptypes.QuerySpStoragePriceRequest{
SpAddr: sp.OperatorKey.GetAddr().String(),
})
s.Require().NoError(err)
s.T().Log("price", priceRes.SpStoragePrice)
Expand Down Expand Up @@ -1711,9 +1709,8 @@ func (s *PaymentTestSuite) TestDiscontinue_InBlocks_WithPriceChangeReserveTimeCh
streamRecordsBefore := s.getStreamRecords(streamAddresses)

// query storage price
priceRes, err := s.Client.QueryGetSpStoragePriceByTime(ctx, &sptypes.QueryGetSpStoragePriceByTimeRequest{
SpAddr: sp.OperatorKey.GetAddr().String(),
Timestamp: 0,
priceRes, err := s.Client.QuerySpStoragePrice(ctx, &sptypes.QuerySpStoragePriceRequest{
SpAddr: sp.OperatorKey.GetAddr().String(),
})
s.Require().NoError(err)
s.T().Log("price", priceRes.SpStoragePrice)
Expand Down Expand Up @@ -2173,21 +2170,14 @@ func (s *PaymentTestSuite) calculateLockFee(sp *core.StorageProvider, bucketName
func (s *PaymentTestSuite) getPrices(sp *core.StorageProvider, timestamp int64) (sdk.Dec, sdk.Dec, sdk.Dec) {
ctx := context.Background()

spStoragePriceByTimeResp, err := s.Client.QueryGetSpStoragePriceByTime(ctx, &sptypes.QueryGetSpStoragePriceByTimeRequest{
SpAddr: sp.OperatorKey.GetAddr().String(),
Timestamp: timestamp,
})
s.T().Logf("spStoragePriceByTimeResp %s, err: %v", spStoragePriceByTimeResp, err)
s.Require().NoError(err)

secondaryStoragePriceByTimeResp, err := s.Client.QueryGetSecondarySpStorePriceByTime(ctx, &sptypes.QueryGetSecondarySpStorePriceByTimeRequest{
spStoragePriceByTimeResp, err := s.Client.QueryGlobalSpStorePriceByTime(ctx, &sptypes.QueryGlobalSpStorePriceByTimeRequest{
Timestamp: timestamp,
})
s.T().Logf("spStoragePriceByTimeResp %s, err: %v", spStoragePriceByTimeResp, err)
s.Require().NoError(err)

return spStoragePriceByTimeResp.SpStoragePrice.ReadPrice, spStoragePriceByTimeResp.SpStoragePrice.StorePrice,
secondaryStoragePriceByTimeResp.SecondarySpStorePrice.StorePrice
return spStoragePriceByTimeResp.GlobalSpStorePrice.ReadPrice, spStoragePriceByTimeResp.GlobalSpStorePrice.PrimaryStorePrice,
spStoragePriceByTimeResp.GlobalSpStorePrice.SecondaryStorePrice
}

func (s *PaymentTestSuite) calculateReadRates(sp *core.StorageProvider, bucketName string) (sdkmath.Int, sdkmath.Int, sdkmath.Int) {
Expand Down
61 changes: 33 additions & 28 deletions e2e/tests/sp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,11 @@ func (s *StorageProviderTestSuite) TestDeposit() {

func (s *StorageProviderTestSuite) TestSpStoragePrice() {
ctx := context.Background()
s.CheckSecondarySpPrice()
s.CheckGlobalSpStorePrice()
sp := s.BaseSuite.PickStorageProvider()
spAddr := sp.OperatorKey.GetAddr().String()
spStoragePrice, err := s.Client.QueryGetSpStoragePriceByTime(ctx, &sptypes.QueryGetSpStoragePriceByTimeRequest{
SpAddr: spAddr,
Timestamp: 0,
spStoragePrice, err := s.Client.QuerySpStoragePrice(ctx, &sptypes.QuerySpStoragePriceRequest{
SpAddr: spAddr,
})
s.Require().NoError(err)
s.T().Log(spStoragePrice)
Expand All @@ -177,62 +176,68 @@ func (s *StorageProviderTestSuite) TestSpStoragePrice() {
}
_ = s.SendTxBlock(sp.OperatorKey, msgUpdateSpStoragePrice)
// query and assert
spStoragePrice2, err := s.Client.QueryGetSpStoragePriceByTime(ctx, &sptypes.QueryGetSpStoragePriceByTimeRequest{
SpAddr: spAddr,
Timestamp: 0,
spStoragePrice2, err := s.Client.QuerySpStoragePrice(ctx, &sptypes.QuerySpStoragePriceRequest{
SpAddr: spAddr,
})
s.Require().NoError(err)
s.T().Log(spStoragePrice2)
// check price changed as expected
s.Require().Equal(newReadPrice, spStoragePrice2.SpStoragePrice.ReadPrice)
s.Require().Equal(newStorePrice, spStoragePrice2.SpStoragePrice.StorePrice)
s.CheckSecondarySpPrice()
s.CheckGlobalSpStorePrice()
// query sp storage price by time before it exists, expect error
_, err = s.Client.QueryGetSecondarySpStorePriceByTime(ctx, &sptypes.QueryGetSecondarySpStorePriceByTimeRequest{
Timestamp: 1,
})
s.Require().Error(err)
_, err = s.Client.QueryGetSpStoragePriceByTime(ctx, &sptypes.QueryGetSpStoragePriceByTimeRequest{
SpAddr: spAddr,
_, err = s.Client.QueryGlobalSpStorePriceByTime(ctx, &sptypes.QueryGlobalSpStorePriceByTimeRequest{
Timestamp: 1,
})
s.Require().Error(err)
}

func (s *StorageProviderTestSuite) CheckSecondarySpPrice() {
func (s *StorageProviderTestSuite) CheckGlobalSpStorePrice() {
ctx := context.Background()
queryGetSecondarySpStorePriceByTimeResp, err := s.Client.QueryGetSecondarySpStorePriceByTime(ctx, &sptypes.QueryGetSecondarySpStorePriceByTimeRequest{
queryGlobalSpStorePriceByTimeResp, err := s.Client.QueryGlobalSpStorePriceByTime(ctx, &sptypes.QueryGlobalSpStorePriceByTimeRequest{
Timestamp: 0,
})
s.Require().NoError(err)
s.T().Logf("Secondary SP store price: %s", core.YamlString(queryGetSecondarySpStorePriceByTimeResp.SecondarySpStorePrice))
s.T().Logf("global SP store price: %s", core.YamlString(queryGlobalSpStorePriceByTimeResp.GlobalSpStorePrice))
// query all sps
sps, err := s.Client.StorageProviders(ctx, &sptypes.QueryStorageProvidersRequest{})
s.Require().NoError(err)
s.T().Logf("sps: %s", sps)
spNum := int64(sps.Pagination.Total)
prices := make([]sdk.Dec, 0)
storePrices := make([]sdk.Dec, 0)
readPrices := make([]sdk.Dec, 0)
for _, sp := range sps.Sps {
spStoragePrice, err := s.Client.QueryGetSpStoragePriceByTime(ctx, &sptypes.QueryGetSpStoragePriceByTimeRequest{
SpAddr: sp.OperatorAddress,
Timestamp: 0,
spStoragePrice, err := s.Client.QuerySpStoragePrice(ctx, &sptypes.QuerySpStoragePriceRequest{
SpAddr: sp.OperatorAddress,
})
s.Require().NoError(err)
s.T().Logf("sp: %s, storage price: %s", sp.OperatorAddress, core.YamlString(spStoragePrice.SpStoragePrice))
prices = append(prices, spStoragePrice.SpStoragePrice.StorePrice)
storePrices = append(storePrices, spStoragePrice.SpStoragePrice.StorePrice)
readPrices = append(readPrices, spStoragePrice.SpStoragePrice.ReadPrice)
}
sort.Slice(prices, func(i, j int) bool { return prices[i].LT(prices[j]) })
var median sdk.Dec

sort.Slice(storePrices, func(i, j int) bool { return storePrices[i].LT(storePrices[j]) })
var storeMedian sdk.Dec
if spNum%2 == 0 {
storeMedian = storePrices[spNum/2-1].Add(storePrices[spNum/2]).QuoInt64(2)
} else {
storeMedian = storePrices[spNum/2]
}

sort.Slice(readPrices, func(i, j int) bool { return readPrices[i].LT(readPrices[j]) })
var readMedian sdk.Dec
if spNum%2 == 0 {
median = prices[spNum/2-1].Add(prices[spNum/2]).QuoInt64(2)
readMedian = readPrices[spNum/2-1].Add(readPrices[spNum/2]).QuoInt64(2)
} else {
median = prices[spNum/2]
readMedian = readPrices[spNum/2]
}

s.Require().Equal(storeMedian, queryGlobalSpStorePriceByTimeResp.GlobalSpStorePrice.PrimaryStorePrice)
params, err := s.Client.SpQueryClient.Params(ctx, &sptypes.QueryParamsRequest{})
s.Require().NoError(err)
expectedSecondarySpStorePrice := params.Params.SecondarySpStorePriceRatio.Mul(median)
s.Require().Equal(expectedSecondarySpStorePrice, queryGetSecondarySpStorePriceByTimeResp.SecondarySpStorePrice.StorePrice)
expectedSecondarySpStorePrice := params.Params.SecondarySpStorePriceRatio.Mul(storeMedian)
s.Require().Equal(expectedSecondarySpStorePrice, queryGlobalSpStorePriceByTimeResp.GlobalSpStorePrice.SecondaryStorePrice)
s.Require().Equal(readMedian, queryGlobalSpStorePriceByTimeResp.GlobalSpStorePrice.ReadPrice)
}

func TestStorageProviderTestSuite(t *testing.T) {
Expand Down
Loading

0 comments on commit 8bf45ff

Please sign in to comment.