https://github.com/MetaMask/metamask-extension/tree/master/app/scripts/controllers/network
metamask-extension/app/scripts/controllers/network/network.js
const assert = require('assert')
const EventEmitter = require('events')
const createMetamaskProvider = require('web3-provider-engine/zero.js')
const SubproviderFromProvider = require('web3-provider-engine/subproviders/provider.js')
const createInfuraProvider = require('eth-json-rpc-infura/src/createProvider')
const ObservableStore = require('obs-store')//ObservableStore是一个内存中的同步存储,只存储一个值,用于订阅更新。详细看https://github.com/MetaMask/obs-store
const ComposedStore = require('obs-store/lib/composed')
const extend = require('xtend')
const EthQuery = require('eth-query')
const createEventEmitterProxy = require('../../lib/events-proxy.js')
const log = require('loglevel')
const urlUtil = require('url')
const {//这就是我们在metamask中看见的五种网络
ROPSTEN,
RINKEBY,
KOVAN,
MAINNET,
LOCALHOST,
} = require('./enums')
const LOCALHOST_RPC_URL = 'http://localhost:8545' //本地网络设置的host:port信息
const INFURA_PROVIDER_TYPES = [ROPSTEN, RINKEBY, KOVAN, MAINNET] //infura支持的网络的类型有这四种 const env = process.env.METAMASK_ENV //process是nodejs中的一个全局变量,metamask-extension/package.json进行METAMASK_ENV设置,设置为test
const METAMASK_DEBUG = process.env.METAMASK_DEBUG //如果之前没有设置则为false,这是在调用网页的时候进行设置的window.METAMASK_DEBUG = true
const testMode = (METAMASK_DEBUG || env === 'test') //如果满足其中之一则说明使用的是测试模式 const defaultProviderConfig = {
type: testMode ? RINKEBY : MAINNET,//如果是测试模式,则自动设置为RINKEBY,否则为MAINNET
} module.exports = class NetworkController extends EventEmitter { constructor (opts = {}) {//如果没有opts传入,则默认为{}
super() // parse options
const providerConfig = opts.provider || defaultProviderConfig
// create stores
this.providerStore = new ObservableStore(providerConfig)//即上面的provider,到底是default的RINKEBY或MAINNET还是用户自己传进来的provider
this.networkStore = new ObservableStore('loading') //network处于加载状态
this.store = new ComposedStore({ provider: this.providerStore, network: this.networkStore }) //即将两个store组合起来
// create event emitter proxy
this._proxy = createEventEmitterProxy() this.on('networkDidChange', this.lookupNetwork)
} initializeProvider (_providerParams) { //初始化provider
this._baseProviderParams = _providerParams
const { type, rpcTarget } = this.providerStore.getState() //得到此时provider的状态,type即ROPSTEN, RINKEBY, KOVAN, MAINNET四种之一,rpcTarget即连接的网络信息
this._configureProvider({ type, rpcTarget }) //配置provider,rpcTarget即比如http://localhost:8545
this._proxy.on('block', this._logBlock.bind(this))
this._proxy.on('error', this.verifyNetwork.bind(this))
this.ethQuery = new EthQuery(this._proxy)
this.lookupNetwork()
return this._proxy
} verifyNetwork () {
// Check network when restoring connectivity:
if (this.isNetworkLoading()) this.lookupNetwork()
} getNetworkState () {
return this.networkStore.getState()
} setNetworkState (network) {
return this.networkStore.putState(network)
} isNetworkLoading () {
return this.getNetworkState() === 'loading'
} lookupNetwork () {
// Prevent firing when provider is not defined.
if (!this.ethQuery || !this.ethQuery.sendAsync) {
return log.warn('NetworkController - lookupNetwork aborted due to missing ethQuery')
}
this.ethQuery.sendAsync({ method: 'net_version' }, (err, network) => {
if (err) return this.setNetworkState('loading')
log.info('web3.getNetwork returned ' + network)
this.setNetworkState(network)
})
} setRpcTarget (rpcTarget) {
const providerConfig = {
type: 'rpc',
rpcTarget,
}
this.providerConfig = providerConfig
} async setProviderType (type) {
assert.notEqual(type, 'rpc', `NetworkController - cannot call "setProviderType" with type 'rpc'. use "setRpcTarget"`)
assert(INFURA_PROVIDER_TYPES.includes(type) || type === LOCALHOST, `NetworkController - Unknown rpc type "${type}"`)
const providerConfig = { type }
this.providerConfig = providerConfig
} resetConnection () {
this.providerConfig = this.getProviderConfig()
} set providerConfig (providerConfig) {
this.providerStore.updateState(providerConfig)
this._switchNetwork(providerConfig)
} getProviderConfig () {
return this.providerStore.getState()
} //
// Private
// _switchNetwork (opts) {
this.setNetworkState('loading')
this._configureProvider(opts)
this.emit('networkDidChange')
} _configureProvider (opts) {
const { type, rpcTarget } = opts
// infura type-based endpoints
const isInfura = INFURA_PROVIDER_TYPES.includes(type)//type即infura中支持的那四种网络的类型ROPSTEN, RINKEBY, KOVAN, MAINNET,metamask的这四种网络的provider都是使用了infura
if (isInfura) {//是就调用infura配置this._configureInfuraProvider(opts)
// other type-based rpc endpoints
} else if (type === LOCALHOST) {//否则就查看它是否使用了本地的8545接口,如ganache
this._configureStandardProvider({ rpcUrl: LOCALHOST_RPC_URL })
// url-based rpc endpoints
} else if (type === 'rpc') {//或者是自己设置的其他区块链接口
this._configureStandardProvider({ rpcUrl: rpcTarget })
} else {
throw new Error(`NetworkController - _configureProvider - unknown type "${type}"`)//没有连接到这样的接口时报错
}
} _configureInfuraProvider ({ type }) {
log.info('_configureInfuraProvider', type)
const infuraProvider = createInfuraProvider({ network: type })
const infuraSubprovider = new SubproviderFromProvider(infuraProvider)
const providerParams = extend(this._baseProviderParams, {
engineParams: {
pollingInterval: ,
blockTrackerProvider: infuraProvider,
},
dataSubprovider: infuraSubprovider,
})
const provider = createMetamaskProvider(providerParams)
this._setProvider(provider)
} _configureStandardProvider ({ rpcUrl }) {
// urlUtil handles malformed urls
rpcUrl = urlUtil.parse(rpcUrl).format()
const providerParams = extend(this._baseProviderParams, {
rpcUrl,
engineParams: {
pollingInterval: ,
},
})
const provider = createMetamaskProvider(providerParams)
this._setProvider(provider)
} _setProvider (provider) {
// collect old block tracker events
const oldProvider = this._provider
let blockTrackerHandlers
if (oldProvider) {
// capture old block handlers
blockTrackerHandlers = oldProvider._blockTracker.proxyEventHandlers
// tear down
oldProvider.removeAllListeners()
oldProvider.stop()
}
// override block tracler
provider._blockTracker = createEventEmitterProxy(provider._blockTracker, blockTrackerHandlers)
// set as new provider
this._provider = provider
this._proxy.setTarget(provider)
} _logBlock (block) {
log.info(`BLOCK CHANGED: #${block.number.toString('hex')} 0x${block.hash.toString('hex')}`)
this.verifyNetwork()
}
}
metamask-extension/app/scripts/controllers/network/enums.js
有关网络的配置信息
const ROPSTEN = 'ropsten'
const RINKEBY = 'rinkeby'
const KOVAN = 'kovan'
const MAINNET = 'mainnet'
const LOCALHOST = 'localhost'
//各个网络的networkId值
const MAINNET_CODE =
const ROPSTEN_CODE =
const RINKEYBY_CODE =
const KOVAN_CODE =
//各个网络在metamask上的显示名字
const ROPSTEN_DISPLAY_NAME = 'Ropsten'
const RINKEBY_DISPLAY_NAME = 'Rinkeby'
const KOVAN_DISPLAY_NAME = 'Kovan'
const MAINNET_DISPLAY_NAME = 'Main Ethereum Network' module.exports = {
ROPSTEN,
RINKEBY,
KOVAN,
MAINNET,
LOCALHOST,
MAINNET_CODE,
ROPSTEN_CODE,
RINKEYBY_CODE,
KOVAN_CODE,
ROPSTEN_DISPLAY_NAME,
RINKEBY_DISPLAY_NAME,
KOVAN_DISPLAY_NAME,
MAINNET_DISPLAY_NAME,
}
metamask源码学习-controllers-network的更多相关文章
-
metamask源码学习-ui/index.js
The UI-即上图左下角metamask-ui部分,即其图形化界面 The MetaMask UI is essentially just a website that can be configu ...
-
metamask源码学习导论
()MetaMask Browser Extension https://github.com/MetaMask/metamask-extension 这就是整个metamask的源码所在之处,好好看 ...
-
metamask源码学习-metamask-controller.js
The MetaMask Controller——The central metamask controller. Aggregates other controllers and exports a ...
-
metamask源码学习-controller-transaction
()metamask-extension/app/scripts/controllers/transactions Transaction Controller is an aggregate of ...
-
metamask源码学习-inpage.js
The most confusing part about porting MetaMask to a new platform is the way we provide the Web3 API ...
-
metamask源码学习-background.js
这个就是浏览器后台所进行操作的地方了,它就是页面也区块链进行交互的中间部分. metamask-background描述了为web扩展单例的文件app/scripts/background.js.该上 ...
-
metamask源码学习-contentscript.js
When a new site is visited, the WebExtension creates a new ContentScript in that page's context, whi ...
-
MVC系列——MVC源码学习:打造自己的MVC框架(四:了解神奇的视图引擎)
前言:通过之前的三篇介绍,我们基本上完成了从请求发出到路由匹配.再到控制器的激活,再到Action的执行这些个过程.今天还是趁热打铁,将我们的View也来完善下,也让整个系列相对完整,博主不希望烂尾. ...
-
MVC系列——MVC源码学习:打造自己的MVC框架(三:自定义路由规则)
前言:上篇介绍了下自己的MVC框架前两个版本,经过两天的整理,版本三基本已经完成,今天还是发出来供大家参考和学习.虽然微软的Routing功能已经非常强大,完全没有必要再“重复造*”了,但博主还是觉 ...
随机推荐
-
【极力分享】[C#/.NET]Entity Framework(EF) Code First 多对多关系的实体增,删,改,查操作全程详细示例【转载自https://segmentfault.com/a/1190000004152660】
[C#/.NET]Entity Framework(EF) Code First 多对多关系的实体增,删,改,查操作全程详细示例 本文我们来学习一下在Entity Framework中使用Cont ...
-
miniprofiler的对数据库的监测使用。以nancy,petapoco为例
miniprofiler的使用 miniprofiler的详细介绍请看这里http://miniprofiler.com/.(可以对数据库和页面等监控如 ado ef mvc mongodb) 本文以 ...
-
java web 中的servlet讲解
首先,解释一下解释一下什么是servlet?说一说Servlet的生命周期? servlet有良好的生存期的定义,包括加载和实例化.初始化.处理请求以及服务结束.这个生存期由javax.servlet ...
-
关于nginx配置的不完全总结
请参考官方: http://wiki.nginx.org/HttpRewriteModule#Synopsis 关于缓存大小权限 error log 有如下日志:an upstream respons ...
-
[Locked] Sparse Matrix Multiplication
Given two sparse matrices A and B, return the result of AB. You may assume that A's column number is ...
-
【ArcGIS 10.2新特性】ArcGIS 10.2 for Desktop 新特性(一)
ArcGIS 10.2 for Desktop是在10.1的成功基础上进行的改进,它的改进包括:性能提升.附加的安全性.40多个新的分析工具.3D功能提高.栅格增强.新的地理数据管理能力以及其它更多的 ...
-
C#异常性能影响
何谓异常 很多人在讨论异常的时候很模糊,仿佛所谓异常就是try{}catch{},异常就是Exception,非常的片面,所以导致异常影响性能,XXXX……等很多奇怪的言论,所以在此我意在对异常正名. ...
-
hibernate------->;第一个程序
今天学习hibernate . 创建User类: package com.hibernate; import java.util.Date; public class User { private I ...
-
接口(迭代器) Iterator
Iterator接口简介 在程序开发中,经常需要遍历集合中的所有元素.针对这种需求,JDK专门提供了一个接口java.util.Iterator.Iterator接口也是Java集合中的一员,但它与C ...
-
[原创]Zynq AXI-CDMA的使用
Xilinx 提供了3种DMA AXI-DMA AXI-CDMA AXI-VDMA 使用CDMA能够满足项目需求(MM-MM),DS文档介绍如下: The Xilinx LogiCORE™ IP AX ...