Skip to content

Commit

Permalink
Merge pull request #1591 from skalenetwork/ticket-1413/improved-SNB
Browse files Browse the repository at this point in the history
ticket-1413 improved SNB discovery of connected S-chains
  • Loading branch information
sergiy-skalelabs authored Sep 18, 2023
2 parents fd32410 + fe1925c commit ac99e34
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 22 deletions.
6 changes: 4 additions & 2 deletions agent/clpTools.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1652,7 +1652,8 @@ export function commandLineTaskBrowseSkaleNetwork() {
const opts = {
imaState: imaState,
"details": log,
"bStopNeeded": false
"bStopNeeded": false,
"isLoadConnectedOnly": false
};
const arrSChains = await skaleObserver.loadSChains( opts );
const cnt = arrSChains.length;
Expand Down Expand Up @@ -1685,7 +1686,8 @@ export function commandLineTaskBrowseConnectedSChains() {
const opts = {
"imaState": imaState,
"details": log,
"bStopNeeded": false
"bStopNeeded": false,
"isLoadConnectedOnly": true
};
const arrSChainsCached = await skaleObserver.loadSChainsConnectedOnly(
imaState.chainProperties.sc.strChainName, opts );
Expand Down
102 changes: 83 additions & 19 deletions npms/skale-observer/observer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,8 @@ export async function loadSChain( idxSChain, hash, joData, cntSChains, opts ) {
await loadSChainParts( joSChain, opts );
if( opts && opts.details ) {
if( log.verboseGet() >= log.verboseReversed().trace ) {
opts.details.write( cc.debug( " Desc " ) + cc.j( joSChain.data ) + "\n" );
opts.details.write( cc.success( "Done" ) + "\n" );
opts.details.write( cc.debug( " SNB did loaded parts of S-chain " ) +
cc.j( joSChain.data ) + "\n" ); opts.details.write( cc.success( "Done" ) + "\n" );
}
}
joSChain.isConnected = false;
Expand All @@ -411,10 +411,12 @@ export async function loadSChainsWithEMC( opts ) {
if( opts && opts.details ) {
if( log.verboseGet() >= log.verboseReversed().trace ) {
opts.details.write( cc.debug( "Have " ) + cc.info( cntSChains ) +
cc.debug( " S-Chain(s) to load in " ) +
cc.debug( " S-Chain(s) to EMC-load in " ) +
threadInfo.threadDescription() + cc.debug( "..." ) + "\n" );
}
}
const isLoadConnectedOnly = ( "isLoadConnectedOnly" in opts )
? ( !!opts.isLoadConnectedOnly ) : true;
const multicall = new EMC.Multicall( {
ethersProvider: opts.imaState.chainProperties.mn.ethersProvider,
tryAggregate: true
Expand Down Expand Up @@ -548,6 +550,12 @@ export async function loadSChainsWithEMC( opts ) {
if( opts && opts.bStopNeeded )
return null;
}
const joMessageProxySChain =
new owaspUtils.ethersMod.ethers.Contract(
opts.imaState.chainProperties.sc.joAbiIMA.message_proxy_chain_address,
opts.imaState.chainProperties.sc.joAbiIMA.message_proxy_chain_abi,
opts.imaState.chainProperties.sc.ethersProvider
);
const arrSChains = [];
for( let idxSChain = 0; idxSChain < cntSChains; ++ idxSChain ) {
if( opts && opts.bStopNeeded )
Expand All @@ -557,13 +565,22 @@ export async function loadSChainsWithEMC( opts ) {
const joSChain = await loadSChain( // with hash + joData
idxSChain, hash, joData, cntSChains, opts );
if( ! joSChain )
break;
continue;
let isConnected = true;
if( isLoadConnectedOnly ) {
const strSChainName = joSChain.data.name;
isConnected = await checkWhetherSChainIsConnected(
strSChainName, joMessageProxySChain, opts );
if( ! isConnected )
continue;
}
joSChain.isConnected = isConnected;
arrSChains.push( joSChain );
}
if( opts && opts.details ) {
if( log.verboseGet() >= log.verboseReversed().trace ) {
opts.details.write( cc.success( "All " ) + cc.info( cntSChains ) +
cc.debug( " S-Chain(s) loaded:" ) + cc.j( arrSChains ) + "\n" );
cc.debug( " S-Chain(s) EMC-loaded:" ) + cc.j( arrSChains ) + "\n" );
}
}
return arrSChains;
Expand All @@ -572,30 +589,47 @@ export async function loadSChainsWithEMC( opts ) {
export async function loadSChainsOptimal( opts ) {
owaspUtils.ensureObserverOptionsInitialized( opts );
if( ! opts.imaState ) {
throw new Error( "Cannot load S-Chains in observer, no imaState is provided in " +
threadInfo.threadDescription( false ) );
throw new Error( "Cannot un-filtered optimal-load S-Chains in observer, " +
"no imaState is provided in " + threadInfo.threadDescription( false ) );
}
const isLoadConnectedOnly = ( "isLoadConnectedOnly" in opts )
? ( !!opts.isLoadConnectedOnly ) : true;
const cntSChains = await getSChainsCount( opts );
if( opts && opts.details ) {
if( log.verboseGet() >= log.verboseReversed().trace ) {
opts.details.write( cc.debug( "Have " ) + cc.info( cntSChains ) +
cc.debug( " S-Chain(s) to load in " ) +
cc.debug( " un-filtered S-Chain(s) to optimal-load in " ) +
threadInfo.threadDescription() + cc.debug( "..." ) + "\n" );
}
}
const joMessageProxySChain =
new owaspUtils.ethersMod.ethers.Contract(
opts.imaState.chainProperties.sc.joAbiIMA.message_proxy_chain_address,
opts.imaState.chainProperties.sc.joAbiIMA.message_proxy_chain_abi,
opts.imaState.chainProperties.sc.ethersProvider
);
const arrSChains = [];
for( let idxSChain = 0; idxSChain < cntSChains; ++ idxSChain ) {
if( opts && opts.bStopNeeded )
break;
const joSChain = await loadSChain( idxSChain, null, null, cntSChains, opts );
if( ! joSChain )
break;
continue;
let isConnected = true;
if( isLoadConnectedOnly ) {
const strSChainName = joSChain.data.name;
isConnected = await checkWhetherSChainIsConnected(
strSChainName, joMessageProxySChain, opts );
if( ! isConnected )
continue;
}
joSChain.isConnected = isConnected;
arrSChains.push( joSChain );
}
if( opts && opts.details ) {
if( log.verboseGet() >= log.verboseReversed().trace ) {
opts.details.write( cc.success( "All " ) + cc.info( cntSChains ) +
cc.debug( " S-Chain(s) loaded in " ) +
cc.debug( " un-filtered S-Chain(s) optimal-loaded in " ) +
threadInfo.threadDescription() + cc.success( ": " ) +
cc.j( arrSChains ) + "\n" );
}
Expand All @@ -605,15 +639,17 @@ export async function loadSChainsOptimal( opts ) {

export async function loadCachedSChainsSimplified( addressFrom, opts ) {
if( ! opts.imaState ) {
throw new Error( "Cannot load S-Chains in observer, no imaState is provided in " +
threadInfo.threadDescription( false ) );
throw new Error( "Cannot simplified-load S-Chains in observer, " +
"no imaState is provided in " + threadInfo.threadDescription( false ) );
}
if( opts && opts.details ) {
if( log.verboseGet() >= log.verboseReversed().trace ) {
opts.details.write( cc.debug( "Will request all S-Chain(s) hashes in " ) +
threadInfo.threadDescription() + cc.debug( "..." ) + "\n" );
}
}
const isLoadConnectedOnly = ( "isLoadConnectedOnly" in opts )
? ( !!opts.isLoadConnectedOnly ) : true;
const arrSChainHashes =
await opts.imaState.joSChainsInternal.callStatic.getSchains();
const cntSChains = arrSChainHashes.length;
Expand All @@ -623,6 +659,12 @@ export async function loadCachedSChainsSimplified( addressFrom, opts ) {
cc.debug( " S-Chain(s) hashes: " ) + cc.j( arrSChainHashes ) + "\n" );
}
}
const joMessageProxySChain =
new owaspUtils.ethersMod.ethers.Contract(
opts.imaState.chainProperties.sc.joAbiIMA.message_proxy_chain_address,
opts.imaState.chainProperties.sc.joAbiIMA.message_proxy_chain_abi,
opts.imaState.chainProperties.sc.ethersProvider
);
const arrSChains = [];
for( let idxSChain = 0; idxSChain < cntSChains; ++ idxSChain ) {
if( opts && opts.bStopNeeded )
Expand All @@ -640,11 +682,27 @@ export async function loadCachedSChainsSimplified( addressFrom, opts ) {
}
if( opts && opts.bStopNeeded )
break;
let isConnected = true;
if( isLoadConnectedOnly ) {
isConnected = await checkWhetherSChainIsConnected(
strSChainName, joMessageProxySChain, opts );
if( ! isConnected )
continue;
}
const joSChain = await loadSChain( idxSChain, strSChainHash, null, cntSChains, opts );
if( ! joSChain )
break;
continue;
joSChain.isConnected = isConnected;
arrSChains.push( joSChain );
}
if( opts && opts.details ) {
if( log.verboseGet() >= log.verboseReversed().trace ) {
opts.details.write( cc.success( "All " ) + cc.info( cntSChains ) +
cc.debug( " S-Chain(s) simplified-loaded in " ) +
threadInfo.threadDescription() + cc.success( ": " ) +
cc.j( arrSChains ) + "\n" );
}
}
return arrSChains;
}

Expand Down Expand Up @@ -702,6 +760,9 @@ export async function loadSChainsConnectedOnly( strChainNameConnectedTo, opts )
threadInfo.threadDescription() + cc.debug( "..." ) + "\n" );
}
}
// NOTICE: we are always check and filter connected status here,
// not depending on what is in opts
const isLoadConnectedOnly = true;
const arrSChainHashes = await opts.imaState.joSChainsInternal.callStatic.getSchains();
const cntSChains = arrSChainHashes.length;
if( opts && opts.details ) {
Expand Down Expand Up @@ -752,14 +813,17 @@ export async function loadSChainsConnectedOnly( strChainNameConnectedTo, opts )
cc.info( strChainNameConnectedTo ) + cc.debug( "..." ) + "\n" );
}
}
const isConnected = await checkWhetherSChainIsConnected(
strSChainName, joMessageProxySChain, opts );
if( ! isConnected )
continue;
let isConnected = false;
if( isLoadConnectedOnly ) {
isConnected = await checkWhetherSChainIsConnected(
strSChainName, joMessageProxySChain, opts );
if( ! isConnected )
continue;
}
const joSChain = await loadSChain( idxSChain, strSChainHash, null, cntSChains, opts );
if( ! joSChain )
break;
joSChain.isConnected = true;
continue;
joSChain.isConnected = isConnected;
arrSChains.push( joSChain );
} catch ( err ) {
if( opts && opts.details ) {
Expand Down
2 changes: 1 addition & 1 deletion npms/skale-observer/observerWorker.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ class ObserverServer extends SocketServer {
if( log.verboseGet() >= log.verboseReversed().debug ) {
self.opts.details.write(
cc.debug( "SKALE Observer in " ) + threadInfo.threadDescription() +
cc.debug( " will start periodic SNB refresh..." ) + "\n" );
cc.debug( " did invoked periodic SNB refresh" ) + "\n" );
}
self.intervalPeriodicSchainsCaching = setInterval(
fnPeriodicCaching, secondsToReDiscoverSkaleNetwork * 1000 );
Expand Down

0 comments on commit ac99e34

Please sign in to comment.