Skip to content

Commit

Permalink
Merge pull request MetaMask#154 from MetaMask/InflightCache
Browse files Browse the repository at this point in the history
Inflight cache
  • Loading branch information
FlySwatter authored May 30, 2017
2 parents 5f88a58 + 1791119 commit 30f993e
Show file tree
Hide file tree
Showing 8 changed files with 24,817 additions and 17,960 deletions.
20,631 changes: 10,528 additions & 10,103 deletions dist/ProviderEngine.js

Large diffs are not rendered by default.

22,061 changes: 14,216 additions & 7,845 deletions dist/ZeroClientProvider.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "web3-provider-engine",
"version": "12.1.0",
"version": "12.2.0",
"description": "",
"repository": "https://github.com/MetaMask/provider-engine",
"main": "index.js",
Expand All @@ -23,6 +23,7 @@
"ethereumjs-vm": "^2.0.2",
"fetch-ponyfill": "^4.0.0",
"json-rpc-error": "^2.0.0",
"json-stable-stringify": "^1.0.1",
"promise-to-callback": "^1.0.0",
"readable-stream": "^2.2.9",
"request": "^2.67.0",
Expand Down
44 changes: 44 additions & 0 deletions subproviders/inflight-cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const cacheIdentifierForPayload = require('../util/rpc-cache-utils.js').cacheIdentifierForPayload
const Subprovider = require('./subprovider.js')


class InflightCacheSubprovider extends Subprovider {

constructor (opts) {
super()
this.inflightRequests = {}
}

addEngine (engine) {
this.engine = engine
}

handleRequest (req, next, end) {
const cacheId = cacheIdentifierForPayload(req)

// if not cacheable, skip
if (!cacheId) return next()

// check for matching requests
let activeRequestHandlers = this.inflightRequests[cacheId]

if (!activeRequestHandlers) {
activeRequestHandlers = []
this.inflightRequests[cacheId] = activeRequestHandlers

next((err, result, cb) => {
delete this.inflightRequests[cacheId]
activeRequestHandlers.forEach((handler) => handler(err, result))
cb(err, result)
})

} else {
// setup the response lister
activeRequestHandlers.push(end)
}

}
}

module.exports = InflightCacheSubprovider

16 changes: 9 additions & 7 deletions test/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ filterTest('basic block filter', { method: 'eth_newBlockFilter' },
cb()
},
function filterChangesTwo(t, testMeta, response, cb){
var results = response.result
var results = response.result
t.equal(results.length, 0, 'correct number of results')
cb()
}
Expand Down Expand Up @@ -49,7 +49,7 @@ filterTest('log filter - basic', {
cb()
},
function filterChangesTwo(t, testMeta, response, cb){
var results = response.result
var results = response.result
t.equal(results.length, 0, 'correct number of results')
cb()
}
Expand Down Expand Up @@ -88,7 +88,7 @@ filterTest('log filter - and logic', {
cb()
},
function filterChangesTwo(t, testMeta, response, cb){
var results = response.result
var results = response.result
t.equal(results.length, 0, 'correct number of results')
cb()
}
Expand Down Expand Up @@ -134,7 +134,7 @@ filterTest('log filter - or logic', {
cb()
},
function filterChangesTwo(t, testMeta, response, cb){
var results = response.result
var results = response.result
t.equal(results.length, 0, 'correct number of results')
cb()
}
Expand Down Expand Up @@ -181,7 +181,7 @@ filterTest('log filter - wildcard logic', {
cb()
},
function filterChangesTwo(t, testMeta, response, cb){
var results = response.result
var results = response.result
t.equal(results.length, 0, 'correct number of results')
cb()
}
Expand Down Expand Up @@ -211,7 +211,10 @@ function filterTest(label, filterPayload, afterInstall, filterChangesOne, filter
engine.addProvider(filterProvider)
engine.addProvider(blockProvider)
engine.once('block', startTest)
engine.start()

setTimeout(() => {
engine.start()
}, 1)

function startTest(){
// install block filter
Expand All @@ -228,7 +231,6 @@ function filterTest(label, filterPayload, afterInstall, filterChangesOne, filter

afterInstall(t, testMeta, response, function(err){
t.ifError(err, 'did not error')

engine.once('block', continueTest)
})
})
Expand Down
13 changes: 10 additions & 3 deletions test/util/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ function TestBlockProvider(methods){
} else {
result = self._blockChain[blockRef]
}

end(null, result)
},
eth_getLogs: function(payload, next, end){
Expand Down Expand Up @@ -72,7 +71,7 @@ TestBlockProvider.prototype.addTx = function(txParams){
function createBlock(blockParams, prevBlock, txs) {
blockParams = blockParams || {}
txs = txs || []
var defaultNumber = prevBlock ? incrementHex(prevBlock.number) : '0x01'
var defaultNumber = prevBlock ? incrementHex(prevBlock.number) : '0x1'
var defaultGasLimit = ethUtil.intToHex(4712388)
return extend({
// defaults
Expand All @@ -99,9 +98,17 @@ function createBlock(blockParams, prevBlock, txs) {
}

function incrementHex(hexString){
return ethUtil.intToHex(Number(hexString)+1)
return stripLeadingZeroes(ethUtil.intToHex(Number(hexString)+1))
}

function randomHash(){
return ethUtil.intToHex(Math.floor(Math.random()*Number.MAX_SAFE_INTEGER))
}

function stripLeadingZeroes (hexString) {
let strippedHex = ethUtil.stripHexPrefix(hexString)
while (strippedHex[0] === '0') {
strippedHex = strippedHex.substr(1)
}
return ethUtil.addHexPrefix(strippedHex)
}
4 changes: 3 additions & 1 deletion util/rpc-cache-utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const stringify = require('json-stable-stringify')

module.exports = {
cacheIdentifierForPayload: cacheIdentifierForPayload,
canCache: canCache,
Expand All @@ -10,7 +12,7 @@ module.exports = {
function cacheIdentifierForPayload(payload){
var simpleParams = paramsWithoutBlockTag(payload)
if (canCache(payload)) {
return payload.method+':'+JSON.stringify(simpleParams)
return payload.method+':'+ stringify(simpleParams)
} else {
return null
}
Expand Down
5 changes: 5 additions & 0 deletions zero.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const DefaultFixture = require('./subproviders/default-fixture.js')
const NonceTrackerSubprovider = require('./subproviders/nonce-tracker.js')
const CacheSubprovider = require('./subproviders/cache.js')
const FilterSubprovider = require('./subproviders/filters.js')
const InflightCacheSubprovider = require('./subproviders/inflight-cache')
const HookedWalletSubprovider = require('./subproviders/hooked-wallet.js')
const SanitizingSubprovider = require('./subproviders/sanitizer.js')
const RpcSubprovider = require('./subproviders/rpc.js')
Expand Down Expand Up @@ -36,6 +37,10 @@ function ZeroClientProvider(opts){
const filterSubprovider = new FilterSubprovider()
engine.addProvider(filterSubprovider)

// inflight cache
const inflightCache = new InflightCacheSubprovider()
engine.addProvider(inflightCache)

// id mgmt
const idmgmtSubprovider = new HookedWalletSubprovider({
// accounts
Expand Down

0 comments on commit 30f993e

Please sign in to comment.