-
Notifications
You must be signed in to change notification settings - Fork 1
audit: implement LEP-6 heal-op lifecycle and recheck handlers #120
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,289 @@ | ||
| package keeper | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| errorsmod "cosmossdk.io/errors" | ||
| sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
|
||
| "github.com/LumeraProtocol/lumera/x/audit/v1/types" | ||
| ) | ||
|
|
||
| func (m msgServer) SubmitStorageRecheckEvidence(ctx context.Context, req *types.MsgSubmitStorageRecheckEvidence) (*types.MsgSubmitStorageRecheckEvidenceResponse, error) { | ||
| if req == nil { | ||
| return nil, errorsmod.Wrap(types.ErrInvalidSigner, "empty request") | ||
| } | ||
| if req.Creator == "" { | ||
| return nil, errorsmod.Wrap(types.ErrInvalidSigner, "creator is required") | ||
| } | ||
| if req.ChallengedSupernodeAccount == "" { | ||
| return nil, errorsmod.Wrap(types.ErrInvalidRecheckEvidence, "challenged_supernode_account is required") | ||
| } | ||
| if req.ChallengedSupernodeAccount == req.Creator { | ||
| return nil, errorsmod.Wrap(types.ErrInvalidRecheckEvidence, "challenged_supernode_account must not equal creator") | ||
| } | ||
| if req.TicketId == "" { | ||
| return nil, errorsmod.Wrap(types.ErrInvalidRecheckEvidence, "ticket_id is required") | ||
| } | ||
| if req.ChallengedResultTranscriptHash == "" { | ||
| return nil, errorsmod.Wrap(types.ErrInvalidRecheckEvidence, "challenged_result_transcript_hash is required") | ||
| } | ||
| if req.RecheckTranscriptHash == "" { | ||
| return nil, errorsmod.Wrap(types.ErrInvalidRecheckEvidence, "recheck_transcript_hash is required") | ||
| } | ||
|
|
||
| sdkCtx := sdk.UnwrapSDKContext(ctx) | ||
| if _, found := m.GetEpochAnchor(sdkCtx, req.EpochId); !found { | ||
| return nil, errorsmod.Wrapf(types.ErrInvalidEpochID, "epoch anchor not found for epoch_id %d", req.EpochId) | ||
| } | ||
|
|
||
| if _, found, err := m.supernodeKeeper.GetSuperNodeByAccount(sdkCtx, req.Creator); err != nil { | ||
| return nil, err | ||
| } else if !found { | ||
| return nil, errorsmod.Wrap(types.ErrReporterNotFound, "creator is not a registered supernode") | ||
| } | ||
| if _, found, err := m.supernodeKeeper.GetSuperNodeByAccount(sdkCtx, req.ChallengedSupernodeAccount); err != nil { | ||
| return nil, err | ||
| } else if !found { | ||
| return nil, errorsmod.Wrap(types.ErrInvalidRecheckEvidence, "challenged_supernode_account is not a registered supernode") | ||
| } | ||
|
|
||
| switch req.RecheckResultClass { | ||
| case types.StorageProofResultClass_STORAGE_PROOF_RESULT_CLASS_PASS, | ||
| types.StorageProofResultClass_STORAGE_PROOF_RESULT_CLASS_HASH_MISMATCH, | ||
| types.StorageProofResultClass_STORAGE_PROOF_RESULT_CLASS_TIMEOUT_OR_NO_RESPONSE, | ||
| types.StorageProofResultClass_STORAGE_PROOF_RESULT_CLASS_OBSERVER_QUORUM_FAIL, | ||
| types.StorageProofResultClass_STORAGE_PROOF_RESULT_CLASS_INVALID_TRANSCRIPT, | ||
| types.StorageProofResultClass_STORAGE_PROOF_RESULT_CLASS_RECHECK_CONFIRMED_FAIL: | ||
| default: | ||
| return nil, errorsmod.Wrap(types.ErrInvalidRecheckEvidence, "recheck_result_class is invalid") | ||
| } | ||
|
|
||
| return nil, errorsmod.Wrap( | ||
| types.ErrNotImplemented, | ||
| "storage recheck submission is not active in the LEP-6 heal-op lifecycle milestone", | ||
| ) | ||
| } | ||
|
|
||
| func (m msgServer) ClaimHealComplete(ctx context.Context, req *types.MsgClaimHealComplete) (*types.MsgClaimHealCompleteResponse, error) { | ||
| if req == nil { | ||
| return nil, errorsmod.Wrap(types.ErrInvalidSigner, "empty request") | ||
| } | ||
| if req.Creator == "" { | ||
| return nil, errorsmod.Wrap(types.ErrInvalidSigner, "creator is required") | ||
| } | ||
| if req.HealOpId == 0 { | ||
| return nil, errorsmod.Wrap(types.ErrHealOpNotFound, "heal_op_id is required") | ||
| } | ||
| if req.TicketId == "" { | ||
| return nil, errorsmod.Wrap(types.ErrHealOpTicketMismatch, "ticket_id is required") | ||
| } | ||
| if req.HealManifestHash == "" { | ||
| return nil, errorsmod.Wrap(types.ErrHealOpInvalidState, "heal_manifest_hash is required") | ||
| } | ||
|
|
||
| sdkCtx := sdk.UnwrapSDKContext(ctx) | ||
| healOp, found := m.GetHealOp(sdkCtx, req.HealOpId) | ||
| if !found { | ||
| return nil, errorsmod.Wrapf(types.ErrHealOpNotFound, "heal op %d not found", req.HealOpId) | ||
| } | ||
| if healOp.TicketId != req.TicketId { | ||
| return nil, errorsmod.Wrapf(types.ErrHealOpTicketMismatch, "ticket_id %q does not match heal op ticket_id %q", req.TicketId, healOp.TicketId) | ||
| } | ||
| if healOp.HealerSupernodeAccount != req.Creator { | ||
| return nil, errorsmod.Wrap(types.ErrHealOpUnauthorized, "creator is not assigned healer for this heal op") | ||
| } | ||
| if healOp.Status != types.HealOpStatus_HEAL_OP_STATUS_SCHEDULED && healOp.Status != types.HealOpStatus_HEAL_OP_STATUS_IN_PROGRESS { | ||
| return nil, errorsmod.Wrapf(types.ErrHealOpInvalidState, "heal op status %s does not accept healer completion claim", healOp.Status.String()) | ||
| } | ||
|
|
||
| healOp.Status = types.HealOpStatus_HEAL_OP_STATUS_HEALER_REPORTED | ||
| healOp.UpdatedHeight = uint64(sdkCtx.BlockHeight()) | ||
| healOp.ResultHash = req.HealManifestHash | ||
| healOp.Notes = appendStorageTruthNote(healOp.Notes, req.Details) | ||
|
|
||
| // Single-node networks may not have verifier assignments; finalize immediately. | ||
| if len(healOp.VerifierSupernodeAccounts) == 0 { | ||
| // Details were already appended above when marking healer-reported. | ||
| if err := m.finalizeHealOp(sdkCtx, healOp, true, req.HealManifestHash, ""); err != nil { | ||
| return nil, err | ||
| } | ||
| sdkCtx.EventManager().EmitEvent( | ||
| sdk.NewEvent( | ||
| types.EventTypeHealOpVerified, | ||
| sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName), | ||
| sdk.NewAttribute(types.AttributeKeyHealOpID, strconv.FormatUint(healOp.HealOpId, 10)), | ||
| sdk.NewAttribute(types.AttributeKeyTicketID, healOp.TicketId), | ||
| sdk.NewAttribute(types.AttributeKeyHealerSupernodeAccount, req.Creator), | ||
| ), | ||
| ) | ||
| return &types.MsgClaimHealCompleteResponse{}, nil | ||
| } | ||
|
|
||
| if err := m.SetHealOp(sdkCtx, healOp); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| sdkCtx.EventManager().EmitEvent( | ||
| sdk.NewEvent( | ||
| types.EventTypeHealOpHealerReported, | ||
| sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName), | ||
| sdk.NewAttribute(types.AttributeKeyHealOpID, strconv.FormatUint(healOp.HealOpId, 10)), | ||
| sdk.NewAttribute(types.AttributeKeyTicketID, healOp.TicketId), | ||
| sdk.NewAttribute(types.AttributeKeyHealerSupernodeAccount, req.Creator), | ||
| sdk.NewAttribute(types.AttributeKeyTranscriptHash, req.HealManifestHash), | ||
| ), | ||
| ) | ||
|
|
||
| return &types.MsgClaimHealCompleteResponse{}, nil | ||
| } | ||
|
|
||
| func (m msgServer) SubmitHealVerification(ctx context.Context, req *types.MsgSubmitHealVerification) (*types.MsgSubmitHealVerificationResponse, error) { | ||
| if req == nil { | ||
| return nil, errorsmod.Wrap(types.ErrInvalidSigner, "empty request") | ||
| } | ||
| if req.Creator == "" { | ||
| return nil, errorsmod.Wrap(types.ErrInvalidSigner, "creator is required") | ||
| } | ||
| if req.HealOpId == 0 { | ||
| return nil, errorsmod.Wrap(types.ErrHealOpNotFound, "heal_op_id is required") | ||
| } | ||
| if req.VerificationHash == "" { | ||
| return nil, errorsmod.Wrap(types.ErrHealOpInvalidState, "verification_hash is required") | ||
| } | ||
|
|
||
| sdkCtx := sdk.UnwrapSDKContext(ctx) | ||
| healOp, found := m.GetHealOp(sdkCtx, req.HealOpId) | ||
| if !found { | ||
| return nil, errorsmod.Wrapf(types.ErrHealOpNotFound, "heal op %d not found", req.HealOpId) | ||
| } | ||
| if healOp.Status != types.HealOpStatus_HEAL_OP_STATUS_HEALER_REPORTED { | ||
| return nil, errorsmod.Wrapf(types.ErrHealOpInvalidState, "heal op status %s does not accept verification", healOp.Status.String()) | ||
| } | ||
| if !containsString(healOp.VerifierSupernodeAccounts, req.Creator) { | ||
| return nil, errorsmod.Wrap(types.ErrHealOpUnauthorized, "creator is not assigned verifier for this heal op") | ||
| } | ||
| if m.HasHealOpVerification(sdkCtx, req.HealOpId, req.Creator) { | ||
| return nil, errorsmod.Wrap(types.ErrHealVerificationExists, "verification already submitted by creator") | ||
| } | ||
|
|
||
| m.SetHealOpVerification(sdkCtx, req.HealOpId, req.Creator, req.Verified) | ||
|
|
||
| verifications, err := m.GetAllHealOpVerifications(sdkCtx, req.HealOpId) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| positive := 0 | ||
| negative := 0 | ||
| for _, verifier := range healOp.VerifierSupernodeAccounts { | ||
| v, ok := verifications[verifier] | ||
| if !ok { | ||
| continue | ||
| } | ||
| if v { | ||
| positive++ | ||
| } else { | ||
| negative++ | ||
| } | ||
| } | ||
|
|
||
| if negative > 0 { | ||
| if err := m.finalizeHealOp(sdkCtx, healOp, false, req.VerificationHash, req.Details); err != nil { | ||
| return nil, err | ||
| } | ||
| sdkCtx.EventManager().EmitEvent( | ||
| sdk.NewEvent( | ||
| types.EventTypeHealOpFailed, | ||
| sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName), | ||
| sdk.NewAttribute(types.AttributeKeyHealOpID, strconv.FormatUint(healOp.HealOpId, 10)), | ||
| sdk.NewAttribute(types.AttributeKeyTicketID, healOp.TicketId), | ||
| sdk.NewAttribute(types.AttributeKeyVerifierSupernodeAccount, req.Creator), | ||
| sdk.NewAttribute(types.AttributeKeyVerified, strconv.FormatBool(req.Verified)), | ||
| ), | ||
| ) | ||
| return &types.MsgSubmitHealVerificationResponse{}, nil | ||
| } | ||
|
|
||
| if positive == len(healOp.VerifierSupernodeAccounts) { | ||
| if err := m.finalizeHealOp(sdkCtx, healOp, true, req.VerificationHash, req.Details); err != nil { | ||
| return nil, err | ||
| } | ||
| sdkCtx.EventManager().EmitEvent( | ||
| sdk.NewEvent( | ||
| types.EventTypeHealOpVerified, | ||
| sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName), | ||
| sdk.NewAttribute(types.AttributeKeyHealOpID, strconv.FormatUint(healOp.HealOpId, 10)), | ||
| sdk.NewAttribute(types.AttributeKeyTicketID, healOp.TicketId), | ||
| sdk.NewAttribute(types.AttributeKeyVerifierSupernodeAccount, req.Creator), | ||
| sdk.NewAttribute(types.AttributeKeyVerificationHash, req.VerificationHash), | ||
| ), | ||
| ) | ||
| return &types.MsgSubmitHealVerificationResponse{}, nil | ||
| } | ||
|
|
||
| return &types.MsgSubmitHealVerificationResponse{}, nil | ||
| } | ||
|
|
||
| func (m msgServer) finalizeHealOp( | ||
| ctx sdk.Context, | ||
| healOp types.HealOp, | ||
| verified bool, | ||
| verificationHash string, | ||
| details string, | ||
| ) error { | ||
| if verified { | ||
| healOp.Status = types.HealOpStatus_HEAL_OP_STATUS_VERIFIED | ||
| } else { | ||
| healOp.Status = types.HealOpStatus_HEAL_OP_STATUS_FAILED | ||
| } | ||
| healOp.UpdatedHeight = uint64(ctx.BlockHeight()) | ||
| if verificationHash != "" { | ||
| healOp.ResultHash = verificationHash | ||
| } | ||
| healOp.Notes = appendStorageTruthNote(healOp.Notes, details) | ||
| if err := m.SetHealOp(ctx, healOp); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| ticketState, found := m.GetTicketDeteriorationState(ctx, healOp.TicketId) | ||
| if !found { | ||
| return nil | ||
| } | ||
| if ticketState.ActiveHealOpId == healOp.HealOpId { | ||
| ticketState.ActiveHealOpId = 0 | ||
| } | ||
| if verified { | ||
| params := m.GetParams(ctx) | ||
| currentEpoch, err := deriveEpochAtHeight(ctx.BlockHeight(), params) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| ticketState.LastHealEpoch = currentEpoch.EpochID | ||
| ticketState.ProbationUntilEpoch = currentEpoch.EpochID + uint64(params.StorageTruthProbationEpochs) | ||
| } | ||
| return m.SetTicketDeteriorationState(ctx, ticketState) | ||
| } | ||
|
|
||
| func containsString(list []string, value string) bool { | ||
| for _, v := range list { | ||
| if v == value { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func appendStorageTruthNote(existing, note string) string { | ||
| note = strings.TrimSpace(note) | ||
| if note == "" { | ||
| return existing | ||
| } | ||
| if existing == "" { | ||
| return note | ||
| } | ||
| return fmt.Sprintf("%s | %s", existing, note) | ||
| } | ||
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EventTypeHealOpHealerReportedemitsAttributeKeyTranscriptHashbut the value isHealManifestHashfromMsgClaimHealComplete. This makes the event payload ambiguous for indexers/consumers (a “transcript” hash vs a “heal manifest” hash are distinct concepts in the API). Consider adding a dedicated attribute key (e.g.,heal_manifest_hash) and emitting that instead of reusingtranscript_hash.