-
Notifications
You must be signed in to change notification settings - Fork 8
/
msg_server_submit_proof.go
343 lines (297 loc) · 13.7 KB
/
msg_server_submit_proof.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package keeper
// TODO_TECHDEBT(@bryanchriswhite): Replace all logs in x/ from `.Info` to
// `.Debug` when the logger is replaced close to or after MainNet launch.
// Ref: https://github.com/pokt-network/poktroll/pull/448#discussion_r1549742985
import (
"context"
"fmt"
cosmostypes "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/gogoproto/proto"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/pokt-network/poktroll/telemetry"
"github.com/pokt-network/poktroll/x/proof/types"
servicekeeper "github.com/pokt-network/poktroll/x/service/keeper"
sessiontypes "github.com/pokt-network/poktroll/x/session/types"
sharedtypes "github.com/pokt-network/poktroll/x/shared/types"
)
// SubmitProof is the server handler to submit and store a proof on-chain.
// A proof that's stored on-chain is what leads to rewards (i.e. inflation)
// downstream, making this a critical part of the protocol.
//
// Note that the validation of the proof is done in `EnsureValidProof`. However,
// preliminary checks are done in the handler to prevent sybil or DoS attacks on
// full nodes because storing and validating proofs is expensive.
//
// We are playing a balance of security and efficiency here, where enough validation
// is done on proof submission, and exhaustive validation is done during session
// settlement.
//
// The entity sending the SubmitProof messages does not necessarily need
// to correspond to the supplier signing the proof. For example, a single entity
// could (theoretically) batch multiple proofs (signed by the corresponding supplier)
// into one transaction to save on transaction fees.
func (k msgServer) SubmitProof(
ctx context.Context,
msg *types.MsgSubmitProof,
) (_ *types.MsgSubmitProofResponse, err error) {
// Declare claim to reference in telemetry.
var (
claim = new(types.Claim)
isExistingProof bool
numRelays uint64
numClaimComputeUnits uint64
)
logger := k.Logger().With("method", "SubmitProof")
sdkCtx := cosmostypes.UnwrapSDKContext(ctx)
logger.Info("About to start submitting proof")
// Basic validation of the SubmitProof message.
if err = msg.ValidateBasic(); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
logger.Info("validated the submitProof message")
// Compare msg session header w/ on-chain session header.
session, err := k.queryAndValidateSessionHeader(ctx, msg.GetSessionHeader(), msg.GetSupplierOperatorAddress())
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
// Defer telemetry calls so that they reference the final values the relevant variables.
defer k.finalizeSubmitProofTelemetry(session, msg, isExistingProof, numRelays, numClaimComputeUnits, err)
if err = k.deductProofSubmissionFee(ctx, msg.GetSupplierOperatorAddress()); err != nil {
logger.Error(fmt.Sprintf("failed to deduct proof submission fee: %v", err))
return nil, status.Error(codes.FailedPrecondition, err.Error())
}
// Construct the proof
proof := types.Proof{
SupplierOperatorAddress: msg.GetSupplierOperatorAddress(),
SessionHeader: session.GetHeader(),
ClosestMerkleProof: msg.GetProof(),
}
// Helpers for logging the same metadata throughout this function calls
logger = logger.With(
"session_id", proof.SessionHeader.SessionId,
"session_end_height", proof.SessionHeader.SessionEndBlockHeight,
"supplier_operator_address", proof.SupplierOperatorAddress)
// Validate proof message commit height is within the respective session's
// proof submission window using the on-chain session header.
if err = k.validateProofWindow(ctx, proof.SessionHeader, proof.SupplierOperatorAddress); err != nil {
return nil, status.Error(codes.FailedPrecondition, err.Error())
}
// Retrieve the corresponding claim for the proof submitted so it can be
// used in the proof validation below.
claim, err = k.queryAndValidateClaimForProof(ctx, proof.SessionHeader, proof.SupplierOperatorAddress)
if err != nil {
return nil, status.Error(codes.Internal, types.ErrProofClaimNotFound.Wrap(err.Error()).Error())
}
// Check if a proof is required for the claim.
proofRequirement, err := k.ProofRequirementForClaim(ctx, claim)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
if proofRequirement == types.ProofRequirementReason_NOT_REQUIRED {
logger.Warn("trying to submit a proof for a claim that does not require one")
return nil, status.Error(codes.FailedPrecondition, types.ErrProofNotRequired.Error())
}
// Get metadata for the event we want to emit
numRelays, err = claim.GetNumRelays()
if err != nil {
return nil, status.Error(codes.Internal, types.ErrProofInvalidClaimRootHash.Wrap(err.Error()).Error())
}
// DEV_NOTE: It is assumed that numClaimComputeUnits = numRelays * serviceComputeUnitsPerRelay
// has been checked during the claim creation process.
numClaimComputeUnits, err = claim.GetNumClaimedComputeUnits()
if err != nil {
return nil, status.Error(codes.Internal, types.ErrProofInvalidClaimRootHash.Wrap(err.Error()).Error())
}
// Check if a prior proof already exists.
_, isExistingProof = k.GetProof(ctx, proof.SessionHeader.SessionId, proof.SupplierOperatorAddress)
// Upsert the proof
k.UpsertProof(ctx, proof)
logger.Info("successfully upserted the proof")
// Emit the appropriate event based on whether the claim was created or updated.
var proofUpsertEvent proto.Message
switch isExistingProof {
case true:
proofUpsertEvent = proto.Message(
&types.EventProofUpdated{
Claim: claim,
Proof: &proof,
NumRelays: numRelays,
NumClaimedComputeUnits: numClaimComputeUnits,
// TODO_BETA(@red-0ne): Add NumEstimatedComputeUnits and ClaimedAmountUpokt
},
)
case false:
proofUpsertEvent = proto.Message(
&types.EventProofSubmitted{
Claim: claim,
Proof: &proof,
NumRelays: numRelays,
NumClaimedComputeUnits: numClaimComputeUnits,
// TODO_BETA(@red-0ne): Add NumEstimatedComputeUnits and ClaimedAmountUpokt
},
)
}
if err = sdkCtx.EventManager().EmitTypedEvent(proofUpsertEvent); err != nil {
return nil, status.Error(
codes.Internal,
sharedtypes.ErrSharedEmitEvent.Wrapf(
"failed to emit event type %T: %v",
proofUpsertEvent,
err,
).Error(),
)
}
return &types.MsgSubmitProofResponse{
Proof: &proof,
}, nil
}
// deductProofSubmissionFee deducts the proof submission fee from the supplier operator's account balance.
func (k Keeper) deductProofSubmissionFee(ctx context.Context, supplierOperatorAddress string) error {
proofSubmissionFee := k.GetParams(ctx).ProofSubmissionFee
supplierOperatorAccAddress, err := cosmostypes.AccAddressFromBech32(supplierOperatorAddress)
if err != nil {
return err
}
accCoins := k.bankKeeper.SpendableCoins(ctx, supplierOperatorAccAddress)
if accCoins.Len() == 0 {
return types.ErrProofNotEnoughFunds.Wrapf(
"account has no spendable coins",
)
}
// Check the balance of upokt is enough to cover the ProofSubmissionFee.
accBalance := accCoins.AmountOf("upokt")
if accBalance.LTE(proofSubmissionFee.Amount) {
return types.ErrProofNotEnoughFunds.Wrapf(
"account has %s, but the proof submission fee is %s",
accBalance, proofSubmissionFee,
)
}
// Deduct the proof submission fee from the supplier operator's balance.
proofSubmissionFeeCoins := cosmostypes.NewCoins(*proofSubmissionFee)
if err = k.bankKeeper.SendCoinsFromAccountToModule(ctx, supplierOperatorAccAddress, types.ModuleName, proofSubmissionFeeCoins); err != nil {
return types.ErrProofFailedToDeductFee.Wrapf(
"account has %s, failed to deduct %s",
accBalance, proofSubmissionFee,
)
}
return nil
}
// ProofRequirementForClaim checks if a proof is required for a claim.
// If it is not, the claim will be settled without a proof.
// If it is, the claim will only be settled if a valid proof is available.
// TODO_BETA(@olshansk): Document safety assumptions of the probabilistic proofs mechanism.
func (k Keeper) ProofRequirementForClaim(ctx context.Context, claim *types.Claim) (_ types.ProofRequirementReason, err error) {
logger := k.logger.With("method", "proofRequirementForClaim")
var requirementReason = types.ProofRequirementReason_NOT_REQUIRED
// Defer telemetry calls so that they reference the final values the relevant variables.
defer k.finalizeProofRequirementTelemetry(requirementReason, claim, err)
proofParams := k.GetParams(ctx)
sharedParams := k.sharedKeeper.GetParams(ctx)
serviceId := claim.GetSessionHeader().GetServiceId()
relayMiningDifficulty, found := k.serviceKeeper.GetRelayMiningDifficulty(ctx, serviceId)
if !found {
relayMiningDifficulty = servicekeeper.NewDefaultRelayMiningDifficulty(ctx, logger, serviceId, servicekeeper.TargetNumRelays)
}
// Retrieve the number of tokens claimed to compare against the threshold.
// Different services have varying compute_unit -> token multipliers, so the
// threshold value is done in a common unit denomination.
claimeduPOKT, err := claim.GetClaimeduPOKT(sharedParams, relayMiningDifficulty)
if err != nil {
return requirementReason, err
}
// Require a proof if the claim's compute units meets or exceeds the threshold.
// TODO_BETA(@red-0ne): Should the threshold be dependant on the stake as well
// so we slash proportional to the compute units?
// TODO_BETA(@red-0ne): It might make sense to include whether there was a proof
// submission error downstream from here. This would require a more comprehensive metrics API.
if claimeduPOKT.Amount.GTE(proofParams.GetProofRequirementThreshold().Amount) {
requirementReason = types.ProofRequirementReason_THRESHOLD
logger.Info(fmt.Sprintf(
"claim requires proof due to claimed tokens (%s) exceeding threshold (%s)",
claimeduPOKT,
proofParams.GetProofRequirementThreshold(),
))
return requirementReason, nil
}
// Hash of block when proof submission is allowed.
proofRequirementSeedBlockHash, err := k.getProofRequirementSeedBlockHash(ctx, claim)
if err != nil {
return requirementReason, err
}
// The probability that a proof is required.
proofRequirementSampleValue, err := claim.GetProofRequirementSampleValue(proofRequirementSeedBlockHash)
if err != nil {
return requirementReason, err
}
// Require a proof probabilistically based on the proof_request_probability param.
// NB: A random value between 0 and 1 will be less than or equal to proof_request_probability
// with probability equal to the proof_request_probability.
if proofRequirementSampleValue <= proofParams.GetProofRequestProbability() {
requirementReason = types.ProofRequirementReason_PROBABILISTIC
logger.Info(fmt.Sprintf(
"claim requires proof due to random sample (%.2f) being less than or equal to probability (%.2f)",
proofRequirementSampleValue,
proofParams.GetProofRequestProbability(),
))
return requirementReason, nil
}
logger.Info(fmt.Sprintf(
"claim does not require proof due to claimed amount (%s) being less than the threshold (%s) and random sample (%.2f) being greater than probability (%.2f)",
claimeduPOKT,
proofParams.GetProofRequirementThreshold(),
proofRequirementSampleValue,
proofParams.GetProofRequestProbability(),
))
return requirementReason, nil
}
// getProofRequirementSeedBlockHash returns the block hash of the seed block for
// the proof requirement probabilistic check.
func (k Keeper) getProofRequirementSeedBlockHash(
ctx context.Context,
claim *types.Claim,
) (blockHash []byte, err error) {
sharedParams, err := k.sharedQuerier.GetParams(ctx)
if err != nil {
return nil, err
}
sessionEndHeight := claim.GetSessionHeader().GetSessionEndBlockHeight()
supplierOperatorAddress := claim.GetSupplierOperatorAddress()
proofWindowOpenHeight := sharedtypes.GetProofWindowOpenHeight(sharedParams, sessionEndHeight)
proofWindowOpenBlockHash := k.sessionKeeper.GetBlockHash(ctx, proofWindowOpenHeight)
// TODO_BETA(@red-0ne): Update the method header of this function to accept (sharedParams, Claim, BlockHash).
// After doing so, please review all calling sites and simplify them accordingly.
earliestSupplierProofCommitHeight := sharedtypes.GetEarliestSupplierProofCommitHeight(
sharedParams,
sessionEndHeight,
proofWindowOpenBlockHash,
supplierOperatorAddress,
)
// The proof requirement seed block is the last block of the session, and it is
// the block that is before the earliest block at which a proof can be committed.
return k.sessionKeeper.GetBlockHash(ctx, earliestSupplierProofCommitHeight-1), nil
}
// finalizeSubmitProofTelemetry finalizes telemetry updates for SubmitProof, incrementing counters as needed.
// Meant to run deferred.
func (k msgServer) finalizeSubmitProofTelemetry(session *sessiontypes.Session, msg *types.MsgSubmitProof, isExistingProof bool, numRelays, numClaimComputeUnits uint64, err error) {
if !isExistingProof {
serviceId := session.Header.ServiceId
applicationAddress := session.Header.ApplicationAddress
supplierOperatorAddress := msg.GetSupplierOperatorAddress()
claimProofStage := types.ClaimProofStage_PROVEN.String()
telemetry.ClaimCounter(claimProofStage, 1, serviceId, applicationAddress, supplierOperatorAddress, err)
telemetry.ClaimRelaysCounter(claimProofStage, numRelays, serviceId, applicationAddress, supplierOperatorAddress, err)
telemetry.ClaimComputeUnitsCounter(claimProofStage, numClaimComputeUnits, serviceId, applicationAddress, supplierOperatorAddress, err)
}
}
// finalizeProofRequirementTelemetry finalizes telemetry updates for proof requirements.
// Meant to run deferred.
func (k Keeper) finalizeProofRequirementTelemetry(requirementReason types.ProofRequirementReason, claim *types.Claim, err error) {
telemetry.ProofRequirementCounter(
requirementReason.String(),
claim.SessionHeader.ServiceId,
claim.SessionHeader.ApplicationAddress,
claim.SupplierOperatorAddress,
err,
)
}