@@ -4,7 +4,7 @@ import { JsonRpcProvider, Signer, ethers } from "ethers";
44import chalk from "chalk" ;
55import { stdin as input , stdout } from "node:process" ;
66import { createInterface } from "readline/promises" ;
7- import { unitsToAmount } from "@oceanprotocol/lib" ;
7+ import { unitsToAmount , ProviderInstance , isP2pUri } from "@oceanprotocol/lib" ;
88import { toBoolean } from "./helpers.js" ;
99
1010async function initializeSigner ( ) {
@@ -36,6 +36,74 @@ export async function createCLI() {
3636 process . exit ( 1 ) ;
3737 }
3838
39+ if ( isP2pUri ( process . env . NODE_URL ) ) {
40+ const extra = process . env . BOOTSTRAP_PEERS ?. split ( "," ) . filter ( Boolean ) || [ ] ;
41+
42+ // Default Ocean bootstrap nodes (must be included explicitly since passing
43+ // bootstrapPeers to setupP2P replaces the built-in defaults)
44+ const oceanDefaults = [
45+ "/dns4/bootstrap1.oncompute.ai/tcp/9001/ws/p2p/16Uiu2HAmLhRDqfufZiQnxvQs2XHhd6hwkLSPfjAQg1gH8wgRixiP" ,
46+ "/dns4/bootstrap2.oncompute.ai/tcp/9001/ws/p2p/16Uiu2HAmHwzeVw7RpGopjZe6qNBJbzDDBdqtrSk7Gcx1emYsfgL4" ,
47+ "/dns4/bootstrap3.oncompute.ai/tcp/9001/ws/p2p/16Uiu2HAmBKSeEP3v4tYEPsZsZv9VELinyMCsrVTJW9BvQeFXx28U" ,
48+ "/dns4/bootstrap4.oncompute.ai/tcp/9001/ws/p2p/16Uiu2HAmSTVTArioKm2wVcyeASHYEsnx2ZNq467Z4GMDU4ErEPom" ,
49+ ] ;
50+
51+ const nodeUrl = process . env . NODE_URL ;
52+ const isFullMultiaddr =
53+ nodeUrl . startsWith ( "/" ) && nodeUrl . includes ( "/p2p/" ) ;
54+ const localPeer = isFullMultiaddr
55+ ? [ nodeUrl ]
56+ : [ `/ip4/127.0.0.1/tcp/9001/ws/p2p/${ nodeUrl } ` ] ;
57+ const bootstrapPeers = [ ...localPeer , ...extra , ...oceanDefaults ] ;
58+ console . log ( chalk . cyan ( "P2P mode detected. Initializing libp2p..." ) ) ;
59+ console . log ( chalk . cyan ( `Bootstrap peers: ${ bootstrapPeers . length } ` ) ) ;
60+
61+ for ( const peer of localPeer ) {
62+ console . log ( chalk . cyan ( ` Local: ${ peer } ` ) ) ;
63+ }
64+ // Allow localhost connections / local nodes
65+ await ProviderInstance . setupP2P ( {
66+ bootstrapPeers,
67+ libp2p : {
68+ connectionGater : {
69+ denyDialMultiaddr : ( ) => false ,
70+ } ,
71+ } ,
72+ } as any ) ;
73+ console . log (
74+ chalk . cyan ( "libp2p node started. Waiting for peer connections..." )
75+ ) ;
76+
77+ // Wait for at least one active P2P connection before running commands
78+ const maxWait = 20_000 ;
79+ const interval = 500 ;
80+ let waited = 0 ;
81+ const libp2p = ( ProviderInstance as any ) . p2pProvider ?. libp2pNode ;
82+ while ( waited < maxWait ) {
83+ const conns = libp2p ?. getConnections ( ) ?. length ?? 0 ;
84+ if ( conns > 0 ) {
85+ console . log (
86+ chalk . green ( `Connected to ${ conns } peer(s) in ${ waited } ms` )
87+ ) ;
88+ break ;
89+ }
90+ await new Promise ( ( r ) => setTimeout ( r , interval ) ) ;
91+ waited += interval ;
92+ if ( waited % 3000 === 0 ) {
93+ console . log (
94+ chalk . yellow ( ` Still waiting for peers... (${ waited / 1000 } s)` )
95+ ) ;
96+ }
97+ }
98+ if ( ( libp2p ?. getConnections ( ) ?. length ?? 0 ) === 0 ) {
99+ console . error (
100+ chalk . red (
101+ `No P2P peers connected after ${ maxWait / 1000 } s. Commands may fail.`
102+ )
103+ ) ;
104+ }
105+ }
106+
39107 const program = new Command ( ) ;
40108
41109 program
@@ -707,12 +775,21 @@ export async function createCLI() {
707775 )
708776 . argument ( "[from]" , "Start time (epoch ms) to get logs from" )
709777 . argument ( "[to]" , "End time (epoch ms) to get logs to" )
710- . argument ( "[maxLogs]" , "Maximum number of logs to retrieve (default: 100, max: 1000)" )
778+ . argument (
779+ "[maxLogs]" ,
780+ "Maximum number of logs to retrieve (default: 100, max: 1000)"
781+ )
711782 . option ( "-o, --output <output>" , "Output directory to save the logs" )
712- . option ( "-l, --last [last]" , "Period of time to get logs from now (in hours)" )
783+ . option (
784+ "-l, --last [last]" ,
785+ "Period of time to get logs from now (in hours)"
786+ )
713787 . option ( "-f, --from [from]" , "Start time (epoch ms) to get logs from" )
714788 . option ( "-t, --to [to]" , "End time (epoch ms) to get logs to" )
715- . option ( "-m, --maxLogs [maxLogs]" , "Maximum number of logs to retrieve (default: 100, max: 1000)" )
789+ . option (
790+ "-m, --maxLogs [maxLogs]" ,
791+ "Maximum number of logs to retrieve (default: 100, max: 1000)"
792+ )
716793 . action ( async ( output , last , from , to , options ) => {
717794 const { signer, chainId } = await initializeSigner ( ) ;
718795 const commands = new Commands ( signer , chainId ) ;
0 commit comments