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/QB-1733: Add reporter owner validation for MsgFileUpload #237

Merged
merged 1 commit into from
Apr 3, 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
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/improbable-eng/grpc-web v0.15.0
github.com/ipfs/go-cid v0.1.0
github.com/kelindar/bitmap v1.4.1
github.com/pkg/errors v0.9.1
github.com/rakyll/statik v0.1.7
github.com/regen-network/cosmos-proto v0.3.1
Expand Down Expand Up @@ -105,9 +106,10 @@ require (
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
github.com/jmhodges/levigo v1.0.0 // indirect
github.com/kelindar/simd v1.1.2 // indirect
github.com/keybase/go-keychain v0.0.0-20190712205309-48d3d31d256d // indirect
github.com/klauspost/compress v1.15.11 // indirect
github.com/klauspost/cpuid/v2 v2.0.4 // indirect
github.com/klauspost/cpuid/v2 v2.0.12 // indirect
github.com/lib/pq v1.10.6 // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
Expand Down
7 changes: 6 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,10 @@ github.com/julienschmidt/httprouter v1.1.1-0.20170430222011-975b5c4c7c21/go.mod
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/karalabe/usb v0.0.0-20190919080040-51dc0efba356/go.mod h1:Od972xHfMJowv7NGVDiWVxk2zxnWgjLlJzE+F4F7AGU=
github.com/kelindar/bitmap v1.4.1 h1:Ih0BWMYXkkZxPMU536DsQKRhdvqFl7tuNjImfLJWC6E=
github.com/kelindar/bitmap v1.4.1/go.mod h1:4QyD+TDbfgy8oYB9oC4JzqfudYCYIjhbSP7iLraP+28=
github.com/kelindar/simd v1.1.2 h1:KduKb+M9cMY2HIH8S/cdJyD+5n5EGgq+Aeeleos55To=
github.com/kelindar/simd v1.1.2/go.mod h1:inq4DFudC7W8L5fhxoeZflLRNpWSs0GNx6MlWFvuvr0=
github.com/keybase/go-keychain v0.0.0-20190712205309-48d3d31d256d h1:Z+RDyXzjKE0i2sTjZ/b1uxiGtPhFy34Ou/Tk0qwN0kM=
github.com/keybase/go-keychain v0.0.0-20190712205309-48d3d31d256d/go.mod h1:JJNrCn9otv/2QP4D7SMJBgaleKpOf66PnW6F5WGNRIc=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
Expand All @@ -525,8 +529,9 @@ github.com/klauspost/compress v1.10.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYs
github.com/klauspost/compress v1.11.7/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/klauspost/compress v1.15.11 h1:Lcadnb3RKGin4FYM/orgq0qde+nc15E5Cbqg4B9Sx9c=
github.com/klauspost/compress v1.15.11/go.mod h1:QPwzmACJjUTFsnSHH934V6woptycfrDDJnH7hvFVbGM=
github.com/klauspost/cpuid/v2 v2.0.4 h1:g0I61F2K2DjRHz1cnxlkNSBIaePVoJIjjnHui8QHbiw=
github.com/klauspost/cpuid/v2 v2.0.4/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.0.12 h1:p9dKCg8i4gmOxtv35DvrYoWqYzQrvEVdjQ762Y0OqZE=
github.com/klauspost/cpuid/v2 v2.0.12/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
Expand Down
23 changes: 16 additions & 7 deletions proto/stratos/sds/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,25 @@ option go_package = "github.com/stratosnet/stratos-chain/x/sds/types";

// GenesisState defines the register module's genesis state.
message GenesisState {
Params params = 1 [
Params params = 1 [
(gogoproto.jsontag) = "params",
(gogoproto.moretags) = "yaml:\"params\""
];
repeated FileUpload file_uploads = 2 [
(gogoproto.jsontag) = "file_uploads",
(gogoproto.moretags) = "yaml:\"file_uploads\""
repeated GenesisFileInfo files = 2 [
(gogoproto.nullable) = false,
(gogoproto.jsontag) = "files",
(gogoproto.moretags) = "yaml:\"files\""
];
}




message GenesisFileInfo {
string file_hash = 1 [
(gogoproto.jsontag) = "file_hash",
(gogoproto.moretags) = "yaml:\"file_hash\""
];
FileInfo file_info = 2 [
(gogoproto.nullable) = false,
(gogoproto.jsontag) = "file_info",
(gogoproto.moretags) = "yaml:\"file_info\""
];
}
29 changes: 13 additions & 16 deletions proto/stratos/sds/v1/sds.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,19 @@ message Params {
];
}

message FileUpload {
string file_hash = 1 [
(gogoproto.jsontag) = "file_hash",
(gogoproto.moretags) = "yaml:\"file_hash\""
];
FileInfo file_info = 2 [
(gogoproto.jsontag) = "file_info",
(gogoproto.moretags) = "yaml:\"file_info\""
];
}

message FileInfo {
string height = 1 [
string height = 1 [
(gogoproto.nullable) = false,
(gogoproto.jsontag) = "height",
(gogoproto.moretags) = "yaml:\"height\"",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int"
];
string reporter = 2;
string uploader = 3;
}

bytes reporters = 2 [
(gogoproto.jsontag) = "reporters",
(gogoproto.moretags) = "yaml:\"reporters\""
];
string uploader = 3 [
(gogoproto.jsontag) = "uploader",
(gogoproto.moretags) = "yaml:\"uploader\""
];
}
9 changes: 6 additions & 3 deletions x/register/abci.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package register

import (
abci "github.com/tendermint/tendermint/abci/types"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/stratosnet/stratos-chain/x/register/keeper"
abci "github.com/tendermint/tendermint/abci/types"
)

// BeginBlocker check for infraction evidence or downtime of validators
// on every begin block
func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.Keeper) {
// TODO: fill out if your application requires beginblock, if not you can delete this function
k.UpdateMetaNodeBitMapIdxCache(ctx)
}

// EndBlocker called every block, process inflation, update validator set.
func EndBlocker(ctx sdk.Context, k keeper.Keeper) []abci.ValidatorUpdate {
return k.BlockRegisteredNodesUpdates(ctx)
k.BlockRegisteredNodesUpdates(ctx)
return []abci.ValidatorUpdate{}
}
4 changes: 4 additions & 0 deletions x/register/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package register
import (
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

"github.com/stratosnet/stratos-chain/x/register/keeper"
"github.com/stratosnet/stratos-chain/x/register/types"
)
Expand Down Expand Up @@ -116,6 +117,9 @@ func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, data *types.GenesisState

keeper.SetSlashing(ctx, walletAddress, sdk.NewInt(slashing.Value))
}

keeper.ReloadMetaNodeBitMapIdxCache(ctx)

return
}

Expand Down
54 changes: 30 additions & 24 deletions x/register/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"time"

"github.com/tendermint/tendermint/libs/log"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
Expand All @@ -14,41 +16,45 @@ import (
stratos "github.com/stratosnet/stratos-chain/types"
"github.com/stratosnet/stratos-chain/x/register/types"
regtypes "github.com/stratosnet/stratos-chain/x/register/types"
"github.com/tendermint/tendermint/libs/log"
)

var (
metaNodeBitMapIndexCacheStatus = types.CACHE_DIRTY
)

// Keeper of the register store
type Keeper struct {
storeKey sdk.StoreKey
cdc codec.Codec
// module specific parameter space that can be configured through governance
paramSpace paramtypes.Subspace
accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
distrKeeper types.DistrKeeper
hooks types.RegisterHooks
resourceNodeCache map[string]cachedResourceNode
resourceNodeCacheList *list.List
metaNodeCache map[string]cachedMetaNode
metaNodeCacheList *list.List
storeKey sdk.StoreKey
cdc codec.Codec
paramSpace paramtypes.Subspace
accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
distrKeeper types.DistrKeeper
hooks types.RegisterHooks
resourceNodeCache map[string]cachedResourceNode
resourceNodeCacheList *list.List
metaNodeCache map[string]cachedMetaNode
metaNodeCacheList *list.List
metaNodeBitMapIndexCache map[string]int
}

// NewKeeper creates a register keeper
func NewKeeper(cdc codec.Codec, key sdk.StoreKey, paramSpace paramtypes.Subspace,
accountKeeper types.AccountKeeper, bankKeeper types.BankKeeper, distrKeeper types.DistrKeeper) Keeper {

keeper := Keeper{
storeKey: key,
cdc: cdc,
paramSpace: paramSpace.WithKeyTable(types.ParamKeyTable()),
accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
distrKeeper: distrKeeper,
hooks: nil,
resourceNodeCache: make(map[string]cachedResourceNode, resourceNodeCacheSize),
resourceNodeCacheList: list.New(),
metaNodeCache: make(map[string]cachedMetaNode, metaNodeCacheSize),
metaNodeCacheList: list.New(),
storeKey: key,
cdc: cdc,
paramSpace: paramSpace.WithKeyTable(types.ParamKeyTable()),
accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
distrKeeper: distrKeeper,
hooks: nil,
resourceNodeCache: make(map[string]cachedResourceNode, resourceNodeCacheSize),
resourceNodeCacheList: list.New(),
metaNodeCache: make(map[string]cachedMetaNode, metaNodeCacheSize),
metaNodeCacheList: list.New(),
metaNodeBitMapIndexCache: make(map[string]int),
}
return keeper
}
Expand Down
78 changes: 78 additions & 0 deletions x/register/keeper/meta_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package keeper

import (
"bytes"
"errors"
"fmt"
"sort"
"strings"
"time"

Expand Down Expand Up @@ -350,6 +353,9 @@ func (k Keeper) HandleVoteForMetaNodeRegistration(ctx sdk.Context, nodeAddr stra
node.Status = stakingtypes.Bonded
node.Suspend = false
k.SetMetaNode(ctx, node)
// add new available meta node to cache
networkAddr, _ := stratos.SdsAddressFromBech32(node.GetNetworkAddress())
k.AddMetaNodeToBitMapIdxCache(networkAddr)
// increase ozone limit after vote is approved
_ = k.IncreaseOzoneLimitByAddStake(ctx, node.Tokens)
// increase mata node count
Expand Down Expand Up @@ -474,3 +480,75 @@ func (k Keeper) OwnMetaNode(ctx sdk.Context, ownerAddr sdk.AccAddress, p2pAddr s
}
return true
}

func (k Keeper) GetMetaNodeBitMapIndex(ctx sdk.Context, networkAddr stratos.SdsAddress) (index int, err error) {
k.UpdateMetaNodeBitMapIdxCache(ctx)

index, ok := k.metaNodeBitMapIndexCache[networkAddr.String()]
if !ok {
return index, errors.New(fmt.Sprintf("Can not find meta-node %v from cache", networkAddr.String()))
}
if index < 0 {
return index, errors.New(fmt.Sprintf("Can not find correct index of meta-node %v from cache", networkAddr.String()))
}

return index, nil
}

func (k Keeper) AddMetaNodeToBitMapIdxCache(networkAddr stratos.SdsAddress) {
k.metaNodeBitMapIndexCache[networkAddr.String()] = -1
metaNodeBitMapIndexCacheStatus = types.CACHE_DIRTY
}

func (k Keeper) RemoveMetaNodeFromBitMapIdxCache(networkAddr stratos.SdsAddress) {
delete(k.metaNodeBitMapIndexCache, networkAddr.String())
metaNodeBitMapIndexCacheStatus = types.CACHE_DIRTY
}

func (k Keeper) UpdateMetaNodeBitMapIdxCache(ctx sdk.Context) {
if metaNodeBitMapIndexCacheStatus == types.CACHE_NOT_DIRTY {
return
}
if len(k.metaNodeBitMapIndexCache) == 0 {
k.ReloadMetaNodeBitMapIdxCache(ctx)
return
}

keys := make([]string, 0)
for key, _ := range k.metaNodeBitMapIndexCache {
keys = append(keys, key)
}
sort.Slice(keys, func(i, j int) bool {
return keys[i] < keys[j]
})
for index, key := range keys {
k.metaNodeBitMapIndexCache[key] = index
}
metaNodeBitMapIndexCacheStatus = types.CACHE_NOT_DIRTY
}

func (k Keeper) ReloadMetaNodeBitMapIdxCache(ctx sdk.Context) {
if metaNodeBitMapIndexCacheStatus == types.CACHE_NOT_DIRTY {
return
}
keys := make([]string, 0)
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.MetaNodeKey)
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
node := types.MustUnmarshalMetaNode(k.cdc, iterator.Value())
if node.GetSuspend() || node.GetStatus() == stakingtypes.Unbonded {
continue
}
keys = append(keys, node.GetNetworkAddress())
}

sort.Slice(keys, func(i, j int) bool {
return keys[i] < keys[j]
})
for index, key := range keys {
k.metaNodeBitMapIndexCache[key] = index
}
metaNodeBitMapIndexCacheStatus = types.CACHE_NOT_DIRTY
}
8 changes: 5 additions & 3 deletions x/register/keeper/node_state_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (

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"
abci "github.com/tendermint/tendermint/abci/types"
)

// Called in each EndBlock
func (k Keeper) BlockRegisteredNodesUpdates(ctx sdk.Context) []abci.ValidatorUpdate {
func (k Keeper) BlockRegisteredNodesUpdates(ctx sdk.Context) {
// Remove all mature unbonding nodes from the ubd queue.
ctx.Logger().Debug("Enter BlockRegisteredNodesUpdates")
matureUBDs := k.DequeueAllMatureUBDQueue(ctx, ctx.BlockHeader().Time)
Expand Down Expand Up @@ -42,7 +42,7 @@ func (k Keeper) BlockRegisteredNodesUpdates(ctx sdk.Context) []abci.ValidatorUpd
}

// UpdateNode won't create UBD node
return []abci.ValidatorUpdate{}
return
}

// Node state transitions
Expand Down Expand Up @@ -135,6 +135,8 @@ func (k Keeper) completeUnbondingNode(ctx sdk.Context, node interface{}, isMetaN
temp := node.(types.MetaNode)
temp.Status = stakingtypes.Unbonded
k.SetMetaNode(ctx, temp)
networkAddr, _ := stratos.SdsAddressFromBech32(temp.GetNetworkAddress())
k.RemoveMetaNodeFromBitMapIdxCache(networkAddr)
return temp
} else {
temp := node.(types.ResourceNode)
Expand Down
7 changes: 7 additions & 0 deletions x/register/types/meta_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ import (
stratos "github.com/stratosnet/stratos-chain/types"
)

type CacheStatus uint8

const (
CACHE_NOT_DIRTY CacheStatus = 0
CACHE_DIRTY CacheStatus = 1
)

// NewMetaNode - initialize a new meta node
func NewMetaNode(networkAddr stratos.SdsAddress, pubKey cryptotypes.PubKey, ownerAddr sdk.AccAddress, description *Description, creationTime time.Time) (MetaNode, error) {
pkAny, err := codectypes.NewAnyWithValue(pubKey)
Expand Down
5 changes: 0 additions & 5 deletions x/sds/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cli

import (
"encoding/hex"
"fmt"

"github.com/cosmos/cosmos-sdk/client/tx"
Expand Down Expand Up @@ -108,10 +107,6 @@ func newBuildFileuploadMsg(clientCtx client.Context, txf tx.Factory, fs *flag.Fl
if err != nil {
return txf, nil, err
}
_, err = hex.DecodeString(fileHash)
if err != nil {
return txf, nil, err
}

flagReporterStr, err := fs.GetString(FlagReporter)
if err != nil {
Expand Down
Loading