Skip to content

Modified Peer payload to send taker infos about maker#99

Merged
grunch merged 2 commits into
developfrom
feature-taker-info-to-maker
Apr 4, 2025
Merged

Modified Peer payload to send taker infos about maker#99
grunch merged 2 commits into
developfrom
feature-taker-info-to-maker

Conversation

@arkanoider

@arkanoider arkanoider commented Apr 3, 2025

Copy link
Copy Markdown
Collaborator

@grunch @Catrya

this is another update on mostro-core.

I added an optional field in Peer payload to inform maker about taker reputation, as said if it's a privacy user all values will be 0.

pub struct Peer {
    pub pubkey: String,
    pub reputation: Option<UserDisputeInfo>,
}

Let me know if you like it @grunch ...

Summary by CodeRabbit

  • New Features
    • Enhanced peer profiles now support optional reputation details to provide additional context during dispute resolution.
  • Bug Fixes
    • Improved unit tests for the Peer struct to ensure proper functionality with and without the new reputation field.

@coderabbitai

coderabbitai Bot commented Apr 3, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

The changes modify the Peer struct in src/message.rs by adding a new optional field reputation of type Option<UserDisputeInfo>. The constructor method new is updated to accept an additional parameter for this field, alongside the existing pubkey parameter. Additionally, a test case in src/lib.rs has been updated to instantiate the Peer object with the new optional parameter set to None. This ensures that the functionality of the Peer struct is validated both with and without the reputation field.

Changes

File Change Summary
src/message.rs Added pub reputation: Option<UserDisputeInfo> to Peer struct; updated new method signature to accept (pubkey: String, reputation: Option<UserDisputeInfo>).
src/lib.rs Updated test_message_payload_signature to instantiate Peer with an additional None parameter in the new method call.

Sequence Diagram(s)

sequenceDiagram
    participant App as Application
    participant Peer as Peer Struct
    App->>Peer: new(pubkey, reputation)
    Note right of Peer: Initialize pubkey and reputation fields
    Peer-->>App: Return new Peer instance
Loading

Poem

I hopped through code with a joyful bounce,
Adding fields that make the struct renounce,
Reputation now at my side,
In every dispute logic I confide,
Here’s to the changes, with a hop and a pounce!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@grunch

grunch commented Apr 3, 2025

Copy link
Copy Markdown
Member

@coderabbitai can you add some tests for this?

@coderabbitai

coderabbitai Bot commented Apr 3, 2025

Copy link
Copy Markdown
Contributor

Tip

For best results, initiate chat on the files or code changes.

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# First, get an overview of the repo structure
find . -type d -not -path "*/\.*" | sort

# Then, check the implementation of message.rs to understand the Peer struct
cat src/message.rs

# Look for existing tests related to the Peer struct
rg -l "Peer" --type rust tests/

# Look for UserDisputeInfo definition
rg -l "UserDisputeInfo" --type rust

Length of output: 12472


🏁 Script executed:

#!/bin/bash
# Look at the dispute.rs file to see UserDisputeInfo definition
cat src/dispute.rs

# Check if there are tests defined elsewhere
find . -name "*test*.rs" -o -name "*spec*.rs"

# Check the cargo.toml file for test configurations
cat Cargo.toml

Length of output: 8784


🏁 Script executed:

#!/bin/bash
# Look for inline tests in the message.rs file
grep -n "#\[cfg(test)" src/message.rs

# Let's check if there are any test modules or test functions in the project
rg -n "#\[cfg\(test\)\]" --type rust

# Look for any test functions in the codebase
rg -n "#\[test\]" --type rust

Length of output: 295


🏁 Script executed:

#!/bin/bash
# Let's look at the test module in lib.rs to understand the testing patterns
cat src/lib.rs

Length of output: 7586


@grunch I'll create tests for the changes made to the Peer struct. Here are tests that cover the new functionality:

#[test]
fn test_peer_with_reputation() {
    // Test creating a Peer with reputation information
    let reputation = UserDisputeInfo {
        rating: 4.5,
        reviews: 10,
        operating_days: 30,
    };
    let peer = Peer::new(
        "npub1testjsf0runcqdht5apkfcalajxkf8txdxqqk5kgm0agc38ke4vsfsgzf8".to_string(),
        Some(reputation.clone()),
    );
    
    // Assert the fields are set correctly
    assert_eq!(peer.pubkey, "npub1testjsf0runcqdht5apkfcalajxkf8txdxqqk5kgm0agc38ke4vsfsgzf8");
    assert!(peer.reputation.is_some());
    let peer_reputation = peer.reputation.unwrap();
    assert_eq!(peer_reputation.rating, 4.5);
    assert_eq!(peer_reputation.reviews, 10);
    assert_eq!(peer_reputation.operating_days, 30);
    
    // Test JSON serialization and deserialization
    let json = peer.as_json().unwrap();
    let deserialized_peer = Peer::from_json(&json).unwrap();
    assert_eq!(deserialized_peer.pubkey, peer.pubkey);
    assert!(deserialized_peer.reputation.is_some());
    let deserialized_reputation = deserialized_peer.reputation.unwrap();
    assert_eq!(deserialized_reputation.rating, 4.5);
    assert_eq!(deserialized_reputation.reviews, 10);
    assert_eq!(deserialized_reputation.operating_days, 30);
}

#[test]
fn test_peer_without_reputation() {
    // Test creating a Peer without reputation information
    let peer = Peer::new(
        "npub1testjsf0runcqdht5apkfcalajxkf8txdxqqk5kgm0agc38ke4vsfsgzf8".to_string(),
        None,
    );
    
    // Assert the reputation field is None
    assert_eq!(peer.pubkey, "npub1testjsf0runcqdht5apkfcalajxkf8txdxqqk5kgm0agc38ke4vsfsgzf8");
    assert!(peer.reputation.is_none());
    
    // Test JSON serialization and deserialization
    let json = peer.as_json().unwrap();
    let deserialized_peer = Peer::from_json(&json).unwrap();
    assert_eq!(deserialized_peer.pubkey, peer.pubkey);
    assert!(deserialized_peer.reputation.is_none());
}

#[test]
fn test_peer_in_message() {
    let uuid = uuid!("308e1272-d5f4-47e6-bd97-3504baea9c23");
    
    // Test with reputation
    let reputation = UserDisputeInfo {
        rating: 4.5,
        reviews: 10,
        operating_days: 30,
    };
    let peer_with_reputation = Peer::new(
        "npub1testjsf0runcqdht5apkfcalajxkf8txdxqqk5kgm0agc38ke4vsfsgzf8".to_string(),
        Some(reputation),
    );
    let payload_with_reputation = Payload::Peer(peer_with_reputation);
    let message_with_reputation = Message::Order(MessageKind::new(
        Some(uuid),
        Some(1),
        Some(2),
        Action::FiatSentOk,
        Some(payload_with_reputation),
    ));
    
    // Verify message with reputation
    assert!(message_with_reputation.verify());
    let message_json = message_with_reputation.as_json().unwrap();
    let deserialized_message = Message::from_json(&message_json).unwrap();
    assert!(deserialized_message.verify());
    
    // Test without reputation
    let peer_without_reputation = Peer::new(
        "npub1testjsf0runcqdht5apkfcalajxkf8txdxqqk5kgm0agc38ke4vsfsgzf8".to_string(),
        None,
    );
    let payload_without_reputation = Payload::Peer(peer_without_reputation);
    let message_without_reputation = Message::Order(MessageKind::new(
        Some(uuid),
        Some(1),
        Some(2),
        Action::FiatSentOk,
        Some(payload_without_reputation),
    ));
    
    // Verify message without reputation
    assert!(message_without_reputation.verify());
    let message_json = message_without_reputation.as_json().unwrap();
    let deserialized_message = Message::from_json(&message_json).unwrap();
    assert!(deserialized_message.verify());
}

Also, I need to fix the existing test that uses the old Peer constructor by updating it to use the new signature:

#[test]
fn test_message_payload_signature() {
    let uuid = uuid!("308e1272-d5f4-47e6-bd97-3504baea9c23");
    let peer = Peer::new(
        "npub1testjsf0runcqdht5apkfcalajxkf8txdxqqk5kgm0agc38ke4vsfsgzf8".to_string(),
        None, // Add None for the reputation parameter
    );
    let payload = Payload::Peer(peer);
    let test_message = Message::Order(MessageKind::new(
        Some(uuid),
        Some(1),
        Some(2),
        Action::FiatSentOk,
        Some(payload),
    ));
    assert!(test_message.verify());
    let test_message_json = test_message.as_json().unwrap();
    // Message should be signed with the trade keys
    let trade_keys =
        Keys::parse("110e43647eae221ab1da33ddc17fd6ff423f2b2f49d809b9ffa40794a2ab996c")
            .unwrap();
    let sig = Message::sign(test_message_json.clone(), &trade_keys);

    assert!(Message::verify_signature(
        test_message_json,
        trade_keys.public_key(),
        sig
    ));
}

These tests cover:

  1. Creating a Peer with and without reputation
  2. JSON serialization/deserialization of both cases
  3. Using the Peer struct within a Message
  4. Fixing the existing test to work with the updated constructor

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/message.rs (1)

544-572: Duplicate test implementation.

This test duplicates the same test from src/lib.rs. While not causing immediate issues, maintaining duplicate test cases across different files could lead to maintenance problems if one is updated but the other isn't.

Consider either:

  1. Removing this duplicate test since it's already covered in src/lib.rs
  2. Extracting common test logic into a helper function used by both test modules
  3. Adding a comment explaining why the duplication exists
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e49f6ba and 7a083fd.

📒 Files selected for processing (2)
  • src/lib.rs (1 hunks)
  • src/message.rs (3 hunks)
🧰 Additional context used
🧬 Code Definitions (1)
src/message.rs (2)
src/dispute.rs (2)
  • new (125-186)
  • new (190-202)
src/rating.rs (1)
  • new (16-30)
🔇 Additional comments (5)
src/lib.rs (1)

118-121: Updated Peer constructor call to accommodate new reputation parameter.

The constructor call now includes None for the reputation parameter, aligning with the modified signature in the Peer struct.

src/message.rs (4)

2-2: Import added for UserDisputeInfo.

Correctly imports the UserDisputeInfo struct from the dispute module to support the new reputation field.


24-25: Added reputation field to Peer struct.

The new optional field allows for storing reputation information about users. Using Option<UserDisputeInfo> is appropriate as it makes the field optional, maintaining backward compatibility with existing code.


29-31: Updated Peer constructor to handle reputation information.

The constructor signature has been properly updated to accommodate the new optional reputation parameter, maintaining the structure of the original method.


430-543: Test coverage added for new reputation functionality.

Comprehensive tests cover:

  1. Creating a Peer with reputation
  2. Creating a Peer without reputation
  3. Using Peers in Messages with and without reputation
  4. JSON serialization/deserialization

The tests are well-designed and thoroughly validate the new functionality.

@arkanoider

Copy link
Copy Markdown
Collaborator Author

@grunch

fixed tests and added new one suggested by rabbit.

@grunch grunch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

utACK

@grunch grunch merged commit ad07d70 into develop Apr 4, 2025
@grunch grunch deleted the feature-taker-info-to-maker branch April 4, 2025 17:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants