Skip to content
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

Fraud reporting improvements #1375

Merged
merged 30 commits into from
Oct 10, 2023
Merged

Fraud reporting improvements #1375

merged 30 commits into from
Oct 10, 2023

Conversation

dwasse
Copy link
Collaborator

@dwasse dwasse commented Sep 14, 2023

Description
Various improvements to the Guard's fraud reporting:

  • Only submit state reports on necessary chains
  • Ensure agents are active before processing submitted states
  • Functionalize more contract calls and move them to calls.go
  • Add StateValidationData interface for providing args for functions that require snapshots or attestations
  • Add README for guard
  • Add "Testing Suite" section to agents README

Metadata

Summary by CodeRabbit

  • New Feature: Introduced new functions and interfaces for agent status, state verification, and state reporting. This enhances the monitoring and management of agents across different chains.
  • Refactor: Renamed and refactored several functions in the Guard struct to improve modularity and readability. The changes include renaming handleSnapshot, handleAttestation, and handleReceipt to handleSnapshotAccepted, handleAttestationAccepted, and handleReceiptAccepted respectively.
  • New Feature: Added SnapshotWithMetadata and AttestationWithMetadata types to provide additional metadata for snapshots and attestations.
  • Documentation: Updated README files with instructions on how to run tests and usage details for the new Guard component.

@dwasse dwasse marked this pull request as draft September 14, 2023 20:44
@github-actions github-actions bot added go Pull requests that update Go code size/m labels Sep 14, 2023
@codecov
Copy link

codecov bot commented Sep 14, 2023

Codecov Report

Attention: 115 lines in your changes are missing coverage. Please review.

Comparison is base (98c4d3b) 50.88921% compared to head (1997382) 50.58048%.
Report is 47 commits behind head on master.

Additional details and impacted files
@@                 Coverage Diff                 @@
##              master       #1375         +/-   ##
===================================================
- Coverage   50.88921%   50.58048%   -0.30873%     
===================================================
  Files            353         343         -10     
  Lines          24966       24118        -848     
  Branches         277         277                 
===================================================
- Hits           12705       12199        -506     
+ Misses         10981       10716        -265     
+ Partials        1280        1203         -77     
Flag Coverage Δ
agents 47.32611% <56.43939%> (+0.39376%) ⬆️
cctp-relayer ?
promexporter ?

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
agents/agents/executor/executor_utils.go 42.94479% <100.00000%> (ø)
agents/agents/guard/guard.go 71.38365% <100.00000%> (ø)
agents/agents/guard/fraud.go 51.48515% <78.84615%> (+0.20747%) ⬆️
agents/agents/guard/calls.go 66.66667% <66.66667%> (ø)
agents/types/state_validation_data.go 0.00000% <0.00000%> (ø)

... and 11 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@github-actions github-actions bot added M-contracts Module: Contracts 2-reviewers C-Protocol-Critical PRs that modify protocol-critical code. labels Sep 14, 2023
@trajan0x trajan0x force-pushed the feat/fraud-report-enhancement branch from fe830b9 to 806df4e Compare September 14, 2023 22:56
@github-actions github-actions bot removed M-contracts Module: Contracts 2-reviewers C-Protocol-Critical PRs that modify protocol-critical code. labels Sep 14, 2023
@dwasse dwasse force-pushed the feat/fraud-report-enhancement branch from fed0213 to 13c6cff Compare September 15, 2023 16:26
@dwasse dwasse marked this pull request as ready for review September 15, 2023 18:37
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Sep 15, 2023

Walkthrough

This pull request introduces significant changes to the agent management and state validation logic. It refactors existing functions, introduces new data structures, and enhances modularity. The changes also include improved documentation and testing instructions.

Changes

File(s) Summary
agents/.../executor_utils.go Updated a conditional statement in logToSnapshot function due to a change from field to method.
agents/.../guard/calls.go, agents/.../guard/fraud.go, agents/.../guard/guard.go Introduced new functions and interfaces related to agent status, state verification, and state reporting. Refactored existing functions for better modularity.
agents/.../fraud_test.go Modified the TestFraudulentAttestationOnDestination function in the GuardSuite struct.
agents/.../inbox/parser.go, agents/.../lightinbox/parser.go Changed return types of parsing methods and updated their implementations.
agents/types/state_validation_data.go Introduced two new types: SnapshotWithMetadata and AttestationWithMetadata. These types contain additional metadata for snapshots and attestations.
agents/README.md, agents/.../guard/README.md Added documentation for the Guard component and testing instructions for the agents package.

🐇💻

Code is hopping, changes are popping,

In the land of agents, there's no stopping.

With each pull request, we're improving,

To the rhythm of the code, we're grooving.

So here's to the devs, keep on coding,

On the path of progress, we're forever treading! 🎉🥕


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • Mention @coderabbitai in any review comment for bot assistance.
  • Note: Review comments are made on code diffs or files, not on the PR overview.

Pause Incremental Reviews

  • Insert @coderabbitai: ignore in the PR description to halt the bot's ongoing reviews. Remove the line to resume.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 6

Commits Files that changed from the base of the PR and between 98c4d3b and c8c2b00 commits.
Files selected for processing (8)
  • agents/agents/executor/executor_utils.go (1 hunks)
  • agents/agents/guard/calls.go (1 hunks)
  • agents/agents/guard/fraud.go (9 hunks)
  • agents/agents/guard/fraud_test.go (1 hunks)
  • agents/agents/guard/guard.go (1 hunks)
  • agents/contracts/inbox/parser.go (2 hunks)
  • agents/contracts/lightinbox/parser.go (3 hunks)
  • agents/types/state_validation_data.go (1 hunks)
Files skipped from review due to trivial changes (1)
  • agents/agents/guard/guard.go
Additional comments (Suppressed): 19
agents/agents/guard/fraud_test.go (2)
  • 330-342: The variable fraudAttestation has been renamed to attestationData. Ensure that all references to this variable in the codebase have been updated accordingly.

  • 343-343:

agents/agents/executor/executor_utils.go (1)
  • 68-70: The AgentDomain method is now being called instead of accessing the AgentDomain field directly. Ensure that all references to this field throughout the codebase have been updated to use the new method.
- if snapshotMetadata.Snapshot == nil || snapshotMetadata.AgentDomain == 0 {
+ if snapshotMetadata.Snapshot == nil || snapshotMetadata.AgentDomain() == 0 {
agents/contracts/lightinbox/parser.go (1)
  • 13-16: The return type of ParseAttestationAccepted method has been changed from *types.FraudAttestation to *types.AttestationWithMetadata. Ensure that all calls to this function throughout the codebase have been updated to handle the new return type.
agents/contracts/inbox/parser.go (1)
  • 16-18: The return type of ParseSnapshotAccepted has been changed from *types.FraudSnapshot to *types.SnapshotWithMetadata. Ensure that all calls to this function throughout the codebase have been updated to handle the new return type.
agents/agents/guard/calls.go (3)
  • 44-83: This function verifies a state on a given chain. However, there is no check for whether the state actually exists on the chain or not before verifying it. It would be better to add a check for the existence of the state on the chain before proceeding with the verification.

  • 93-146: Similar to the previous comment, this function submits a state report to a given chain. But there is no check for whether the state actually exists on the chain or not before submitting the report. It would be better to add a check for the existence of the state on the chain before proceeding with the submission.

  • 192-210: This function relays an Active agent status from Summit to a remote chain where the agent is unknown. However, there is no validation that the agent is indeed active on the Summit before relaying the status. It would be better to add a check to confirm the agent's status on the Summit before relaying it.

agents/types/state_validation_data.go (4)
  • 1-24: The StateValidationData interface is well defined and provides a clear contract for any type that needs to provide state validation data. This will make it easier to add new types of state validation data in the future.

  • 26-29: The HasAttestation function is a good utility function that makes it easy to check if a given StateValidationData instance corresponds to an attestation or not. However, it's important to ensure that this function is used correctly throughout the codebase to avoid potential issues.

  • 31-85: The SnapshotWithMetadata struct and its methods are well implemented. The use of a constructor function (NewSnapshotWithMetadata) ensures that all necessary fields are set before the struct can be used, which helps prevent potential bugs due to uninitialized fields. The error handling in the constructor function is also well done.

  • 87-147: Similar to SnapshotWithMetadata, the AttestationWithMetadata struct and its methods are well implemented. The constructor function (NewAttestationWithMetadata) ensures that all necessary fields are set before the struct can be used. The addition of the SetSnapshotPayload method allows for the snapshot payload to be set separately, which could be useful in certain scenarios. However, it's important to ensure that this method is used correctly to avoid potential issues.

agents/agents/guard/fraud.go (7)
  • 19-30: The function handleSnapshotAccepted has been refactored to call another function handleSnapshot. Ensure that the logic in handleSnapshot is equivalent to the old code in handleSnapshotAccepted.

  • 76-89: The function handleAttestationAccepted has been refactored to call other functions handleValidAttestation and handleInvalidAttestation. Ensure that the logic in these new functions is equivalent to the old code in handleAttestationAccepted.

  • 109-117: The function handleValidAttestation has been refactored to call another function handleSnapshot. Ensure that the logic in handleSnapshot is equivalent to the old code in handleValidAttestation.

  • 141-178: The function handleSnapshot seems to have been significantly refactored. Ensure that the new logic correctly implements the intended functionality and does not introduce any regressions.

  • 198-217: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [182-217]

The function handleInvalidAttestation seems to have been significantly refactored. Ensure that the new logic correctly implements the intended functionality and does not introduce any regressions.

  • 232-235: The function handleReceiptAccepted has been renamed from handleReceipt. Ensure that all calls to this function throughout the codebase have been updated to match the new name.

  • 396-398: The condition if chainID == g.summitDomainID has been added. Ensure that this condition is correct and does not alter the intended functionality.

agents/contracts/lightinbox/parser.go Show resolved Hide resolved
agents/contracts/inbox/parser.go Show resolved Hide resolved
agents/agents/guard/calls.go Outdated Show resolved Hide resolved
agents/agents/guard/calls.go Outdated Show resolved Hide resolved
agents/agents/guard/fraud.go Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 1

Commits Files that changed from the base of the PR and between c8c2b00 and 76ff0a4 commits.
Files selected for processing (1)
  • agents/contracts/lightinbox/parser.go (2 hunks)
Additional comments (Suppressed): 1
agents/contracts/lightinbox/parser.go (1)
  • 13-16: The return type of ParseAttestationAccepted method has been changed from *types.FraudAttestation to *types.AttestationWithMetadata. Ensure that all calls to this function throughout the codebase have been updated to handle the new return type.

agents/contracts/lightinbox/parser.go Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 2

Commits Files that changed from the base of the PR and between 76ff0a4 and 83f2ec3 commits.
Files selected for processing (1)
  • agents/agents/guard/calls.go (1 hunks)
Additional comments (Suppressed): 6
agents/agents/guard/calls.go (6)
  • 16-19: The agentStatusContract interface is introduced to abstract the retrieval of agent status. This is a good practice as it allows for easier testing and decouples the implementation details from the usage.

  • 43-84: The verifyState function verifies a state on a given chain. It checks whether the agent that provided the snapshot is active on origin before submitting the transaction. This is a good practice as it ensures only valid agents can submit transactions.

  • 86-91: The stateReportContract interface is introduced to abstract the submission of state reports. This is a good practice as it allows for easier testing and decouples the implementation details from the usage.

  • 93-146: The submitStateReport function submits a state report to a given chain. It also ensures that the agent that provided the snapshot is active on the agent's respective domain before submitting the transaction. This is a good practice as it ensures only valid agents can submit transactions.

  • 165-198: The ensureAgentActive function checks if the given agent is in a slashable status (Active or Unstaking), and relays the agent status from Summit to the given chain if necessary. This is a good practice as it ensures only valid agents can submit transactions.

  • 200-218: The relayAgentStatus function relays an Active agent status from Summit to a remote chain where the agent is unknown. It's a good practice to handle such cases, ensuring that the agent status is updated across chains.

agents/agents/guard/calls.go Outdated Show resolved Hide resolved
agents/agents/guard/calls.go Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 2

Commits Files that changed from the base of the PR and between 83f2ec3 and 3eccc29 commits.
Files selected for processing (1)
  • agents/agents/guard/fraud.go (9 hunks)
Additional comments (Suppressed): 7
agents/agents/guard/fraud.go (7)
  • 15-30: The function handleSnapshotAccepted has been refactored to separate the parsing of the snapshot and handling it into two different steps. This improves readability and maintainability of the code.

  • 32-49: The function getStateReportChains is introduced to determine which chains need to receive the state report. This is a good practice as it separates concerns and makes the code more modular.

  • 76-89: The function handleAttestationAccepted has been refactored similar to handleSnapshotAccepted. The changes improve readability and maintainability of the code.

  • 123-191: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [105-138]

The function handleValidAttestation has been refactored to separate the fetching of the attested snapshot and handling it into two different steps. This improves readability and maintainability of the code.

  • 141-178: The function handleSnapshot has been introduced to handle a snapshot by validating each state in the snapshot. If an invalid state is found, it initiates slashing and submits state reports on eligible chains. This is a good practice as it separates concerns and makes the code more modular.

  • 198-217: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [180-217]

The function handleInvalidAttestation has been refactored to separate the initiation of slashing for invalid attestation and submitting a fraud report into two different steps. This improves readability and maintainability of the code.

  • 229-235: The function handleReceiptAccepted has been renamed from handleReceipt. This change improves the clarity of the function's purpose.

agents/agents/guard/fraud.go Show resolved Hide resolved
agents/agents/guard/fraud.go Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 4

Commits Files that changed from the base of the PR and between 3eccc29 and 82db3d8 commits.
Files selected for processing (1)
  • agents/agents/guard/calls.go (1 hunks)

agents/agents/guard/calls.go Show resolved Hide resolved
agents/agents/guard/calls.go Show resolved Hide resolved
agents/agents/guard/calls.go Show resolved Hide resolved
agents/agents/guard/calls.go Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Commits Files that changed from the base of the PR and between 82db3d8 and 61b4492 commits.
Files selected for processing (2)
  • agents/README.md (1 hunks)
  • agents/agents/guard/README.md (1 hunks)
Files skipped from review due to trivial changes (2)
  • agents/README.md
  • agents/agents/guard/README.md

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 1

Commits Files that changed from the base of the PR and between 61b4492 and 1997382 commits.
Files selected for processing (1)
  • agents/agents/guard/README.md (1 hunks)

Comment on lines +1 to +37
# Guard
The Guard is an agent responsible for verifying actions from other Agents in the optimistic messaging model. This includes polling for invalid states and attestations as well as submitting fraud reports.

## Components
The Guard operates with four main components:
### Run
`streamLogs` is the data-getter for the Guard. It works by instantiating a gRPC connection to Scribe, and puts logs in a channel for the origin and destination contracts on each chain in the config. From here, it verifies the logs' order since the order of logs are very important for merkle tree construction.
<br /> Additionally, if the Guard unexpectedly stops in the middle of streaming logs, it will use the current database state to reconstruct the tree up to where the last log was, then continue to use gRPC.
<br /> <br > `receiveLogs` is the data-processor for the Guard. It works by taking the logs streamed from `streamLogs` and parsing the logs into either a `Message` on the `Origin.sol` contract, or a `Attestation` on the `Destination.sol` contract. It then stores the data into the Guard's database and builds the tree accordingly.
<br /> <br > `loadOriginLatestStates` polls Origin states and caches them in order to make the latest data available.
<br /> <br > `submitLatestSnapshot` fetches the latest snapshot from Origin and submits it on Summit.
<br /> <br > `updateAgentStatuses` polls the database for `RelayableAgentStatus` entries and calls `updateAgentStatus()` once a sufficient agent root is passed to the given remote chain.

### Fraud Reporting
The fraud reporting logic can be found in `fraud.go`, which consists mostly of handlers for various logs. The two major handlers are `handleSnapshotAccepted` and `handleAttestationAccepted`, both of which verify states corresponding to the incoming snapshot/attestation, initiate slashing if applicable, and submit state reports to eligible chains.

## Usage

Navigate to `sanguine/agents/agents/guard/main` and run the following command to start the Guard:

```bash
$ go run main.go
```
Then the Guard command line will be exposed. The Guard requires a gRPC connection to a Scribe instance to stream logs. This can be done with either a remote Scribe or an embedded Scribe.

For more information on how to interact with Scribe see the Executor README.

## Directory Structure

<pre>
Guard
├── <a href="./api">main</a>: API server
├── <a href="./cmd">cmd</a>: CLI commands
├── <a href="./db">db</a>: Database interface
│ └── <a href="db/sql">sql</a>: Database writer, reader, and migrations
├── <a href="./main">main</a>: CLI entrypoint
</pre>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The README is well written and provides a clear overview of the Guard component, its main functions, usage instructions, and directory structure. It would be helpful to include a brief explanation of the new data structures SnapshotWithMetadata and AttestationWithMetadata that have been introduced in this PR.

Copy link
Contributor

@CryptoMaxPlanck CryptoMaxPlanck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 🔥 🚒 🧯 🧑‍🚒

### Run
`streamLogs` is the data-getter for the Guard. It works by instantiating a gRPC connection to Scribe, and puts logs in a channel for the origin and destination contracts on each chain in the config. From here, it verifies the logs' order since the order of logs are very important for merkle tree construction.
<br /> Additionally, if the Guard unexpectedly stops in the middle of streaming logs, it will use the current database state to reconstruct the tree up to where the last log was, then continue to use gRPC.
<br /> <br > `receiveLogs` is the data-processor for the Guard. It works by taking the logs streamed from `streamLogs` and parsing the logs into either a `Message` on the `Origin.sol` contract, or a `Attestation` on the `Destination.sol` contract. It then stores the data into the Guard's database and builds the tree accordingly.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Builds the tree accordingly is a bit confusing here, since Executor builds a real Merkle tree for submitting proofs, but the Guard does more of gets for tree states (at least now)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ooh i like this file haha

@github-actions
Copy link

github-actions bot commented Oct 5, 2023

This PR is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot added the Stale label Oct 5, 2023
@github-actions github-actions bot closed this Oct 10, 2023
@aureliusbtc aureliusbtc reopened this Oct 10, 2023
@dwasse dwasse merged commit cd4aaef into master Oct 10, 2023
14 checks passed
@dwasse dwasse deleted the feat/fraud-report-enhancement branch October 10, 2023 22:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
go Pull requests that update Go code size/m Stale
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fraud reporting enhancements
3 participants