Ubuntu系统下以太坊Go语言搭建以及控制台基础命令的使用

Ubuntu系统下以太坊Go语言搭建以及控制台基础命令的使用

官方github:https://github.com/ethereum/go-ethereum

Ubuntu安装说明

sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install ethereum

安装过程中如果出错请自行查询,安装完成后在你的节点上创建一个账户

geth account new

查看 geth 帮助

 geth --help

搭建Geth(命令行客户端)

如果没有安装 git 请首先安装

sudo apt install git

转到你的目录

cd /usr/src
sudo mkdir mygit
cd mygit

Git克隆go-ethereum

sudo git clone https://github.com/ethereum/go-ethereum

构建geth需要安装Go和C编译器:

sudo apt-get install -y build-essential

安装golang开发环境

sudo apt-get install golang

最后,geth使用以下命令构建程序

进入目录

cd go-ethereum 
sudo make geth

进入你的目录创建一个ethdev文件夹,并且附加权限

cd /usr/src
sudo mkdir ethdev
sudo chmod 777 ethdev/ -R
cd ethdev/

启动geth开发模式

geth --datadir "./" --rpc --rpcport 8545 --port 30303 --rpccorsdomain="*" -rpcapi eth,web3,personal,net --nodiscover console 2>>geth.log --dev --dev.period 1

datadir 这是你私链的地址 这里就是/usr/src/ethdev下面
rpc –启动rpc通信,可以进行智能合约的部署和调试
rpcport –rpc接口的端口号
port –网络监听端口,用于节点之间通信
rpcapi –设置rpc的范围,暂时开启eth,web3,personal足够
console –启动命令行模式
更多选项参考:https://github.com/ethereum/go-ethereum/wiki/Command-Line-Options

以后也可以附加到geth控制台

ubuntu@VM-0-14-ubuntu:/$ cd /usr/src/ethdev/
ubuntu@VM-0-14-ubuntu:/usr/src/ethdev$ ll
total 656
drwxrwxrwx 4 root   root     4096 Apr  6 23:35 ./
drwxr-xr-x 6 root   root     4096 Apr  6 23:28 ../
drwx------ 4 ubuntu ubuntu   4096 Apr  6 23:35 geth/
srw------- 1 ubuntu ubuntu      0 Apr  6 23:35 geth.ipc=
-rw-rw-r-- 1 ubuntu ubuntu 647583 Apr  6 23:45 geth.log
drwx------ 2 ubuntu ubuntu   4096 Apr  6 23:35 keystore/
ubuntu@VM-0-14-ubuntu:/usr/src/ethdev$ geth attach geth.ipc 
WARN [04-06|23:45:28.592] Sanitizing cache to Go's GC limits       provided=1024 updated=328
Welcome to the Geth JavaScript console!

instance: Geth/v1.8.23-stable-c9427004/linux-amd64/go1.10.4
coinbase: 0x63835863263e7d235464da7d8ab08eb6560772d8
at block: 869 (Sat, 06 Apr 2019 23:45:28 CST)
 datadir: /usr/src/ethdev
 modules: admin:1.0 clique:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 shh:1.0 txpool:1.0 web3:1.0

> 

进入控制台,使用eth.getBalance查看默认账户余额,开发模式自动设置余额,自动开始挖矿

ubuntu@VM-0-14-ubuntu:/usr/src/ethdev$ geth --datadir "./" --rpc --rpcport 8545 --port 30303 --rpccorsdomain="*" -rpcapi eth,web3,personal,net --nodiscover console 2>>geth.log --dev --dev.period 1
Welcome to the Geth JavaScript console!

instance: Geth/v1.8.23-stable-c9427004/linux-amd64/go1.10.4
coinbase: 0x63835863263e7d235464da7d8ab08eb6560772d8
at block: 1 (Sat, 06 Apr 2019 23:29:26 CST)
 datadir: /usr/src/ethdev
 modules: admin:1.0 clique:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 shh:1.0 txpool:1.0 web3:1.0

> eth.accounts	//查询用户
["0x63835863263e7d235464da7d8ab08eb6560772d8"]
> user1=eth.accounts[0]
"0x63835863263e7d235464da7d8ab08eb6560772d8"
> user1
"0x63835863263e7d235464da7d8ab08eb6560772d8"
> eth.getBalance(user1)	//查询余额
1.15792089237316195423570985008687907853269984665640564039457584007913129639927e+77
> eth.blockNumber
174
> eth.blockNumber
177
> eth.blockNumber
178
> personal.newAccount("输入密码"); //创建用户
0x3ea19c66a3c1f6e868ee275150361bc23d278704
> user2='0x3ea19c66a3c1f6e868ee275150361bc23d278704'
"0x3ea19c66a3c1f6e868ee275150361bc23d278704"
> personal.unlockAccount(user1,"密码");	//解锁用户,开发模式不需要解锁可以跳过
> eth.sendTransaction({from: eth.accounts[0],to: eth.accounts[0],value: web3.toWei(3,"ether")});	//发起交易,可以自己转给自己
"0x8793e03c3477e1f5bb56c7e7352af562b56e3c3f1c253cedcc45c5ee8497af22"
> eth.getTransaction("0x8793e03c3477e1f5bb56c7e7352af562b56e3c3f1c253cedcc45c5ee8497af22")	//通过交易 Hash 查看交易
{
  blockHash: "0x70f2ae4380f3fd7fb5722ddc3e9b8c00792eb91ac9efc4b4a34b1f639ce37045",
  blockNumber: 1255,
  from: "0x63835863263e7d235464da7d8ab08eb6560772d8",
  gas: 90000,
  gasPrice: 1,
  hash: "0x8793e03c3477e1f5bb56c7e7352af562b56e3c3f1c253cedcc45c5ee8497af22",
  input: "0x",
  nonce: 0,
  r: "0x8ad7b49d35bbc02418d57a7231445da4eecbc13478c081ab7099e6ab15c597f5",
  s: "0x3c44c7bf251f4860b4d013f5ec041a38ad608cea655e9d130acc7cc6931a0fa0",
  to: "0x63835863263e7d235464da7d8ab08eb6560772d8",
  transactionIndex: 0,
  v: "0xa96",
  value: 3000000000000000000
}
> 

注意:创建新账户,内存太小会直接退出控制台,查看geth.log有提示内存过小等错误信息

geth控制台更多命令参考:https://www.jianshu.com/p/9fa31e4cdf4d

到此geth客户端搭建以及简单命令使用的教程结束,但是控制台始终不方便Dapp开发,所以下一篇文章发布Web3与geth的交互,感谢阅读!

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
1. 安装Ubuntu系统 首先需要在服务器上安装Ubuntu系统(建议使用16.04及以上版本)。可以使用云服务器或者本地安装。 2. 安装Geth Geth是以太坊的官方客户端,可以用于搭建私有链和联盟链。可以使用以下命令安装: sudo add-apt-repository -y ppa:ethereum/ethereum sudo apt-get update sudo apt-get install -y ethereum 安装完成后,可以使用以下命令查看版本号: geth version 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 --nodiscover --rpc --rpcaddr "0.0.0.0" --rpcport 8545 --rpcapi "eth,web3,personal,net" console 其中,/path/to/chaindata为存储区块链数据的路径,--networkid指定联盟链的ID,--nodiscover禁用节点发现功能,--rpc启用RPC服务,--rpcaddr指定RPC服务监听的IP地址,--rpcport指定RPC服务监听的端口号,--rpcapi指定可用的RPC接口,console表示启动控制台。 5. 创建账户 在控制台使用以下命令创建账户: personal.newAccount("password") 其中,password为账户的密码。 6. 启动挖矿 使用以下命令启动挖矿: miner.start() 7. 部署智能合约 使用Solidity编写智能合约代码,并使用Remix或Truffle等工具进行编译和部署。 以上就是在Ubuntu系统搭建以太坊联盟链的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值