Skip to content

Commit

Permalink
feat: add api for querying lock fee (#315) (#327)
Browse files Browse the repository at this point in the history
  • Loading branch information
forcodedancing authored Jul 7, 2023
1 parent fd31a4e commit c64374d
Show file tree
Hide file tree
Showing 5 changed files with 704 additions and 122 deletions.
22 changes: 22 additions & 0 deletions proto/greenfield/storage/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ service Query {
option (google.api.http).get = "/greenfield/storage/policy_by_id/{policy_id}";
}

// Queries lock fee for storing an object
rpc QueryLockFee(QueryLockFeeRequest) returns (QueryLockFeeResponse) {
option (google.api.http).get = "/greenfield/storage/lock_fee";
}

// this line is used by starport scaffolding # 2
}

Expand Down Expand Up @@ -267,4 +272,21 @@ message QueryPolicyByIdResponse {
permission.Policy policy = 1;
}

message QueryLockFeeRequest {
// primary_sp_address is the address of the primary sp.
string primary_sp_address = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
// create_at define the block timestamp when the object created.
int64 create_at = 2;
// payloadSize is the total size of the object payload
uint64 payload_size = 3;
}

message QueryLockFeeResponse {
string amount = 1 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
}

// this line is used by starport scaffolding # 3
24 changes: 24 additions & 0 deletions x/storage/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,27 @@ func (k Keeper) QueryParamsByTimestamp(c context.Context, req *types.QueryParams

return &types.QueryParamsByTimestampResponse{Params: params}, nil
}

func (k Keeper) QueryLockFee(c context.Context, req *types.QueryLockFeeRequest) (*types.QueryLockFeeResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(c)

createAt := req.GetCreateAt()
if createAt == 0 {
createAt = ctx.BlockTime().Unix()
}

primary, err := sdk.AccAddressFromHexUnsafe(req.PrimarySpAddress)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "invalid primary storage provider address")
}

amount, err := k.GetObjectLockFee(ctx, primary.String(), createAt, req.PayloadSize)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

return &types.QueryLockFeeResponse{Amount: amount}, nil
}
19 changes: 7 additions & 12 deletions x/storage/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,9 @@ func (k Keeper) CreateBucket(
return sdkmath.ZeroUint(), errors.Wrapf(types.ErrInvalidApproval, "The approval of sp is expired.")
}

if !ctx.IsCheckTx() {
err = k.VerifySPAndSignature(ctx, primarySpAcc, opts.ApprovalMsgBytes, opts.PrimarySpApproval.Sig)
if err != nil {
return sdkmath.ZeroUint(), err
}
err = k.VerifySPAndSignature(ctx, primarySpAcc, opts.ApprovalMsgBytes, opts.PrimarySpApproval.Sig)
if err != nil {
return sdkmath.ZeroUint(), err
}

bucketInfo := types.BucketInfo{
Expand Down Expand Up @@ -491,13 +489,10 @@ func (k Keeper) CreateObject(
return sdkmath.ZeroUint(), errors.Wrapf(types.ErrInvalidApproval, "The approval of sp is expired.")
}

var err error
if !ctx.IsCheckTx() { // no signature verification for simulation
err = k.VerifySPAndSignature(ctx, sdk.MustAccAddressFromHex(bucketInfo.PrimarySpAddress), opts.ApprovalMsgBytes,
opts.PrimarySpApproval.Sig)
if err != nil {
return sdkmath.ZeroUint(), err
}
err := k.VerifySPAndSignature(ctx, sdk.MustAccAddressFromHex(bucketInfo.PrimarySpAddress), opts.ApprovalMsgBytes,
opts.PrimarySpApproval.Sig)
if err != nil {
return sdkmath.ZeroUint(), err
}

objectKey := types.GetObjectKey(bucketName, objectName)
Expand Down
Loading

0 comments on commit c64374d

Please sign in to comment.