Skip to content

Commit

Permalink
add multiAddrs for directCommand (#796)
Browse files Browse the repository at this point in the history
* add multiAddrs for directCommand
  • Loading branch information
alexcos20 authored Jan 13, 2025
1 parent bc7edcc commit 0bd7079
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
15 changes: 8 additions & 7 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -501,13 +501,14 @@ returns an empty object if it is valid otherwise an array with error

#### Parameters

| name | type | required | description |
| ---------- | ------ | -------- | ------------------------------------------------- |
| command | string | v | command name |
| node | string | | if not present it means current node |
| id | string | v | document id or did |
| chainId | number | v | chain id of network on which document is provided |
| nftAddress | string | v | address of nft token |
| name | type | required | description |
| ---------- | -------- | -------- | ------------------------------------------------- |
| command | string | v | command name |
| node | string | | if not present it means current node |
| multiAddrs | string[] | | if passed, use this instead of peerStore & DHT |
| id | string | v | document id or did |
| chainId | number | v | chain id of network on which document is provided |
| nftAddress | string | v | address of nft token |

#### Request

Expand Down
17 changes: 14 additions & 3 deletions src/components/P2P/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import { GENERIC_EMOJIS, LOG_LEVELS_STR } from '../../utils/logging/Logger.js'
import { INDEXER_DDO_EVENT_EMITTER } from '../Indexer/index.js'
import { P2P_LOGGER } from '../../utils/logging/common.js'
import { CoreHandlersRegistry } from '../core/handler/coreHandlersRegistry'
import { type Multiaddr, multiaddr } from '@multiformats/multiaddr'
import { Multiaddr, multiaddr } from '@multiformats/multiaddr'
// import { getIPv4, getIPv6 } from '../../utils/ip.js'

const DEFAULT_OPTIONS = {
Expand Down Expand Up @@ -608,7 +608,8 @@ export class OceanP2P extends EventEmitter {
async sendTo(
peerName: string,
message: string,
sink: any
sink: any,
multiAddrs?: string[]
): Promise<P2PCommandResponse> {
P2P_LOGGER.logMessage('SendTo() node ' + peerName + ' task: ' + message, true)

Expand All @@ -630,7 +631,17 @@ export class OceanP2P extends EventEmitter {
response.status.error = 'Invalid peer'
return response
}
const multiaddrs: Multiaddr[] = await this.getPeerMultiaddrs(peerName)
let multiaddrs: Multiaddr[] = []

if (!multiAddrs || multiAddrs.length < 1) {
// if they are no forced multiaddrs, try to find node multiaddr from peerStore/dht
multiaddrs = await this.getPeerMultiaddrs(peerName)
} else {
// just used what we were instructed to use
for (const addr of multiAddrs) {
multiaddrs.push(new Multiaddr(addr))
}
}
if (multiaddrs.length < 1) {
response.status.httpStatus = 404
response.status.error = `Cannot find any address to dial for peer: ${peerId}`
Expand Down
7 changes: 6 additions & 1 deletion src/components/httpRoutes/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,12 @@ directCommandRoute.post(
// send to another peer (Only here we need P2P networking)
response = await req.oceanNode
.getP2PNode()
.sendTo(req.body.node as string, JSON.stringify(req.body), sink)
.sendTo(
req.body.node as string,
JSON.stringify(req.body),
sink,
req.body.multiAddrs
)
} else {
response = {
stream: null,
Expand Down

0 comments on commit 0bd7079

Please sign in to comment.