Reduce time complexity of events comparison in monitor (#475)

This commit is contained in:
Kirill Fedoseev 2020-10-23 13:48:37 +03:00 committed by GitHub
parent fbce0fc035
commit 389cea3c39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,44 +1,27 @@
const web3Utils = require('web3').utils
const { parseAMBMessage } = require('../../commons') const { parseAMBMessage } = require('../../commons')
function deliveredMsgNotProcessed(processedList) { const keyAMB = e => [e.messageId, e.sender, e.executor].join(',').toLowerCase()
return deliveredMsg => {
let msgData = deliveredMsg.returnValues.encodedData const normalizeAMBMessage = e => {
if (!deliveredMsg.returnValues.messageId) { let msgData = e.returnValues.encodedData
// append tx hash to an old message, where message id was not used if (!e.returnValues.messageId) {
msgData = deliveredMsg.transactionHash + msgData.slice(2) // append tx hash to an old message, where message id was not used
} // for old messages, e.messageId is a corresponding transactionHash
const msg = parseAMBMessage(msgData) msgData = e.transactionHash + msgData.slice(2)
return (
processedList.filter(processedMsg => {
return messageEqualsEvent(msg, processedMsg.returnValues)
}).length === 0
)
} }
return parseAMBMessage(msgData)
}
function deliveredMsgNotProcessed(processedList) {
const keys = new Set()
processedList.forEach(processedMsg => keys.add(keyAMB(processedMsg.returnValues)))
return deliveredMsg => !keys.has(keyAMB(normalizeAMBMessage(deliveredMsg)))
} }
function processedMsgNotDelivered(deliveredList) { function processedMsgNotDelivered(deliveredList) {
return processedMsg => { const keys = new Set()
return ( deliveredList.forEach(deliveredMsg => keys.add(keyAMB(normalizeAMBMessage(deliveredMsg))))
deliveredList.filter(deliveredMsg => { return processedMsg => !keys.has(keyAMB(processedMsg.returnValues))
let msgData = deliveredMsg.returnValues.encodedData
if (!deliveredMsg.returnValues.messageId) {
// append tx hash to an old message, where message id was not used
msgData = deliveredMsg.transactionHash + msgData.slice(2)
}
const msg = parseAMBMessage(msgData)
return messageEqualsEvent(msg, processedMsg.returnValues)
}).length === 0
)
}
}
function messageEqualsEvent(parsedMsg, event) {
return (
web3Utils.toChecksumAddress(parsedMsg.sender) === event.sender &&
web3Utils.toChecksumAddress(parsedMsg.executor) === event.executor &&
parsedMsg.messageId === event.messageId // for an old messages, event.messageId is actually a transactionHash
)
} }
/** /**
@ -60,9 +43,13 @@ const normalizeEventInformation = event => ({
value: event.returnValues.value value: event.returnValues.value
}) })
const eventWithoutReference = otherSideEvents => e => const key = e => [e.referenceTx, e.recipient, e.value].join(',').toLowerCase()
otherSideEvents.filter(a => a.referenceTx === e.referenceTx && a.recipient === e.recipient && a.value === e.value)
.length === 0 const eventWithoutReference = otherSideEvents => {
const keys = new Set()
otherSideEvents.forEach(e => keys.add(key(e)))
return e => !keys.has(key(e))
}
module.exports = { module.exports = {
deliveredMsgNotProcessed, deliveredMsgNotProcessed,