Git Product home page Git Product logo

provider-engine's Introduction

Web3 ProviderEngine

Greenkeeper badge

Web3 ProviderEngine is a tool for composing your own web3 providers.

Status: WIP - expect breaking changes and strange behaviour

Composable

Built to be modular - works via a stack of 'sub-providers' which are like normal web3 providers but only handle a subset of rpc methods.

The subproviders can emit new rpc requests in order to handle their own; e.g. eth_call may trigger eth_getAccountBalance, eth_getCode, and others. The provider engine also handles caching of rpc request results.

const ProviderEngine = require('web3-provider-engine')
const CacheSubprovider = require('web3-provider-engine/subproviders/cache.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 HookedWalletSubprovider = require('web3-provider-engine/subproviders/hooked-wallet.js')
const NonceSubprovider = require('web3-provider-engine/subproviders/nonce-tracker.js')
const RpcSubprovider = require('web3-provider-engine/subproviders/rpc.js')

var engine = new ProviderEngine()
var web3 = new Web3(engine)

// static results
engine.addProvider(new FixtureSubprovider({
  web3_clientVersion: 'ProviderEngine/v0.0.0/javascript',
  net_listening: true,
  eth_hashrate: '0x00',
  eth_mining: false,
  eth_syncing: true,
}))

// cache layer
engine.addProvider(new CacheSubprovider())

// filters
engine.addProvider(new FilterSubprovider())

// pending nonce
engine.addProvider(new NonceSubprovider())

// vm
engine.addProvider(new VmSubprovider())

// id mgmt
engine.addProvider(new HookedWalletSubprovider({
  getAccounts: function(cb){ ... },
  approveTransaction: function(cb){ ... },
  signTransaction: function(cb){ ... },
}))

// data source
engine.addProvider(new RpcSubprovider({
  rpcUrl: 'https://testrpc.metamask.io/',
}))

// log new blocks
engine.on('block', function(block){
  console.log('================================')
  console.log('BLOCK CHANGED:', '#'+block.number.toString('hex'), '0x'+block.hash.toString('hex'))
  console.log('================================')
})

// network connectivity error
engine.on('error', function(err){
  // report connectivity errors
  console.error(err.stack)
})

// start polling for blocks
engine.start()

Built For Zero-Clients

The Ethereum JSON RPC was not designed to have one node service many clients. However a smaller, lighter subset of the JSON RPC can be used to provide the blockchain data that an Ethereum 'zero-client' node would need to function. We handle as many types of requests locally as possible, and just let data lookups fallback to some data source ( hosted rpc, blockchain api, etc ). Categorically, we don’t want / can’t have the following types of RPC calls go to the network:

  • id mgmt + tx signing (requires private data)
  • filters (requires a stateful data api)
  • vm (expensive, hard to scale)

Change Log

13.0.0
12.0.0
11.0.0
  • zero.js - replaced http subprovider with fetch provider (includes polyfill for node).
10.0.0
  • renamed HookedWalletSubprovider personalRecoverSigner to recoverPersonalSignature
9.0.0
  • pollingShouldUnref option now defaults to false

Current RPC method support:

static
  • web3_clientVersion
  • net_version
  • net_listening
  • net_peerCount
  • eth_protocolVersion
  • eth_hashrate
  • eth_mining
  • eth_syncing
filters
  • eth_newBlockFilter
  • eth_newPendingTransactionFilter
  • eth_newFilter
  • eth_uninstallFilter
  • eth_getFilterLogs
  • eth_getFilterChanges
accounts manager
vm
  • eth_call
  • eth_estimateGas
db source
  • db_putString
  • db_getString
  • db_putHex
  • db_getHex
compiler
  • eth_getCompilers
  • eth_compileLLL
  • eth_compileSerpent
  • eth_compileSolidity
shh gateway
  • shh_version
  • shh_post
  • shh_newIdentity
  • shh_hasIdentity
  • shh_newGroup
  • shh_addToGroup
data source ( fallback to rpc )
  • eth_gasPrice
  • eth_blockNumber
  • eth_getBalance
  • eth_getBlockByHash
  • eth_getBlockByNumber
  • eth_getBlockTransactionCountByHash
  • eth_getBlockTransactionCountByNumber
  • eth_getCode
  • eth_getStorageAt
  • eth_getTransactionByBlockHashAndIndex
  • eth_getTransactionByBlockNumberAndIndex
  • eth_getTransactionByHash
  • eth_getTransactionCount
  • eth_getTransactionReceipt
  • eth_getUncleByBlockHashAndIndex
  • eth_getUncleByBlockNumberAndIndex
  • eth_getUncleCountByBlockHash
  • eth_getUncleCountByBlockNumber
  • eth_sendRawTransaction
  • eth_getLogs ( not used in web3.js )
( not supported )
  • eth_getWork
  • eth_submitWork
  • eth_submitHashrate ( not used in web3.js )

provider-engine's People

Contributors

2-am-zzz avatar aakilfernandes avatar anton44eg avatar axic avatar chejazi avatar danfinlay avatar denisgranha avatar ethers avatar fabioberger avatar flyswatter avatar frankiebee avatar greenkeeper[bot] avatar istoramandiri avatar jamesyoung avatar jorpic avatar kumavis avatar lastperson avatar logvinovleon avatar mbeaudru avatar mhhf avatar mvayngrib avatar tcoulter avatar ukstv avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.