Skip to content

Commit

Permalink
Merge pull request #1556 from skalenetwork/ticket-828/add-ima-api
Browse files Browse the repository at this point in the history
Ticket 828/add ima api
  • Loading branch information
sergiy-skalelabs authored Aug 16, 2023
2 parents ed8695e + fbbb5bb commit ecaeb1b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 12 deletions.
23 changes: 12 additions & 11 deletions agent/bls.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1477,19 +1477,19 @@ async function doSignConfigureChainAccessParams( optsSignOperation ) {
async function doSignProcessHandleCall(
optsSignOperation,
joNode, joParams,
joIn, joOut, err
joIn, joOut, err, strNodeURL, i
) {
++optsSignOperation.joGatheringTracker.nCountReceived; // including errors
if( err ) {
++optsSignOperation.joGatheringTracker.nCountErrors;
const strErrorMessage =
optsSignOperation.strLogPrefix + cc.fatal( "CRITICAL ERROR:" ) +
cc.error( " JSON RPC call to S-Chain node " ) + strNodeDescColorized +
cc.error( " failed, RPC call reported error: " ) +
cc.warning( owaspUtils.extractErrorMessage( err ) ) +
cc.error( ", " ) + cc.notice( "sequence ID" ) +
cc.error( " is " ) + cc.attention( optsSignOperation.sequenceId ) +
"\n";
cc.error( "(node #" ) + cc.info( i ) + cc.error( " via " ) +
cc.notice( strNodeURL ) + cc.error( ") failed, RPC call reported error: " ) +
cc.warning( owaspUtils.extractErrorMessage( err ) ) + cc.error( ", " ) +
cc.notice( "sequence ID" ) + cc.error( " is " ) +
cc.attention( optsSignOperation.sequenceId ) + "\n";
if( log.verboseGet() >= log.verboseReversed().error ) {
if( log.id != optsSignOperation.details.id )
log.write( strErrorMessage );
Expand All @@ -1502,7 +1502,8 @@ async function doSignProcessHandleCall(
optsSignOperation.details.write( optsSignOperation.strLogPrefix +
log.generateTimestampString( null, true ) + " " +
cc.debug( "Got answer from " ) + cc.info( "skale_imaVerifyAndSign" ) +
cc.debug( " for transfer from chain " ) + cc.info( optsSignOperation.fromChainName ) +
cc.error( "(node #" ) + cc.info( i ) + cc.error( " via " ) + cc.notice( strNodeURL ) +
cc.debug( ") for transfer from chain " ) + cc.info( optsSignOperation.fromChainName ) +
cc.debug( " to chain " ) + cc.info( optsSignOperation.targetChainName ) +
cc.debug( " with params " ) + cc.j( joParams ) + cc.debug( ", answer is " ) +
cc.j( joOut ) + cc.debug( ", " ) + cc.notice( "sequence ID" ) + cc.debug( " is " ) +
Expand Down Expand Up @@ -1713,7 +1714,9 @@ async function doSignProcessOneImpl( i, optsSignOperation ) {
if( log.verboseGet() >= log.verboseReversed().trace ) {
optsSignOperation.details.write( optsSignOperation.strLogPrefix +
log.generateTimestampString( null, true ) + " " + cc.debug( "Will invoke " ) +
cc.info( "skale_imaVerifyAndSign" ) + cc.debug( " for transfer from chain " ) +
cc.info( "skale_imaVerifyAndSign" ) + cc.debug( " to node #" ) + cc.info( i ) +
cc.debug( " via " ) + cc.notice( strNodeURL ) +
cc.debug( " for transfer from chain " ) +
cc.info( optsSignOperation.fromChainName ) + cc.debug( " to chain " ) +
cc.info( optsSignOperation.targetChainName ) + cc.debug( " with params " ) +
cc.j( joParams ) + cc.debug( ", " ) + cc.notice( "sequence ID" ) +
Expand All @@ -1724,9 +1727,7 @@ async function doSignProcessOneImpl( i, optsSignOperation ) {
"params": joParams
}, async function( joIn, joOut, err ) {
await doSignProcessHandleCall(
optsSignOperation,
joNode, joParams,
joIn, joOut, err
optsSignOperation, joNode, joParams, joIn, joOut, err, strNodeURL, i
);
} ); // joCall.call ...
} ); // rpcCall.create ...
Expand Down
25 changes: 25 additions & 0 deletions agent/main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import * as imaBLS from "./bls.mjs";
import * as pwa from "./pwa.mjs";
import * as clpTools from "./clpTools.mjs";
import * as discoveryTools from "./discoveryTools.mjs";
import * as skaleObserver from "../npms/skale-observer/observer.mjs";

import * as state from "./state.mjs";

Expand Down Expand Up @@ -364,6 +365,30 @@ function initJsonRpcServer() {
) )
await loop.spreadArrivedStateOfPendingWorkAnalysis( joMessage );

break;
case "skale_getCachedSNB":
joAnswer.arrSChainsCached = skaleObserver.getLastCachedSChains();
break;
case "skale_historySNB":
joAnswer.arrCacheHistory = skaleObserver.getLastCachedHistory();
break;
case "skale_refreshSNB":
{
const opts = {
imaState: imaState,
"details": log,
"bStopNeeded": false,
"secondsToReDiscoverSkaleNetwork":
imaState.optsS2S.secondsToReDiscoverSkaleNetwork,
"secondsToWaitForSkaleNetworkDiscovered":
imaState.optsS2S.secondsToWaitForSkaleNetworkDiscovered,
"chain": imaState.chainProperties.sc,
"bParallelModeRefreshSNB":
imaState.optsS2S.bParallelModeRefreshSNB ? true : false
};
skaleObserver.refreshNowSNB( opts ); // async call, no await here
joAnswer.result = "Done";
}
break;
default:
throw new Error( "Unknown method name \"" + joMessage.method + "\" was specified" );
Expand Down
29 changes: 28 additions & 1 deletion npms/skale-observer/observer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,8 @@ export function mergeSChainsArrayFromTo( arrSrc, arrDst, arrNew, arrOld, opts )
}

let gArrSChainsCached = [];
const gArrCacheHistory = [];
let nMaxSizeOfArrCacheHistory = 20;

export async function cacheSChains( strChainNameConnectedTo, addressFrom, opts ) {
owaspUtils.ensureObserverOptionsInitialized( opts );
Expand Down Expand Up @@ -1011,11 +1013,17 @@ export function setLastCachedSChains( arrSChainsCached ) {
}
if( arrSChainsCached && typeof arrSChainsCached == "object" ) {
gArrSChainsCached = JSON.parse( JSON.stringify( arrSChainsCached ) );
gArrCacheHistory.push( {
"ts": "" + log.generateTimestampString( null, false ),
"arrSChainsCached": JSON.parse( JSON.stringify( arrSChainsCached ) )
} );
const nMaxSize = getLastCachedHistoryMaxSize();
while( gArrCacheHistory.length > nMaxSize )
gArrCacheHistory.shift();
if( log.verboseGet() >= log.verboseReversed().debug ) {
log.write( cc.debug( "Will dispatch arrSChainsCached event in " ) +
threadInfo.threadDescription() + cc.debug( "..." ) + "\n" );
}

events.dispatchEvent(
new UniversalDispatcherEvent(
"chainsCacheChanged",
Expand All @@ -1030,6 +1038,25 @@ export function setLastCachedSChains( arrSChainsCached ) {
}
}

export function getLastCachedHistory() {
return gArrCacheHistory;
}

export function getLastCachedHistoryMaxSize() {
return 0 + nMaxSizeOfArrCacheHistory;
}
export function setLastCachedHistoryMaxSize( m ) {
nMaxSizeOfArrCacheHistory = 0 + n;
if( nMaxSizeOfArrCacheHistory < 0 )
nMaxSizeOfArrCacheHistory = 0;
}

export async function refreshNowSNB( opts ) {
const addressFrom = opts.imaState.chainProperties.mn.joAccount.address();
const strChainNameConnectedTo = opts.imaState.chainProperties.sc.strChainName;
await cacheSChains( strChainNameConnectedTo, addressFrom, opts );
}

let gWorker = null;
let gClient = null;

Expand Down

0 comments on commit ecaeb1b

Please sign in to comment.