做课设使用到了以太坊搭建私链做测试,记录一下
系统os:win11 wsl
Geth版本:1.10.26
安装Geth
点击SHOW OLDER RELEASES,下载1.10.26
下载完后找个目录解压,解压后内容如下:
[root@zlucelia geth]# pwd
/mnt/d/dev_software/geth
[root@zlucelia geth]# ll
total 142520
-rwxrwxrwx 1 root root 15976648 Nov 3 19:18 abigen
-rwxrwxrwx 1 root root 14204504 Nov 3 19:18 bootnode
-rwxrwxrwx 1 root root 38305328 Nov 3 19:18 clef
-rwxrwxrwx 1 root root 35149 Nov 3 19:16 COPYING
-rwxrwxrwx 1 root root 16597256 Nov 3 19:19 evm
-rwxrwxrwx 1 root root 43269776 Nov 3 19:19 geth
-rwxrwxrwx 1 root root 14941056 Nov 3 19:19 puppeth
-rwxrwxrwx 1 root root 2600247 Nov 3 19:19 rlpdump
配置环境变量
root@zlucelia:/mnt/d/dev_software/geth# vim ~/.bashrc
root@zlucelia:/mnt/d/dev_software/geth# source ~/.bashrc
export GOROOT=/mnt/d/dev_software/wsl-dev/go
export GOPATH=/mnt/f/wsl_go_workspace
export GOPROXY=https://goproxy.cn
export ETH_HOME=/mnt/d/dev_software/geth
PATH=$GOROOT/bin:$GOPATH/bin:$ETH_HOME:$PATH
检查安装效果
root@zlucelia:/mnt/e/privatechain# geth help
NAME:
geth - the go-ethereum command line interface
USAGE:
geth [global options] command [command options] [arguments...]
COMMANDS:
account Manage accounts
attach Start an interactive JavaScript environment (connect to node)
console Start an interactive JavaScript environment
db Low level database operations
dump Dump a specific block from storage
dumpconfig Show configuration values
dumpgenesis Dumps genesis block JSON configuration to stdout
export Export blockchain into file
export-preimages Export the preimage database into an RLP stream
import Import a blockchain file
import-preimages Import the preimage database from an RLP stream
init Bootstrap and initialize a new genesis block
js (DEPRECATED) Execute the specified JavaScript files
license Display license information
makecache Generate ethash verification cache (for testing)
makedag Generate ethash mining DAG (for testing)
removedb Remove blockchain and state databases
show-deprecated-flags Show flags that have been deprecated
snapshot A set of commands based on the snapshot
version Print version numbers
version-check Checks (online) for known Geth security vulnerabilities
wallet Manage Ethereum presale wallets
help, h Shows a list of commands or help for one command
以太坊创世
参考链接:Private Networks | go-ethereum
参考链接:rungeth/Geth安装与启动.md at master · yekai1003/rungeth (github.com)
创建工作目录:
root@zlucelia:/mnt/e/privatechain# pwd
/mnt/e/privatechain
编辑genesis.json
root@zlucelia:/mnt/e/privatechain# vim genesis.json
{
"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" }
}
}
coinbase 挖矿后获得奖励的账户地址
difficulty 挖矿难度
gasLimit 一个区块所能容纳gas的上限
nonce 随机值
mixhash 一个256位的哈希证明,与nonce相结合,验证本块的有效性
extraData 附加信息,随意填写
parentHash 前一块的hash值,由于是创世块,所以为0
初始化
初始化geth数据库
geth init --datadir data genesis.json
查看下目录结构
tree data
创建挖矿账户
geth account new --datadir data
创建时需要输入口令,并再次确认口令,不要忘记!!不要忘记!!不要忘记!!
Your new account is locked with a password. Please give a password. Do not forget this password.
Password: 123456
Repeat password:
Your new key was generated
Public address of the key: 0x868021C871D9c1ccb9Ff6f17FBCE2b14Db3D2EaA
Path of the secret key file: data/keystore/UTC--2023-04-16T01-53-06.178756011Z--868021c871d9c1ccb9ff6f17fbce2b14db3d2eaa
- 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!
启动节点
geth --datadir ./data --networkid 12345 --http --http.addr 0.0.0.0 --http.vhosts "*" --http.api "db,net,eth,web3,personal" --http.corsdomain "*" --snapshot=false --mine --miner.threads 1 --allow-insecure-unlock console 2> 1.log --ipcdisable
不加ipcdisable会报错,不清楚为什么
有时候提示数据库旧了,可以加一个--ignore-legacy-receipts
geth --datadir ./data --networkid 12345 --http --http.addr 0.0.0.0 --http.vhosts "*" --http.api "db,net,eth,web3,personal" --http.corsdomain "*" --snapshot=false --mine --miner.threads 1 --allow-insecure-unlock console 2> 1.log --ipcdisable --ignore-legacy-receipts
不带挖矿的
geth --datadir ./data --networkid 12345 --http --http.addr 0.0.0.0 --http.vhosts "*" --http.api "db,net,eth,web3,personal" --http.corsdomain "*" --snapshot=false --allow-insecure-unlock console 2> 1.log --ipcdisable --ignore-legacy-receipts
不带控制台,直接打印信息的
geth --datadir ./data --networkid 12345 --http --http.addr 0.0.0.0 --http.vhosts "*" --http.api "db,net,eth,web3,personal" --http.corsdomain "*" --snapshot=false --allow-insecure-unlock --ipcdisable --ignore-legacy-receipts
挂在后台
nohup command &
datadir 指定之前初始化的数据目录文件
networkid 配置成与配置文件config内的chainId相同值,代表加入哪个网络,私链就自己随意编号即可
http 代表开启远程调用服务
http.port 远程服务的端口,默认是8545
http.api 远程服务提供的远程调用函数集
http.corsdomain 指定可以接收请求来源的域名列表(浏览器访问,必须开启)
allow-insecure-unlock 新版本增加的选项,允许在Geth命令窗口解锁账户
mine 开启挖矿
miner.threads 设置挖矿的线程数量
console 进入管理台
2> 1.log Unix系统下的重定向,将Geth产生的日志输出都重定向到1.log中,以免刷日志影响操作
账户操作
01:查看当前存在的账户
> eth.accounts
[]
默认不会有账户信息,需要创建后才能有
02:创建账户,注意传入的123是密码
> personal.newAccount("123")
"0x70aea0aea5bf9568a650598dfef44d6d3cca209a"
> eth.accounts
["0x70aea0aea5bf9568a650598dfef44d6d3cca209a"]
03:启动挖矿,start内的1是代表1个线程挖矿,null并非代表错误
> miner.start(1)
null
04:查看账户余额,都是挖矿所得
> eth.getBalance(eth.accounts[0])
95000000000000000000
05:再创建一个账户,密码456
> personal.newAccount("456")
"0xd56f07dc185c32f0367469b353c6cae711fa4a46"
06:给两个账户起别名acc0,acc1
> acc0=eth.accounts[0]
"0x70aea0aea5bf9568a650598dfef44d6d3cca209a"
> acc1=eth.accounts[1]
"0xd56f07dc185c32f0367469b353c6cae711fa4a46"
07:解锁账户1
> personal.unlockAccount(acc0)
Unlock account 0x70aea0aea5bf9568a650598dfef44d6d3cca209a
Password:
true
08:转账给账户2,10个ether
> eth.sendTransaction({from:acc0,to:acc1,value:web3.toWei(10)})
"0x0ccfda6137ca5c9095f03498404223ae08910d811ab7b1a7497abe3834f9fae9"
09:查看账户2余额
> eth.getBalance(acc1)
10000000000000000000
10:查看交易
eth.getTransaction('0x1c7186aed873f0ed5b83a89d271b4a2188fa76ccce18980991b9b3af44dea68b')
{
blockHash: null,
blockNumber: null,
chainId: "0x3039",
from: "0xd1826b5560c115a8a398b0fb1560c2f158864d3a",
gas: 21000,
gasPrice: 1000000000,
hash: "0x1c7186aed873f0ed5b83a89d271b4a2188fa76ccce18980991b9b3af44dea68b",
input: "0x",
nonce: 3,
r: "0x7c4deb075831c35258f322e1160bc70779806e05f6ebbdaea05f0233d252f1e2",
s: "0x3ff83eb34371172b30951ad84adc7374a3b30b355df25e7af75e5ffe5a0eaca3",
to: "0xfeb2ad18dd32bb9644efa2087a96aeda80bd7c06",
transactionIndex: null,
type: "0x0",
v: "0x6095",
value: 1e+21
}
11:此时交易已经提交到区块链,返回了交易的hash,但还未被处理,这可以通过查看txpool来验证:
> txpool.status
{
pending: 4,
queued: 0
}
其中有一条pending的交易,pending表示已提交但还未被处理的交易。
要使交易被处理,必须要挖矿。这里我们启动挖矿,然后等待挖到一个区块之后就停止挖矿
12:退出geth
> exit
bogon:rungeth yk$
remix基础使用
基本代码.sol
// SPDX-License-Identifier: GPL-3.0
pragma solidity^0.8.0;
contract hello {
bytes[10] hh;
string public say="2";
}
编译
部署
vm部署
本地私网部署