Repository: skalenetwork/IMA (language: python)

Issue:
Ticket 871/use local ip for same ima agent
- Use local IP when contacting same IMA Agent in universally written code

Verified on regression network 


Discussion / hints:

Fix patch:
diff --git a/agent/bls.mjs b/agent/bls.mjs
index 61774953b..45c1a1212 100644
--- a/agent/bls.mjs
+++ b/agent/bls.mjs
@@ -1717,9 +1717,11 @@ async function doSignProcessHandleCall(
 }
 
 async function doSignProcessOneImpl( i, optsSignOperation ) {
+    const imaState = state.get();
+    const isThisNode = ( i == imaState.nNodeNumber ) ? true : false;
     const joNode = optsSignOperation.jarrNodes[i];
     const strNodeURL = optsSignOperation.imaState.isCrossImaBlsMode
-        ? imaUtils.composeImaAgentNodeUrl( joNode )
+        ? imaUtils.composeImaAgentNodeUrl( joNode, isThisNode )
         : imaUtils.composeSChainNodeUrl( joNode );
     const strNodeDescColorized = cc.u( strNodeURL ) + " " +
         cc.debug( "(" ) + cc.bright( i ) + cc.debug( "/" ) +
@@ -2010,10 +2012,13 @@ async function prepareSignU256( optsSignU256 ) {
     return true;
 }
 
-async function doSignU256OneImpl( optsSignU256 ) {
+async function doSignU256OneImpl( i, optsSignU256 ) {
+    const imaState = state.get();
+    const isThisNode = ( i == imaState.nNodeNumber ) ? true : false;
     const joNode = optsSignU256.jarrNodes[i];
     const strNodeURL = optsSignU256.imaState.isCrossImaBlsMode
-        ? imaUtils.composeImaAgentNodeUrl( joNode ) : imaUtils.composeSChainNodeUrl( joNode );
+        ? imaUtils.composeImaAgentNodeUrl( joNode, isThisNode )
+        : imaUtils.composeSChainNodeUrl( joNode );
     const strNodeDescColorized = cc.u( strNodeURL ) + " " + cc.debug( "(" ) + cc.bright( i ) +
         cc.debug( "/" ) + cc.bright( optsSignU256.jarrNodes.length ) +
         cc.debug( ", ID " ) + cc.info( joNode.nodeID ) + cc.debug( ")" );
@@ -2429,7 +2434,7 @@ export async function doSignU256( u256, details, fn ) {
     if( ! ( await prepareSignU256( optsSignU256 ) ) )
         return;
     for( let i = 0; i < optsSignU256.jarrNodes.length; ++i )
-        await doSignU256OneImpl( optsSignU256 );
+        await doSignU256OneImpl( i, optsSignU256 );
     await doSignU256Gathering( optsSignU256 );
     if( log.verboseGet() >= log.verboseReversed().trace )
         optsSignU256.details.write( cc.debug( "Will await BLS u256 sign result..." ) + "\n" );
diff --git a/agent/loop.mjs b/agent/loop.mjs
index 64673cbb4..71a1170a6 100644
--- a/agent/loop.mjs
+++ b/agent/loop.mjs
@@ -746,10 +746,8 @@ export async function ensureHaveWorkers( opts ) {
                         "strPathHashG1": opts.imaState.strPathHashG1,
                         "strPathBlsVerify": opts.imaState.strPathBlsVerify,
                         "isEnabledMultiCall": opts.imaState.isEnabledMultiCall,
-
                         "bNoWaitSChainStarted": opts.imaState.bNoWaitSChainStarted,
                         "nMaxWaitSChainAttempts": opts.imaState.nMaxWaitSChainAttempts,
-
                         "nTransferBlockSizeM2S": opts.imaState.nTransferBlockSizeM2S,
                         "nTransferBlockSizeS2M": opts.imaState.nTransferBlockSizeS2M,
                         "nTransferBlockSizeS2S": opts.imaState.nTransferBlockSizeS2S,
@@ -768,7 +766,6 @@ export async function ensureHaveWorkers( opts ) {
                         "nBlockAgeS2S": opts.imaState.nBlockAgeS2S,
 
                         "nLoopPeriodSeconds": opts.imaState.nLoopPeriodSeconds,
-
                         "nNodeNumber": opts.imaState.nNodeNumber,
                         "nNodesCount": opts.imaState.nNodesCount,
                         "nTimeFrameSeconds": opts.imaState.nTimeFrameSeconds,
@@ -781,7 +778,6 @@ export async function ensureHaveWorkers( opts ) {
                         "joDepositBoxERC1155": null,
                         "joDepositBoxERC721WithMetadata": null,
                         "joLinker": null,
-
                         "isWithMetadata721": false,
 
                         "joTokenManagerETH": null,
@@ -841,7 +837,6 @@ export async function ensureHaveWorkers( opts ) {
         while( ! aClient.logicalInitComplete ) {
             if( log.verboseGet() >= log.verboseReversed().info )
                 log.write( "LOOP server is not inited yet...\n" );
-
             await threadInfo.sleep( 1000 );
             aClient.send( jo );
         }
@@ -863,6 +858,15 @@ export async function ensureHaveWorkers( opts ) {
         if( threadInfo.isMainThread() )
             notifyCacheChangedSNB( eventData.detail.arrSChainsCached );
     } );
+    // Force broadcast what we have in SNB right now because works above can start later than SNB
+    // is finished download connected chains quickly
+    if( log.verboseGet() >= log.verboseReversed().debug ) {
+        log.write(
+            cc.debug( "Loop module will do first initial broadcast of arrSChainsCached to its " ) +
+            cc.info( cntWorkers ) + cc.debug( " worker(s) in " ) +
+            threadInfo.threadDescription() + cc.debug( "..." ) + "\n" );
+    }
+    notifyCacheChangedSNB( skaleObserver.getLastCachedSChains() );
 }
 
 export async function runParallelLoops( opts ) {
diff --git a/agent/main.mjs b/agent/main.mjs
index a70a6ebef..5d0edc449 100644
--- a/agent/main.mjs
+++ b/agent/main.mjs
@@ -118,13 +118,13 @@ function parseCommandLine() {
                 cc.info( imaState.nAutoExitAfterSeconds ) +
                 cc.warning( " second(s) is requested." ) + "\n" );
         }
-        const iv = setInterval( function() {
+        const iv = owaspUtils.setInterval2( function() {
             if( log.verboseGet() >= log.verboseReversed().warning ) {
                 log.write( cc.warning( "Performing automatic exit after " ) +
                     cc.info( imaState.nAutoExitAfterSeconds ) + cc.warning( " second(s)..." ) +
                     "\n" );
             }
-            clearInterval( iv );
+            owaspUtils.clearInterval2( iv );
             process.exit( 0 );
         }, imaState.nAutoExitAfterSeconds * 1000 );
     } else
diff --git a/agent/pwa.mjs b/agent/pwa.mjs
index 81372f6ed..5f5523e92 100644
--- a/agent/pwa.mjs
+++ b/agent/pwa.mjs
@@ -286,10 +286,11 @@ async function notifyOnLoopImpl( imaState, strLoopWorkType, nIndexS2S, isStart )
             nIndexS2S, isStart, nUtcUnixTimeStamp, signature
         ); // save own started
         for( let i = 0; i < jarrNodes.length; ++i ) {
-            if( i == imaState.nNodeNumber )
+            const isThisNode = ( i == imaState.nNodeNumber ) ? true : false;
+            if( isThisNode )
                 continue; // skip this node
             const joNode = jarrNodes[i];
-            const strNodeURL = imaUtils.composeImaAgentNodeUrl( joNode );
+            const strNodeURL = imaUtils.composeImaAgentNodeUrl( joNode, isThisNode );
             const rpcCallOpts = null;
             rpcCall.create( // NOTICE: no await here, executed async
                 strNodeURL, rpcCallOpts, async function( joCall, err ) {
diff --git a/agent/run.sh b/agent/run.sh
index 996b36b1e..e71c6a06f 100644
--- a/agent/run.sh
+++ b/agent/run.sh
@@ -111,7 +111,8 @@ BASE_OPTIONS="--gas-price-multiplier=$GAS_PRICE_MULTIPLIER \
 --time-gap=$TIME_GAP \
 --monitoring-port=$MONITORING_PORT \
 --pwa \
---no-expose-pwa"
+--no-expose-pwa \
+--auto-exit=86400"
 
 echo "Running loop cmd..."
 node "$DIR/main.mjs" --loop $BASE_OPTIONS
diff --git a/agent/utils.mjs b/agent/utils.mjs
index 7411c6758..539040c70 100644
--- a/agent/utils.mjs
+++ b/agent/utils.mjs
@@ -500,7 +500,7 @@ export function composeSChainNodeUrl( joNode ) {
     return "";
 }
 
-export function composeImaAgentNodeUrl( joNode ) {
+export function composeImaAgentNodeUrl( joNode, isThisNode ) {
     let nPort = -1;
     if( "imaAgentRpcPort" in joNode &&
         typeof joNode.imaAgentRpcPort === "number" &&
@@ -542,7 +542,9 @@ export function composeImaAgentNodeUrl( joNode ) {
         joNode.wssRpcPort > 0
     )
         nPort = joNode.wssRpcPort - 7 + 10;
-    if( nPort > 0 )
-        return "http://" + joNode.ip + ":" + nPort;
+    if( nPort > 0 ) {
+        const strNodeIP = isThisNode ? "127.0.0.1" : joNode.ip;
+        return "http://" + strNodeIP + ":" + nPort;
+    }
     return "";
 }


Test patch:
diff --git a/test/agent-test.mjs b/test/agent-test.mjs
index 08f0bb0d7..d71f210bd 100644
--- a/test/agent-test.mjs
+++ b/test/agent-test.mjs
@@ -1093,7 +1093,10 @@ describe( "Agent Utils Module-1", function() {
             // as result, 14999 + 7 = 15006
             assert.equal(
                 imaUtils.composeImaAgentNodeUrl(
-                    { ip: "127.0.0.1", httpRpcPort: 14999 } ), "http://127.0.0.1:15006" );
+                    { ip: "127.0.0.1", httpRpcPort: 14999 }, true ), "http://127.0.0.1:15006" );
+            assert.equal(
+                imaUtils.composeImaAgentNodeUrl(
+                    { ip: "127.0.0.1", httpRpcPort: 14999 }, false ), "http://127.0.0.1:15006" );
         } );
 
     } );