fix subscription data/topics; get old logs

pull/7/head
Iuri Matias 5 years ago
parent 517f6db8c5
commit 572dbb0edb
  1. 76
      remix-lib/src/execution/logsManager.js
  2. 9
      remix-simulator/src/methods/filters.js
  3. 8
      remix-simulator/src/provider.js

@ -6,6 +6,7 @@ class LogsManager {
constructor() {
this.notificationCallbacks = []
this.subscriptions = {}
this.oldLogs = []
}
checkBlock(blockNumber, block, web3) {
@ -14,6 +15,7 @@ class LogsManager {
web3.eth.getTransactionReceipt(txHash, (_error, receipt) => {
for (let log of receipt.logs) {
this.oldLogs.push({ type: 'block', blockNumber, block, tx, log, txNumber: i })
let subscriptions = this.getSubscriptionsFor({ type: 'block', blockNumber, block, tx, log })
for (let subscriptionId of subscriptions) {
@ -42,6 +44,25 @@ class LogsManager {
});
}
eventMatchesFilter(changeEvent, queryType, queryFilter) {
console.dir("--> matching topics")
if (queryFilter.topics.filter((logTopic) => changeEvent.log.topics.indexOf(logTopic) >= 0).length === 0) return false
console.dir("topic matched")
if (queryType === 'logs') {
if ((queryFilter.address === ("0x" + changeEvent.tx.to.toString('hex')))
&& (queryFilter.address === ('0x' + changeEvent.tx.from.toString('hex')))) {
if (!queryFilter.toBlock) {
return true;
} else if (parseInt(queryFilter.toBlock) > parseInt(changeEvent.blockNumber)) {
return true;
}
}
}
return false;
}
// TODO:
// * need to get address of deployed contract if it's a tx that create a contract
getSubscriptionsFor(changeEvent) {
@ -49,21 +70,9 @@ class LogsManager {
for (let subscriptionId of Object.keys(this.subscriptions)) {
const subscriptionParams = this.subscriptions[subscriptionId]
const [queryType, queryFilter] = subscriptionParams
if (queryType === 'logs') {
if (queryFilter.address === ("0x" + changeEvent.tx.to.toString('hex'))) {
if (!queryFilter.toBlock) {
matchedSubscriptions.push(subscriptionId)
} else if (parseInt(queryFilter.toBlock) > parseInt(changeEvent.blockNumber)) {
matchedSubscriptions.push(subscriptionId)
}
}
if (queryFilter.address === ('0x' + changeEvent.tx.from.toString('hex'))) {
if (!queryFilter.toBlock) {
matchedSubscriptions.push(subscriptionId)
} else if (parseInt(queryFilter.toBlock) > parseInt(changeEvent.blockNumber)) {
matchedSubscriptions.push(subscriptionId)
}
}
if (this.eventMatchesFilter(changeEvent, queryType, queryFilter)) {
matchedSubscriptions.push(subscriptionId)
}
}
return matchedSubscriptions;
@ -76,13 +85,24 @@ class LogsManager {
console.dir(result)
// TODO: manage subscriptions
// need to associate subscriptions to notificationCallbacks
this.notificationCallbacks.forEach((callback) => {
if (result.params.result.raw) {
result.params.result.data = result.params.result.raw.data
result.params.result.topics = result.params.result.raw.topics
}
console.dir("transmitting back")
console.dir(result)
// console.dir(result.params.result.returnValues)
// console.dir(result.params.result.raw)
callback(result)
});
}
addListener(type, cb) {
console.dir("<<<<<<<<<<<------------------------->>>>>>>>>>>>>");
console.dir("adding listener...");
this.notificationCallbacks.push(cb)
console.dir("--------------------------------------------------------->")
console.dir("==========================")
@ -103,16 +123,22 @@ class LogsManager {
}
getLogsFor(params) {
let results = [{
"logIndex": "0x1", // 1
"blockNumber": "0x1b4", // 436
"blockHash": "0x8216c5785ac562ff41e2dcfdf5785ac562ff41e2dcfdf829c5a142f1fccd7d",
"transactionHash": "0xdf829c5a142f1fccd7d8216c5785ac562ff41e2dcfdf5785ac562ff41e2dcf",
"transactionIndex": "0x0", // 0
"address": "0x16c5785ac562ff41e2dcfdf829c5a142f1fccd7d",
"data": "0x0000000000000000000000000000000000000000000000000000000000000000",
"topics": ["0x59ebeb90bc63057b6515673c3ecf9438e5058bca0f92585014eced636878c9a5"]
}]
let results = []
for (let log of this.oldLogs) {
if (this.eventMatchesFilter(log, 'logs', params)) {
results.push({
"logIndex": "0x1", // 1
"blockNumber": log.blockNumber,
"blockHash": ('0x' + log.block.hash().toString('hex')),
"transactionHash": ('0x' + log.tx.hash().toString('hex')),
"transactionIndex": "0x" + logs.txNumber.toString(16),
// TODO: if it's a contract deploy, it should be that address instead
"address": log.log.address,
"data": log.log.data,
"topics": log.log.topics,
})
}
}
return results
}

@ -18,12 +18,13 @@ Filters.prototype.eth_getLogs = function (payload, cb) {
console.dir("===============================")
console.dir("===============================")
console.dir("=== eth_getLogs")
// payload.params[0].topics = payload.params[0].topics.filter((x) => x)
console.dir(payload.params)
// [ { fromBlock: '0x0',
// address: '0xdb2eb1480cb3ac3a5c0ee957045d1ad9dcd34f01',
// topics: [] } ]
let results = executionContext.logsManager.getLogsFor(payload.params);
let results = executionContext.logsManager.getLogsFor(payload.params[0]);
console.dir("results are ---")
console.dir(results)
cb(null, results)
}

@ -29,9 +29,9 @@ var Provider = function (options) {
generateBlock()
setTimeout(() => {
console.dir("hello!")
}, 10 * 1000)
// setTimeout(() => {
// console.dir("hello!")
// }, 10 * 1000)
}
Provider.prototype.init = async function () {
@ -69,6 +69,8 @@ Provider.prototype.isConnected = function () {
}
Provider.prototype.on = function (type, cb) {
console.dir("on")
console.dir(arguments)
executionContext.logsManager.addListener(type, cb)
}

Loading…
Cancel
Save