前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Ubuntu 创建私有链

Ubuntu 创建私有链

作者头像
余生
发布2022-09-27 09:14:55
4840
发布2022-09-27 09:14:55
举报
文章被收录于专栏:余生开发余生开发

geth 安装

代码语言:javascript
复制
sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install ethereum

定义创世区块

代码语言:javascript
复制
# genesis.json 文件
{
  "config": {
    "chainId": 666,
    "homesteadBlock": 0,
    "eip150Block": 0,
    "eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "eip155Block": 0,
    "eip158Block": 0,
    "byzantiumBlock": 0,
    "constantinopleBlock": 0,
    "petersburgBlock": 0,
    "istanbulBlock": 0,
    "ethash": {}
  },
  "nonce": "0x0",
  "timestamp": "0x5ddf8f3e",
  "extraData": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "gasLimit": "0x47b760",
  "difficulty": "0x00001",
  "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "coinbase": "0x0000000000000000000000000000000000000000",
  "alloc": { },
  "number": "0x0",
  "gasUsed": "0x0",
  "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
# 创建创世区块
geth --datadir data init genesis.json

启动

代码语言:javascript
复制
# geth help 查看具体命令
geth --datadir="data" console 2>eth.log

使用

代码语言:javascript
复制
# 区块高度
eth.blockNumber

# 创建用户(密码 123)
personal.newAccount("123")

# 所有账户信息
eth.accounts

# 启动挖矿
miner.start()

# 挖矿中?
eth.mining

# 停止挖矿
miner.stop()

# get 账户余额
eth.getBalance(eth.accounts[0])

# 转账(sendTx后需要有人挖矿)
amount = web3.toWei(8,'ether')
eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:amount})

eth.getTransaction("txID")

web3 调用

代码语言:javascript
复制
 npm install web3 --save
代码语言:javascript
复制
 # test.js
 
var Web3 = require('web3'); //引入web3
//console.log(Web3);
if (typeof web3 !== 'undefined') {
//console.log(1);
  web3 = new Web3(web3.currentProvider);
} else {
//console.log(2);
  // set the provider you want from Web3.providers
  web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}
//此处是获取所有的用户列表,在回调中直接打印出来
 web3.eth.getAccounts().then(console.log);
var v = web3.version;  //获取web3的版本
console.log(v);
代码语言:javascript
复制
node test.js
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-08-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • geth 安装
  • 定义创世区块
  • 启动
  • 使用
  • web3 调用
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档