Skip to content

Commit

Permalink
Merge pull request #1631 from skalenetwork/ticket-1620/rollback-event…
Browse files Browse the repository at this point in the history
…-log-scanner

Ticket 1620/rollback event log scanner
  • Loading branch information
sergiy-skalelabs authored Oct 31, 2023
2 parents 1f2459f + 0d63e3e commit 17a59ef
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 44 deletions.
2 changes: 0 additions & 2 deletions agent/loop.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ async function singleTransferLoopPartM2S( optsLoop, strLogPrefix ) {
imaState.chainProperties.mn.joAccount,
imaState.chainProperties.sc.ethersProvider,
imaState.joMessageProxySChain,

imaState.chainProperties.sc.joAccount,
imaState.chainProperties.mn.strChainName,
imaState.chainProperties.sc.strChainName,
Expand Down Expand Up @@ -309,7 +308,6 @@ async function singleTransferLoopPartS2M( optsLoop, strLogPrefix ) {
imaState.chainProperties.sc.joAccount,
imaState.chainProperties.mn.ethersProvider,
imaState.joMessageProxyMainNet,

imaState.chainProperties.mn.joAccount,
imaState.chainProperties.sc.strChainName,
imaState.chainProperties.mn.strChainName,
Expand Down
30 changes: 27 additions & 3 deletions npms/skale-ima/imaEventLogScan.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,26 @@ export function createProgressiveEventsScanPlan( details, nLatestBlockNumber ) {
return arrProgressiveEventsScanPlan;
}

function generateWhileTransferringLogMessageSuffix( optsChainPair ) {
if( ! optsChainPair )
return "";
if( ! optsChainPair.strDirection )
return "";
if( optsChainPair.strDirection == "S2S" ) {
return cc.debug( " (while performing " ) + cc.attention( optsChainPair.strDirection ) +
cc.debug( " transfer with external S-Chain " ) +
cc.info( optsChainPair.optsSpecificS2S.joSChain.data.name ) + cc.debug( " / " ) +
cc.notice( optsChainPair.optsSpecificS2S.joSChain.data.computed.chainId ) +
cc.debug( " node " ) + cc.info( optsChainPair.optsSpecificS2S.idxNode ) +
cc.debug( ")" );
}
return cc.debug( " (while performing " ) + cc.attention( optsChainPair.strDirection ) +
cc.debug( " transfer)" );
}

export async function safeGetPastEventsProgressive(
details, strLogPrefix,
ethersProvider, attempts, joContract, strEventName,
nBlockFrom, nBlockTo, joFilter
details, strLogPrefix, ethersProvider, attempts, joContract, strEventName,
nBlockFrom, nBlockTo, joFilter, optsChainPair
) {
if( ! imaTransferErrorHandling.getEnabledProgressiveEventsScan() ) {
details.write( strLogPrefix +
Expand All @@ -104,6 +120,14 @@ export async function safeGetPastEventsProgressive(
nBlockFrom, nBlockTo, joFilter
);
}
if( log.verboseGet() >= log.verboseReversed().information ) {
details.write( strLogPrefix +
cc.info( "Will run progressive event log search for event " ) +
cc.j( strEventName ) + cc.info( " via URL " ) +
cc.u( owaspUtils.ethersProviderToUrl( ethersProvider ) ) +
generateWhileTransferringLogMessageSuffix( optsChainPair ) +
cc.info( "..." ) );
}
const nLatestBlockNumber = owaspUtils.toBN(
await imaHelperAPIs.safeGetBlockNumber( details, 10, ethersProvider ) );
const nLatestBlockNumberPlus1 = nLatestBlockNumber.add( owaspUtils.toBN( 1 ) );
Expand Down
67 changes: 28 additions & 39 deletions npms/skale-ima/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,13 @@ const perMessageGasForTransfer = 1000000;
const additionalS2MTransferOverhead = 200000;

async function findOutReferenceLogRecord(
details, strLogPrefix,
ethersProvider, joMessageProxy,
details, strLogPrefix, ethersProvider, joMessageProxy,
bnBlockId, nMessageNumberToFind, isVerbose
) {
const bnMessageNumberToFind = owaspUtils.toBN( nMessageNumberToFind.toString() );
const strEventName = "PreviousMessageReference";
const arrLogRecords = await imaEventLogScan.safeGetPastEventsProgressive(
details, strLogPrefix,
ethersProvider, 10, joMessageProxy, strEventName,
details, strLogPrefix, ethersProvider, 10, joMessageProxy, strEventName,
bnBlockId, bnBlockId, joMessageProxy.filters[strEventName]()
);
const cntLogRecord = arrLogRecords.length;
Expand Down Expand Up @@ -98,9 +96,8 @@ async function findOutReferenceLogRecord(
}

async function findOutAllReferenceLogRecords(
details, strLogPrefix,
ethersProvider, joMessageProxy,
bnBlockId, nIncMsgCnt, nOutMsgCnt, isVerbose
details, strLogPrefix, ethersProvider, joMessageProxy,
bnBlockId, nIncMsgCnt, nOutMsgCnt, isVerbose, optsChainPair
) {
if( isVerbose ) {
if( log.verboseGet() >= log.verboseReversed().debug ) {
Expand Down Expand Up @@ -128,12 +125,9 @@ async function findOutAllReferenceLogRecords(
let nWalkMsgNumber = nOutMsgCnt - 1;
let nWalkBlockId = bnBlockId;
for( ; nWalkMsgNumber >= nIncMsgCnt; -- nWalkMsgNumber ) {
const joReferenceLogRecord =
await findOutReferenceLogRecord(
details, strLogPrefix,
ethersProvider, joMessageProxy,
nWalkBlockId, nWalkMsgNumber, isVerbose
);
const joReferenceLogRecord = await findOutReferenceLogRecord(
details, strLogPrefix, ethersProvider, joMessageProxy,
nWalkBlockId, nWalkMsgNumber, isVerbose, optsChainPair );
if( joReferenceLogRecord == null )
break;
nWalkBlockId = owaspUtils.toBN( joReferenceLogRecord.previousOutgoingMessageBlockId );
Expand Down Expand Up @@ -287,11 +281,10 @@ async function doQueryOutgoingMessageCounter( optsTransfer ) {
optsTransfer.strActionName =
"in-getOutgoingMessagesCounter()--findOutAllReferenceLogRecords()";
optsTransfer.arrLogRecordReferences =
await findOutAllReferenceLogRecords(
optsTransfer.details, optsTransfer.strLogPrefixShort,
optsTransfer.ethersProviderSrc, optsTransfer.joMessageProxySrc,
bnBlockId, optsTransfer.nIncMsgCnt, optsTransfer.nOutMsgCnt, true
);
await findOutAllReferenceLogRecords( optsTransfer.details,
optsTransfer.strLogPrefixShort, optsTransfer.ethersProviderSrc,
optsTransfer.joMessageProxySrc, bnBlockId, optsTransfer.nIncMsgCnt,
optsTransfer.nOutMsgCnt, true, optsTransfer.optsChainPair );
return true; // success, finish at this point
} catch ( err ) {
optsTransfer.arrLogRecordReferences = [];
Expand Down Expand Up @@ -332,9 +325,7 @@ async function doQueryOutgoingMessageCounter( optsTransfer ) {
const arrLogRecordReferencesWalk = await imaEventLogScan.safeGetPastEventsProgressive(
optsTransfer.details, optsTransfer.strLogPrefixShort,
optsTransfer.ethersProviderSrc, attempts, optsTransfer.joMessageProxySrc,
strEventName,
nBlockFrom, nBlockTo, joFilter
);
strEventName, nBlockFrom, nBlockTo, joFilter, optsTransfer.optsChainPair );
optsTransfer.arrLogRecordReferences =
optsTransfer.arrLogRecordReferences.concat( arrLogRecordReferencesWalk );
}
Expand Down Expand Up @@ -432,13 +423,13 @@ async function gatherMessages( optsTransfer ) {
cc.notice( optsTransfer.strActionName ) + cc.debug( " for " ) +
cc.info( strEventName ) + cc.debug( " event..." ) + "\n" );
}
r = await imaEventLogScan.safeGetPastEventsProgressive(
optsTransfer.details, optsTransfer.strLogPrefixShort, optsTransfer.ethersProviderSrc,
10, optsTransfer.joMessageProxySrc, strEventName, nBlockFrom, nBlockTo,
r = await imaEventLogScan.safeGetPastEventsProgressive( optsTransfer.details,
optsTransfer.strLogPrefixShort, optsTransfer.ethersProviderSrc, 10,
optsTransfer.joMessageProxySrc, strEventName, nBlockFrom, nBlockTo,
optsTransfer.joMessageProxySrc.filters[strEventName](
owaspUtils.ethersMod.ethers.utils.id( optsTransfer.chainNameDst ), // dstChainHash
owaspUtils.ethersMod.ethers.utils.id( optsTransfer.chainNameDst ),
owaspUtils.toBN( optsTransfer.nIdxCurrentMsg )
) );
), optsTransfer.optsChainPair );
const joValues = await analyzeGatheredRecords( optsTransfer, r );
if( joValues == null )
return false;
Expand Down Expand Up @@ -928,26 +919,24 @@ async function checkOutgoingMessageEvent( optsTransfer, joSChain ) {
let bEventIsFound = false;
try {
// eslint-disable-next-line dot-notation
const ethersProviderNode =
owaspUtils.getEthersProviderFromURL( strUrlHttp );
const joMessageProxyNode =
new owaspUtils.ethersMod.ethers.Contract(
optsTransfer.imaState.chainProperties.sc
.joAbiIMA.message_proxy_chain_address,
optsTransfer.imaState.chainProperties.sc
.joAbiIMA.message_proxy_chain_abi,
ethersProviderNode
);
const ethersProviderNode = owaspUtils.getEthersProviderFromURL( strUrlHttp );
const joMessageProxyNode = new owaspUtils.ethersMod.ethers.Contract(
optsTransfer.imaState.chainProperties.sc
.joAbiIMA.message_proxy_chain_address,
optsTransfer.imaState.chainProperties
.sc.joAbiIMA.message_proxy_chain_abi,
ethersProviderNode );
const strEventName = "OutgoingMessage";
const node_r = await imaEventLogScan.safeGetPastEventsProgressive(
optsTransfer.details, optsTransfer.strLogPrefixShort,
ethersProviderNode, 10, joMessageProxyNode, strEventName,
optsTransfer.details, optsTransfer.strLogPrefixShort, ethersProviderNode,
10, joMessageProxyNode, strEventName,
joMessage.savedBlockNumberForOptimizations,
joMessage.savedBlockNumberForOptimizations,
joMessageProxyNode.filters[strEventName](
owaspUtils.ethersMod.ethers.utils.id( optsTransfer.chainNameDst ),
owaspUtils.toBN( idxImaMessage )
)
),
optsTransfer.optsChainPair
);
const cntEvents = node_r.length;
if( log.verboseGet() >= log.verboseReversed().trace ) {
Expand Down

0 comments on commit 17a59ef

Please sign in to comment.