创建启动geth私有链

一、二进制安装geth

1\下载安装geth(二进制包):wget https://gethstore.blob.core.windows.net/builds/geth-alltools-linux-amd64-1.10.26-e5eb32ac.tar.gz

2\解压

tar -xzvf 

3、配置系统环境变量

echo "export PATH=$PATH:/usr/local/software/go-ethereum" >> ~/.bashrc

4、验证是否安装成功

geth version

二、源码安装geth

需提前下载go

1、选择安装目录,并切换到该目录下(我的是/usr/local/software),下载go的二进制包:

wget https://dl.google.com/go/go1.20.5.linux-amd64.tar.gz

2、解压:tar -xzvf go1.20.5.linux-amd64.tar.gz

3、设置环境变量:

echo "export PATH=$PATH:/usr/local/software/go/bin" >> ~/.bashrc

source ~/.bashrc

4、验证是否安装成功

go version

下载geth源码

1、从github上下载源码:

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

出现一下内容,即为下载成功:

正克隆到 'go-ethereum'...
remote: Enumerating objects: 101980, done.
接收对象中: 100% (101980/101980), 169.42 MiB | 108.00 KiB/s, done.
remote: Total 101980 (delta 0), reused 0 (delta 0), pack-reused 101980
处理 delta 中: 100% (60878/60878), done.

2、切换到go-ethereum目录,cd go-ethereum,查看
accounts      COPYING           ethstats      Makefile     rpc
appveyor.yml  COPYING.LESSER       event      metrics      SECURITY.md
AUTHORS       core           go.mod      miner        signer
build          crypto           go.sum      mobile       swarm
circle.yml    Dockerfile       graphql      node           tests
cmd          Dockerfile.alltools  interfaces.go  oss-fuzz.sh  trie
common          docs           internal      p2p
consensus     eth           les          params
console       ethclient           light      README.md
contracts     ethdb           log          rlp
 

3、make geth 对源码进行编译,出现以下内容:

build/env.sh go run build/ci.go install ./cmd/geth
>>> /usr/lib/golang/bin/go install -ldflags -X main.gitCommit=4bcc0a37ab70cb79b16893556cffdaad6974e7d8 -v ./cmd/geth
github.com/ethereum/go-ethereum/vendor/golang.org/x/sys/unix
github.com/ethereum/go-ethereum/vendor/github.com/elastic/gosigar
github.com/ethereum/go-ethereum/common/math
github.com/ethereum/go-ethereum/crypto/secp256k1
github.com/ethereum/go-ethereum/rlp
github.com/ethereum/go-ethereum/crypto
github.com/ethereum/go-ethereum/vendor/github.com/allegro/bigcache/queue
github.com/ethereum/go-ethereum/vendor/github.com/allegro/bigcache
github.com/ethereum/go-ethereum/common/prque
github.com/ethereum/go-ethereum/vendor/github.com/go-stack/stack
github.com/ethereum/go-ethereum/log
github.com/ethereum/go-ethereum/metrics
github.com/ethereum/go-ethereum/vendor/github.com/syndtr/goleveldb/leveldb/util
github.com/ethereum/go-ethereum/vendor/github.com/syndtr/goleveldb/leveldb/cache
github.com/ethereum/go-ethereum/vendor/github.com/syndtr/goleveldb/leveldb/comparer
github.com/ethereum/go-ethereum/vendor/github.com/syndtr/goleveldb/leveldb/storage
github.com/ethereum/go-ethereum/vendor/github.com/syndtr/goleveldb/leveldb/errors
github.com/ethereum/go-ethereum/vendor/github.com/syndtr/goleveldb/leveldb/filter
github.com/ethereum/go-ethereum/vendor/github.com/syndtr/goleveldb/leveldb/iterator
github.com/ethereum/go-ethereum/vendor/github.com/syndtr/goleveldb/leveldb/journal
github.com/ethereum/go-ethereum/vendor/github.com/syndtr/goleveldb/leveldb/memdb
github.com/ethereum/go-ethereum/vendor/github.com/syndtr/goleveldb/leveldb/opt
github.com/ethereum/go-ethereum/vendor/github.com/golang/snappy
github.com/ethereum/go-ethereum/vendor/github.com/syndtr/goleveldb/leveldb/table
github.com/ethereum/go-ethereum/vendor/github.com/syndtr/goleveldb/leveldb
github.com/ethereum/go-ethereum/ethdb
github.com/ethereum/go-ethereum/trie
github.com/ethereum/go-ethereum/core/types
github.com/ethereum/go-ethereum
github.com/ethereum/go-ethereum/vendor/github.com/aristanetworks/goarista/monotime
github.com/ethereum/go-ethereum/common/mclock
github.com/ethereum/go-ethereum/event
github.com/ethereum/go-ethereum/accounts
github.com/ethereum/go-ethereum/vendor/github.com/deckarep/golang-set
github.com/ethereum/go-ethereum/vendor/github.com/pborman/uuid
github.com/ethereum/go-ethereum/vendor/github.com/rjeczalik/notify
github.com/ethereum/go-ethereum/vendor/golang.org/x/crypto/pbkdf2
github.com/ethereum/go-ethereum/vendor/golang.org/x/crypto/scrypt
github.com/ethereum/go-ethereum/accounts/keystore
github.com/ethereum/go-ethereum/cmd/internal/browser
github.com/ethereum/go-ethereum/common/fdlimit
github.com/ethereum/go-ethereum/vendor/github.com/hashicorp/golang-lru/simplelru
github.com/ethereum/go-ethereum/vendor/github.com/hashicorp/golang-lru
github.com/ethereum/go-ethereum/core/state
github.com/ethereum/go-ethereum/p2p/netutil
github.com/ethereum/go-ethereum/vendor/golang.org/x/net/context
github.com/ethereum/go-ethereum/vendor/github.com/rs/xhandler
github.com/ethereum/go-ethereum/vendor/github.com/rs/cors
github.com/ethereum/go-ethereum/vendor/golang.org/x/net/websocket
github.com/ethereum/go-ethereum/rpc
github.com/ethereum/go-ethereum/consensus
github.com/ethereum/go-ethereum/consensus/misc
github.com/ethereum/go-ethereum/consensus/clique
github.com/ethereum/go-ethereum/vendor/github.com/edsrzf/mmap-go
github.com/ethereum/go-ethereum/common/bitutil
github.com/ethereum/go-ethereum/consensus/ethash
github.com/ethereum/go-ethereum/core/rawdb
github.com/ethereum/go-ethereum/vendor/golang.org/x/sys/cpu
github.com/ethereum/go-ethereum/crypto/bn256/cloudflare
github.com/ethereum/go-ethereum/crypto/bn256
github.com/ethereum/go-ethereum/vendor/golang.org/x/crypto/ripemd160
github.com/ethereum/go-ethereum/core/vm
github.com/ethereum/go-ethereum/core
github.com/ethereum/go-ethereum/crypto/ecies
github.com/ethereum/go-ethereum/p2p/enr
github.com/ethereum/go-ethereum/p2p/enode
github.com/ethereum/go-ethereum/p2p/discover
github.com/ethereum/go-ethereum/vendor/github.com/huin/goupnp/httpu
github.com/ethereum/go-ethereum/vendor/github.com/huin/goupnp/scpd
github.com/ethereum/go-ethereum/vendor/github.com/huin/goupnp/soap
github.com/ethereum/go-ethereum/vendor/github.com/huin/goupnp/ssdp
github.com/ethereum/go-ethereum/vendor/golang.org/x/net/html/atom
github.com/ethereum/go-ethereum/vendor/golang.org/x/net/html
github.com/ethereum/go-ethereum/vendor/golang.org/x/text/encoding/internal/identifier
github.com/ethereum/go-ethereum/vendor/golang.org/x/text/transform
github.com/ethereum/go-ethereum/vendor/golang.org/x/text/encoding
github.com/ethereum/go-ethereum/vendor/golang.org/x/text/encoding/internal
github.com/ethereum/go-ethereum/vendor/golang.org/x/text/encoding/charmap
github.com/ethereum/go-ethereum/vendor/golang.org/x/text/encoding/japanese
github.com/ethereum/go-ethereum/vendor/golang.org/x/text/encoding/korean
github.com/ethereum/go-ethereum/vendor/golang.org/x/text/encoding/simplifiedchinese
github.com/ethereum/go-ethereum/vendor/golang.org/x/text/encoding/traditionalchinese
github.com/ethereum/go-ethereum/vendor/golang.org/x/text/internal/utf8internal
github.com/ethereum/go-ethereum/vendor/golang.org/x/text/runes
github.com/ethereum/go-ethereum/vendor/golang.org/x/text/encoding/unicode
github.com/ethereum/go-ethereum/vendor/golang.org/x/text/internal/tag
github.com/ethereum/go-ethereum/vendor/golang.org/x/text/language
github.com/ethereum/go-ethereum/vendor/golang.org/x/text/encoding/htmlindex
github.com/ethereum/go-ethereum/vendor/golang.org/x/net/html/charset
github.com/ethereum/go-ethereum/vendor/github.com/huin/goupnp
github.com/ethereum/go-ethereum/vendor/github.com/huin/goupnp/dcps/internetgateway1
github.com/ethereum/go-ethereum/vendor/github.com/huin/goupnp/dcps/internetgateway2
github.com/ethereum/go-ethereum/vendor/github.com/jackpal/go-nat-pmp
github.com/ethereum/go-ethereum/p2p/nat
github.com/ethereum/go-ethereum/p2p/discv5
github.com/ethereum/go-ethereum/p2p
github.com/ethereum/go-ethereum/vendor/github.com/mohae/deepcopy
github.com/ethereum/go-ethereum/dashboard
github.com/ethereum/go-ethereum/core/bloombits
github.com/ethereum/go-ethereum/eth/downloader
github.com/ethereum/go-ethereum/eth/fetcher
github.com/ethereum/go-ethereum/eth/filters
github.com/ethereum/go-ethereum/vendor/github.com/davecgh/go-spew/spew
github.com/ethereum/go-ethereum/internal/ethapi
github.com/ethereum/go-ethereum/eth/gasprice
github.com/ethereum/go-ethereum/eth/tracers/internal/tracers
github.com/ethereum/go-ethereum/vendor/gopkg.in/olebedev/go-duktape.v3
github.com/ethereum/go-ethereum/eth/tracers
github.com/ethereum/go-ethereum/miner
github.com/ethereum/go-ethereum/vendor/github.com/golang/protobuf/proto
github.com/ethereum/go-ethereum/vendor/github.com/golang/protobuf/protoc-gen-go/descriptor
github.com/ethereum/go-ethereum/accounts/usbwallet/internal/trezor
github.com/ethereum/go-ethereum/vendor/github.com/karalabe/hid
github.com/ethereum/go-ethereum/accounts/usbwallet
github.com/ethereum/go-ethereum/metrics/exp
github.com/ethereum/go-ethereum/vendor/github.com/fjl/memsize
github.com/ethereum/go-ethereum/vendor/github.com/fjl/memsize/memsizeui
github.com/ethereum/go-ethereum/vendor/github.com/mattn/go-isatty
github.com/ethereum/go-ethereum/vendor/github.com/mattn/go-colorable
github.com/ethereum/go-ethereum/vendor/gopkg.in/urfave/cli.v1
github.com/ethereum/go-ethereum/internal/debug
github.com/ethereum/go-ethereum/vendor/github.com/prometheus/prometheus/util/flock
github.com/ethereum/go-ethereum/node
github.com/ethereum/go-ethereum/eth
github.com/ethereum/go-ethereum/les/flowcontrol
github.com/ethereum/go-ethereum/light
github.com/ethereum/go-ethereum/les
github.com/ethereum/go-ethereum/ethstats
github.com/ethereum/go-ethereum/vendor/github.com/influxdata/influxdb/pkg/escape
github.com/ethereum/go-ethereum/vendor/github.com/influxdata/influxdb/models
github.com/ethereum/go-ethereum/vendor/github.com/influxdata/influxdb/client
github.com/ethereum/go-ethereum/metrics/influxdb
github.com/ethereum/go-ethereum/vendor/golang.org/x/sync/syncmap
github.com/ethereum/go-ethereum/whisper/whisperv6
github.com/ethereum/go-ethereum/cmd/utils
github.com/ethereum/go-ethereum/internal/jsre/deps
github.com/ethereum/go-ethereum/vendor/github.com/fatih/color
github.com/ethereum/go-ethereum/vendor/gopkg.in/sourcemap.v1/base64vlq
github.com/ethereum/go-ethereum/vendor/gopkg.in/sourcemap.v1
github.com/ethereum/go-ethereum/vendor/github.com/robertkrimen/otto/file
github.com/ethereum/go-ethereum/vendor/github.com/robertkrimen/otto/token
github.com/ethereum/go-ethereum/vendor/github.com/robertkrimen/otto/ast
github.com/ethereum/go-ethereum/vendor/github.com/robertkrimen/otto/dbg
github.com/ethereum/go-ethereum/vendor/github.com/robertkrimen/otto/parser
github.com/ethereum/go-ethereum/vendor/github.com/robertkrimen/otto/registry
github.com/ethereum/go-ethereum/vendor/github.com/robertkrimen/otto
github.com/ethereum/go-ethereum/internal/jsre
github.com/ethereum/go-ethereum/internal/web3ext
github.com/ethereum/go-ethereum/vendor/github.com/peterh/liner
github.com/ethereum/go-ethereum/console
github.com/ethereum/go-ethereum/ethclient
github.com/ethereum/go-ethereum/vendor/github.com/maruel/panicparse/stack
github.com/ethereum/go-ethereum/vendor/github.com/mattn/go-runewidth
github.com/ethereum/go-ethereum/vendor/github.com/mitchellh/go-wordwrap
github.com/ethereum/go-ethereum/vendor/github.com/nsf/termbox-go
github.com/ethereum/go-ethereum/vendor/github.com/gizak/termui
github.com/ethereum/go-ethereum/vendor/github.com/naoina/go-stringutil
github.com/ethereum/go-ethereum/vendor/github.com/naoina/toml/ast
github.com/ethereum/go-ethereum/vendor/github.com/naoina/toml
github.com/ethereum/go-ethereum/cmd/geth
Done building.
Run "/root/go-ethereum/build/bin/geth" to launch geth.

4、设置geth环境变量

echo "export PATH=$PATH:/usr/local/software/go-ethereum/build/bin" >> ~/.bashrc

5、验证安装是否成功

geth version
 

三、创建区块链创世文件:

下载的geth客户端源码go-ethereum源码一般不跟启动文件不在一个目录。

我的做法,go-ethereum在一个目录,另外建一个启动文件,rungeth文件夹

1、在rungeth启动文件夹中新建genesis.json文件:

{
  "config": {
    "chainId": 1008,
    "homesteadBlock": 0,
    "eip150Block": 0,
    "eip155Block": 0,
    "eip158Block": 0,
    "byzantiumBlock": 0,
    "constantinopleBlock": 0,
    "petersburgBlock": 0,
    "ethash": {}
  },
  "difficulty": "1",
  "gasLimit": "8000000",
  "alloc": {
    "0x182e495EfFDAa6CBD36A842F4FFdF99476A3Bf70": { "balance": "30000000000000000000" },
    "0x2930837DF0812C9a760bF7057B6eeAc4b893e04f": { "balance": "40000000000000000000" }
  }

2、创建账户

geth account new --datadir node1

此时创建的账户,由geth管理,创建完的账户,填到genesis.json中去(启动的私有链的账户alloc就有了初始余额,不再非要挖矿就有余额了)。
 

3、、初始化私有链

geth --datadir node1 init genesis.json

4、启动私有链

geth --datadir node1 --networkid 1008 console 2>1.log

启动并开启rpc服务

普通模式
geth --datadir node1 --networkid 1008  --http --http.addr 127.0.0.1 --http.vhosts "*" --http.api "db,net,eth,web3,personal,miner" --http.corsdomain "*" --nodiscover -- --allow-insecure-unlock  console 2> 1.log
 
 
开发者模式
 
geth --datadir ./devdata --networkid 18 --port 30303 --http --http.addr 0.0.0.0 --http.vhosts "*"  --http.port 8545 --http.api 'db,net,eth,web3,personal' --http.corsdomain "*"  --dev --dev.period 1 console 2> 1.log

挂起模式
nohup geth --datadir node1 --networkid 1009  --http --http.addr 127.0.0.1 --http.port 8545 --http.vhosts "*" --http.api "db,net,eth,web3,personal,miner" --http.corsdomain "*" --nodiscover --snapshot=false --allow-insecure-unlock 2> 1.log &

挂起模式进入控制台:
geth attach http://localhost:8545
 

成功启动私有链

另外,personal.newAccount()出错,是因为版本过高,降低geth版本即可,为1.10.8。

目前搭建私链只能是老版本的geth,以太坊升级转pos后,如何搭建私链,需要后续继续研究。

API和控制台选项

API AND CONSOLE OPTIONS:              API 和控制台选项
  --ipcdisable                        禁用 IPC-RPC 服务器
  --ipcpath value                     datadir 中 IPC 套接字/管道的文件名(显式路径对其进行转义)
  --http                              启用 HTTP-RPC 服务器
  --http.addr value                   HTTP-RPC 服务器监听接口(默认:“localhost”)
  --http.port value                   HTTP-RPC 服务器监听端口(默认:8545)
  --http.api value                    通过 HTTP-RPC 接口提供的 API
  --http.rpcprefix value              提供 JSON-RPC 的 HTTP 路径路径前缀。使用“/”在所有路径上提供服务。
  --http.corsdomain value             接受跨源请求的域的逗号分隔列表(浏览器强制执行)
  --http.vhosts value                 逗号分隔的虚拟主机名列表,从中接受请求(服务器强制执行)。接受“*”通配符。 (默认:“本地主机”)
  --ws                                启用 WS-RPC 服务器
  --ws.addr value                     WS-RPC 服务器监听接口(默认:“localhost”)
  --ws.port value                     WS-RPC 服务器监听端口(默认:8546)
  --ws.api value                      通过 WS-RPC 接口提供的 API
  --ws.rpcprefix value                提供 JSON-RPC 的 HTTP 路径前缀。使用“/”在所有路径上提供服务。
  --ws.origins value                  接受 websockets 请求的来源
  --graphql                           在 HTTP-RPC 服务器上启用 GraphQL。请注意,GraphQL 只能在 HTTP 服务器启动的情况下启动。
  --graphql.corsdomain value          接受跨源请求的域的逗号分隔列表(浏览器强制执行)
  --graphql.vhosts value              逗号分隔的虚拟主机名列表,从中接受请求(服务器强制执行)。接受“*”通配符。 (默认:“本地主机”)
  --rpc.gascap value                  设置可以在 eth_call/estimateGas 中使用的 gas 上限(0=无限)(默认值:50000000)
  --rpc.txfeecap value                设置可以通过 RPC API 发送的交易费用上限(以以太为单位)(0 = 无上限)(默认值:1)
  --rpc.allow-unprotected-txs         允许通过 RPC 提交不受保护的(非 EIP155 签名)交易
  --jspath loadScript                 loadScript 的 JavaScript 根路径(默认值:“.”)
  --exec value                        执行 JavaScript 语句
  --preload value                     要预加载到控制台的以逗号分隔的 JavaScript 文件列表

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值