-
Notifications
You must be signed in to change notification settings - Fork 16
/
msg_server_attest.go
150 lines (111 loc) · 3.59 KB
/
msg_server_attest.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package keeper
import (
"context"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/tendermint/tendermint/libs/rand"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/jackalLabs/canine-chain/v3/x/storage/types"
)
const ( // TODO: Figure out optimal values for these and replace them with chain params
True = "true"
)
func (k Keeper) Attest(ctx sdk.Context, cid string, creator string) error {
form, found := k.GetAttestationForm(ctx, cid)
if !found {
return sdkerrors.Wrapf(types.ErrAttestInvalid, "cannot find this attestation")
}
done := false
var count int64
attestations := form.Attestations
for _, attestation := range attestations {
if attestation.Provider == creator {
attestation.Complete = true
done = true
}
if attestation.Complete {
count++
}
}
if !done {
return sdkerrors.Wrapf(types.ErrAttestInvalid, "you cannot attest to this deal")
}
if count < k.GetParams(ctx).AttestMinToPass {
form.Attestations = attestations
k.SetAttestationForm(ctx, form)
return nil
}
deal, found := k.GetActiveDeals(ctx, cid)
if !found {
return sdkerrors.Wrapf(types.ErrDealNotFound, "cannot find active deal from form")
}
deal.LastProof = ctx.BlockHeight()
k.SetActiveDeals(ctx, deal)
k.RemoveAttestation(ctx, cid)
return nil
}
func (k msgServer) Attest(goCtx context.Context, msg *types.MsgAttest) (*types.MsgAttestResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
err := k.Keeper.Attest(ctx, msg.Cid, msg.Creator)
if err != nil {
ctx.Logger().Debug(err.Error())
}
return &types.MsgAttestResponse{}, nil
}
func (k Keeper) RequestAttestation(ctx sdk.Context, cid string, creator string) ([]string, error) {
deal, found := k.GetActiveDeals(ctx, cid)
if !found {
return nil, sdkerrors.Wrapf(types.ErrDealNotFound, "cannot find active deal for attestation form")
}
if deal.Provider != creator {
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "you do not own this deal")
}
_, found = k.GetAttestationForm(ctx, cid)
if found {
return nil, sdkerrors.Wrapf(types.ErrAttestAlreadyExists, "attestation form already exists")
}
dealProvider := deal.Provider
provider, found := k.GetProviders(ctx, dealProvider)
if !found {
return nil, sdkerrors.Wrapf(types.ErrProviderNotFound, "cannot find provider matching deal")
}
providers := k.GetActiveProviders(ctx, provider.Ip) // get a random list of active providers
params := k.GetParams(ctx)
if len(providers) < int(params.AttestFormSize) {
return nil, sdkerrors.Wrapf(types.ErrInvalidLengthQuery, "not enough providers online")
}
rand.Seed(ctx.BlockHeight())
attestations := make([]*types.Attestation, params.AttestFormSize)
providerAddresses := make([]string, params.AttestFormSize)
for i := 0; i < int(params.AttestFormSize); i++ {
p := providers[i]
providerAddresses[i] = p.Address
attestations[i] = &types.Attestation{
Provider: p.Address,
Complete: false,
}
}
form := types.AttestationForm{
Attestations: attestations,
Cid: cid,
}
k.SetAttestationForm(ctx, form)
return providerAddresses, nil
}
func (k msgServer) RequestAttestationForm(goCtx context.Context, msg *types.MsgRequestAttestationForm) (*types.MsgRequestAttestationFormResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
cid := msg.Cid
creator := msg.Creator
providerAddresses, err := k.RequestAttestation(ctx, cid, creator)
success := true
errorString := ""
if err != nil {
success = false
errorString = err.Error()
}
return &types.MsgRequestAttestationFormResponse{
Providers: providerAddresses,
Success: success,
Error: errorString,
Cid: cid,
}, nil
}