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的更多相关文章

  1. metamask源码学习-ui/index.js

    The UI-即上图左下角metamask-ui部分,即其图形化界面 The MetaMask UI is essentially just a website that can be configu ...

  2. metamask源码学习导论

    ()MetaMask Browser Extension https://github.com/MetaMask/metamask-extension 这就是整个metamask的源码所在之处,好好看 ...

  3. metamask源码学习-metamask-controller.js

    The MetaMask Controller——The central metamask controller. Aggregates other controllers and exports a ...

  4. metamask源码学习-controller-transaction

    ()metamask-extension/app/scripts/controllers/transactions Transaction Controller is an aggregate of ...

  5. metamask源码学习-inpage.js

    The most confusing part about porting MetaMask to a new platform is the way we provide the Web3 API ...

  6. metamask源码学习-background.js

    这个就是浏览器后台所进行操作的地方了,它就是页面也区块链进行交互的中间部分. metamask-background描述了为web扩展单例的文件app/scripts/background.js.该上 ...

  7. metamask源码学习-contentscript.js

    When a new site is visited, the WebExtension creates a new ContentScript in that page's context, whi ...

  8. MVC系列——MVC源码学习:打造自己的MVC框架(四:了解神奇的视图引擎)

    前言:通过之前的三篇介绍,我们基本上完成了从请求发出到路由匹配.再到控制器的激活,再到Action的执行这些个过程.今天还是趁热打铁,将我们的View也来完善下,也让整个系列相对完整,博主不希望烂尾. ...

  9. MVC系列——MVC源码学习:打造自己的MVC框架(三:自定义路由规则)

    前言:上篇介绍了下自己的MVC框架前两个版本,经过两天的整理,版本三基本已经完成,今天还是发出来供大家参考和学习.虽然微软的Routing功能已经非常强大,完全没有必要再“重复造轮子”了,但博主还是觉 ...

随机推荐

  1. .net导出excle无需任何插件,直接通过一个tablehtml实现

    项目背景: 项目需要导出样式复杂的excl表格,主要是一些样式布局比较复杂 技术分析: 目前比较通用的实现方式有 1.借助微软的excle插件 2.通过NPOI插件实现 3.直接导出一个html(ta ...

  2. c# API接受图片文件以文件格式上传图片

    /// 文件图片上传 /// </summary> /// <returns>成功上传返回上传后的文件名</returns> [HttpPost] public a ...

  3. EF 查询所有字段

    1简单方式 var query=db.StudentScore.Where(r=> r.SubjectId==subjectId).Select(g=>g).ToList(); 2 var ...

  4. 列表与for循环

    一.list列表 1.概述 变量:使用变量存储数据,但是,有一个缺点:一个变量每次只能存储一个数据 #需求:存储5个人的年龄,求他们的平均年龄 age1 = 29 age2 = 36 age3 = 3 ...

  5. 【读书笔记】iOS-iPad与iPhone

    在开发通用型应用的时候,你总是需要记住,iPad并不是一个大大的iPod touch,为iPad开发的应用的界面应该更好地利用iPad的大屏幕,而不应该是iPhone应用的复制品. 参考资料:< ...

  6. 【读书笔记】iOS-深入解剖对等网络

    协议本身是一个运行在UDP之上的定制协议.我所以决定使用一个定制协议很简单.首先,当前这个任务看起来足够简单,因此与尝试改进一个现在协议相比,直接构建一个定制协议更为容易.其次,定制协议可以将开销减少 ...

  7. layer插件layer.photos()动态插入的图片无法正常显示

    layer插件layer.photos()动态插入的图片无法正常显示,点击后面插入的图片,显示的是之前的图片列表,再次点击又是正常 有朋友遇到同样的问题 http://fly.layui.com/ji ...

  8. 关于Bootstrap fileinput 上传新文件,移除时触发服务器同步删除的配置

    在Bootstrap fileinput中移除预览文件时可以通过配置initialPreviewConfig: [ { url:'deletefile',key:fileid } ] 来同步删除服务器 ...

  9. 接口自动化 基于python+Testlink+Jenkins实现的接口自动化测试框架[V2.0改进版]

    基于python+Testlink+Jenkins实现的接口自动化测试框架[V2.0改进版]   by:授客 QQ:1033553122 由于篇幅问题,,暂且采用网盘分享的形式: 下载地址: [授客] ...

  10. Android--实现ViewPager边界回弹效果(转)

    该View转自   http://blog.csdn.net/Kalwang/article/details/4708721  ,感谢这位大神. public class BounceBackView ...