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: add index by id for storage module #80

Merged
merged 6 commits into from
Feb 28, 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
2 changes: 1 addition & 1 deletion e2e/tests/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (s *StorageTestSuite) TestCreateObject() {
checksum := sdk.Keccak256(buffer.Bytes())
expectChecksum := [][]byte{checksum, checksum, checksum, checksum, checksum, checksum, checksum}
contextType := "text/event-stream"
msgCreateObject := storagetypes.NewMsgCreateObject(user.GetAddr(), bucketName, objectName, uint64(payloadSize), false, expectChecksum, contextType, math.MaxUint, nil, nil)
msgCreateObject := storagetypes.NewMsgCreateObject(user.GetAddr(), bucketName, objectName, uint64(payloadSize), false, expectChecksum, contextType, storagetypes.REDUNDANCY_EC_TYPE, math.MaxUint, nil, nil)
msgCreateObject.PrimarySpApproval.Sig, err = s.StorageProvider.ApprovalKey.GetPrivKey().Sign(msgCreateObject.GetApprovalBytes())
s.Require().NoError(err)
s.SendTxBlock(msgCreateObject, user)
Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.18

require (
cosmossdk.io/errors v1.0.0-beta.7
cosmossdk.io/math v1.0.0-beta.3
github.com/cosmos/cosmos-proto v1.0.0-beta.1
github.com/cosmos/cosmos-sdk v0.46.4
github.com/cosmos/ibc-go/v5 v5.0.0
Expand All @@ -29,6 +28,7 @@ require (
require golang.org/x/text v0.7.0 // indirect

require (
cosmossdk.io/math v1.0.0-beta.3
github.com/cosmos/go-bip39 v1.0.0
github.com/cosmos/gogoproto v1.4.6
github.com/prysmaticlabs/prysm v0.0.0-20220124113610-e26cde5e091b
Expand Down Expand Up @@ -228,8 +228,9 @@ require (
)

replace (
cosmossdk.io/math => github.com/bnb-chain/greenfield-cosmos-sdk/math v0.0.0-20230228075616-68ac309b432c
github.com/confio/ics23/go => github.com/cosmos/cosmos-sdk/ics23/go v0.8.0
github.com/cosmos/cosmos-sdk => github.com/bnb-chain/gnfd-cosmos-sdk v0.0.7
github.com/cosmos/cosmos-sdk => github.com/bnb-chain/greenfield-cosmos-sdk v0.0.2-0.20230228075616-68ac309b432c
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
github.com/tendermint/tendermint => github.com/bnb-chain/gnfd-tendermint v0.0.2
github.com/tendermint/tendermint => github.com/bnb-chain/greenfield-tendermint v0.0.2
)
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ collectd.org v0.3.0/go.mod h1:A/8DzQBkF6abtvrT2j/AU/4tiBgJWYyh0y/oB/4MlWE=
contrib.go.opencensus.io/exporter/jaeger v0.2.1/go.mod h1:Y8IsLgdxqh1QxYxPC5IgXVmBaeLUeQFfBeBi9PbeZd0=
cosmossdk.io/errors v1.0.0-beta.7 h1:gypHW76pTQGVnHKo6QBkb4yFOJjC+sUGRc5Al3Odj1w=
cosmossdk.io/errors v1.0.0-beta.7/go.mod h1:mz6FQMJRku4bY7aqS/Gwfcmr/ue91roMEKAmDUDpBfE=
cosmossdk.io/math v1.0.0-beta.3 h1:TbZxSopz2LqjJ7aXYfn7nJSb8vNaBklW6BLpcei1qwM=
cosmossdk.io/math v1.0.0-beta.3/go.mod h1:3LYasri3Zna4XpbrTNdKsWmD5fHHkaNAod/mNT9XdE4=
dmitri.shuralyov.com/app/changes v0.0.0-20180602232624-0a106ad413e3/go.mod h1:Yl+fi1br7+Rr3LqpNJf1/uxUdtRUV+Tnj0o93V2B9MU=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
dmitri.shuralyov.com/html/belt v0.0.0-20180602232347-f7d459c86be0/go.mod h1:JLBrvjyP0v+ecvNYvCpyZgu5/xkfAUhi6wJj28eUfSU=
Expand Down Expand Up @@ -218,10 +216,12 @@ github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdn
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
github.com/bmizerany/pat v0.0.0-20170815010413-6226ea591a40/go.mod h1:8rLXio+WjiTceGBHIoTvn60HIbs7Hm7bcHjyrSqYB9c=
github.com/bnb-chain/gnfd-cosmos-sdk v0.0.7 h1:HlTcn8YF9VSB1Dl/ys0mAehtm2tM9WEbF7M2zw/8y9I=
github.com/bnb-chain/gnfd-cosmos-sdk v0.0.7/go.mod h1:FrFE0FmMfUYySW5GdlV45b02SYUkNguECpVdZcUJVuU=
github.com/bnb-chain/gnfd-tendermint v0.0.2 h1:2Oy7B5A7W/55teZNhlrFR9SamYNjCXAQNjwsbt534jM=
github.com/bnb-chain/gnfd-tendermint v0.0.2/go.mod h1:/v9z9F6cq0+f7EGG92lYSLBcPYQDILoK91X8YM28hWo=
github.com/bnb-chain/greenfield-cosmos-sdk v0.0.2-0.20230228075616-68ac309b432c h1:+YSuQalvzdJbJIDlsIR6oaSewU9KUbY/TGGJn5ktjIE=
github.com/bnb-chain/greenfield-cosmos-sdk v0.0.2-0.20230228075616-68ac309b432c/go.mod h1:FrFE0FmMfUYySW5GdlV45b02SYUkNguECpVdZcUJVuU=
github.com/bnb-chain/greenfield-cosmos-sdk/math v0.0.0-20230228075616-68ac309b432c h1:BLmdYaj7Dx0YOhfk77+KPPJSMCwpQl6f4Y30+801bf0=
github.com/bnb-chain/greenfield-cosmos-sdk/math v0.0.0-20230228075616-68ac309b432c/go.mod h1:u/MXvf8wbUbCsAEyQSSYXXMsczAsFX48e2D6JI86T4o=
github.com/bnb-chain/greenfield-tendermint v0.0.2 h1:SNb3C9QhYklaLgY6onimLHR42IlB6NQJvQqwmj/1ybM=
github.com/bnb-chain/greenfield-tendermint v0.0.2/go.mod h1:/v9z9F6cq0+f7EGG92lYSLBcPYQDILoK91X8YM28hWo=
github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps=
github.com/bradfitz/go-smtpd v0.0.0-20170404230938-deb6d6237625/go.mod h1:HYsPBTaaSFSlLx/70C2HPIMNZpVV8+vt/A+FMnYP11g=
github.com/bradfitz/gomemcache v0.0.0-20170208213004-1952afaa557d/go.mod h1:PmM6Mmwb0LSuEubjR8N7PtNe1KxZLtOUHtbeikc5h60=
Expand Down
28 changes: 17 additions & 11 deletions proto/greenfield/storage/events.proto
Original file line number Diff line number Diff line change
Expand Up @@ -83,30 +83,36 @@ message EventCreateObject {
string bucket_name = 3;
// object_name define the name of object
string object_name = 4;
// id define an u256 id for object
string id = 6 [
// bucket_id define an u256 id for object
string bucket_id = 6 [
(cosmos_proto.scalar) = "cosmos.Uint",
(gogoproto.customtype) = "Uint",
(gogoproto.nullable) = false
];
// object_id define an u256 id for object
string object_id = 7 [
(cosmos_proto.scalar) = "cosmos.Uint",
(gogoproto.customtype) = "Uint",
(gogoproto.nullable) = false
];
// primary_sp_address define the account address of primary sp
string primary_sp_address = 7 [(cosmos_proto.scalar) = "cosmos.AddressString"];
string primary_sp_address = 8 [(cosmos_proto.scalar) = "cosmos.AddressString"];
// payload_size define the size of payload data which you want upload
uint64 payload_size = 8;
uint64 payload_size = 9;
// is_public define the highest permission of object.
bool is_public = 9;
bool is_public = 10;
// content_type define the content type of the payload data
string content_type = 10;
string content_type = 11;
// create_at define the block number when the object created
int64 create_at = 11;
int64 create_at = 12;
// status define the status of the object. INIT or IN_SERVICE or others
ObjectStatus status = 12;
ObjectStatus status = 13;
// redundancy_type define the type of redundancy. Replication or EC
RedundancyType redundancy_type = 13;
RedundancyType redundancy_type = 14;
// source_type define the source of the object. CrossChain or Greenfield origin
SourceType source_type = 14;
SourceType source_type = 15;
// checksums define the total checksums of the object which generated by redundancy
repeated bytes checksums = 15;
repeated bytes checksums = 16;
}

// EventSealObject is emitted on MsgSealObject
Expand Down
4 changes: 3 additions & 1 deletion proto/greenfield/storage/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@ message MsgCreateObject {
Approval primary_sp_approval = 7;
// expect_checksums is a list of hashes which was generate by redundancy algorithm.
repeated bytes expect_checksums = 8;
// redundancy_type can be ec or replica
RedundancyType redundancy_type = 9;
// expect_secondarySPs is a list of StorageProvider address, which is optional
repeated string expect_secondary_sp_addresses = 9 [(cosmos_proto.scalar) = "cosmos.AddressString"];
repeated string expect_secondary_sp_addresses = 10 [(cosmos_proto.scalar) = "cosmos.AddressString"];
}

message MsgCreateObjectResponse {}
Expand Down
2 changes: 1 addition & 1 deletion proto/greenfield/storage/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ message ObjectInfo {
// secondary_sp_addresses define the addresses of secondary_sps
repeated string secondary_sp_addresses = 13 [(cosmos_proto.scalar) = "cosmos.AddressString"];
// lockedBalance
string lockedBalance = 14 [
string locked_balance = 14 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int"
];
Expand Down
9 changes: 6 additions & 3 deletions x/sp/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,17 @@ func (k msgServer) CreateStorageProvider(goCtx context.Context, msg *types.MsgCr

// check to see if the funding address has been registered before
if _, found := k.GetStorageProviderByFundingAddr(ctx, fundingAcc); found {
return nil, types.ErrStorageProviderOwnerExists
return nil, types.ErrStorageProviderFundingAddrExists
}

// check to see if the seal address has been registered before
if _, found := k.GetStorageProviderBySealAddr(ctx, sealAcc); found {
return nil, types.ErrStoraveProviderSealAddrExists
return nil, types.ErrStorageProviderSealAddrExists
}

// check to see if the approval address has been registered before
if _, found := k.GetStorageProviderByApprovalAddr(ctx, approvalAcc); found {
return nil, types.ErrStoraveProviderApprovalAddrExists
return nil, types.ErrStorageProviderApprovalAddrExists
}

if _, err := msg.Description.EnsureLength(); err != nil {
Expand Down Expand Up @@ -114,6 +114,9 @@ func (k msgServer) CreateStorageProvider(goCtx context.Context, msg *types.MsgCr
}

k.SetStorageProvider(ctx, sp)
k.SetStorageProviderByApprovalAddr(ctx, sp)
k.SetStorageProviderByFundingAddr(ctx, sp)
k.SetStorageProviderBySealAddr(ctx, sp)

if err := ctx.EventManager().EmitTypedEvents(&types.EventCreateStorageProvider{
SpAddress: spAcc.String(),
Expand Down
16 changes: 16 additions & 0 deletions x/sp/keeper/sp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@ import (
"github.com/bnb-chain/greenfield/x/sp/types"
)

func (k Keeper) IsStorageProviderExistAndInService(ctx sdk.Context, addr sdk.AccAddress) error {
store := ctx.KVStore(k.storeKey)

value := store.Get(types.GetStorageProviderKey(addr))
if value == nil {
return types.ErrStorageProviderNotFound
}

sp := types.MustUnmarshalStorageProvider(k.cdc, value)
if sp.Status != types.STATUS_IN_SERVICE {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CheckIfValidStorageProvider seems the validator must be in service, any way to reflect this in the name of this function to make it more readable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about IsStorageProviderExistAndInService?

return types.ErrStorageProviderNotInService
}
return nil
}

func (k Keeper) GetStorageProvider(ctx sdk.Context, addr sdk.AccAddress) (sp types.StorageProvider, found bool) {
store := ctx.KVStore(k.storeKey)

Expand Down Expand Up @@ -54,6 +69,7 @@ func (k Keeper) GetStorageProviderByApprovalAddr(ctx sdk.Context, approvalAddr s
func (k Keeper) SetStorageProvider(ctx sdk.Context, sp types.StorageProvider) {
store := ctx.KVStore(k.storeKey)
bz := types.MustMarshalStorageProvider(k.cdc, &sp)

store.Set(types.GetStorageProviderKey(sp.GetOperator()), bz)
}

Expand Down
20 changes: 9 additions & 11 deletions x/sp/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ import (

// x/sp module sentinel errors
var (
ErrEmptyStorageProviderAddr = errors.Register(ModuleName, 1, "empty StorageProvider address")
ErrStorageProviderNotFound = errors.Register(ModuleName, 2, "StorageProvider does not exist")
ErrEmptyStorageProviderPubKey = errors.Register(ModuleName, 3, "empty storage provider public key")
ErrStorageProviderOwnerExists = errors.Register(ModuleName, 4, "StorageProvider already exist for this operator address; must use new StorageProvider operator address")
ErrStorageProviderPubKeyExists = errors.Register(ModuleName, 5, "StorageProvider already exist for this pubkey; must use new StorageProvider pubkey")
ErrInsufficientDepositAmount = errors.Register(ModuleName, 6, "insufficient deposit amount")
ErrDepositAccountNotAllowed = errors.Register(ModuleName, 7, "the deposit address must be the sp address or the fund address of sp.")
ErrInvalidDepositDenom = errors.Register(ModuleName, 8, "the deposit address must be the sp address or the fund address of sp.")
ErrStoraveProviderFundingAddrExists = errors.Register(ModuleName, 9, "StorageProvider already exist for this funding address; must use new StorageProvider funding address.")
ErrStoraveProviderSealAddrExists = errors.Register(ModuleName, 10, "StorageProvider already exist for this seal address; must use new StorageProvider seal address.")
ErrStoraveProviderApprovalAddrExists = errors.Register(ModuleName, 11, "StorageProvider already exist for this approval address; must use new StorageProvider approval address.")
ErrStorageProviderNotFound = errors.Register(ModuleName, 1, "StorageProvider does not exist")
ErrStorageProviderNotInService = errors.Register(ModuleName, 2, "StorageProvider does not exist")
ErrStorageProviderOwnerExists = errors.Register(ModuleName, 3, "StorageProvider already exist for this operator address; must use new StorageProvider operator address")
ErrInsufficientDepositAmount = errors.Register(ModuleName, 4, "insufficient deposit amount")
ErrDepositAccountNotAllowed = errors.Register(ModuleName, 5, "the deposit address must be the sp address or the fund address of sp.")
ErrInvalidDepositDenom = errors.Register(ModuleName, 6, "the deposit address must be the sp address or the fund address of sp.")
ErrStorageProviderFundingAddrExists = errors.Register(ModuleName, 7, "StorageProvider already exist for this funding address; must use new StorageProvider funding address.")
ErrStorageProviderSealAddrExists = errors.Register(ModuleName, 8, "StorageProvider already exist for this seal address; must use new StorageProvider seal address.")
ErrStorageProviderApprovalAddrExists = errors.Register(ModuleName, 9, "StorageProvider already exist for this approval address; must use new StorageProvider approval address.")

ErrSignerNotGovModule = errors.Register(ModuleName, 40, "signer is not gov module account")
ErrSignerEmpty = errors.Register(ModuleName, 41, "signer is empty")
Expand Down
1 change: 1 addition & 0 deletions x/storage/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ func CmdCreateObject() *cobra.Command {
isPublic,
expectChecksum,
contentType,
types.REDUNDANCY_EC_TYPE,
math.MaxUint,
nil,
nil, // NOTE(fynn): Not specified here.
Expand Down
Loading