Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make sp receive storage fee with funding addr #195

Merged
merged 3 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions e2e/tests/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ func (s *StorageTestSuite) GetStreamRecord(addr string) (sr paymenttypes.StreamR
func (s *StorageTestSuite) GetStreamRecords() (streamRecords StreamRecords) {
streamRecords.User = s.GetStreamRecord(s.User.GetAddr().String())
for _, sp := range s.StorageProviders {
sr := s.GetStreamRecord(sp.OperatorKey.GetAddr().String())
sr := s.GetStreamRecord(sp.FundingKey.GetAddr().String())
streamRecords.SPs = append(streamRecords.SPs, sr)
}
streamRecords.Tax = s.GetStreamRecord(paymenttypes.ValidatorTaxPoolAddress.String())
Expand All @@ -421,15 +421,15 @@ func (s *StorageTestSuite) CheckStreamRecordsBeforeAndAfter(streamRecordsBefore
return m
}, make(map[string]sdkmath.Int))
if payloadSize != 0 {
primarySpAddr := s.StorageProviders[0].OperatorKey.GetAddr().String()
primarySpFundingAddr := s.StorageProviders[0].FundingKey.GetAddr().String()
s.Require().Equal(
userOutflowMap[primarySpAddr].Sub(readChargeRate).String(),
spRateDiffMap[primarySpAddr].String())
diff := spRateDiffMap[primarySpAddr].Sub(primaryStorePrice.MulInt(sdk.NewIntFromUint64(chargeSize)).TruncateInt())
userOutflowMap[primarySpFundingAddr].Sub(readChargeRate).String(),
spRateDiffMap[primarySpFundingAddr].String())
diff := spRateDiffMap[primarySpFundingAddr].Sub(primaryStorePrice.MulInt(sdk.NewIntFromUint64(chargeSize)).TruncateInt())
s.T().Logf("readPrice: %s, readChargeRate: %s", readPrice, readChargeRate)
s.T().Logf("diff %s", diff.String())
s.Require().Equal(diff.String(), sdkmath.ZeroInt().String())
s.Require().Equal(spRateDiffMap[primarySpAddr].String(), primaryStorePrice.MulInt(sdk.NewIntFromUint64(chargeSize)).TruncateInt().String())
s.Require().Equal(spRateDiffMap[primarySpFundingAddr].String(), primaryStorePrice.MulInt(sdk.NewIntFromUint64(chargeSize)).TruncateInt().String())
for i, sp := range secondarySPs {
secondarySpAddr := sp.String()
s.Require().Equal(userOutflowMap[secondarySpAddr].String(), spRateDiffMap[secondarySpAddr].String(), "sp %d", i+1)
Expand Down Expand Up @@ -565,6 +565,9 @@ func (s *StorageTestSuite) TestPayment_Smoke() {
secondarySPs := lo.Map(secondaryStorageProviders, func(sp core.SPKeyManagers, i int) sdk.AccAddress {
return sp.OperatorKey.GetAddr()
})
secondarySPFundingKeys := lo.Map(secondaryStorageProviders, func(sp core.SPKeyManagers, i int) sdk.AccAddress {
return sp.FundingKey.GetAddr()
})
msgSealObject := storagetypes.NewMsgSealObject(sp.SealKey.GetAddr(), bucketName, objectName, secondarySPs, nil)
secondarySigs := lo.Map(secondaryStorageProviders, func(sp core.SPKeyManagers, i int) []byte {
sr := storagetypes.NewSecondarySpSignDoc(sp.OperatorKey.GetAddr(), queryHeadObjectResponse.ObjectInfo.Id, checksum)
Expand All @@ -582,7 +585,7 @@ func (s *StorageTestSuite) TestPayment_Smoke() {
streamRecordsAfterSeal := s.GetStreamRecords()
s.T().Logf("streamRecordsAfterSeal %s", core.YamlString(streamRecordsAfterSeal))
s.Require().Equal(sdkmath.ZeroInt(), streamRecordsAfterSeal.User.LockBalance)
s.CheckStreamRecordsBeforeAndAfter(streamRecordsAfterCreateObject, streamRecordsAfterSeal, readPrice, readChargeRate, primaryStorePrice, secondaryStorePrice, chargeSize, secondarySPs, uint64(payloadSize))
s.CheckStreamRecordsBeforeAndAfter(streamRecordsAfterCreateObject, streamRecordsAfterSeal, readPrice, readChargeRate, primaryStorePrice, secondaryStorePrice, chargeSize, secondarySPFundingKeys, uint64(payloadSize))

// query dynamic balance
time.Sleep(3 * time.Second)
Expand Down
46 changes: 33 additions & 13 deletions x/storage/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"testing"
"time"

"github.com/samber/lo"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
Expand All @@ -19,12 +21,15 @@ import (
type IntegrationTestSuite struct {
suite.Suite

keeper *keeper.Keeper
depKeepers keepertest.StorageDepKeepers
ctx sdk.Context
PrimarySpAddr sdk.AccAddress
UserAddr sdk.AccAddress
Denom string
keeper *keeper.Keeper
depKeepers keepertest.StorageDepKeepers
ctx sdk.Context
PrimarySpAddr sdk.AccAddress
PrimarySpFundingAddr sdk.AccAddress
PrimarySp sptypes.StorageProvider
SecondarySps []sptypes.StorageProvider
UserAddr sdk.AccAddress
Denom string
}

func (s *IntegrationTestSuite) SetupTest() {
Expand All @@ -33,7 +38,23 @@ func (s *IntegrationTestSuite) SetupTest() {
ctx := s.ctx.WithBlockTime(time.Now())
// init data
s.PrimarySpAddr = sample.RandAccAddress()
s.PrimarySpFundingAddr = sample.RandAccAddress()
s.UserAddr = sample.RandAccAddress()
sp := sptypes.StorageProvider{
OperatorAddress: s.PrimarySpAddr.String(),
FundingAddress: s.PrimarySpFundingAddr.String(),
}
s.depKeepers.SpKeeper.SetStorageProvider(ctx, &sp)
for i := 0; i < 6; i++ {
secondarySpAddr := sample.RandAccAddress()
secondarySpFundingAddr := sample.RandAccAddress()
secondarySp := sptypes.StorageProvider{
OperatorAddress: secondarySpAddr.String(),
FundingAddress: secondarySpFundingAddr.String(),
}
s.SecondarySps = append(s.SecondarySps, secondarySp)
s.depKeepers.SpKeeper.SetStorageProvider(ctx, &secondarySp)
}
s.depKeepers.SpKeeper.SetSpStoragePrice(ctx, sptypes.SpStoragePrice{
SpAddress: s.PrimarySpAddr.String(),
UpdateTimeSec: 1,
Expand Down Expand Up @@ -71,7 +92,7 @@ func (s *IntegrationTestSuite) TestCreateCreateBucket_Payment() {
userStreamRecordCreateBucket, found := s.depKeepers.PaymentKeeper.GetStreamRecord(ctx, s.UserAddr)
s.Require().True(found)
s.T().Logf("userStreamRecordCreateBucket: %+v", userStreamRecordCreateBucket)
spStreamRecordCreateBucket, found := s.depKeepers.PaymentKeeper.GetStreamRecord(ctx, s.PrimarySpAddr)
spStreamRecordCreateBucket, found := s.depKeepers.PaymentKeeper.GetStreamRecord(ctx, s.PrimarySpFundingAddr)
s.Require().True(found)
s.T().Logf("spStreamRecordCreateBucket: %+v", spStreamRecordCreateBucket)

Expand All @@ -89,23 +110,22 @@ func (s *IntegrationTestSuite) TestCreateCreateBucket_Payment() {
userStreamRecordCreateObject, found := s.depKeepers.PaymentKeeper.GetStreamRecord(ctx, s.UserAddr)
s.Require().True(found)
s.T().Logf("userStreamRecordCreateObject: %+v", userStreamRecordCreateObject)
spStreamRecordCreateObject, found := s.depKeepers.PaymentKeeper.GetStreamRecord(ctx, s.PrimarySpAddr)
spStreamRecordCreateObject, found := s.depKeepers.PaymentKeeper.GetStreamRecord(ctx, s.PrimarySpFundingAddr)
s.Require().True(found)
s.T().Logf("spStreamRecordCreateObject: %+v", spStreamRecordCreateObject)

// mock seal object
var secondarySpAddresses []string
for i := 0; i < 6; i++ {
secondarySpAddresses = append(secondarySpAddresses, sample.RandAccAddress().String())
}
secondarySpAddresses := lo.Map(s.SecondarySps, func(sp sptypes.StorageProvider, index int) string {
return sp.OperatorAddress
})
object.SecondarySpAddresses = secondarySpAddresses
err = s.keeper.UnlockAndChargeStoreFee(ctx, &bucket, &object)
s.Require().NoError(err)
s.T().Logf("seal object")
userStreamRecordSealObject, found := s.depKeepers.PaymentKeeper.GetStreamRecord(ctx, s.UserAddr)
s.Require().True(found)
s.T().Logf("userStreamRecordSealObject: %+v", userStreamRecordSealObject)
spStreamRecordSealObject, found := s.depKeepers.PaymentKeeper.GetStreamRecord(ctx, s.PrimarySpAddr)
spStreamRecordSealObject, found := s.depKeepers.PaymentKeeper.GetStreamRecord(ctx, s.PrimarySpFundingAddr)
s.Require().True(found)
s.T().Logf("spStreamRecordSealObject: %+v", spStreamRecordSealObject)

Expand Down
20 changes: 18 additions & 2 deletions x/storage/keeper/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ import (
storagetypes "github.com/bnb-chain/greenfield/x/storage/types"
)

func (k Keeper) GetFundingAddressBySpAddr(ctx sdk.Context, spAddr sdk.AccAddress) (string, error) {
sp, found := k.spKeeper.GetStorageProvider(ctx, spAddr)
if !found {
return "", fmt.Errorf("storage provider %s not found", spAddr)
}
return sp.FundingAddress, nil
}

func (k Keeper) ChargeInitialReadFee(ctx sdk.Context, bucketInfo *storagetypes.BucketInfo) error {
if bucketInfo.ChargedReadQuota == 0 {
return nil
Expand Down Expand Up @@ -154,13 +162,17 @@ func (k Keeper) GetBucketBill(ctx sdk.Context, bucketInfo *storagetypes.BucketIn
if err != nil {
return userFlows, fmt.Errorf("get storage price failed: %w", err)
}
primarySpFundingAddr, err := k.GetFundingAddressBySpAddr(ctx, sdk.MustAccAddressFromHex(bucketInfo.PrimarySpAddress))
if err != nil {
return userFlows, fmt.Errorf("get funding address by sp address failed: %w, sp: %s", err, bucketInfo.PrimarySpAddress)
}
totalUserOutRate := sdkmath.ZeroInt()
readFlowRate := price.ReadPrice.MulInt(sdkmath.NewIntFromUint64(bucketInfo.ChargedReadQuota)).TruncateInt()
primaryStoreFlowRate := price.PrimaryStorePrice.MulInt(sdkmath.NewIntFromUint64(bucketInfo.BillingInfo.TotalChargeSize)).TruncateInt()
primarySpRate := readFlowRate.Add(primaryStoreFlowRate)
if primarySpRate.IsPositive() {
userFlows.Flows = append(userFlows.Flows, types.OutFlow{
ToAddress: bucketInfo.PrimarySpAddress,
ToAddress: primarySpFundingAddr,
Rate: primarySpRate,
})
totalUserOutRate = totalUserOutRate.Add(primarySpRate)
Expand All @@ -170,8 +182,12 @@ func (k Keeper) GetBucketBill(ctx sdk.Context, bucketInfo *storagetypes.BucketIn
if rate.IsZero() {
continue
}
spFundingAddr, err := k.GetFundingAddressBySpAddr(ctx, sdk.MustAccAddressFromHex(spObjectsSize.SpAddress))
if err != nil {
return userFlows, fmt.Errorf("get funding address by sp address failed: %w, sp: %s", err, spObjectsSize.SpAddress)
}
userFlows.Flows = append(userFlows.Flows, types.OutFlow{
ToAddress: spObjectsSize.SpAddress,
ToAddress: spFundingAddr,
Rate: rate,
})
totalUserOutRate = totalUserOutRate.Add(rate)
Expand Down