@@ -1084,6 +1084,7 @@ async fn main() -> Result<()> {
10841084 // Also handles dynamic route discovery via /.well-known/routes
10851085 if let ( Some ( mut rx) , Some ( orch) ) = ( orchestrator_cmd_rx, challenge_orchestrator. clone ( ) ) {
10861086 let routes_map = rpc_handler. as_ref ( ) . map ( |h| h. challenge_routes . clone ( ) ) ;
1087+ let endpoints_for_orch = challenge_endpoints. clone ( ) ;
10871088 tokio:: spawn ( async move {
10881089 info ! ( "Orchestrator command handler started (with route discovery)" ) ;
10891090 while let Some ( cmd) = rx. recv ( ) . await {
@@ -1095,13 +1096,26 @@ async fn main() -> Result<()> {
10951096 } else {
10961097 info ! ( "Challenge container '{}' started successfully" , config. name) ;
10971098
1099+ // Get actual endpoint from orchestrator and update the endpoints map
1100+ let endpoint = if let Some ( instance) = orch. get_challenge ( & config. challenge_id ) {
1101+ instance. endpoint . clone ( )
1102+ } else {
1103+ // Fallback to constructed URL if instance not found
1104+ let container_name = config. name . to_lowercase ( ) . replace ( ' ' , "-" ) ;
1105+ format ! ( "http://challenge-{}:8080" , container_name)
1106+ } ;
1107+
1108+ // Update endpoints map for HTTP proxying (store by both UUID and name)
1109+ {
1110+ let mut eps = endpoints_for_orch. write ( ) ;
1111+ eps. insert ( config. challenge_id . to_string ( ) , endpoint. clone ( ) ) ;
1112+ eps. insert ( config. name . clone ( ) , endpoint. clone ( ) ) ;
1113+ }
1114+ info ! ( "Updated endpoint for challenge '{}' ({}): {}" , config. name, config. challenge_id, endpoint) ;
1115+
10981116 // Discover routes from the container via /.well-known/routes
10991117 if let Some ( ref routes) = routes_map {
1100- let container_name = config. name . to_lowercase ( ) . replace ( ' ' , "-" ) ;
1101- let routes_url = format ! (
1102- "http://challenge-{}:8080/.well-known/routes" ,
1103- container_name
1104- ) ;
1118+ let routes_url = format ! ( "{}/.well-known/routes" , endpoint) ;
11051119
11061120 // Wait a bit for container to be ready
11071121 tokio:: time:: sleep ( std:: time:: Duration :: from_secs ( 3 ) ) . await ;
@@ -1233,9 +1247,10 @@ async fn main() -> Result<()> {
12331247
12341248 // Spawn startup route discovery task for pre-existing challenges
12351249 // This runs after RPC is ready and discovers routes from containers started at sync
1236- if let ( Some ( ref handler) , Some ( ref _orch ) ) = ( & rpc_handler, & challenge_orchestrator) {
1250+ if let ( Some ( ref handler) , Some ( ref orch ) ) = ( & rpc_handler, & challenge_orchestrator) {
12371251 let routes_map = handler. challenge_routes . clone ( ) ;
12381252 let state_for_discovery = chain_state. clone ( ) ;
1253+ let orch_for_discovery = orch. clone ( ) ;
12391254 tokio:: spawn ( async move {
12401255 // Wait for containers to be fully started
12411256 tokio:: time:: sleep ( std:: time:: Duration :: from_secs ( 5 ) ) . await ;
@@ -1246,11 +1261,14 @@ async fn main() -> Result<()> {
12461261 } ;
12471262
12481263 for config in configs {
1249- let container_name = config. name . to_lowercase ( ) . replace ( ' ' , "-" ) ;
1250- let routes_url = format ! (
1251- "http://challenge-{}:8080/.well-known/routes" ,
1252- container_name
1253- ) ;
1264+ // Get actual endpoint from orchestrator (includes validator suffix in dev mode)
1265+ let routes_url = if let Some ( instance) = orch_for_discovery. get_challenge ( & config. challenge_id ) {
1266+ format ! ( "{}/.well-known/routes" , instance. endpoint)
1267+ } else {
1268+ // Fallback to constructed URL if instance not found
1269+ let container_name = config. name . to_lowercase ( ) . replace ( ' ' , "-" ) ;
1270+ format ! ( "http://challenge-{}:8080/.well-known/routes" , container_name)
1271+ } ;
12541272
12551273 info ! (
12561274 "Discovering routes for startup challenge '{}'..." ,
@@ -1471,6 +1489,8 @@ async fn main() -> Result<()> {
14711489 let auth_sessions_for_p2p = Some ( container_auth_sessions. clone ( ) ) ;
14721490 // Get challenge_routes Arc for auto-registration when receiving via P2P
14731491 let challenge_routes_for_p2p = rpc_handler. as_ref ( ) . map ( |h| h. challenge_routes . clone ( ) ) ;
1492+ // Get challenge_endpoints Arc for endpoint updates when starting containers from P2P
1493+ let endpoints_for_p2p = Some ( challenge_endpoints. clone ( ) ) ;
14741494 // Get distributed_db for P2P message handling
14751495 let db_for_p2p = Some ( distributed_db. clone ( ) ) ;
14761496 let storage = Arc :: new ( storage) ; // Keep reference for state persistence
@@ -1913,7 +1933,7 @@ async fn main() -> Result<()> {
19131933
19141934 if has_sufficient_stake {
19151935 // Forward all messages to consensus handler
1916- handle_message( & consensus, signed, & chain_state_clone, challenge_orchestrator. as_ref( ) , challenge_routes_for_p2p. as_ref( ) , db_for_p2p. as_ref( ) , keypair_for_p2p. as_ref( ) , auth_sessions_for_p2p. as_ref( ) ) . await ;
1936+ handle_message( & consensus, signed, & chain_state_clone, challenge_orchestrator. as_ref( ) , challenge_routes_for_p2p. as_ref( ) , endpoints_for_p2p . as_ref ( ) , db_for_p2p. as_ref( ) , keypair_for_p2p. as_ref( ) , auth_sessions_for_p2p. as_ref( ) ) . await ;
19171937 } else {
19181938 // Allow Sudo to bypass stake check for bootstrapping and upgrades
19191939 let is_sudo = {
@@ -1923,7 +1943,7 @@ async fn main() -> Result<()> {
19231943
19241944 if is_sudo {
19251945 info!( "Bypassing stake check for Sudo message from {}" , & signer_hex[ ..16 ] ) ;
1926- handle_message( & consensus, signed, & chain_state_clone, challenge_orchestrator. as_ref( ) , challenge_routes_for_p2p. as_ref( ) , db_for_p2p. as_ref( ) , keypair_for_p2p. as_ref( ) , auth_sessions_for_p2p. as_ref( ) ) . await ;
1946+ handle_message( & consensus, signed, & chain_state_clone, challenge_orchestrator. as_ref( ) , challenge_routes_for_p2p. as_ref( ) , endpoints_for_p2p . as_ref ( ) , db_for_p2p. as_ref( ) , keypair_for_p2p. as_ref( ) , auth_sessions_for_p2p. as_ref( ) ) . await ;
19271947 } else {
19281948 warn!(
19291949 "Rejected message from {} - insufficient stake (min {} TAO required)" ,
@@ -2292,6 +2312,7 @@ async fn handle_message(
22922312 challenge_routes : Option <
22932313 & Arc < RwLock < HashMap < String , Vec < platform_challenge_sdk:: ChallengeRoute > > > > ,
22942314 > ,
2315+ challenge_endpoints : Option < & Arc < RwLock < std:: collections:: HashMap < String , String > > > > ,
22952316 distributed_db : Option < & Arc < distributed_db:: DistributedDB > > ,
22962317 keypair : Option < & Keypair > ,
22972318 container_auth_sessions : Option < & Arc < RwLock < HashMap < String , ContainerAuthSession > > > > ,
@@ -2336,6 +2357,16 @@ async fn handle_message(
23362357 error ! ( "Failed to start challenge container: {}" , e) ;
23372358 } else {
23382359 info ! ( "Challenge container started: {}" , config. name) ;
2360+
2361+ // Update endpoints map with actual container endpoint
2362+ if let Some ( endpoints) = challenge_endpoints {
2363+ if let Some ( instance) = orchestrator. get_challenge ( & config. challenge_id ) {
2364+ let mut eps = endpoints. write ( ) ;
2365+ eps. insert ( config. challenge_id . to_string ( ) , instance. endpoint . clone ( ) ) ;
2366+ eps. insert ( config. name . clone ( ) , instance. endpoint . clone ( ) ) ;
2367+ info ! ( "Updated endpoint for challenge '{}': {}" , config. name, instance. endpoint) ;
2368+ }
2369+ }
23392370 }
23402371 }
23412372
@@ -2454,6 +2485,16 @@ async fn handle_message(
24542485 error ! ( "Failed to start challenge container from P2P: {}" , e) ;
24552486 } else {
24562487 info ! ( "Challenge container '{}' started from P2P" , config. name) ;
2488+
2489+ // Update endpoints map with actual container endpoint
2490+ if let Some ( endpoints) = challenge_endpoints {
2491+ if let Some ( instance) = orchestrator. get_challenge ( & config. challenge_id ) {
2492+ let mut eps = endpoints. write ( ) ;
2493+ eps. insert ( config. challenge_id . to_string ( ) , instance. endpoint . clone ( ) ) ;
2494+ eps. insert ( config. name . clone ( ) , instance. endpoint . clone ( ) ) ;
2495+ info ! ( "Updated endpoint for challenge '{}' (P2P): {}" , config. name, instance. endpoint) ;
2496+ }
2497+ }
24572498 }
24582499 }
24592500 }
0 commit comments