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

chore: allow anyone to trigger the settlement of GVG/GVG family #512

Merged
merged 2 commits into from
Oct 30, 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: 4 additions & 13 deletions e2e/tests/payment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1147,27 +1147,18 @@ func (s *PaymentTestSuite) TestVirtualGroup_Settle() {

// settle gvg family
msgSettle := virtualgrouptypes.MsgSettle{
StorageProvider: sp.FundingKey.GetAddr().String(),
StorageProvider: user.GetAddr().String(),
GlobalVirtualGroupFamilyId: family.Id,
}
s.SendTxBlock(sp.FundingKey, &msgSettle)
s.SendTxBlock(user, &msgSettle)

// settle gvg
var secondarySp *core.StorageProvider
for _, sp := range s.StorageProviders {
for _, id := range gvg.SecondarySpIds {
if sp.Info.Id == id {
secondarySp = sp
break
}
}
}
msgSettle = virtualgrouptypes.MsgSettle{
StorageProvider: secondarySp.FundingKey.GetAddr().String(),
StorageProvider: user.GetAddr().String(),
GlobalVirtualGroupFamilyId: 0,
GlobalVirtualGroupIds: []uint32{gvg.Id},
}
s.SendTxBlock(secondarySp.FundingKey, &msgSettle)
s.SendTxBlock(user, &msgSettle)

// assertions - balance has been checked in other tests in virtual group
streamRecordsAfter := s.getStreamRecords(streamAddresses)
Expand Down
34 changes: 8 additions & 26 deletions e2e/tests/virtualgroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ func (s *VirtualGroupTestSuite) TestBasic() {
}

func (s *VirtualGroupTestSuite) TestSettle() {
user := s.GenAndChargeAccounts(1, 1000000)[0]

_, _, primarySp, secondarySps, gvgFamilyId, gvgId := s.createObject()
s.T().Log("gvg family", gvgFamilyId, "gvg", gvgId)

Expand All @@ -197,12 +199,10 @@ func (s *VirtualGroupTestSuite) TestSettle() {
secondarySpIds[id] = struct{}{}
}

var lastSecondarySp *core.StorageProvider
secondarySpAddrs := make([]string, 0)
for _, secondarySp := range secondarySps {
if _, ok := secondarySpIds[secondarySp.Info.Id]; ok {
secondarySpAddrs = append(secondarySpAddrs, secondarySp.FundingKey.GetAddr().String())
lastSecondarySp = secondarySp
}
}

Expand All @@ -224,40 +224,26 @@ func (s *VirtualGroupTestSuite) TestSettle() {

// settle gvg family
msgSettle := virtualgroupmoduletypes.MsgSettle{
StorageProvider: primarySp.FundingKey.GetAddr().String(),
StorageProvider: user.GetAddr().String(),
GlobalVirtualGroupFamilyId: gvgFamily.Id,
}

simulateRes := s.SimulateTx(&msgSettle, primarySp.FundingKey)
gasLimit := simulateRes.GasInfo.GetGasUsed()
gasPrice, _ := sdk.ParseCoinNormalized(simulateRes.GasInfo.GetMinGasPrice())
feeAmount := gasPrice.Amount.Mul(sdk.NewInt(int64(gasLimit)))
s.T().Logf("fee amount: %s", feeAmount.String())

s.SendTxBlock(primarySp.FundingKey, &msgSettle)
s.SendTxBlock(user, &msgSettle)

primaryBalanceAfter, err := s.Client.BankQueryClient.Balance(context.Background(), &types2.QueryBalanceRequest{
Denom: s.Config.Denom, Address: primarySp.FundingKey.GetAddr().String(),
})
s.Require().NoError(err)

s.T().Logf("primaryBalance: %s, after: %s", primaryBalance.String(), primaryBalanceAfter.String())
s.Require().True(primaryBalanceAfter.Balance.Amount.Add(feeAmount).GT(primaryBalance.Balance.Amount))
s.Require().True(primaryBalanceAfter.Balance.Amount.GT(primaryBalance.Balance.Amount))

// settle gvg
msgSettle = virtualgroupmoduletypes.MsgSettle{
StorageProvider: lastSecondarySp.FundingKey.GetAddr().String(),
StorageProvider: user.GetAddr().String(),
GlobalVirtualGroupFamilyId: 0,
GlobalVirtualGroupIds: []uint32{gvgId},
}

simulateRes = s.SimulateTx(&msgSettle, lastSecondarySp.FundingKey)
gasLimit = simulateRes.GasInfo.GetGasUsed()
gasPrice, _ = sdk.ParseCoinNormalized(simulateRes.GasInfo.GetMinGasPrice())
feeAmount = gasPrice.Amount.Mul(sdk.NewInt(int64(gasLimit)))
s.T().Logf("fee amount: %s", feeAmount.String())

s.SendTxBlock(lastSecondarySp.FundingKey, &msgSettle)
s.SendTxBlock(user, &msgSettle)

secondaryBalancesAfter := make([]sdkmath.Int, 0, len(secondaryBalances))
for _, addr := range secondarySpAddrs {
Expand All @@ -270,11 +256,7 @@ func (s *VirtualGroupTestSuite) TestSettle() {

for i := range secondaryBalances {
s.T().Logf("secondaryBalance: %s, after: %s", secondaryBalances[i].String(), secondaryBalancesAfter[i].String())
if i != len(secondaryBalances)-1 {
s.Require().True(secondaryBalancesAfter[i].GT(secondaryBalances[i]))
} else {
s.Require().True(secondaryBalancesAfter[i].Add(feeAmount).GT(secondaryBalances[i]))
}
s.Require().True(secondaryBalancesAfter[i].GT(secondaryBalances[i]))
}
}

Expand Down
3 changes: 2 additions & 1 deletion proto/greenfield/virtualgroup/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ message MsgCancelSwapOutResponse {}
message MsgSettle {
option (cosmos.msg.v1.signer) = "storage_provider";

// storage_provider defines the operator/funding account address of the storage provider who initial settle request.
// storage_provider defines the account address who initiates the settle request.
// After Pampas upgrade, not only storage provider, anyone can submit this message.
string storage_provider = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
// global_virtual_group_family_id is the identifier of the global virtual group family.
uint32 global_virtual_group_family_id = 2;
Expand Down
13 changes: 8 additions & 5 deletions x/virtualgroup/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,22 @@ func GetTxCmd() *cobra.Command {

func CmdSettle() *cobra.Command {
cmd := &cobra.Command{
Use: "settle",
Use: "settle [gvg family id] [gvg ids]",
Short: "Broadcast message settle",
Args: cobra.ExactArgs(2),
Long: `Settle will do the settlement of a GVG family or several GVGs (by specifying comma seperated ids).
If zero is provided for GVG family, then the provided GVGs will be settled.
If none zero is provided for GVG family, then the provided GVG family will be settled and the provided GVGs will be ignored.`,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) (err error) {
gvgFamilyId, err := strconv.ParseInt(args[0], 10, 32)
if err != nil || gvgFamilyId <= 0 {
return fmt.Errorf("invalid GVG family id %s", args[1])
if err != nil || gvgFamilyId < 0 {
return fmt.Errorf("invalid GVG family id %s", args[0])
}
gvgIds := make([]uint32, 0)
splits := strings.Split(args[1], ",")
for _, split := range splits {
gvgId, err := strconv.ParseInt(split, 10, 32)
if err != nil || gvgFamilyId <= 0 {
if err != nil || gvgId < 0 {
return fmt.Errorf("invalid GVG id %s", args[1])
}
gvgIds = append(gvgIds, uint32(gvgId))
Expand Down
40 changes: 27 additions & 13 deletions x/virtualgroup/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,12 +350,16 @@ func (k msgServer) Settle(goCtx context.Context, req *types.MsgSettle) (*types.M

addr := sdk.MustAccAddressFromHex(req.StorageProvider)
var sp *sptypes.StorageProvider
found := false
sp, found = k.spKeeper.GetStorageProviderByOperatorAddr(ctx, addr)
if !found {
sp, found = k.spKeeper.GetStorageProviderByFundingAddr(ctx, addr)

pampasUpgraded := ctx.IsUpgraded(upgradetypes.Pampas)
if !pampasUpgraded {
found := false
sp, found = k.spKeeper.GetStorageProviderByOperatorAddr(ctx, addr)
if !found {
return nil, sptypes.ErrStorageProviderNotFound.Wrapf("The address must be operator/funding address of sp.")
sp, found = k.spKeeper.GetStorageProviderByFundingAddr(ctx, addr)
if !found {
return nil, sptypes.ErrStorageProviderNotFound.Wrapf("The address must be operator/funding address of sp.")
}
}
}

Expand All @@ -365,6 +369,13 @@ func (k msgServer) Settle(goCtx context.Context, req *types.MsgSettle) (*types.M
return nil, types.ErrGVGFamilyNotExist
}

if pampasUpgraded {
sp, found = k.spKeeper.GetStorageProvider(ctx, family.PrimarySpId)
if !found {
return nil, sptypes.ErrStorageProviderNotFound.Wrapf("Cannot find storage provider %d.", family.PrimarySpId)
}
}

err := k.SettleAndDistributeGVGFamily(ctx, sp, family)
if err != nil {
return nil, types.ErrSettleFailed
Expand All @@ -374,21 +385,24 @@ func (k msgServer) Settle(goCtx context.Context, req *types.MsgSettle) (*types.M
for _, gvgID := range req.GlobalVirtualGroupIds {
m[gvgID] = struct{}{}
}

for gvgID := range m {
gvg, found := k.GetGVG(ctx, gvgID)
if !found {
return nil, types.ErrGVGNotExist
}

permitted := false
for _, id := range gvg.SecondarySpIds {
if id == sp.Id {
permitted = true
break
if !pampasUpgraded {
permitted := false
for _, id := range gvg.SecondarySpIds {
if id == sp.Id {
permitted = true
break
}
}
if !permitted {
return nil, sdkerrors.Wrapf(types.ErrSettleFailed, "storage provider %d is not in the group", sp.Id)
}
}
if !permitted {
return nil, sdkerrors.Wrapf(types.ErrSettleFailed, "storage provider %d is not in the group", sp.Id)
}

err := k.SettleAndDistributeGVG(ctx, gvg)
Expand Down
6 changes: 3 additions & 3 deletions x/virtualgroup/types/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,9 @@ func (msg *MsgUpdateParams) ValidateBasic() error {
return nil
}

func NewMsgSettle(fundingAddress sdk.AccAddress, globalVirtualGroupFamilyID uint32, globalVirtualGroupIDs []uint32) *MsgSettle {
func NewMsgSettle(submitter sdk.AccAddress, globalVirtualGroupFamilyID uint32, globalVirtualGroupIDs []uint32) *MsgSettle {
return &MsgSettle{
StorageProvider: fundingAddress.String(),
StorageProvider: submitter.String(),
GlobalVirtualGroupFamilyId: globalVirtualGroupFamilyID,
GlobalVirtualGroupIds: globalVirtualGroupIDs,
}
Expand Down Expand Up @@ -314,7 +314,7 @@ func (msg *MsgSettle) GetSigners() []sdk.AccAddress {
func (msg *MsgSettle) ValidateBasic() error {
_, err := sdk.AccAddressFromHexUnsafe(msg.StorageProvider)
if err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid storage provider address (%s)", err)
return sdkerrors.ErrInvalidAddress.Wrapf("invalid submitter address (%s)", err)
}

if msg.GlobalVirtualGroupFamilyId == NoSpecifiedFamilyId {
Expand Down
3 changes: 2 additions & 1 deletion x/virtualgroup/types/tx.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading