《如何搭建一条私有多Geth节点的链》最新版以太坊私链搭建官方文档要点翻译

私链 Private Networks

Last edited on January 31, 2023

This guide explains how to set up a private network of multiple Geth nodes. An Ethereum network is private if the nodes are not connected to the main network. In this context private only means reserved or isolated, rather than protected or secure. A fully controlled, private Ethereum network is useful as a backend for core developers working on issues relating to networking/blockchain syncing etc. Private networks are also useful for Dapp developers testing multi-block and multi-user scenarios.

如何搭建一条私有多Geth节点的链

Prerequisites

To follow the tutorial on this page it is necessary to have a working Geth installation (instructions here). It is also helpful to understand Geth fundamentals (see Getting Started).

私链 Private Networks

A private network is composed of multiple Ethereum nodes that can only connect to each other. In order to run multiple nodes locally, each one requires a separate data directory (--datadir). The nodes must also know about each other and be able to exchange information, share an initial state and a common consensus algorithm. The remainder of this page will explain how to configure Geth so that these basic requirements are met, enabling a private network to be started.

网络ID Choosing A Network ID

Ethereum Mainnet has Network ID = 1. There are also many other networks that Geth can connect to by providing alternative Chain IDs, some are testnets and others are alternative networks built from forks of the Geth source code. Providing a network ID that is not already being used by an existing network or testnet means the nodes using that network ID can only connect to each other, creating a private network. A list of current network IDs is available at Chainlist.org(可用于链id的辅助查重). The network ID is controlled using the networkid flag, e.g.

geth --networkid 12345

共识算法 Choosing A Consensus Algorithm

While the main network uses proof-of-stake (PoS) to secure the blockchain, Geth also supports the the 'Clique' proof-of-authority (PoA) consensus algorithm and the Ethash proof-of-work (POW)algorithm as alternatives for private networks. Clique is strongly recommended for private testnets because PoA(能耗低,强推荐) is far less resource-intensive than PoW. The key differences(关键不同点) between the consensus algorithms available in Geth are:

Ethash (×)

Geth's PoW algorithm, Ethash, is a system that allows open participation by anyone willing to dedicate resources to mining. While this is a critical property for a public network, the overall security of the blockchain strictly depends on the total amount of resources used to secure it. As such, PoW is a poor choice (不是好选项对私链来说) for private networks with few miners. The Ethash mining 'difficulty' is adjusted automatically so that new blocks are created approximately 12 seconds apart. As more mining resources are deployed on the network, creating a new block becomes harder so that the average block time matches the target block time.

Clique(√)

Clique consensus is a PoA system where new blocks can be created by authorized 'signers' only.  块只可以在被授权为“signers”的节点产生。The clique consenus protocol is specified in EIP-225. The initial set of authorized signers is configured in the genesis block. 创世块中初始化设置授权的签名者。 Signers can be authorized and de-authorized using a voting mechanism, 签名者可以被授权或者反授权通过投票的机制。thus allowing the set of signers to change while the blockchain operates. Clique can be configured to target any block time (within reasonable limits) since it isn't tied to the difficulty adjustment.

创建创世区块  Creating The Genesis Block

Every blockchain starts with a genesis block. When Geth is run with default settings for the first time, it commits the Mainnet genesis to the database. For a private network, it is generally preferable to use a different genesis block. The genesis block is configured using a genesis.json 唯它可将geth从主链引开  file whose path must be provided to Geth on start-up. When creating a genesis block, a few initial parameters for the private blockchain must be defined:

  • Ethereum platform features enabled at launch (config).链的特征只能在配置阶段定义  Enabling and disabling features once the blockchain is running requires scheduling a hard fork. 一旦链跑起来后,就只能在日程上增加一次硬分叉

  • Initial block gas limit (gasLimit). This impacts how much EVM computation can happen within a single block. Mirroring the main Ethereum network is generally a good choice. The block gas limit can be adjusted after launch using the --miner.gastarget command-line flag. gas limit可以在启动后调整使用 --miner.gastarget 命令行标签

  • Initial allocation(配额) of ether (alloc). This determines how much ether is available to the addresses listed in the genesis block. Additional ether can be created through mining as the chain progresses. alloc在创世区块中列明的预置资产,之外的资产就只能在后期的挖矿中创造了

Clique Example(POA示例)

Below is an example of a genesis.json file for a PoA network. The config section ensures that all known protocol changes are available and configures the 'clique' engine to be used for consensus. Note that the initial signer set must be configured through the extradata field(初始的签名者必须通过extradata字段去配置好,POA共识下这个字段必须配置才能正常工作). This field is required for Clique to work.

The signer account keys can be generated using the geth account command (this command can be run multiple times to create more than one signer key). 签名者的钥匙可以通过geth account命令产生(可多次运行以创造多个签名者key,如下)

geth account new --datadir data  #指明数据位置,创建一个singer key

The Ethereum address printed by this command should be recorded(记下上面命令返回的地址). To encode the signer addresses in extradata, concatenate 32 zero bytes, all signer addresses and 65 further zero bytes(extradata字段里编码签名者地址的方法). The result of this concatenation is then used as the value accompanying the extradata key in genesis.json. In the example below, extradata contains a single initial signer address, (将这个连接的结果作为值与genesis.json中的extradata键一起使用。在下面的示例中,extradata包含一个初始签名者地址。)0x7df9a875a174b3bc565e6424a0050ebc1b2d1d82.

The period configuration option sets the target block time of the chain.

{
  "config": {
    "chainId": 12345,
    "homesteadBlock": 0,
    "eip150Block": 0,
    "eip155Block": 0,
    "eip158Block": 0,
    "byzantiumBlock": 0,
    "constantinopleBlock": 0,
    "petersburgBlock": 0,
    "istanbulBlock": 0,
    "berlinBlock": 0,
    "clique": {
      "period": 5,
      "epoch": 30000
    }
  },
  "difficulty": "1",
  "gasLimit": "8000000",
  "extradata": "0x00000000000000000000000000000000000000000000000000000000000000007df9a875a174b3bc565e6424a0050ebc1b2d1d820000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  "alloc": {
    "7df9a875a174b3bc565e6424a0050ebc1b2d1d82": { "balance": "300000" },
    "f41c74c9ae680c1aa78f42e5647a62f353b7bdde": { "balance": "400000" }
  }
}

Ethash Example(POW示例)

Since Ethash is the default consensus algorithm, no additional parameters need to be configured in order to use it.自从POW成为以太坊默认共识,它不需要额外的参数配置就能生效。 The initial mining difficulty is influenced using the difficulty parameter, but note that the difficulty adjustment algorithm will quickly adapt to the amount of mining resources deployed on the chain.(初始的挖矿难度受到难度参数的影响,但需要注意的是,难度调整算法将快速适应链上部署的挖矿资源量。)

{
  "config": {
    "chainId": 12345,
    "homesteadBlock": 0,
    "eip150Block": 0,
    "eip155Block": 0,
    "eip158Block": 0,
    "byzantiumBlock": 0,
    "constantinopleBlock": 0,
    "petersburgBlock": 0,
    "istanbulBlock": 0,
    "berlinBlock": 0,
    "ethash": {}
  },
  "difficulty": "1",
  "gasLimit": "8000000",
  "alloc": {
    "7df9a875a174b3bc565e6424a0050ebc1b2d1d82": { "balance": "300000" },
    "f41c74c9ae680c1aa78f42e5647a62f353b7bdde": { "balance": "400000" }
  }
}

初始化Geth数据库  Initializing the Geth Database

To create a blockchain node that uses this genesis block, first use geth init to import and sets the canonical genesis block for the new chain. This requires the path to genesis.json to be passed as an argument. (要创建一个使用此创世块的区块链节点,首先使用geth init导入并设置新链的规范创世块。这需要将genesis.json的路径作为参数传递。)

geth init --datadir data genesis.json

When Geth is started using 【 --datadir data】 the genesis block defined in genesis.json will be used. For example:

geth --datadir data --networkid 12345

计划硬分叉 Scheduling Hard Forks

As Ethereum protocol development progresses, new features become available. To enable these features on an existing private network, a hard fork must be scheduled.当以太坊协议不断开发进展,为了让这些新特性在已经存在的私链上生效,硬分叉必须提上日程。 To do this, a future block number must be chosen which determines precisely when the hard fork will activate. Continuing the genesis.json example above and assuming the current block number is 35421, a hard fork might be scheduled for block 40000. This hard fork might upgrade the network to conform to the 'London' specs. First, all the Geth instances on the private network must be recent enough to support the specific hard fork. If so, genesis.json can be updated so that the londonBlock key gets the value 40000. The Geth instances are then shut down and geth init is run to update their configuration. When the nodes are restarted they will pick up where they left off and run normally until block 40000, at which point they will automatically upgrade.(为此,必须选择一个未来的块号,以精确确定硬分叉何时激活。继续上面的 genesis.json 示例并假设当前区块号为 35421,可能会为区块 40000 安排硬分叉。这个硬分叉可能会升级网络以符合“伦敦”规范。首先,私有网络上的所有 Geth 实例都必须足够新,以支持特定的硬分叉。如果是这样,genesis.json可以更新,以便londonBlock密钥获得值40000。然后关闭 Geth 实例并运行 geth init 以更新其配置。当节点重新启动时,它们将从中断的地方继续并正常运行,直到块 40000,此时它们将自动升级。
翻译中文)

The modification to genesis.json is as follows:

{
  "config": {
    "londonBlock": 40000
  }
}

The upgrade command is:

geth init --datadir data genesis.json

搭建网络 Setting Up Networking

With the node configured and initialized, the next step is to set up a peer-to-peer network. This requires a bootstrap node. The bootstrap node is a normal node that is designated to be the entry point that other nodes use to join the network. Any node can be chosen to be the bootstrap node.

To configure a bootstrap node, the IP address of the machine the bootstrap node will run on must be known. The bootsrap node needs to know its own IP address so that it can broadcast it to other nodes. On a local machine this can be found using tools such as ifconfig and on cloud instances such as Amazon EC2 the IP address of the virtual machine can be found in the management console. Any firewalls must allow UDP and TCP traffic on port 30303.

The bootstrap node IP is set using the --nat flag (the command below contains an example address - replace it with the correct one).

配置并初始化节点后,下一步是设置P2P网络。这需要一个引导节点。引导节点是一个普通节点,被指定为其他节点用于加入网络的入口点。可以选择任何节点作为引导节点。

要配置引导节点,必须知道将运行引导节点的计算机的 IP 地址。引导节点需要知道自己的 IP 地址,以便它可以将其广播到其他节点。在本地计算机上,可以使用 ifconfig 等工具找到此地址,而在 Amazon EC2 等云实例上,可以在管理控制台中找到虚拟机的 IP 地址。任何防火墙都必须允许端口 30303 上的 UDP 和 TCP 流量。

引导节点 IP 是使用 --nat 标志设置的(下面的命令包含一个示例地址 - 将其替换为正确的地址)。

geth --datadir data --networkid 15 --nat extip:172.16.254.4

The 'node record' of the bootnode can be extracted using the JS console:(引导节点的“节点记录”可以使用 JS 控制台提取)

geth attach --exec admin.nodeInfo.enr data/geth.ipc

This command should print a base64 string such as the following example. Other nodes will use the information contained in the bootstrap node record to connect to the peer-to-peer network. 此命令应打印 base64 字符串,如以下示例所示。其他节点将使用引导节点记录中包含的信息连接到对等网络。

"enr:-Je4QEiMeOxy_h0aweL2DtZmxnUMy-XPQcZllrMt_2V1lzynOwSx7GnjCf1k8BAsZD5dvHOBLuldzLYxpoD5UcqISiwDg2V0aMfGhGlQhqmAgmlkgnY0gmlwhKwQ_gSJc2VjcDI1NmsxoQKX_WLWgDKONsGvxtp9OeSIv2fRoGwu5vMtxfNGdut4cIN0Y3CCdl-DdWRwgnZf"

If the nodes are intended to connect across the Internet, the bootnode and all other nodes must have public IP addresses assigned, and both TCP and UDP traffic can pass their firewalls. If Internet connectivity is not required or all member nodes connect using well-known IPs, Geth should be set up to restrict peer-to-peer connectivity to an IP subnet. Doing so will further isolate the network and prevents cross-connecting with other blockchain networks in case the nodes are reachable from the Internet. Use the --netrestrict flag to configure a whitelist of IP networks:(如果节点打算通过 Internet 连接,则引导节点和所有其他节点必须分配有公共 IP 地址,并且 TCP 和 UDP 流量都可以通过其防火墙。如果不需要互联网连接或所有成员节点都使用已知 IP 进行连接,则应将 Geth 设置为只限于某一 IP 子网内的P2P连接。这样做将进一步隔离网络,并防止与其他区块链网络交叉连接,以防节点可以从互联网访问。使用 --netlimit 标志配置 IP 网络白名单:)

geth <other-flags> --netrestrict 172.16.254.0/24  #仅限这个子网

With the above setting, Geth will only allow connections from the 172.16.254.0/24 subnet, and will not attempt to connect to other nodes outside of the set IP range.

跑成员节点 Running Member Nodes

Before running a member node, it must be initialized with the same genesis file as used for the bootstrap node. With the bootnode operational and externally reachable (telnet <ip> <port> will confirm that it is indeed reachable), more Geth nodes can be started and connected to them via the bootstrap node using the --bootnodes flag. The process is to start Geth on the same machine as the bootnode, with a separate data directory and listening port and the bootnode node record provided as an argument:

在运行成员节点之前,必须使用与引导节点相同的创世文件对其进行初始化。随着引导节点正常运行且可从外部访问(telnet <ip> <port> 将确认它确实可访问),可以使用 --bootnodes 标志通过引导节点启动更多 Geth 节点并连接到它们。该过程是在与引导节点相同的机器上启动 Geth,具有单独的数据目录和侦听端口以及作为参数提供的引导节点节点记录:

例如,使用数据目录(例如:data2)和侦听端口(例如:30305):

For example, using data directory (example: data2) and listening port (example: 30305):

geth --datadir data2 --networkid 12345 --port 30305 --bootnodes <bootstrap-node-record>

With the member node running, it is possible to check that it is connected to the bootstrap node or any other node in the network by attaching a console and running admin.peers. It may take up to a few seconds for the nodes to get connected.在成员节点运行时,可以通过连接控制台并运行 admin.peers 来检查它是否已连接到引导节点或网络中的任何其他节点。节点最多可能需要几秒钟才能连接。

geth attach data2/geth.ipc --exec admin.peers

跑一个签名者(POA的) Running A Signer (Clique)

To set up Geth for signing blocks in Clique, a signer account must be available. The account must already be available as a keyfile in the keystore. To use it for signing blocks, it must be unlocked. The following command, for address 0x7df9a875a174b3bc565e6424a0050ebc1b2d1d82 will prompt for the account password, then start signing blocks:要设置 Geth 以在 Clique 中签署块,签名者帐户必须可用。该帐户必须已作为密钥库中的密钥文件提供。要将其用于签名块,必须将其解锁。以下命令,对于地址0x7df9a875a174b3bc565e6424a0050ebc1b2d1d82将提示输入帐户密码,然后开始签名块:

geth <other-flags> --unlock 0x7df9a875a174b3bc565e6424a0050ebc1b2d1d82 --mine

Mining can be further configured by changing the default gas limit blocks converge to (with --miner.gastarget) and the price transactions are accepted at (with --miner.gasprice).

挖矿可以通过更改默认的气体限制块收敛到(使用 --miner.gastarget)和价格交易被接受在 (使用 --miner.gasprice) 来进一步配置。
运行矿工(Ethash)

跑一个旷工(POW的) Running A Miner (Ethash)

For PoW in a simple private network, a single CPU miner instance is enough to create a stable stream of blocks at regular intervals. To start a Geth instance for mining, it can be run with all the usual flags plus the following to configure mining:对于简单内网中的PoW,单个CPU矿工实例足以定期创建稳定的区块流。要启动用于挖矿的 Geth 实例,可以使用所有常用标志以及以下内容来配置挖矿:

geth <other-flags> --mine --miner.threads=1 --miner.etherbase=0xf41c74c9ae680c1aa78f42e5647a62f353b7bdde

This will start mining blocks and transactions on a single CPU thread, crediting all block rewards to the account specified by --miner.etherbase(指定挖矿获益地址).

端到端 End-to-end example

This section will run through the commands for setting up a simple private network of two nodes. Both nodes will run on the local machine using the same genesis block and network ID. The data directories for each node will be named node1 and node2. 本节将介绍用于设置两个节点的简单专用网络的命令。两个节点将使用相同的创世区块和网络 ID 在本地计算机上运行。每个节点的数据目录将命名为 node1 和node2。

`mkdir node1 node2`

Each node will have an associated account that will receive some ether at launch. The following command creates an account for Node 1:每个节点都有一个关联的账户,该账户将在启动时收到一些以太币。以下命令为节点 1 创建一个帐户:

geth --datadir node1 account new

This command returns a request for a password. Once a password has been provided the following information is returned to the terminal:此命令返回密码请求。提供密码后,以下信息将返回给终端:

Your new account is locked with a password. Please give a password. Do not forget this password.

Password:

Repeat password:

Your new key was generated Public address of the key: 0xC1B2c0dFD381e6aC08f34816172d6343Decbb12b

Path of the secret key file: node1/keystore/UTC--2022-05-13T14-25-49.229126160Z--c1b2c0dfd381e6ac08f34816172d6343decbb12b

- You can share your public address with anyone. Others need it to interact with you.

- You must NEVER share the secret key with anyone! The key controls access to your funds!

- You must BACKUP your key file! Without the key, it's impossible to access account funds!

- You must remember your password! Without the password, it's impossible to decrypt the key!

The keyfile and account password should be backed up securely. These steps can then be repeated for Node 2. These commands create keyfiles that are stored in the keystore directory in node1 and node2 data directories. In order to unlock the accounts later the passwords for each account should be saved to a text file in each node's data directory.密钥文件和帐户密码应安全备份。然后,可以对Node2 重复这些步骤。这些命令创建存储在node1 和node2 数据目录中的keystore目录中的密钥文件。为了稍后解锁帐户,每个帐户的密码应保存到每个节点数据目录中的文本文件中。

In each data directory save a copy of the following genesis.json to the top level project directory. The account addresses in the alloc field should be replaced with those created for each node in the previous step (without the leading 0x).在每个数据目录中,将以下 genesis.json 的副本保存到顶级项目目录。alloc字段中的帐户地址应替换为上一步中为每个节点创建的帐户地址(不带前导 0x)。

{
  "config": {
    "chainId": 12345,
    "homesteadBlock": 0,
    "eip150Block": 0,
    "eip155Block": 0,
    "eip158Block": 0,
    "byzantiumBlock": 0,
    "constantinopleBlock": 0,
    "petersburgBlock": 0,
    "istanbulBlock": 0,
    "muirGlacierBlock": 0,
    "berlinBlock": 0,
    "londonBlock": 0,
    "arrowGlacierBlock": 0,
    "grayGlacierBlock": 0,
    "clique": {
      "period": 5,
      "epoch": 30000
    }
  },
  "difficulty": "1",
  "gasLimit": "800000000",
  "extradata": "0x00000000000000000000000000000000000000000000000000000000000000007df9a875a174b3bc565e6424a0050ebc1b2d1d820000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  "alloc": {
    "C1B2c0dFD381e6aC08f34816172d6343Decbb12b": { "balance": "500000" },
    "c94d95a5106270775351eecfe43f97e8e75e59e8": { "balance": "500000" }
  }
}

The nodes can now be set up using geth init as follows:现在可以使用 geth init 设置节点,如下所示

geth init --datadir node1 genesis.json

This should be repeated for both nodes. The following will be returned to the terminal:

INFO [05-13|15:41:47.520] Maximum peer count ETH=50 LES=0 total=50

INFO [05-13|15:41:47.520] Smartcard socket not found, disabling err="stat /run/pcscd/pcscd.comm: no such file or directory"

INFO [05-13|15:41:47.520] Set global gas cap cap=50,000,000

INFO [05-13|15:41:47.520] Allocated cache and file handles database=/home/go-ethereum/node2/geth/chaindata cache=16.00MiB handles=16

INFO [05-13|15:41:47.542] Writing custom genesis block

INFO [05-13|15:41:47.542] Persisted trie from memory database nodes=3 size=397.00B time="41.246µs" gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B

INFO [05-13|15:41:47.543] Successfully wrote genesis state database=chaindata hash=c9a158..d415a0

INFO [05-13|15:41:47.543] Allocated cache and file handles database=/home/go-ethereum/node2/geth/chaindata cache=16.00MiB handles=16

INFO [05-13|15:41:47.556] Writing custom genesis block

INFO [05-13|15:41:47.557] Persisted trie from memory database nodes=3 size=397.00B time="81.801µs" gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B

INFO [05-13|15:41:47.558] Successfully wrote genesis state database=chaindata hash=c9a158..d415a0

The next step is to configure a bootnode. This can be any node, but for this tutorial the developer tool bootnode will be used to quickly and easily configure a dedicated bootnode. First the bootnode requires a key, which can be created with the following command, which will save a key to boot.key:下一步是配置引导节点。这可以是任何节点,但在本教程中,开发人员工具引导节点将用于快速轻松地配置专用引导节点。首先,引导节点需要一个密钥,可以使用以下命令创建该密钥,该命令将保存一个密钥以进行启动.key:

bootnode -genkey boot.key

This key can then be used to generate a bootnode as follows:

bootnode -nodekey boot.key -addr :30305

The choice of port passed to -addr is arbitrary, but public Ethereum networks use 30303, so this is best avoided. The bootnode command returns the following logs to the terminal, confirming that it is running:传递给 -addr 的端口选择是任意的,但公共以太坊网络使用 30303,因此最好避免这种情况。bootnode 命令将以下日志返回到终端,确认它正在运行:

enode://f7aba85ba369923bffd3438b4c8fde6b1f02b1c23ea0aac825ed7eac38e6230e5cadcf868e73b0e28710f4c9f685ca71a86a4911461637ae9ab2bd852939b77f@127.0.0.1:0?discport=30305 Note: you're using cmd/bootnode, a developer tool. We recommend using a regular node as bootstrap node for production deployments.

INFO [05-13|15:50:03.645] New local node record seq=1,652,453,403,645 id=a2d37f4a7d515b3a ip=nil udp=0 tcp=0

The two nodes can now be started. Open separate terminals for each node, leaving the bootnode running in the original terminal. In each terminal, run the following command (replacing node1 with node2 where appropriate, and giving each node different --port and authrpc.port IDs. The account address and password file for node 1 must also be provided:现在可以启动这两个节点。为每个节点打开单独的终端,使引导节点在原始终端中运行。在每个终端中,运行以下命令(在适当的情况下将node1替换为node2,并为每个节点提供不同的--port和authrpc.port ID。还必须提供节点 1 的帐户地址和密码文件:

./geth --datadir node1 --port 30306 --bootnodes enode://f7aba85ba369923bffd3438b4c8fde6b1f02b1c23ea0aac825ed7eac38e6230e5cadcf868e73b0e28710f4c9f685ca71a86a4911461637ae9ab2bd852939b77f@127.0.0.1:0?discport=30305  --networkid 123454321 --unlock 0xC1B2c0dFD381e6aC08f34816172d6343Decbb12b --password node1/password.txt --authrpc.port 8551

This will start the node using the bootnode as an entry point. Repeat the same command with the information appropriate to node 2. In each terminal, the following logs indicate success:这将使用引导节点作为入口点启动节点。使用适用于节点 2 的信息重复相同的命令。在每个终端中,以下日志指示成功:

INFO [05-13|16:17:40.061] Maximum peer count ETH=50 LES=0 total=50

INFO [05-13|16:17:40.061] Smartcard socket not found, disabling err="stat /run/pcscd/pcscd.comm: no such file or directory"

INFO [05-13|16:17:40.061] Set global gas cap cap=50,000,000

INFO [05-13|16:17:40.061] Allocated trie memory caches clean=154.00MiB dirty=256.00MiB

INFO [05-13|16:17:40.061] Allocated cache and file handles database=/home/go-ethereum/node1/geth/chaindata cache=512.00MiB handles=524,288

INFO [05-13|16:17:40.094] Opened ancient database database=/home/go-ethereum/node1/geth/chaindata/ancient readonly=false

INFO [05-13|16:17:40.095] Initialised chain configuration config="{ChainID: 123454321 Homestead: 0 DAO: nil DAOSupport: false EIP150: 0 EIP155: 0 EIP158: 0 Byzantium: 0 Constantinople: 0 Petersburg: 0 Istanbul: nil, Muir Glacier: nil, Berlin: nil, London: nil, Arrow Glacier: nil, MergeFork: nil, Terminal TD: nil, Engine: clique}"

INFO [05-13|16:17:40.096] Initialising Ethereum protocol network=123,454,321 dbversion=8

INFO [05-13|16:17:40.098] Loaded most recent local header number=0 hash=c9a158..d415a0 td=1 age=53y1mo2w

INFO [05-13|16:17:40.098] Loaded most recent local full block number=0 hash=c9a158..d415a0 td=1 age=53y1mo2w

INFO [05-13|16:17:40.098] Loaded most recent local fast block number=0 hash=c9a158..d415a0 td=1 age=53y1mo2w

INFO [05-13|16:17:40.099] Loaded local transaction journal transactions=0 dropped=0

INFO [05-13|16:17:40.100] Regenerated local transaction journal transactions=0 accounts=0

INFO [05-13|16:17:40.100] Gasprice oracle is ignoring threshold set threshold=2

WARN [05-13|16:17:40.100] Unclean shutdown detected booted=2022-05-13T16:16:46+0100 age=54s

INFO [05-13|16:17:40.100] Starting peer-to-peer node instance=Geth/v1.10.18-unstable-8d84a701-20220503/linux-amd64/go1.18.1

INFO [05-13|16:17:40.130] New local node record seq=1,652,454,949,228 id=f1364e6d060c4625 ip=127.0.0.1 udp=30306 tcp=30306

INFO [05-13|16:17:40.130] Started P2P networking self=enode://87606cd0b27c9c47ca33541d4b68cf553ae6765e22800f0df340e9788912b1e3d2759b3d1933b6f739c720701a56ce26f672823084420746d04c25fc7b8c6824@127.0.0.1:30306

INFO [05-13|16:17:40.133] IPC endpoint opened url=/home/go-ethereum/node1/geth.ipc INFO [05-13|16:17:40.785] Unlocked account address=0xC1B2c0dFD381e6aC08f34816172d6343Decbb12b

INFO [05-13|16:17:42.636] New local node record seq=1,652,454,949,229 id=f1364e6d060c4625 ip=82.11.59.221 udp=30306 tcp=30306

INFO [05-13|16:17:43.309] Mapped network port proto=tcp extport=30306 intport=30306 interface="UPNP IGDv1-IP1"

INFO [05-13|16:17:43.822] Mapped network port proto=udp extport=30306 intport=30306 interface="UPNP IGDv1-IP1" [05-13|16:17:50.150] Looking for peers peercount=0 tried=0 static=0

INFO [05-13|16:18:00.164] Looking for peers peercount=0 tried=0 static=0

In the first terminal that is currently running the logs resembling the following will be displayed, showing the discovery process in action:在当前正在运行的第一个终端中,将显示类似于以下内容的日志,显示正在运行的发现过程:

INFO [05-13|15:50:03.645] New local node record seq=1,652,453,403,645 id=a2d37f4a7d515b3a ip=nil udp=0 tcp=0

TRACE[05-13|16:15:49.228] PING/v4 id=f1364e6d060c4625 addr=127.0.0.1:30306 err=nil TRACE[05-13|16:15:49.229] PONG/v4 id=f1364e6d060c4625 addr=127.0.0.1:30306 err=nil TRACE[05-13|16:15:49.229] PING/v4 id=f1364e6d060c4625 addr=127.0.0.1:30306 err=nil TRACE[05-13|16:15:49.230] PONG/v4 id=f1364e6d060c4625 addr=127.0.0.1:30306 err=nil TRACE[05-13|16:15:49.730] FINDNODE/v4 id=f1364e6d060c4625 addr=127.0.0.1:30306 err=nil

TRACE[05-13|16:15:49.731] NEIGHBORS/v4 id=f1364e6d060c4625 addr=127.0.0.1:30306 err=nil

TRACE[05-13|16:15:50.231] FINDNODE/v4 id=f1364e6d060c4625 addr=127.0.0.1:30306 err=nil

TRACE[05-13|16:15:50.231] NEIGHBORS/v4 id=f1364e6d060c4625 addr=127.0.0.1:30306 err=nil

TRACE[05-13|16:15:50.561] FINDNODE/v4 id=f1364e6d060c4625 addr=127.0.0.1:30306 err=nil

TRACE[05-13|16:15:50.561] NEIGHBORS/v4 id=f1364e6d060c4625 addr=127.0.0.1:30306 err=nil

TRACE[05-13|16:15:50.731] FINDNODE/v4 id=f1364e6d060c4625 addr=127.0.0.1:30306 err=nil

TRACE[05-13|16:15:50.731] NEIGHBORS/v4 id=f1364e6d060c4625 addr=127.0.0.1:30306 err=nil

TRACE[05-13|16:15:51.231] FINDNODE/v4 id=f1364e6d060c4625 addr=127.0.0.1:30306 err=nil

TRACE[05-13|16:15:51.232] NEIGHBORS/v4 id=f1364e6d060c4625 addr=127.0.0.1:30306 err=nil

TRACE[05-13|16:15:52.591] FINDNODE/v4 id=f1364e6d060c4625 addr=127.0.0.1:30306 err=nil

TRACE[05-13|16:15:52.591] NEIGHBORS/v4 id=f1364e6d060c4625 addr=127.0.0.1:30306 err=nil

TRACE[05-13|16:15:57.767] PING/v4 id=f1364e6d060c4625 addr=127.0.0.1:30306 err=nil

It is now possible to attach a Javascript console to either node to query the network properties:现在可以将 Javascript 控制台附加到任一节点以查询网络属性

geth attach node1/geth.ipc

Once the Javascript console is running, check that the node is connected to one other peer (node 2):运行 Javascript 控制台后,检查节点是否连接到另一个对等节点(节点 2):

net.peerCount

The details of this peer can also be queried and used to check that the peer really is Node 2:还可以查询此peer的详细信息,并用于检查该peer是否确实是节点 2:

admin.peers

This should return the following:

[{ caps: ["eth/66", "snap/1"], enode: "enode://6a4576fb12004aa13949dbf25de978102483a6521e6d5d87c5b7ccb1944bbf8995dc730303ae891732410b1dd2e684277e9292fc0a17372a789bb4e87bdf366b@127.0.0.1:30307", id: "d300c59ba301abcb5f4a3866aab6f833857c3ddf2f0febb583410b1dc466f175", name: "Geth/v1.10.18-unstable-8d84a701-20220503/linux-amd64/go1.18.1", network: { inbound: false, localAddress: "127.0.0.1:56620", remoteAddress: "127.0.0.1:30307", static: false, trusted: false }, protocols: { eth: { difficulty: 1, head: "0xc9a158a687eff8a46128bd5b9aaf6b2f04f10f0683acbd7f031514db9ad415a0", version: 66 }, snap: { version: 1 } } }]

The account associated with Node 1 was supposed to be funded with some ether at the chain genesis. This can be checked easily using eth.getBalance():

eth.getBalance(eth.accounts[0])

This account can then be unlocked and some ether sent to Node 2, using the following commands:

// send some Wei
eth.sendTransaction({
  to: '0xc94d95a5106270775351eecfe43f97e8e75e59e8',
  from: eth.accounts[0],
  value: 25000
});

//check the transaction was successful by querying Node 2's account balance
eth.getBalance('0xc94d95a5106270775351eecfe43f97e8e75e59e8');

The same steps can then be repeated to attach a console to Node 2.

Summary

This page explored the various options for configuring a local private network. A step by step guide showed how to set up and launch a private network, unlock the associated accounts, attach a console to check the network status and make some basic interactions.本页探讨了配置本地私链网络的各种选项。分步指南展示了如何设置和启动私链网络、解锁关联帐户、附加控制台以检查网络状态以及进行一些基本交互。

搭建本地以太坊私链并连接钱包实现转账,需要以下步骤: 1. 安装Geth客户端 Geth是以太坊客户端软件,可以用于连接以太坊网络。可以从https://geth.ethereum.org/downloads/下载Geth安装包并安装。 2. 创建创世块 在创建私链之前,需要先创建创世块。可以使用Puppeth工具来创建创世块,该工具可以从https://github.com/ethereum/go-ethereum/releases下载。使用Puppeth可以选择网络ID、区块时间、初始难度等选项,并生成创世块配置文件。 3. 初始化私链 使用以下命令来初始化私链: ``` geth --datadir /path/to/chaindata init /path/to/genesis.json ``` 其中,/path/to/chaindata为私链数据存储路径,/path/to/genesis.json为创世块配置文件路径。执行该命令后,私链数据存储路径下会生成相应的数据文件。 4. 启动私链 使用以下命令来启动私链: ``` geth --datadir /path/to/chaindata --networkid 1234 --rpc --rpcport "8545" --rpcaddr "127.0.0.1" --rpccorsdomain "*" --rpcapi "eth,net,web3,personal" --nodiscover console ``` 其中,/path/to/chaindata为私链数据存储路径,--networkid为私链的网络ID,--rpc为启用RPC服务,--rpcport为RPC服务端口,--rpcaddr为RPC服务IP地址,--rpccorsdomain为跨域访问配置,--rpcapi为RPC接口配置,--nodiscover为禁用节点发现。执行该命令后,会启动私链节点并进入控制台。 5. 创建钱包 在私链节点控制台中,使用以下命令来创建钱包: ``` personal.newAccount() ``` 执行该命令后,会提示输入密码,并返回钱包地址。 6. 解锁钱包 使用以下命令来解锁钱包: ``` personal.unlockAccount("钱包地址", "密码", 0) ``` 其中,"钱包地址"为钱包地址,"密码"为钱包密码,0为解锁时长,表示永久解锁。执行该命令后,会返回true表示解锁成功。 7. 转账 在私链节点控制台中,使用以下命令来转账: ``` eth.sendTransaction({from: "发送方钱包地址", to: "接收方钱包地址", value: web3.toWei(1, "ether")}) ``` 其中,"发送方钱包地址"为发送方钱包地址,"接收方钱包地址"为接收方钱包地址,web3.toWei(1, "ether")为转账金额,表示1个以太币。执行该命令后,会返回交易哈希值。 8. 查询余额 使用以下命令来查询钱包余额: ``` eth.getBalance("钱包地址") ``` 其中,"钱包地址"为钱包地址。执行该命令后,会返回钱包余额。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值