Skip to content

Commit

Permalink
subp - static -> fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
kumavis committed Jan 27, 2016
1 parent 17c6b5a commit de216e6
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 40 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The provider engine also handles caching of rpc request results.
```js
const ProviderEngine = require('web3-provider-engine')
const CacheSubprovider = require('web3-provider-engine/subproviders/cache.js')
const StaticSubprovider = require('web3-provider-engine/subproviders/static.js')
const FixtureSubprovider = require('web3-provider-engine/subproviders/fixture.js')
const FilterSubprovider = require('web3-provider-engine/subproviders/filters.js')
const VmSubprovider = require('web3-provider-engine/subproviders/vm.js')
const LightWalletSubprovider = require('web3-provider-engine/subproviders/lightwallet.js')
Expand All @@ -27,7 +27,7 @@ var web3 = new Web3(engine)
engine.addProvider(new CacheSubprovider())

// static results
engine.addProvider(new StaticSubprovider({
engine.addProvider(new FixtureSubprovider({
web3_clientVersion: 'MetaMask-ProviderEngine/v0.0.0/javascript',
net_listening: true,
eth_hashrate: '0x0',
Expand Down
4 changes: 2 additions & 2 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const CacheSubprovider = require('./subproviders/cache.js')
const RpcSubprovider = require('./subproviders/rpc.js')
const VmSubprovider = require('./subproviders/vm.js')
const FilterSubprovider = require('./subproviders/filters.js')
const DefaultStatic = require('./subproviders/default-static.js')
const DefaultFixture = require('./subproviders/default-fixture.js')
const LightWalletSubprovider = require('./subproviders/lightwallet.js')

module.exports = zeroClientProvider
Expand All @@ -20,7 +20,7 @@ function zeroClientProvider(opts){
engine.addProvider(cacheSubprovider)

// static
var staticSubprovider = new DefaultStatic()
var staticSubprovider = new DefaultFixture()
engine.addProvider(staticSubprovider)

// filters
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const inherits = require('util').inherits
const StaticProvider = require('./static.js')
const FixtureProvider = require('./fixture.js')

module.exports = DefaultStatic
module.exports = DefaultFixtures

inherits(DefaultStatic, StaticProvider)
inherits(DefaultFixtures, FixtureProvider)

function DefaultStatic() {
function DefaultFixtures() {
const self = this
var responses = {
web3_clientVersion: 'MetaMask-ZeroClient/v0.0.0/javascript',
Expand All @@ -17,5 +17,5 @@ function DefaultStatic() {
eth_mining: false,
eth_syncing: true,
}
StaticProvider.call(self, responses)
FixtureProvider.call(self, responses)
}
27 changes: 27 additions & 0 deletions subproviders/fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const inherits = require('util').inherits
const Subprovider = require('./subprovider.js')

module.exports = FixtureProvider

inherits(FixtureProvider, Subprovider)

function FixtureProvider(staticResponses){
const self = this
staticResponses = staticResponses || {}
self.staticResponses = staticResponses
}

FixtureProvider.prototype.handleRequest = function(payload, next, end){
const self = this
var staticResponse = self.staticResponses[payload.method]
// async function
if ('function' === typeof staticResponse) {
staticResponse(end)
// static response - null is valid response
} else if (staticResponse !== undefined) {
end(null, staticResponse)
// no prepared response - skip
} else {
next()
}
}
22 changes: 0 additions & 22 deletions subproviders/static.js

This file was deleted.

8 changes: 4 additions & 4 deletions test/basic.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const test = require('tape')
const ProviderEngine = require('../index.js')
const PassthroughProvider = require('./util/passthrough.js')
const StaticProvider = require('../subproviders/static.js')
const FixtureProvider = require('../subproviders/fixture.js')
const TestBlockProvider = require('./util/block.js')
const createPayload = require('../util/create-payload.js')
const injectMetrics = require('./util/inject-metrics')
Expand All @@ -13,9 +13,9 @@ test('fallthrough test', function(t){
// handle nothing
var providerA = injectMetrics(new PassthroughProvider())
// handle "test_rpc"
var providerB = injectMetrics(new StaticProvider({
test_rpc: true,
}))
var providerB = injectMetrics(new FixtureProvider({
test_rpc: true,
}))
// handle block requests
var providerC = injectMetrics(new TestBlockProvider())

Expand Down
4 changes: 2 additions & 2 deletions test/cache.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const test = require('tape')
const ProviderEngine = require('../index.js')
const StaticProvider = require('../subproviders/static.js')
const FixtureProvider = require('../subproviders/fixture.js')
const CacheProvider = require('../subproviders/cache.js')
const TestBlockProvider = require('./util/block.js')
const createPayload = require('../util/create-payload.js')
Expand Down Expand Up @@ -30,7 +30,7 @@ function cacheTest(label, payload){
// cache layer
var cacheProvider = injectMetrics(new CacheProvider())
// handle balance
var dataProvider = injectMetrics(new StaticProvider({
var dataProvider = injectMetrics(new FixtureProvider({
eth_getBalance: '0xdeadbeef',
}))
// handle dummy block
Expand Down
6 changes: 3 additions & 3 deletions test/util/passthrough.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const inherits = require('util').inherits
const StaticProvider = require('../../subproviders/static.js')
const FixtureProvider = require('../../subproviders/fixture.js')

module.exports = PassthroughProvider

Expand All @@ -8,8 +8,8 @@ module.exports = PassthroughProvider
// mostly useless
//

inherits(PassthroughProvider, StaticProvider)
inherits(PassthroughProvider, FixtureProvider)
function PassthroughProvider(methods){
const self = this
StaticProvider.call(self, {})
FixtureProvider.call(self, {})
}

0 comments on commit de216e6

Please sign in to comment.