Categorygithub.com/ionchain/ionchain-core
modulepackage
1.9.0
Repository: https://github.com/ionchain/ionchain-core.git
Documentation: pkg.go.dev

# README

ionchain-core

本项目是ionchain协议golang版本的实现

在这篇博客中IPOS共识算法设计,详细说明了ionchain的共识算法的设计详情。

源码编译

在编译之前,你现需要安装golang(版本大于等于1.10)和C编译器

clone项目到你指定的目录中:

git clone https://github.com/ionchain/ionchain-core

使用以下命令编译ionc

cd ionchain-core

make ionc

或者可以通过以下命令编译其他平台的ionc版本(Linuxwindows

make all

在ionchain主网上运行全节点

用户在ionchain是最多的使用场景就是:创建账号,转移资产,部署、调用合约。为了满足这个特定的场景,可以使用快速同步方法来启动网络,执行以下命令:

$ ionc console

上面这个命令将产生以下两个操作:

  • 在快速同步模式下,启动ionc节点,在快速同步模式下,节点将会下载所有的状态数据,而不是执行所有ionchain网络上的所有交易.
  • 开启一个内置的javascript console控制台,在控制台中用户可以与ionchain网络进行交互。

使用Docker快速启动节点

启动ionchain网络最快速的方式就是在本地启动一个Docker

docker run -d --name ionchain-node -v /Users/alice/ionchain:/root \
           -p 8545:8545 -p 30303:30303 \
           ionchain/go-ionchain

docker会在/Users/alice/ionchain本地目录中映射一个持久的volume用来存储区块,同时也会映射默认端口。如果你想从其他容器或主机通过RPC方式访问运行的节点,需要加上--rpcaddr 0.0.0.0参数。默认情况下,ionc绑定的本地接口与RPC端点是不能从外部访问的。

以编程的方式与IONC节点交互

作为一个开发人员想通过自己的程序与ionchain网络进行交互,而不是通过JavaScript console的方式,为了满足这种需求,ionc有一个内置JSON-RPC API,这些API通过HTTPWebSocketsIPC方式暴露出去。其中IPC端口是默认开启的,HTTPWS端口需要手动开启,同时为了安全方面的考虑,这两个端口只会暴露部分API。

基于HTTP的JSON-RPC API 选项:

  • --rpc 开启 HTTP-RPC 服务
  • --rpcaddr HTTP-RPC 服务监听地址 (默认: "localhost")
  • --rpcport HTTP-RPC 服务监听端口 (默认: 8545)
  • --rpcapi 通过HTTP暴露出可用的API
  • --rpccorsdomain 逗号分隔的一系列域,通过这些域接收跨域请求

基于WebSocket的 JSON-RPC API选项:

  • --ws 开启 WS-RPC 服务
  • --wsaddr WS-RPC 服务监听地址(默认: "localhost")
  • --wsport WS-RPC 服务监听端口 (默认: 8546)
  • --wsapi 通过WS-PRC暴露出可用的API

基于IPC的JSON-RPC AP选项

  • --ipcdisable 禁用 IPC-RPC 服务
  • --ipcapi 通过IPC-PRC暴露出可用的API

注意:在使用http/ws接口之前,你需要了解相关的安全知识,在公网上,黑客会利用节点在公网上暴露的接口进行破坏式的攻击

创建一个私有链

创建一个自己的私有链会有一点复杂,因为你需要手动修改很多官方创建文件的配置。

定义私有链创世块

首先,为你的私有网络创建一个创始状态,这个创始状态需要你的私有网络中的所有节点都知晓,并达成共识。genesis.json以JSON格式组成:

{
		  "config": {
			"chainId":
		  },
		  "alloc": {},
			"0x0000000000000000000000000000000000000100": {
			  "code": "编译后的保证金合约二进制代码",
			  "storage": {
				"0x0000000000000000000000000000000000000000000000000000000000000000": "0x0a",
				"0x33d4e30ad2c3b9f507062560fe978acc29929f1ee5c2c33abe6d050171fd8c93": "0x0de0b6b3a7640000",
				"0xe0811e07d38b83ef44191e63c263ef79eeed21f1260fd00fef00a37495c1accc": "0xd9a7c07f349d4ac7640000"
			  },
			  "balance": ""
			}
		  },
		  "coinbase": "0x0000000000000000000000000000000000000000",
		  "difficulty": "0x01",
		  "extraData": "0x777573686f756865",
		  "gasLimit": "0x47e7c4",
		  "nonce": "0x0000000000000001",
		  "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
		  "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
		  "timestamp": "0x00",
		  "baseTarget": "0x1bc4fd6588",
		  "blockSignature": "0x00",
		  "generationSignature": "0x00"
		}

以上关于保证金合约是如何创建、编译的将在另外一个项目中做详细说明,我们建议你修改nonce值为一个随机数,这样可以防止未知的远程节点连接到你的网络中。如果你需要给某些账户预设一些资金,可以使用修改alloc值:

"alloc": {
  "0x0000000000000000000000000000000000000001": {"balance": "111111111"},
  "0x0000000000000000000000000000000000000002": {"balance": "222222222"}
}

genesis.json文件创建完成时,你需要在所有的ionc节点执行初始化操作。

$ ionc init path/to/genesis.json

创建bootnode节点

当所有的节点都完成初始化时,你需要启动一个bootstrap节点,通过bootstrap节点可以帮助其他的节点之间进行相互发现,这样他们就可以通过网络连接在一起。

$ bootnode --genkey=boot.key
$ bootnode --nodekey=boot.key

启动节点

bootnode启动后,通过telnet <ip> <port>命令测试一下是否可以从外部访问bootnode,现在启动所有的ionc节点,在启动时加上--bootnodes选项。同时为了保存你的私有链上的数据,你需要创建一个datadir目录,并通过--datadir选项设置。

$ ionc --datadir=path/to/custom/data/folder --bootnodes=<bootnode-enode-url-from-above>

注意:从现在开始你的节点已经完全从主链网络上断开,现在你可以配置miner来处理交易,并创建新的区块

运行私有链miner

ionc网络中miner的算力是通过在保证金合约的保证金数量决定的,启动一个实例用于挖矿:

$ ionc <usual-flags> --mine --etherbase=0x0000000000000000000000000000000000000000

其中—etherbase设置为miner的账号地址,miner可以通过--targetgaslimit调整区块的gas limit--gasprice设置接受交易的gas price

# Packages

Package accounts implements high level IonChain account management.
No description provided by the author
Package common contains various helper functions.
Package consensus implements different IonChain consensus engines.
No description provided by the author
No description provided by the author
Package core implements the IonChain consensus protocol.
No description provided by the author
Package event deals with subscriptions to real-time events.
Package graphql provides a GraphQL interface to IonChain node data.
Package ionc implements the IonChain protocol.
Package ioncclient provides a client for the IonChain RPC API.
Package ioncdb defines the interfaces for an IonChain data store.
Package ioncstats implements the network stats reporting service.
Package les implements the Light IonChain Subprotocol.
Package light implements on-demand retrieval capable state and chain objects for the IonChain Light Client.
Package log15 provides an opinionated, simple toolkit for best-practice logging that is both human and machine readable.
Go port of Coda Hale's Metrics library <https://github.com/rcrowley/go-metrics> Coda Hale's original work: <https://github.com/codahale/metrics>.
Package miner implements IonChain block creation and mining.
Package ionc contains the simplified mobile APIs to go-ionchain.
Package node sets up multi-protocol IonChain nodes.
Package p2p implements the IonChain p2p network protocols.
No description provided by the author
Package rlp implements the RLP serialization format.
Package rpc implements bi-directional JSON-RPC 2.0 on multiple transports.
No description provided by the author
Package trie implements Merkle Patricia Tries.

# Variables

NotFound is returned by API methods if the requested item does not exist.

# Structs

CallMsg contains parameters for contract calls.
FilterQuery contains options for contract log filtering.
SyncProgress gives progress indications when the node is synchronising with the IonChain network.

# Interfaces

ChainReader provides access to the blockchain.
ChainStateReader wraps access to the state trie of the canonical blockchain.
ChainSyncReader wraps access to the node's current sync status.
A ContractCaller provides contract calls, essentially transactions that are executed by the EVM but not mined into the blockchain.
GasEstimator wraps EstimateGas, which tries to estimate the gas needed to execute a specific transaction based on the pending state.
GasPricer wraps the gas price oracle, which monitors the blockchain to determine the optimal gas price given current fee market conditions.
LogFilterer provides access to contract log events using a one-off query or continuous event subscription.
PendingContractCaller can be used to perform calls against the pending state.
A PendingStateEventer provides access to real time notifications about changes to the pending state.
A PendingStateReader provides access to the pending state, which is the result of all known executable transactions which have not yet been included in the blockchain.
Subscription represents an event subscription where events are delivered on a data channel.
TransactionReader provides access to past transactions and their receipts.
TransactionSender wraps transaction sending.