區塊鏈入門教程以太坊原始碼分析node包建立多重協議eth節點

買賣虛擬貨幣

node包建立多重協議eth節點

node包建立多重協議以太坊節點

一個node是一組服務,透過共享資源提供RPC API。
Services提供devp2p協議,當node例項開始執行,服務被wire到devp2p網路

Node管理資源

Node例項使用到的所有檔案系統資源被放到data目錄中。
每個資源的路徑可以透過額外的node配置改寫。
data目錄是可選的。==如果沒有設定或資源路徑沒有指定,node包會在記憶體中建立資源。==

配置Node並開啟p2p服務,來訪問devp2p網路。
每個devp2p網路上的host有一個唯一識別符號,node key.
在重啟過程中,Node例項維持這個key。
Node載入static的和trusted可信的節點列表,保證關於其他hosts的知識持久化。

JSON-RPC伺服器可以在Node上啟動,上面執行著HTTP,WebSocket,IPC。
已註冊服務提供的RPC模組,將透過透過這些endpoints提供。
使用者可以限制任何endpoint為RPC模組的子集。
Node自身提供debug,admin,web3模組。

透過service context,服務實現可以開啟LevelDB資料庫。
node包選擇每個資料庫的檔案系統位置。
如果node配置為沒有data目錄執行,databases替換為記憶體開啟。

Node建立共享的加密的以太坊賬戶keys的store,Services能夠透過service context
訪問account manager

在例項之間共享資料目錄

如果Multiple node有區別的例項名稱,他們能夠共享一個資料目錄。
共享行為依賴於資源的型別。

devp2p相關資源(node key,static/trusted node列表,known hosts database)儲存到與例項名相同的目錄中。

LevelDB資料庫也儲存到例項子目錄中。
如果多節點例項使用同一data目錄,使用唯一名稱開啟資料庫將為每一個例項建立一個資料庫。

賬戶key store在所有node之間共享,使用一個data目錄。
其location可以透過KeyStoreDir配置項修改。

Data Directory Sharing Example見doc.go

本包主要class結構

配置類代表配置項集合,用於微調P2P協議棧的網路層。這些值能被所有註冊服務進一步擴充套件
Config
|-DataDir 檔案系統目錄,node可將其用於任何資料儲存需求。
|-P2P P2P網路的配置
|-KeyStoreDir 不指定,New會建立臨時目錄,node停止時銷燬
|-IPCPath IPC存放IPC endpoint的請求路徑。空路徑disable IPC
|-HTTPHost Host interface,在其上開啟HTTP RPC服務。
|-HTTPPort HTTP RPC服務使用的TCP埠號
|-HTTPModules 透過HTTP RCP介面暴露的API模組列表
|-StaticNodes() 解析static-nodes.json檔案,返回配置的靜態節點enode URLs列表
|-TrustedNodes() 解析trusted-nodes.json檔案,返回配置的靜態節點enode URLs列表
|-NodeDB() returns the path to the discovery node database
|-NodeKey() 檢索當前節點配置的私鑰,先檢查手動設定key,失敗再查配置data目錄,都沒有,新生成。

Node
|-eventmux Event multiplexer used between the services of a stack
|-config
|-accman Manager is an overarching account manager that can communicate with various backends for signing transactions
|-instanceDirLock prevents concurrent use of instance directory
|-serverConfig p2p配置
|-server Server manages all peer connections
|-serviceFuncs ServiceConstructor is the function signature of the constructors
|-services Currently running services
|-rpcAPIs List of APIs currently provided by the node
|-inprocHandler In-process RPC request handler to process the API requests
|-ipc\\http\\ws屬性

備註:
1、Server represents a RPC server
2、// API describes the set of methods offered over the RPC interface
type API struct {
Namespace string // namespace under which the rpc methods of Service are exposed
Version string // api version for DApp\'s
Service interface{} // receiver instance which holds the methods
Public bool // indication if the methods must be considered safe for public use
}

Service
|-Protocols() Protocols retrieves the P2P protocols the service wishes to start.
|-APIs() APIs retrieves the list of RPC descriptors the service provides
|-Start(server *p2p.Server)
|-Stop()

ServiceContext
|-config
|-services Index of the already constructed services
|-EventMux Event multiplexer used for decoupled notifications
|-AccountManager Account manager created by the node.
|-OpenDatabase() 開啟指定資料庫,透過node data目錄。如果是臨時節點,返回記憶體資料庫
|-Service() 檢索指定型別的執行服務

PrivateAdminAPI
|-AddPeer()
// Try to add the url as a static peer and return
node, err := discover.ParseNode(url)
|-RemovePeer() RemovePeer disconnects from a a remote node if the connection exists
|-PeerEvents()

PublicAdminAPI

免責聲明:

  1. 本文版權歸原作者所有,僅代表作者本人觀點,不代表鏈報觀點或立場。
  2. 如發現文章、圖片等侵權行爲,侵權責任將由作者本人承擔。
  3. 鏈報僅提供相關項目信息,不構成任何投資建議

推荐阅读

;