当前位置: 首页 > 编程日记 > 正文

以太坊私链入门

链客,专为开发者而生,有问必答!

此文章来自区块链技术社区,未经允许拒绝转载
在这里插入图片描述
目录

  1. 背景
  2. 软件安装与配置
    2.1. Ubuntu
    2.1.1. 安装 geth
    2.1.2. 安装 solc
    2.2. Windows
    2.3. Mac OS
    2.4. 编译安装
  3. 创世区块
    3.1. 初始化创世区块
    3.2. 启动节点
    3.2.1. rpcaddr
    3.3. 使用节点进行挖矿
    3.3.1. 启动矿工开始挖矿
    3.3.2. 停止挖矿
    3.3.3. 查看所挖金额
  4. 管理
    4.1. 控制台
    4.2. 连接控制台
    4.3. 账号管理
    4.3.1. 新建账号
    4.3.2. 查看账号
    4.4. 运行JS
  5. JavaScript Console
    5.1. personal 管理
    5.1.1. 创建账号
    5.1.2. 显示账号
    5.2. eth 管理
    5.2.1. 矿工账号
    5.2.2. 余额
    5.2.2.1. 单位转换
    5.2.3. 转账
    5.3. admin 管理
    5.3.1. 显示节点
    5.3.2. 查看池
    5.3.3. 添加节点
    5.4. miner 挖矿管理
    5.4.1. 开始挖矿
    5.4.2. 停止挖矿
  6. 总结
  7. FAQ
    7.1. Error: authentication needed: password or unlock
  8. 背景
    区块链是什么?一句话,它是一种特殊的(非关系型)分布式数据库,这种数据库只能做插入和查找操作,并且没有管理员。

首先,区块链的主要作用是储存信息。任何需要保存的信息,都可以写入区块链,也可以从里面读取,所以它是数据库。

其次,任何人都可以架设服务器,加入区块链网络,成为一个节点。区块链的世界里面,没有中心节点,每个节点都是平等的,都保存着整个数据库。你可以向任何一个节点,写入/读取数据,因为所有节点最后都会同步,保证区块链一致。

  1. 软件安装与配置
    2.1. Ubuntu
    2.1.1. 安装 geth
    安装环境

Ubuntu 17.10

sudo apt upgrade -y
sudo apt install software-properties-common
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt update
sudo apt install ethereum
neo@netkiller ~ % geth version
Geth
Version: 1.7.3-stable
Git Commit: 4bb3c89d44e372e6a9ab85a8be0c9345265c763a
Architecture: amd64
Protocol Versions: [63 62]
Network Id: 1
Go Version: go1.9.1
Operating System: linux
GOPATH=
GOROOT=/usr/lib/go-1.9
2.1.2. 安装 solc
sudo apt install solc -y
neo@netkiller ~ % solc --version
solc, the solidity compiler commandline interface
Version: 0.4.19+commit.c4cbbb05.Linux.g++
2.2. Windows
访问 https://geth.ethereum.org/downloads/
下载并安装 Geth for Windows
2.3. Mac OS
brew tap ethereum/ethereum
brew install ethereum
2.4. 编译安装
git clone https://github.com/ethereum/go-ethereum
sudo apt-get install -y build-essential golang
cd go-ethereum
make geth
3. 创世区块
cd ~
mkdir -p ethereum
cd ethereum
3.1. 初始化创世区块
创建文件 genesis.json

{
“nonce”: “0x0000000000000042”,
“difficulty”: “0x020000”,
“mixhash”: “0x0000000000000000000000000000000000000000000000000000000000000000”,
“coinbase”: “0x0000000000000000000000000000000000000000”,
“timestamp”: “0x00”,
“parentHash”: “0x0000000000000000000000000000000000000000000000000000000000000000”,
“extraData”: “0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa”,
“gasLimit”: “0x4c4b40”,
“config”: {
“chainId”: 15,
“homesteadBlock”: 0,
“eip155Block”: 0,
“eip158Block”: 0
},
“alloc”: { }
}

mixhash: 与nonce配合用于挖矿,由上一个区块的一部分生成的hash。注意他和nonce的设置需要满足以太坊的Yellow paper, 4.3.4. Block Header Validity, (44)章节所描述的条件。.

nonce: nonce就是一个64位随机数,用于挖矿,注意他和mixhash的设置需要满足以太坊的Yellow paper, 4.3.4. Block Header Validity, (44)章节所描述的条件。

difficulty: 设置当前区块的难度,如果难度过大,cpu挖矿就很难,这里设置较小难度

alloc: 用来预置账号以及账号的以太币数量,因为私有链挖矿比较容易,所以我们不需要预置有币的账号,需要的时候自己创建即可以。

coinbase: 矿工的账号,随便填

timestamp: 设置创世块的时间戳

parentHash: 上一个区块的hash值,因为是创世块,所以这个值是0

extraData: 附加信息,随便填,可以填你的个性信息

gasLimit: 该值设置对GAS的消耗总量限制,用来限制区块能包含的交易信息总和,因为我们是私有链,所以填最大。

初始化创世区块

neo@netkiller ~/ethereum % geth init genesis.json
WARN [01-19|17:35:17] No etherbase set and no accounts found as default
INFO [01-19|17:35:17] Allocated cache and file handles database=/home/neo/.ethereum/geth/chaindata cache=16 handles=16
INFO [01-19|17:35:17] Writing custom genesis block
INFO [01-19|17:35:17] Successfully wrote genesis state database=chaindata hash=611596…424d04
INFO [01-19|17:35:17] Allocated cache and file handles database=/home/neo/.ethereum/geth/lightchaindata cache=16 handles=16
INFO [01-19|17:35:18] Writing custom genesis block
INFO [01-19|17:35:18] Successfully wrote genesis state database=lightchaindata hash=611596…424d04
默认目录是 /home/neo/.ethereum/ 你可以通过 --datadir 参数指定目录

neo@netkiller ~/ethereum % geth --datadir data init genesis.json
WARN [01-19|17:38:16] No etherbase set and no accounts found as default
INFO [01-19|17:38:16] Allocated cache and file handles database=/home/neo/ethereum/data/geth/chaindata cache=16 handles=16
INFO [01-19|17:38:17] Writing custom genesis block
INFO [01-19|17:38:17] Successfully wrote genesis state database=chaindata hash=611596…424d04
INFO [01-19|17:38:17] Allocated cache and file handles database=/home/neo/ethereum/data/geth/lightchaindata cache=16 handles=16
INFO [01-19|17:38:17] Writing custom genesis block
INFO [01-19|17:38:17] Successfully wrote genesis state database=lightchaindata hash=611596…424d04

neo@netkiller ~/ethereum % find data
data
data/keystore
data/geth
data/geth/chaindata
data/geth/chaindata/LOCK
data/geth/chaindata/LOG
data/geth/chaindata/MANIFEST-000000
data/geth/chaindata/CURRENT
data/geth/chaindata/000001.log
data/geth/lightchaindata
data/geth/lightchaindata/LOCK
data/geth/lightchaindata/LOG
data/geth/lightchaindata/MANIFEST-000000
data/geth/lightchaindata/CURRENT
data/geth/lightchaindata/000001.log
目录结构

data
├── geth
│ ├── chaindata
│ │ ├── 000001.log
│ │ ├── CURRENT
│ │ ├── LOCK
│ │ ├── LOG
│ │ └── MANIFEST-000000
│ └── lightchaindata
│ ├── 000001.log
│ ├── CURRENT
│ ├── LOCK
│ ├── LOG
│ └── MANIFEST-000000
└── keystore
3.2. 启动节点
neo@netkiller ~/ethereum % geth --networkid 123456 --rpc --rpccorsdomain “*” --nodiscover console
WARN [01-19|17:47:06] No etherbase set and no accounts found as default
INFO [01-19|17:47:06] Starting peer-to-peer node instance=Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9.1
INFO [01-19|17:47:06] Allocated cache and file handles database=/home/neo/.ethereum/geth/chaindata cache=128 handles=1024
INFO [01-19|17:47:06] Initialised chain configuration config="{ChainID: 15 Homestead: 0 DAO: DAOSupport: false EIP150: EIP155: 0 EIP158: 0 Byzantium: Engine: unknown}"
INFO [01-19|17:47:06] Disk storage enabled for ethash caches dir=/home/neo/.ethereum/geth/ethash count=3
INFO [01-19|17:47:06] Disk storage enabled for ethash DAGs dir=/home/neo/.ethash count=2
INFO [01-19|17:47:06] Initialising Ethereum protocol versions="[63 62]" network=123456
INFO [01-19|17:47:06] Loaded most recent local header number=0 hash=611596…424d04 td=131072
INFO [01-19|17:47:06] Loaded most recent local full block number=0 hash=611596…424d04 td=131072
INFO [01-19|17:47:06] Loaded most recent local fast block number=0 hash=611596…424d04 td=131072
INFO [01-19|17:47:06] Loaded local transaction journal transactions=0 dropped=0
INFO [01-19|17:47:06] Regenerated local transaction journal transactions=0 accounts=0
INFO [01-19|17:47:06] Starting P2P networking
INFO [01-19|17:47:06] RLPx listener up self=“enode://9f6490ffb5236f2ddc5710ae73d47c740e0a3644bbd2d67029cf4a6c4693d2f470b642fd2cc3507f7e851df60aaeb730a1270b7a477f91ec5b6b17a8a4b40527@[::]:30303?discport=0”
INFO [01-19|17:47:06] IPC endpoint opened: /home/neo/.ethereum/geth.ipc
INFO [01-19|17:47:06] HTTP endpoint opened: http://127.0.0.1:8545
Welcome to the Geth JavaScript console!

instance: Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9.1
modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

INFO [01-19|17:47:09] Mapped network port proto=tcp extport=30303 intport=30303 interface=“UPNP IGDv1-IP1”

identity: 区块链的标示,随便填写,用于标示目前网络的名字

init: 指定创世块文件的位置,并创建初始块

datadir: 设置当前区块链网络数据存放的位置

port: 网络监听端口

rpc: 启动rpc通信,可以进行智能合约的部署和调试

rpcapi: 设置允许连接的rpc的客户端,一般为db,eth,net,web3

networkid: 设置当前区块链的网络ID,用于区分不同的网络,是一个数字

console: 启动命令行模式,可以在Geth中执行命令

3.2.1. rpcaddr
默认是 127.0.0.1

HTTP endpoint closed: http://127.0.0.1:8545

通过 --rpcaddr=“0.0.0.0” 指定监听地址

HTTP endpoint opened: http://0.0.0.0:8545

neo@netkiller ~/ethereum % geth --networkid 123456 --rpc --rpcaddr=“0.0.0.0” --rpccorsdomain “*” --nodiscover console
INFO [01-20|01:41:33] Starting peer-to-peer node instance=Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9.1
INFO [01-20|01:41:33] Allocated cache and file handles database=/home/neo/.ethereum/geth/chaindata cache=128 handles=1024
INFO [01-20|01:41:34] Initialised chain configuration config="{ChainID: 15 Homestead: 0 DAO: DAOSupport: false EIP150: EIP155: 0 EIP158: 0 Byzantium: Engine: unknown}"
INFO [01-20|01:41:34] Disk storage enabled for ethash caches dir=/home/neo/.ethereum/geth/ethash count=3
INFO [01-20|01:41:34] Disk storage enabled for ethash DAGs dir=/home/neo/.ethash count=2
INFO [01-20|01:41:34] Initialising Ethereum protocol versions="[63 62]" network=123456
INFO [01-20|01:41:34] Loaded most recent local header number=531 hash=1a2707…3a27bc td=79083846
INFO [01-20|01:41:34] Loaded most recent local full block number=531 hash=1a2707…3a27bc td=79083846
INFO [01-20|01:41:34] Loaded most recent local fast block number=531 hash=1a2707…3a27bc td=79083846
INFO [01-20|01:41:34] Loaded local transaction journal transactions=0 dropped=0
INFO [01-20|01:41:34] Regenerated local transaction journal transactions=0 accounts=0
WARN [01-20|01:41:34] Blockchain not empty, fast sync disabled
INFO [01-20|01:41:34] Starting P2P networking
INFO [01-20|01:41:34] RLPx listener up self=“enode://9f6490ffb5236f2ddc5710ae73d47c740e0a3644bbd2d67029cf4a6c4693d2f470b642fd2cc3507f7e851df60aaeb730a1270b7a477f91ec5b6b17a8a4b40527@[::]:30303?discport=0”
INFO [01-20|01:41:34] IPC endpoint opened: /home/neo/.ethereum/geth.ipc
INFO [01-20|01:41:34] HTTP endpoint opened: http://0.0.0.0:8545
Welcome to the Geth JavaScript console!

instance: Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9.1
coinbase: 0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6
at block: 531 (Tue, 14 Nov 2017 17:36:05 HST)
datadir: /home/neo/.ethereum
modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

INFO [01-20|01:41:40] Mapped network port proto=tcp extport=30303 intport=30303 interface=“UPNP IGDv1-IP1”
3.3. 使用节点进行挖矿
3.3.1. 启动矿工开始挖矿
miner.start(1)
这里的1表示只使用一个线程运行,第一次运行时将开始创建DAG文件,只需等待进度条到100,则将开始挖矿。 实际你看到的挖矿速度很快,这是因为我们已经在初始化创世区块时配置为:“nonce”: “0x0000000000000042”。 “0x42”难度能让你在私有测试网链上快速挖以太币。

提示
挖矿时必然有矿工账户,而系统默认使用创建的第一个账号。

miner.start(1)
INFO [01-19|21:06:43] Updated mining threads threads=1
INFO [01-19|21:06:43] Transaction pool price threshold updated price=18000000000
INFO [01-19|21:06:43] Starting mining operation
null
INFO [01-19|21:06:43] Commit new mining work number=1 txs=0 uncles=0 elapsed=717.552µs
INFO [01-19|21:06:46] Generating ethash verification cache epoch=0 percentage=91 elapsed=3.000s
INFO [01-19|21:06:46] Generated ethash verification cache epoch=0 elapsed=3.273s
INFO [01-19|21:06:51] Generating DAG in progress epoch=0 percentage=0 elapsed=5.056s
INFO [01-19|21:06:56] Generating DAG in progress epoch=0 percentage=1 elapsed=10.140s
INFO [01-19|21:07:01] Generating DAG in progress epoch=0 percentage=2 elapsed=15.119s
INFO [01-19|21:07:06] Generating DAG in progress epoch=0 percentage=3 elapsed=19.924s
INFO [01-19|21:07:11] Generating DAG in progress epoch=0 percentage=4 elapsed=24.739s
INFO [01-19|21:07:16] Generating DAG in progress epoch=0 percentage=5 elapsed=29.473s
INFO [01-19|21:07:22] Generating DAG in progress epoch=0 percentage=6 elapsed=35.641s
INFO [01-19|21:07:26] Generating DAG in progress epoch=0 percentage=7 elapsed=40.374s
INFO [01-19|21:07:31] Generating DAG in progress epoch=0 percentage=8 elapsed=45.134s
INFO [01-19|21:07:36] Generating DAG in progress epoch=0 percentage=9 elapsed=49.908s
INFO [01-19|21:07:41] Generating DAG in progress epoch=0 percentage=10 elapsed=54.633s



INFO [01-19|21:22:43] Generated ethash verification cache epoch=0 elapsed=15m57.328s
INFO [01-19|21:22:47] Generating ethash verification cache epoch=1 percentage=17 elapsed=3.031s
INFO [01-19|21:22:50] Generating ethash verification cache epoch=1 percentage=34 elapsed=6.056s
INFO [01-19|21:22:53] Generating ethash verification cache epoch=1 percentage=49 elapsed=9.562s
INFO [01-19|21:22:57] Generating ethash verification cache epoch=1 percentage=70 elapsed=13.115s
INFO [01-19|21:23:00] Generating ethash verification cache epoch=1 percentage=90 elapsed=16.123s
INFO [01-19|21:23:01] Generated ethash verification cache epoch=1 elapsed=17.576s
INFO [01-19|21:23:19] Generating DAG in progress epoch=1 percentage=0 elapsed=18.198s
INFO [01-19|21:23:32] Successfully sealed new block number=1 hash=e2b5b9…9b1bfe
INFO [01-19|21:23:32] ? mined potential block number=1 hash=e2b5b9…9b1bfe
INFO [01-19|21:23:32] Commit new mining work number=2 txs=0 uncles=0 elapsed=1.188ms
INFO [01-19|21:23:37] Generating DAG in progress epoch=1 percentage=1 elapsed=35.913s
INFO [01-19|21:23:41] Successfully sealed new block number=2 hash=62db3f…e27b50
INFO [01-19|21:23:41] ? mined potential block number=2 hash=62db3f…e27b50
INFO [01-19|21:23:41] Commit new mining work number=3 txs=0 uncles=0 elapsed=772.239µs
INFO [01-19|21:23:43] Successfully sealed new block number=3 hash=34384b…c387f2
INFO [01-19|21:23:43] ? mined potential block number=3 hash=34384b…c387f2
INFO [01-19|21:23:43] Commit new mining work number=4 txs=0 uncles=0 elapsed=1.002ms
INFO [01-19|21:23:55] Generating DAG in progress epoch=1 percentage=2 elapsed=53.757s
INFO [01-19|21:24:13] Generating DAG in progress epoch=1 percentage=3 elapsed=1m11.561s
INFO [01-19|21:24:30] Generating DAG in progress epoch=1 percentage=4 elapsed=1m28.986s
INFO [01-19|21:24:30] Successfully sealed new block number=4 hash=681970…462135
INFO [01-19|21:24:30] ? mined potential block number=4 hash=681970…462135
INFO [01-19|21:24:30] Commit new mining work number=5 txs=0 uncles=0 elapsed=833.629µs
INFO [01-19|21:24:36] Successfully sealed new block number=5 hash=7b058b…d2f07a
INFO [01-19|21:24:36] ? mined potential block number=5 hash=7b058b…d2f07a
INFO [01-19|21:24:36] Commit new mining work number=6 txs=0 uncles=0 elapsed=897.815µs
INFO [01-19|21:24:43] Successfully sealed new block number=6 hash=a5fc3d…b1221e
INFO [01-19|21:24:43] ? block reached canonical chain number=1 hash=e2b5b9…9b1bfe
INFO [01-19|21:24:43] ? mined potential block number=6 hash=a5fc3d…b1221e
INFO [01-19|21:24:43] Commit new mining work number=7 txs=0 uncles=0 elapsed=758.061µs
INFO [01-19|21:24:47] Successfully sealed new block number=7 hash=003b02…e886fd
INFO [01-19|21:24:47] ? block reached canonical chain number=2 hash=62db3f…e27b50
INFO [01-19|21:24:47] ? mined potential block number=7 hash=003b02…e886fd
INFO [01-19|21:24:47] Commit new mining work number=8 txs=0 uncles=0 elapsed=920.862µs
INFO [01-19|21:24:48] Generating DAG in progress epoch=1 percentage=5 elapsed=1m46.827s
INFO [01-19|21:25:06] Generating DAG in progress epoch=1 percentage=6 elapsed=2m4.338s
INFO [01-19|21:25:23] Successfully sealed new block number=8 hash=fd23c9…361c65
INFO [01-19|21:25:23] ? block reached canonical chain number=3 hash=34384b…c387f2
INFO [01-19|21:25:23] ? mined potential block number=8 hash=fd23c9…361c65
INFO [01-19|21:25:23] Commit new mining work number=9 txs=0 uncles=0 elapsed=825.737µs
INFO [01-19|21:25:23] Generating DAG in progress epoch=1 percentage=7 elapsed=2m22.061s
3.3.2. 停止挖矿
miner.stop()
true

3.3.3. 查看所挖金额

eth.getBalance(eth.accounts[0])
70000000000000000000

  1. 管理
    4.1. 控制台
    neo@netkiller ~/ethereum % geth --networkid 123456 console
    INFO [01-19|22:14:52] Starting peer-to-peer node instance=Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9.1
    INFO [01-19|22:14:52] Allocated cache and file handles database=/home/neo/.ethereum/geth/chaindata cache=128 handles=1024
    INFO [01-19|22:14:52] Initialised chain configuration config="{ChainID: 15 Homestead: 0 DAO: DAOSupport: false EIP150: EIP155: 0 EIP158: 0 Byzantium: Engine: unknown}"
    INFO [01-19|22:14:52] Disk storage enabled for ethash caches dir=/home/neo/.ethereum/geth/ethash count=3
    INFO [01-19|22:14:52] Disk storage enabled for ethash DAGs dir=/home/neo/.ethash count=2
    INFO [01-19|22:14:52] Initialising Ethereum protocol versions="[63 62]" network=123456
    INFO [01-19|22:14:52] Loaded most recent local header number=14 hash=70d7f1…45850a td=1966848
    INFO [01-19|22:14:52] Loaded most recent local full block number=14 hash=70d7f1…45850a td=1966848
    INFO [01-19|22:14:52] Loaded most recent local fast block number=14 hash=70d7f1…45850a td=1966848
    INFO [01-19|22:14:52] Loaded local transaction journal transactions=0 dropped=0
    INFO [01-19|22:14:52] Regenerated local transaction journal transactions=0 accounts=0
    WARN [01-19|22:14:52] Blockchain not empty, fast sync disabled
    INFO [01-19|22:14:52] Starting P2P networking

INFO [01-19|22:14:56] UDP listener up self=enode://9f6490ffb5236f2ddc5710ae73d47c740e0a3644bbd2d67029cf4a6c4693d2f470b642fd2cc3507f7e851df60aaeb730a1270b7a477f91ec5b6b17a8a4b40527@101.232.64.12:30303
INFO [01-19|22:14:56] RLPx listener up self=enode://9f6490ffb5236f2ddc5710ae73d47c740e0a3644bbd2d67029cf4a6c4693d2f470b642fd2cc3507f7e851df60aaeb730a1270b7a477f91ec5b6b17a8a4b40527@101.232.64.12:30303
INFO [01-19|22:14:56] IPC endpoint opened: /home/neo/.ethereum/geth.ipc
Welcome to the Geth JavaScript console!

instance: Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9.1
coinbase: 0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6
at block: 14 (Fri, 19 Jan 2018 21:27:16 HST)
datadir: /home/neo/.ethereum
modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

INFO [01-19|22:14:56] Mapped network port proto=udp extport=30303 intport=30303 interface=“UPNP IGDv1-IP1”
4.2. 连接控制台
一般测试启动我们使用 console,如果是正式启动无需使用 console。同事我们使用&符号使其进入后台运行。

neo@netkiller ~/ethereum % geth --networkid 123456 --rpc --rpcaddr=“0.0.0.0” --rpccorsdomain “*” --nodiscover &
进入控制台

neo@netkiller ~/ethereum % geth attach
Welcome to the Geth JavaScript console!

instance: Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9.1
coinbase: 0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6
at block: 531 (Tue, 14 Nov 2017 17:36:05 HST)
datadir: /home/neo/.ethereum
modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0
退出控制台

exit
连接远程控制台

neo@netkiller ~/ethereum % geth --exec ‘eth.coinbase’ attach http://172.16.0.10:8545
“0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6”
4.3. 账号管理
4.3.1. 新建账号
查看账号

neo@netkiller ~/ethereum % geth account list
Account #0: {83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6} keystore:///home/neo/.ethereum/keystore/UTC–2018-01-20T04-04-06.786586541Z–83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6
创建账号

neo@netkiller ~/ethereum % geth account new
Your new account is locked with a password. Please give a password. Do not forget this password.
Passphrase:
Repeat passphrase:
Address: {e8abf98484325fd6afc59b804ac15804b978e607}
指定密码

echo “abc123” > passwd
geth -password passwd account new
4.3.2. 查看账号
neo@netkiller ~/ethereum % geth account list
Account #0: {83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6} keystore:///home/neo/.ethereum/keystore/UTC–2018-01-20T04-04-06.786586541Z–83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6
Account #1: {e8abf98484325fd6afc59b804ac15804b978e607} keystore:///home/neo/.ethereum/keystore/UTC–2018-01-20T06-11-23.608902164Z–e8abf98484325fd6afc59b804ac15804b978e607
4.4. 运行JS
neo@netkiller ~/ethereum % geth --exec “eth.blockNumber” attach
531
$ geth --exec ‘loadScript("/tmp/checkbalances.js")’ attach http://123.123.123.123:8545
$ geth --jspath “/tmp” --exec ‘loadScript(“checkbalances.js”)’ attach http://123.123.123.123:8545
5. JavaScript Console
5.1. personal 管理
5.1.1. 创建账号

personal.newAccount()
Passphrase:
Repeat passphrase:
“0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6”
5.1.2. 显示账号
列出所有账号

personal.listAccounts
[“0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6”, “0xe8abf98484325fd6afc59b804ac15804b978e607”]
列出指定账号

personal.listAccounts[1]
“0xe8abf98484325fd6afc59b804ac15804b978e607”

personal.listAccounts[0]
“0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6”
5.2. eth 管理
5.2.1. 矿工账号
查看默认旷工账号,系统中的第一个账号。

eth.coinbase
“0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6”
查看账号列表

eth.accounts
[“0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6”, “0xe8abf98484325fd6afc59b804ac15804b978e607”]

eth.accounts[0]
“0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6”
5.2.2. 余额
eth.getBalance(eth.accounts[0])
70000000000000000000
5.2.2.1. 单位转换
eth.getBalance()返回的余额是以太币的最小面额wei,将wei转换为以太币ether。

primary = eth.accounts[0]
balance = web3.fromWei(eth.getBalance(primary), “ether”);
演示

primary = eth.accounts[0]
“0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6”
balance = web3.fromWei(eth.getBalance(primary), “ether”);
70
5.2.3. 转账
personal.unlockAccount(“0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6”, “”, 300)
eth.sendTransaction({from: ‘0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6’, to: ‘0xe8abf98484325fd6afc59b804ac15804b978e607’, value: web3.toWei(1, “ether”)})
默认矿工账号

eth.coinbase
“0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6”

查看系统中的账号,如果没有请参考上面章节创建

eth.accounts
[“0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6”, “0xe8abf98484325fd6afc59b804ac15804b978e607”]

转出账号中又 285 个以太币

web3.fromWei(eth.getBalance(eth.accounts[0]))
285

转入账号目前是 0

web3.fromWei(eth.getBalance(eth.accounts[1]))
0

解锁传出账号,否则不能转出。personal.unlockAccount(账号, 密码, 300)

personal.unlockAccount(“0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6”, “”, 300)
true

转账操作

eth.sendTransaction({from: ‘0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6’, to: ‘0xe8abf98484325fd6afc59b804ac15804b978e607’, value: web3.toWei(10, “ether”)})
“0xb0674a7fee52555d8712f3a1f0f30fbbbf67ff7b5b4b53ab5d131262613215c6”

如果你现在查看转入账号,你会发现余额仍然是 0 ,交易还未成功写进区块,写进区块的方式是挖矿,所以你必须执行挖矿

miner.start(1)
null

稍后几分钟,再次查看转入账号,将会看到有10个以太币入账。传出账号会减少10个以太币,同时仍然继续挖矿中。

web3.fromWei(eth.getBalance(eth.accounts[1]))
10
5.3. admin 管理
5.3.1. 显示节点
显示当前节点信息

admin.nodeInfo
{
enode: “enode://9f6490ffb5236f2ddc5710ae73d47c740e0a3644bbd2d67029cf4a6c4693d2f470b642fd2cc3507f7e851df60aaeb730a1270b7a477f91ec5b6b17a8a4b40527@[::]:30303?discport=0”,
id: “9f6490ffb5236f2ddc5710ae73d47c740e0a3644bbd2d67029cf4a6c4693d2f470b642fd2cc3507f7e851df60aaeb730a1270b7a477f91ec5b6b17a8a4b40527”,
ip: “::”,
listenAddr: “[::]:30303”,
name: “Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9.1”,
ports: {
discovery: 0,
listener: 30303
},
protocols: {
eth: {
difficulty: 131072,
genesis: “0x611596e7979cd4e7ca1531260fa706093a5492ecbdf58f20a39545397e424d04”,
head: “0x611596e7979cd4e7ca1531260fa706093a5492ecbdf58f20a39545397e424d04”,
network: 123456
}
}
}

节点地址

enode://9f6490ffb5236f2ddc5710ae73d47c740e0a3644bbd2d67029cf4a6c4693d2f470b642fd2cc3507f7e851df60aaeb730a1270b7a477f91ec5b6b17a8a4b40527@[::]:30303?discport=0
[::] 是 ipv6 地址,可以改为 ipv4 地址。

5.3.2. 查看池

admin.peers
[]
5.3.3. 添加节点
admin.addPeer(‘enode://9f6490ffb5236f2ddc5710ae73d47c740e0a3644bbd2d67029cf4a6c4693d2f470b642fd2cc3507f7e851df60aaeb730a1270b7a477f91ec5b6b17a8a4b40527@172.16.0.1:30303’)
5.4. miner 挖矿管理
5.4.1. 开始挖矿
miner.start(2)
null
过几分钟后运行

web3.fromWei(eth.getBalance(eth.coinbase), “ether”)
30
这时我们已经看到已经产生了30个以太币。

5.4.2. 停止挖矿

miner.stop()
true

  1. 总结
  2. FAQ
    7.1. Error: authentication needed: password or unlock

eth.sendTransaction({from: ‘0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6’, to: ‘0xe8abf98484325fd6afc59b804ac15804b978e607’, value: web3.toWei(1, “ether”)})
Error: authentication needed: password or unlock
at web3.js:3143:20
at web3.js:6347:15
at web3.js:5081:36
at :1:1
解锁转出账号

personal.unlockAccount(“0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6”, “your_password”, 300)
true
eth.sendTransaction({from: ‘0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6’, to: ‘0xe8abf98484325fd6afc59b804ac15804b978e607’, value: web3.toWei(1, “ether”)})
“0xd9e8c8fdc71e24ee8052048de4ff0acd7157b872393f37344c8ec2083f3fe48f”

相关文章:

数据结构(十)栈的作用--大数的加法运算

一、大数加法的定义 在Java中,整数类型有四种,byte(8位)、short(16位)、int(32位)、long(64位)。 其中,int类型为32为,也就是说最大的…

分布式技术一周技术动态 2016-11-27

分布式系统实践 1. 大数据时代快速SQL引擎-Impala http://dwz.cn/4G9mvt 摘要: 在Dremel论文发表之后,开源社区涌现出了一批基于MPP架构的SQL-on-Hadoop(HDFS)查询引擎,典型代表有Apache Impala、Presto、Apache Drill、Apache HAWQ等,看上去…

vuerouter3种模式_Vue-router的三种传参方式

第一种传递参数:name传参两步完成name传参并显示在模板中;第一在router/index.js中配置name属性,routes: [{path: /,name: HelloWorld,component: HelloWorld},]第二步在src/App.vue接收{{ $route.name }}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~…

以太坊的数据结构

链客,专为开发者而生,有问必答! 此文章来自区块链技术社区,未经允许拒绝转载。 以太坊被描述为为一个交易驱动的状态机,它在某个状态下接受一些输入后,会确定的转移到一个新的状态。具体来说,…

(转载)虚幻引擎3--9掌握虚幻技术UnrealScript 预处理器

第九章 – UNREALSCRIPT预处理器9.1概述9.2 MACRO(宏)的基础知识指南 9.1 –您的第一个宏9.3具有参数的宏指南 9.2 – MACRO参数9.4内置宏DEFINE IF/ELSE/ENDIF 实例: IF/ELSE/ENDIF的应用 INCLUDE ISDEFINED/NOTDEFINED 示例: 结合使用 …

springboot添加多数据源连接池并配置Mybatis

springboot添加多数据源连接池并配置Mybatis 转载请注明出处:https://www.cnblogs.com/funnyzpc/p/9190226.html May 12, 2018 星期六,那是个晴天,天湛蓝湛蓝的非常干净,仿佛飘过一粒尘埃也能看得清清楚楚,然后就发生…

lua菜鸟教程_Lua 环境安装

Lua 环境安装Linux 系统上安装Linux & Mac上安装 Lua 安装非常简单,只需要下载源码包并在终端解压编译即可,本文使用了5.3.0版本进行安装:curl -R -O http://www.lua.org/ftp/lua-5.3.0.tar.gztar zxf lua-5.3.0.tar.gzcd lua-5.3.0make …

以太坊智能合约Demo

链客,专为开发者而生,有问必答! 此文章来自区块链技术社区,未经允许拒绝转载。 智能合约开发用solidity编程语言部署在以太坊这个区块链平台,本文提供一个官方实战demo示例快速入门,用demo例子深入浅出智…

Java学习笔记七——数组工具类Arrays

数组工具类Arrays Java提供的Arrays类里包含的一些static修饰的方法可以直接操作数组。若将里面的方法用熟的话&#xff0c;那开发效率会大大提高。下面介绍其中的方法。 List<T> asList(T... a) 作用&#xff1a;将指定数组或数组元素&#xff0c;转换成固定大小的List。…

c++窗口管理系统是什么_优秀的食堂管理系统让你对校园生活更充满希望

面对今年疫情环境下的种种困难&#xff0c;各大高校纷纷根据情况采取不同的措施&#xff0c;应对开学的各种难题。解决返校学生就餐就是一大难点。学校后勤工作人员少之又少&#xff0c;开设的食堂窗口也供不应求。这也直接导致了后勤人员懒散&#xff0c;食堂阿姨给菜“手抖”…

ACM训练小结-2018年6月16日

今天题目情况如下&#xff1a;A题&#xff1a;线段树XOR性质。情况&#xff1a;由于写法问题&#xff0c;调试困难&#xff0c;浪费大量时间。B题&#xff1a;&#xff08;对所有满足i mod pq&#xff0c;求a[i]之和&#xff09;&#xff0c;无修改&#xff0c;直接上n*sqrt(n)…

加密货币的本质

链客&#xff0c;专为开发者而生&#xff0c;有问必答&#xff01; 此文章来自区块链技术社区&#xff0c;未经允许拒绝转载。 去年&#xff0c;比特币暴涨&#xff0c;其他币也像雨后春笋一样冒出来&#xff0c;已经有1000多种了。 很多人都在问&#xff0c;加密货币&#…

「2018山东一轮集训」 Tree

为什么出题人这么毒瘤啊&#xff1f;&#xff1f;&#xff01;&#xff01;一个分块还要带log的题非要出成n<2*1e5。。。。。。。 为了卡过最后两个点我做了无数常数优化&#xff0c;包括但不限于&#xff1a;把所有线段树改成 存差分的树状数组&#xff1b;把树剖求LCA的极…

mysql 表空间收缩_mysql表碎片清理和表空间收缩

mysql表碎片清理和表空间收缩(即清理碎片后report_site_day.ibd文件磁盘空间减小,该方案基于独立表空间存储方式)OPTIMIZETABLE [tablename],当然这种方式只适用于独立表空间清除碎片的优点:降低访问表时的IO,提高mysql性能,释放表空间降低磁盘空间使用率OPTIMIZE TABLE ipvacl…

spring security remember me实现自动登录

1 默认策略 在我们自定义的login中增加一个选择框 <input type"submit" value"Login" /> <br/> <br/> <input type"checkbox" valuetrue name"_spring_security_remember_me" />记住密码 <!-- 记住…

野指针与内存泄漏那些事

野指针&#xff1a;不是NULL指针&#xff0c;是指向垃圾内存的指针 野指针成因&#xff1a; 1.指针变量没有被初始化&#xff1a;指针变量在创建时同时应当被初始化&#xff0c;要么将指针设置为NULL&#xff0c;要么让它指向合法的内存。 2.指针p被free或者delete,没有被设置为…

参数等效模型可以用于_等效媒质理论(介电参数反演)

听说过超材料的读者大概率会了解一个知识点&#xff0c;复杂的媒质块可以等效为一块平板&#xff0c;当然这是有条件的。比如模型小于十分之一波长之类的&#xff0c;尤其对模型厚度要求严格些。大家在查找等效媒质理论文献的时候&#xff0c;可能会被繁杂的理论解释弄得爆炸&a…

js日期格式化Date

使用Date类进行日期格式化。 1 输入“yyyy-MM-dd hh:mm:ss”格式的String字符串&#xff0c;返回字符串 做一个简单判定&#xff0c;在当日显示为几点几分&#xff0c;同年为月日&#xff0c;不同年显示年月 1 function dateFormat(str){2 //str格式为yyyy-mm-dd h…

(十九)异常处理

什么是异常处理 异常就是程序运行时发生错误的信号&#xff08;在程序出现错误时&#xff0c;则会产生一个异常&#xff0c;若程序没有处理它&#xff0c;则会抛出该异常&#xff0c;程序的运行也随之终止&#xff09;&#xff0c;在python中,错误触发的异常如下 语法错误&…

jquery 获取一组元素的选中项 - 函数、jquery获取复选框值、jquery获取单选按钮值...

做表单提交时&#xff0c;如果现在还在用form提交&#xff0c;用户体验很差&#xff0c;所以一般使用ajax提交。 其中需要获取每个表单输入元素的值&#xff0c;获取的时候像文本框这些还好说&#xff0c;Jquery提供了 .val() 方法&#xff0c;获取很方便&#xff0c;但是获取复…

geany怎么创建文件夹_教程详情|Geany怎么使用,Geany安装使用教程_234游戏网

Geany是利用GTK 2工具包开发的一个快速、轻巧的集成开发环境&#xff0c;具有良好的可移植性和通用性、安全性&#xff0c;广泛应用于各个行业。Geany具有语法高亮、代码折叠、代码自动完成等功能&#xff0c;非常适合开发人员使用。下面是关于Geany安装使用教程&#xff0c;希…

Django模板系统和admin模块

只需要记两种特殊符号&#xff1a;{{ }}和 {% %}变量相关的用 {{}}&#xff0c; 逻辑相关的用 {%%}。 Filters 语法&#xff1a; {{ value|filter_name:参数 }}default{{ value|default: "nothing"}} 如果value值没传的话就显示nothinglength{{ value|length }}|左右…

finalshell文件列表不显示_Jira面板配置_待办事项不显示问题列表

最近&#xff0c;使用jira进行项目管理&#xff0c;出现一些问题&#xff0c;对于其中一些配置&#xff0c;做下记录&#xff0c;后续方便查看&#xff0c;也给需要的人一个参考&#xff0c;传送门&#xff1a;jira使用文档_Java_pang787559613的博客-CSDN博客​blog.csdn.netj…

背单词:3年,34150分钟!

转载于:https://www.cnblogs.com/sx00xs/p/6128618.html

如何学习区块链技术

链客&#xff0c;专为开发者而生&#xff0c;有问必答&#xff01; 此文章来自区块链技术社区&#xff0c;未经允许拒绝转载。 有效地学习区块链技术&#xff0c;您需要深入了解区块链协议和一些编程语言。记住区块链是一种可以用各种编程语言实现的协议。看下面的例子&#…

.net里鼠标选中的text数据怎么获取_Python数据科学实践 | 爬虫1

点击上方蓝色字体&#xff0c;关注我们大家好&#xff0c;基于Python的数据科学实践课程又到来了&#xff0c;大家尽情学习吧。本期内容主要由智亿同学与政委联合推出。前面几章大家学习了如何利用Python处理与清洗数据&#xff0c;如何探索性数据分析&#xff0c;以及如何利用…

redis实现对账(集合比较)功能

现状&#xff1a;每日在进行系统之间的订单对账时&#xff0c;往往是这样的操作流程&#xff1b; 1.从外部系统拉取数据存入本地数据库&#xff1b; 2.查询本地订单数据集合localSet&#xff1b; 3.查询外部系统订单数据集合outerSet; 4.以本地localSet为基准&#xff0c;对照o…

Javascript刷题 》 查找数组元素位置

找出元素 item 在给定数组 arr 中的位置 输出描述: function indexOf(arr, item) {..... } 如果数组中存在 item&#xff0c;则返回元素在数组中的位置&#xff0c;否则返回 -1 输入例子: indexOf([ 1, 2, 3, 4 ], 3) 输出例子: 2 实现方法 1、先将arr转换成字符串&#xff0c;…

Go 语言函数

链客&#xff0c;专为开发者而生&#xff0c;有问必答&#xff01; 此文章来自区块链技术社区&#xff0c;未经允许拒绝转载。 函数是基本的代码块&#xff0c;用于执行一个任务。 Go 语言最少有个 main() 函数。 你可以通过函数来划分不同功能&#xff0c;逻辑上每个函数执…

终端主题_再见 XShell 和 ITerm 2,是时候拥抱全平台高颜值终端工具 Hyper 了!

点击上方“涛哥聊Python”&#xff0c;选择“星标”公众号重磅干货&#xff0c;第一时间送达转自&#xff1a;运维之美不论是 macOS 还是 Windows 下&#xff0c;我们都不推荐使用系统自带终端。无论是可拓展性还是可编程性都被「系统自带」这样的特点限制。特别是 Windows 下的…