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

Fix/QB1716: Add report owner validation for release branch #233

Merged
merged 1 commit into from
Mar 13, 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
10 changes: 3 additions & 7 deletions x/pot/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package keeper
import (
"fmt"

paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
stratos "github.com/stratosnet/stratos-chain/types"
"github.com/tendermint/tendermint/libs/log"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"

stratos "github.com/stratosnet/stratos-chain/types"
"github.com/stratosnet/stratos-chain/x/pot/types"
)

Expand Down Expand Up @@ -69,11 +70,6 @@ func (k Keeper) VolumeReport(ctx sdk.Context, walletVolumes []*types.SingleWalle
return nil
}

func (k Keeper) IsSPNode(ctx sdk.Context, p2pAddr stratos.SdsAddress) (found bool) {
_, found = k.RegisterKeeper.GetMetaNode(ctx, p2pAddr)
return found
}

func (k Keeper) FoundationDeposit(ctx sdk.Context, amount sdk.Coins, from sdk.AccAddress) (err error) {
err = k.BankKeeper.SendCoinsFromAccountToModule(ctx, from, types.FoundationAccount, amount)
if err != nil {
Expand Down
29 changes: 21 additions & 8 deletions x/pot/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
"fmt"
"strconv"

"github.com/tendermint/tendermint/crypto/tmhash"

"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/bech32"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

stratos "github.com/stratosnet/stratos-chain/types"
"github.com/stratosnet/stratos-chain/x/pot/types"
"github.com/tendermint/tendermint/crypto/tmhash"
)

type msgServer struct {
Expand All @@ -31,10 +33,15 @@ func (k msgServer) HandleMsgVolumeReport(goCtx context.Context, msg *types.MsgVo
ctx := sdk.UnwrapSDKContext(goCtx)
reporter, err := stratos.SdsAddressFromBech32(msg.Reporter)
if err != nil {
return &types.MsgVolumeReportResponse{}, sdkerrors.Wrap(types.ErrInvalidAddress, err.Error())
return &types.MsgVolumeReportResponse{}, sdkerrors.Wrap(types.ErrReporterAddress, err.Error())
}
reporterOwner, err := sdk.AccAddressFromBech32(msg.ReporterOwner)
if err != nil {
return &types.MsgVolumeReportResponse{}, sdkerrors.Wrap(types.ErrReporterOwnerAddr, err.Error())
}
if !(k.IsSPNode(ctx, reporter)) {
return &types.MsgVolumeReportResponse{}, sdkerrors.Wrap(types.ErrInvalidAddress, "Volume report is not sent by a superior peer")

if !(k.RegisterKeeper.OwnMetaNode(ctx, reporterOwner, reporter)) {
return &types.MsgVolumeReportResponse{}, types.ErrReporterAddressOrOwner
}

// ensure epoch increment
Expand Down Expand Up @@ -176,13 +183,19 @@ func (k msgServer) HandleMsgFoundationDeposit(goCtx context.Context, msg *types.
func (k msgServer) HandleMsgSlashingResourceNode(goCtx context.Context, msg *types.MsgSlashingResourceNode) (*types.MsgSlashingResourceNodeResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

for _, reporter := range msg.Reporters {
reporterOwners := msg.ReporterOwner
for idx, reporter := range msg.Reporters {
reporterSdsAddr, err := stratos.SdsAddressFromBech32(reporter)
if err != nil {
return &types.MsgSlashingResourceNodeResponse{}, sdkerrors.Wrap(types.ErrInvalidAddress, err.Error())
return &types.MsgSlashingResourceNodeResponse{}, sdkerrors.Wrap(types.ErrReporterAddress, err.Error())
}
if !(k.IsSPNode(ctx, reporterSdsAddr)) {
return &types.MsgSlashingResourceNodeResponse{}, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "Slashing msg is not sent by a meta node")
ownerAddr, err := sdk.AccAddressFromBech32(reporterOwners[idx])
if err != nil {
return &types.MsgSlashingResourceNodeResponse{}, sdkerrors.Wrap(types.ErrReporterOwnerAddr, err.Error())
}

if !(k.RegisterKeeper.OwnMetaNode(ctx, ownerAddr, reporterSdsAddr)) {
return &types.MsgSlashingResourceNodeResponse{}, types.ErrReporterAddressOrOwner
}
}
networkAddress, err := stratos.SdsAddressFromBech32(msg.NetworkAddress)
Expand Down
6 changes: 4 additions & 2 deletions x/pot/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (
codeErrEmptyWalletVolumes
codeErrEpochNotPositive
codeErrEmptyReportReference
codeErrEmptyReporterOwnerAddr
codeErrReporterOwnerAddr
codeErrNegativeVolume
codeErrFoundationDepositAmountInvalid
codeErrBLSSignatureInvalid
Expand All @@ -37,6 +37,7 @@ const (
codeErrVolumeReport
codeErrLegacyAddressNotMatch
codeErrLegacyWithdrawFailure
codeErrReporterAddressOrOwner
)

var (
Expand All @@ -53,7 +54,7 @@ var (
ErrEmptyWalletVolumes = sdkerrors.Register(ModuleName, codeErrEmptyWalletVolumes, "wallet volumes list empty")
ErrEpochNotPositive = sdkerrors.Register(ModuleName, codeErrEpochNotPositive, "report epoch is not positive")
ErrEmptyReportReference = sdkerrors.Register(ModuleName, codeErrEmptyReportReference, "missing report reference")
ErrEmptyReporterOwnerAddr = sdkerrors.Register(ModuleName, codeErrEmptyReporterOwnerAddr, "missing reporter owner address")
ErrReporterOwnerAddr = sdkerrors.Register(ModuleName, codeErrReporterOwnerAddr, "invalid reporter owner address")
ErrNegativeVolume = sdkerrors.Register(ModuleName, codeErrNegativeVolume, "report volume is negative")
ErrFoundationDepositAmountInvalid = sdkerrors.Register(ModuleName, codeErrFoundationDepositAmountInvalid, "foundation deposit amount is invalid")
ErrBLSSignatureInvalid = sdkerrors.Register(ModuleName, codeErrBLSSignatureInvalid, "BLS signature is invalid")
Expand All @@ -72,4 +73,5 @@ var (
ErrVolumeReport = sdkerrors.Register(ModuleName, codeErrVolumeReport, "volume report failed")
ErrLegacyAddressNotMatch = sdkerrors.Register(ModuleName, codeErrLegacyAddressNotMatch, "public key does not mathe the legacy wallet address")
ErrLegacyWithdrawFailure = sdkerrors.Register(ModuleName, codeErrLegacyWithdrawFailure, "failure during legacyWithdraw")
ErrReporterAddressOrOwner = sdkerrors.Register(ModuleName, codeErrReporterAddressOrOwner, "invalid reporter address or owner address")
)
2 changes: 2 additions & 0 deletions x/pot/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"

stratos "github.com/stratosnet/stratos-chain/types"
"github.com/stratosnet/stratos-chain/x/register/types"
)
Expand Down Expand Up @@ -69,6 +70,7 @@ type RegisterKeeper interface {
GetUnbondingNodeBalance(ctx sdk.Context, networkAddr stratos.SdsAddress) sdk.Int

NozSupply(ctx sdk.Context) (remaining, total sdk.Int)
OwnMetaNode(ctx sdk.Context, ownerAddr sdk.AccAddress, p2pAddr stratos.SdsAddress) (found bool)
}

type StakingKeeper interface {
Expand Down
2 changes: 1 addition & 1 deletion x/pot/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (msg MsgVolumeReport) ValidateBasic() error {
return ErrEmptyReportReference
}
if len(msg.ReporterOwner) == 0 {
return ErrEmptyReporterOwnerAddr
return ErrReporterOwnerAddr
}

for _, item := range msg.WalletVolumes {
Expand Down
15 changes: 14 additions & 1 deletion x/register/keeper/meta_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

stratos "github.com/stratosnet/stratos-chain/types"
"github.com/stratosnet/stratos-chain/x/register/types"
)
Expand Down Expand Up @@ -72,7 +73,7 @@ func (k Keeper) SetMetaNode(ctx sdk.Context, metaNode types.MetaNode) {
}

// GetAllMetaNodes get the set of all meta nodes with no limits, used during genesis dump
//Iteration for all meta nodes
// Iteration for all meta nodes
func (k Keeper) GetAllMetaNodes(ctx sdk.Context) (metaNodes types.MetaNodes) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.MetaNodeKey)
Expand Down Expand Up @@ -477,6 +478,18 @@ func (k Keeper) GetMetaNodeIterator(ctx sdk.Context) sdk.Iterator {
return iterator
}

func (k Keeper) OwnMetaNode(ctx sdk.Context, ownerAddr sdk.AccAddress, p2pAddr stratos.SdsAddress) bool {
metaNode, found := k.GetMetaNode(ctx, p2pAddr)
if !found {
return false
}

if metaNode.OwnerAddress != ownerAddr.String() {
return false
}
return true
}

func (k Keeper) SendCoinsFromAccountToMetaNodeNotBondedPool(ctx sdk.Context, fromAcc sdk.AccAddress, amt sdk.Coin) error {
if !k.bankKeeper.HasBalance(ctx, fromAcc, amt) {
return types.ErrInsufficientBalance
Expand Down