Skip to content
This repository has been archived by the owner on Aug 22, 2018. It is now read-only.

[WIP] Improve cache usage #53

Open
wants to merge 1 commit into
base: stable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/client/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ const Repl = require("./repl")
const Scanner = require("./scanning")
const StatsDB = require("./statsdb")

const startScanning = (ms, conf) => {
const startScanning = async (ms, conf) => {
const log = conf.logger

log.info(`Scanning request tracker at ${conf.tracker.address}`)
log.info(`Validating results with factory at ${conf.factory.address}`)
log.info(`Scanning every ${ms / 1000} seconds.`)

setInterval((_) => {
setInterval(async (_) => {
if (conf.scanning) {
Scanner.scanBlockchain(conf).catch(err => log.error(err))
await Scanner.scanBlockchain(conf).catch(err => log.error(err))
}
}, ms)

Expand Down
67 changes: 35 additions & 32 deletions src/client/scanning.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
/* eslint no-await-in-loop: "off" */

const store = (conf, txRequest) => {
const inCache = (conf, address) => {
const log = conf.logger

if (conf.cache.has(txRequest.address)) {
log.cache(`Cache already contains ${txRequest.address}`)
return
if (conf.cache.has(address)) {
log.cache(`Cache HIT ${address}`)
return true
}
log.cache(`Cache MISS ${address}`)
return false
}

const store = (conf, txRequest) => {
const log = conf.logger

log.info(`Storing found txRequest at address ${txRequest.address}`)
conf.cache.set(txRequest.address, txRequest.windowStart)
}
Expand All @@ -31,34 +38,30 @@ const scan = async (conf, left, right) => {

while (nextRequestAddress !== eac.Constants.NULL_ADDRESS) {
log.debug(`Found request - ${nextRequestAddress}`)

// Verify that the request is known to the factory we are validating with.
if (!await requestFactory.isKnownRequest(nextRequestAddress)) {
log.error(`Encountered unknown transaction request: ${
requestFactory.address
} | query: ">=" | value ${left} | address: ${nextRequestAddress}`)
throw new Error(`Encountered unknown address! Please check that you are using the correct contracts JSON file.`)
}

const trackerWindowStart = await requestTracker.windowStartFor(nextRequestAddress)

const txRequest = await eac.transactionRequest(nextRequestAddress)
await txRequest.fillData()

if (!txRequest.windowStart.equals(trackerWindowStart)) {
// The data between the txRequest we have and from the requestTracker do not match.
log.error(`Data mismatch between txRequest and requestTracker. Double check contract addresses.`)
} else if (txRequest.windowStart.lessThanOrEqualTo(right)) {
// This request is within bounds, store it.
store(conf, txRequest)
if (!inCache(conf, nextRequestAddress)) {
const trackerWindowStart = await requestTracker.windowStartFor(nextRequestAddress)
const txRequest = await eac.transactionRequest(nextRequestAddress)
await txRequest.fillData()

if (!txRequest.windowStart.equals(trackerWindowStart)) {
// The data between the txRequest we have and from the requestTracker do not match.
log.error(`Data mismatch between txRequest and requestTracker. Double check contract addresses.`)
} else if (txRequest.windowStart.lessThanOrEqualTo(right)) {
// This request is within bounds, store it.
store(conf, txRequest)
}
} else {
console.log
log.debug(`Scan exit condition hit! Next window start exceeds right bound. WindowStart: ${
txRequest.windowStart
} | right: ${right}`)
break
const windowStart = conf.cache.get(nextRequestAddress)

if (windowStart.greaterThan(right)) {
log.debug(`Scan exit condition hit! Next window start exceeds right bound. WindowStart: ${
windowStart
} | right: ${right}`)
break
}
}
nextRequestAddress = await requestTracker.nextRequest(txRequest.address)

nextRequestAddress = await requestTracker.nextRequest(nextRequestAddress)

// Hearbeat
if (nextRequestAddress === eac.Constants.NULL_ADDRESS) {
Expand All @@ -82,8 +85,8 @@ const scanBlockchain = async (conf) => {
[debug] blocks: ${leftBlock} to ${rightBlock}
[debug] timestamps: ${leftTimestamp} tp ${rightTimestamp}`)

scan(conf, leftBlock, rightBlock)
scan(conf, leftTimestamp, rightTimestamp)
await scan(conf, leftBlock, rightBlock)
await scan(conf, leftTimestamp, rightTimestamp)
}


Expand Down