/
msg_server_post_contract.go
69 lines (53 loc) · 1.51 KB
/
msg_server_post_contract.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package keeper
import (
"context"
"crypto/sha256"
"fmt"
"io"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/jackalLabs/canine-chain/v3/x/storage/types"
)
func (k msgServer) PostContract(goCtx context.Context, msg *types.MsgPostContract) (*types.MsgPostContractResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
provider, ok := k.GetProviders(ctx, msg.Creator)
if !ok {
return nil, fmt.Errorf("can't find provider")
}
ts, ok := sdk.NewIntFromString(provider.Totalspace)
if !ok {
return nil, fmt.Errorf("error parsing total space")
}
fs, ok := sdk.NewIntFromString(msg.Filesize)
if !ok {
return nil, fmt.Errorf("error parsing file size")
}
if k.GetProviderUsing(ctx, msg.Creator)+fs.Int64() > ts.Int64() {
return nil, fmt.Errorf("not enough space on provider")
}
h := sha256.New()
_, err := io.WriteString(h, fmt.Sprintf("%s%s%s", msg.Signee, msg.Creator, msg.Fid))
if err != nil {
return nil, err
}
hashName := h.Sum(nil)
cid, err := MakeCid(hashName)
if err != nil {
return nil, err
}
_, cidtaken := k.GetContracts(ctx, cid)
if cidtaken {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "cannot post the same contract twice")
}
newContract := types.Contracts{
Cid: cid,
Signee: msg.Signee,
Fid: msg.Fid,
Filesize: msg.Filesize,
Creator: msg.Creator,
Merkle: msg.Merkle,
Age: ctx.BlockHeight(),
}
k.SetContracts(ctx, newContract)
return &types.MsgPostContractResponse{}, nil
}