Skip to content

feat: Add P2P networking capability to shell with Peer struct#21

Merged
mikelsr merged 5 commits intomasterfrom
feat/shell-send
Sep 18, 2025
Merged

feat: Add P2P networking capability to shell with Peer struct#21
mikelsr merged 5 commits intomasterfrom
feat/shell-send

Conversation

@lthibault
Copy link
Copy Markdown
Contributor

@lthibault lthibault commented Sep 17, 2025

Overview

This PR adds P2P networking capability to the wetware shell by introducing a new Peer struct that implements core.Invokable. This enables users to send data to remote peers, connect to peers, and perform peer identity operations directly from the shell.

Changes

New Features

  • --with-p2p flag: New capability flag for P2P networking (follows same pattern as --with-ipfs and --with-exec)
  • Peer struct: Implements core.Invokable with methods:
    • :send - Send data to a peer process
    • :connect - Connect to a peer
    • :is-self - Check if peer ID is our own
    • :id - Get our own peer ID
  • Composable expressions: Support for builtin.String types enables expressions like:
    (peer :send (peer :id) "/ww/0.1.0/crU9ZDuzKWr" "hello, wetware!")

Implementation Details

  • Host management: Creates libp2p host when --with-p2p flag is used
  • Type flexibility: Handles both Go string and builtin.String types
  • Error handling: Proper error messages and type validation
  • Self-routing detection: Identifies when sending to self (currently shows expected error)

Known Issues

  • Sending messages to oneself is not yet supported.

Working Example

# Start shell with P2P capability
ww shell --with-p2p

# Get our peer ID
ww> (peer :id)
"12D3KooW..."

# Check if peer ID is our own
ww> (peer :is-self "12D3KooW...")
true

# Send data to a peer (composable expression)
ww> (peer :send (peer :id) "/ww/0.1.0/crU9ZDuzKWr" "hello, wetware!")

- Add --with-p2p flag to CapabilityFlags() for P2P networking capability
- Update --with-all description to include p2p capability
- Follows same pattern as --with-ipfs and --with-exec flags
- Create Peer struct with Host and Ctx fields for P2P networking
- Implement Invokable interface with methods: :send, :connect, :is-self, :id
- Support both string and builtin.String types for composable expressions
- Add proper error handling and type conversion
- Enable expressions like (peer :send (peer :id) proc-id data)
- Use stretchr/testify for clean, readable assertions
- Test core methods: String, ID, IsSelf, Invoke
- Test builtin.String support for composable expressions
- Cover both success and error cases
- All tests run in parallel for performance
- Replace SendToPeer function with Peer struct in globals
- Add Peer to getBaseGlobals() when --with-p2p flag is used
- Create libp2p host in shell setup for P2P functionality
- Remove old SendToPeer function and unused imports
- Update help message to show new peer API syntax
- Pass --with-p2p flag through in host mode
- Update tests to work with new getBaseGlobals() function
- Add tests for P2P functionality when --with-p2p flag is used
- Update help message tests to include new peer commands
- Ensure all existing functionality continues to work

// Connect establishes a connection to a peer
func (p *Peer) Connect(peerAddr string) (core.Any, error) {
ctx := context.TODO()
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.

One of my personal pain points when working with concurrent applications is them not stopping when ctrl+C'd, I'm leaving this comment here because although I think the Connect call in this function won't cause that issue, I want to remember it's here in case I'm wrong.

@mikelsr mikelsr merged commit 8029549 into master Sep 18, 2025
3 checks passed
@mikelsr mikelsr deleted the feat/shell-send branch September 19, 2025 13:52
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