BSC(币安智能链)私有链部署
Posted 看见月亮的人
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了BSC(币安智能链)私有链部署相关的知识,希望对你有一定的参考价值。
文章目录
- BSC链github地址:https://github.com/binance-chain/bsc
- 主网浏览器:https://bscscan.com/
- 主网链官方部署文档:https://docs.binance.org/smart-chain/developer/fullnode.html
- 私有链官方部署文档:https://docs.binance.org/smart-chain/developer/deploy/local.html
一. BSC私链镜像生成
- 下载BSC私链基础镜像
docker pull buildpack-deps:jessie-curl
- 编写Dockerfile
# vim Dockerfile
FROM buildpack-deps:jessie-curl
RUN mkdir /data
RUN wget -P /usr/local/bin/ https://github.com/binance-chain/bsc/releases/download/v1.0.7/geth_linux && mv /usr/local/bin/geth_linux /usr/local/bin/geth && chmod +x /usr/local/bin/geth
EXPOSE 8545 30303
WORKDIR /data
ENTRYPOINT ["/data/start.sh"]
- 生成BSC私链镜像
docker build . -t private_bsc:v1.0.7
注:需要在与Dockerfile同一级目录下执行此命令
二.BSC私链容器生成
2.1 新建创始区块文件
- 新建创始区块文件
# vim /opt/docker/private_bsc/data/genesis.json
{
"config": {
"chainId": 1024,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0
},
"nonce": "0x0000000000000061",
"timestamp": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"gasLimit": "0x8000000",
"difficulty": "0x100",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x3333333333333333333333333333333333333333",
"alloc": {}
}
- genesis.json文件属性详解
config:配置块定义我们的自定义链的设置,并具有创建私有区块链的某些属性。
chainId :标识我们的区块链,主要的以太坊链有自己的ID,但我们会将它设置为我们私有链的唯一值。
homesteadBlock:Homestead是以太坊平台的第二个主要版本,也是以太坊的第一个生产版本。它包括几个协议更改。由于我们已经在Homestead版本,因此该属性为0。- eip155Block/eip158Block:Homestead版本发布时带有一些向后不兼容的协议更改,因此需要硬分叉。通过以太坊改进提案(EIPs)提出的这些协议变更/改进。然而,我们的链条不会因为这些变化而难以分解,所以保留为0。
difficulty:此值用于控制区块链的块生成时间。难度越高,Miner在发现有效块时必须执行的统计更多计算。在我们的测试网络中,我们将保持此值低以避免在测试期间等待,因为需要生成有效块来执行交易处理区块链。
gasLimit:此值指定每块的“gas”支出的当前链范围限制。gas是以太坊在交易过程中消耗的燃料。我们将在这种情况下将此值标记得足够高,以避免在测试期间受到限制。
alloc:这是你可以创建你的钱包并用假ether预填充的地方。但是对于这篇文章,我们将在本地快速挖掘我们的以太,所以我们不使用这个选项。
2.2 新建服务启动脚本
# vim /opt/docker/private_bsc/data/start.sh
#!/bin/bash
set -e
# Init
echo ""
echo "Init geth"
geth --datadir /data/node init /data/genesis.json
sleep 3
# Start geth
echo ""
echo "Start geth"
geth --datadir /data/node --networkid 1024 --gasprice "0" --rpc --rpcapi eth,net,web3,miner,txpool --rpcaddr "0.0.0.0" --rpcport "8545" --syncmode "full" --gcmode "archive" --allow-insecure-unlock &
sleep 10
while true; do
sleep 1000000000
done
注意:
-
以上指定了一个名为networkid的参数。这标志着你的以太坊网络的身份。我们在这个例子中使用了1024,应该选择一个随机数来创建你自己的网络并防止其他人无意中连接到你的网络,此ID也必须与上文genesis.json文件中的"chainId"的ID一致
-
–syncmode “full” 此参数并不陌生,即以全节点的模式进行同步
-
–gcmode “archive” 区块链垃圾收集模式(“full”,“archive”)(默认为“full”) ,此参数并不常见,又意为归档模式,归档模式意味着存储智能契约中的所有值状态以及帐户的所有余额。
2.3 新建容器启动脚本
# vim /opt/docker/private_bsc/data/run.sh
#!/bin/bash
docker run -itd --restart=unless-stopped -v /etc/localtime:/etc/localtime -v /etc/timezone:/etc/timezone --name private_bsc -v $(pwd):/data -p 30303:30303 -p 8545:8545 private_bsc:v1.0.7
- 为脚本赋予执行权限
# chmod +x /opt/docker/private_bsc/data/start.sh
# chmod +x /opt/docker/private_bsc/data/run.sh
- 启动以太坊私有链容器
# cd /opt/docker/private_bsc/data
# sh run.sh
dbda5c09f940e59ec157f87ab28d7781e7aa9a02d505a1a376099af86b76bf8d
- 查看BSC私有链容器启动日志
# docker logs -f private_bsc
// 初始化geth
Init geth
INFO [04-25|12:53:44.391] Maximum peer count ETH=50 LES=0 total=50
INFO [04-25|12:53:44.391] Smartcard socket not found, disabling err="stat /run/pcscd/pcscd.comm: no such file or directory"
ERROR[04-25|12:53:44.391] Failed to enumerate USB devices hub=ledger vendor=11415 failcount=1 err="failed to initialize libusb:
INFO [04-25|12:53:44.405] Successfully wrote genesis state database=chaindata hash=acdfa6…7365e9
// 启动geth
Start geth
INFO [04-25|12:53:47.505] Maximum peer count ETH=50 LES=0 total=50
ERROR[04-25|12:53:47.506] Failed to enumerate USB devices hub=trezor vendor=4617 failcount=2 err="failed to initialize libusb: libusb: unknown error [code -99]"
INFO [04-25|12:53:47.552] Initialised chain configuration config="{ChainID: 1024 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: 0 EIP155: 0 EIP158: 0 Byzantium: <nil> Constantinople: <nil> Petersburg: <nil> Istanbul: <nil>, Muir Glacier: <nil>, Ramanujan: <nil>, Niels: <nil>, MirrorSync: <nil>, Engine: unknown}"
INFO [04-25|12:53:47.569] Started P2P networking self=enode://16e5178fec6d30bf113f5307f0e5e6f1352d00fd0e6c1f24d3aa9943fdf527f120e671e2ae9de43e4df6cd3ad523209f671a60e2af7bd166efc395d67f01a3d1@127.0.0.1:30303
INFO [04-25|12:53:47.570] IPC endpoint opened url=/data/node/geth.ipc
ERROR[04-25|12:53:48.506] Failed to enumerate USB devices hub=trezor vendor=4617 failcount=3 err="failed to initialize libusb: libusb: unknown error [code -99]"
三.BSC私有链启动挖矿
3.1 BSC私有链启动挖矿
- 控制台进入
// 进入容器
# docker exec -it private_bsc bash
// 进入控制台
# geth attach /data/node/geth.ipc
Welcome to the Geth javascript console!
instance: Geth/v1.0.7-f1d92bef/linux-amd64/go1.15.5
at block: 0 (Thu Jan 01 1970 08:00:00 GMT+0800 (CST))
datadir: /data/node
modules: admin:1.0 debug:1.0 eth:1.0 ethash:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0
- 创建账户
personal.newAccount()
Passphrase: 输入账号密码
Repeat passphrase: 再次输入账号密码
"0x2770212031615c2fe2e0a9d3ae40461f85c5ee61" 获得账号地址
注:此时在容器外的映射目录下,可查看生成的keystore,必须保存好此账户的密码及keystore,因为你需要它来管理你的帐户。
- 查看地址余额
> eth.getBalance("0x2770212031615c2fe2e0a9d3ae40461f85c5ee61")
0
注:
因为此账户为新生成,所以账户内还没有余额
有两种方法可以让以太币进入你的帐户,或者有人向你发送一些,或者你挖掘交易区块并依次获得以太币奖励
既然你现在独自一人在你的私人网络中,你现在唯一的选择是挖掘一些区块并获得奖励
- 开启挖矿
> > miner.start(1)
null
注:开启一个线程进行挖矿
- 此时可在容器外查看BSC私有链容器的日志,可查看到如下信息:
# docker logs -f eth-miner
// 利用刚刚生成的地址进行挖矿
INFO [04-25|12:58:58.691] Updated mining threads threads=1
INFO [04-25|12:58:58.691] Transaction pool price threshold updated price=1000000000
INFO [04-25|12:58:58.691] Etherbase automatically configured address=0x2770212031615C2fE2E0a9D3ae40461f85c5EE61
INFO [04-25|12:58:58.691] Commit new mining work number=1 sealhash=821559…53523a uncles=0 txs=0 gas=0 fees=0 elapsed=279.675µs
.......................
// 生成DAG数据,作用为:用于共识POW算法的工作量证明
INFO [04-25|12:58:59.841] Generating DAG in progress epoch=0 percentage=0 elapsed=398.375ms
INFO [04-25|12:59:00.237] Generating DAG in progress epoch=0 percentage=1 elapsed=794.129ms
INFO [04-25|12:59:00.639] Generating DAG in progress epoch=0 percentage=2 elapsed=1.196s
INFO [04-25|12:59:01.033] Generating DAG in progress epoch=0 percentage=3 elapsed=1.590s
INFO [04-25|12:59:01.427] Generating DAG in progress epoch=0 percentage=4 elapsed=1.984s
// 开始挖矿,代表矿工已经成功挖掘到的块
INFO [04-25|13:00:10.261] Successfully sealed new block number=4 sealhash=a1cc07…3f6cb2 hash=25dae6…22f964 elapsed=5.991s
INFO [04-25|13:00:10.261] mined potential block number=4 hash=25dae6…22f964
INFO [04-25|13:00:10.262] Commit new mining work number=5 sealhash=c0bdb0…8bdc19 uncles=0 txs=0 gas=0 fees=0 elapsed=183.649µs
3.2 其它命令使用
此时:
我们可以再次检查你地址内的余额。
> eth.getBalance("0x2770212031615c2fe2e0a9d3ae40461f85c5ee61")
320000000000000000000
哇噢!你的新帐户中会出现大量BSC。
(记住这是假的BSC,你不能使用这个以太网在主以太网网络上进行交易)但是你可以用它来测试区块链的几个功能,包括转账,部署合约等。
- 当前最新区块号
> eth.blockNumber
83
- 当前最新区块的详细信息
> eth.getBlock( eth.blockNumber )
{
difficulty: 136850,
extraData: "0xd883010007846765746888676f312e31352e35856c696e7578",
gasLimit: 91818738,
gasUsed: 0,
hash: "0x5e48ec96cc782a515d189ef8c6ae121cfc0b370bd513a2d7c49559ecdfb014eb",
logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
miner: "0x2770212031615c2fe2e0a9d3ae40461f85c5ee61",
mixHash: "0xff0d8b47d74a3f9a67aff7b2b74f5c57497170b057082b07e16807172cac91ee",
nonce: "0x2ea73a57d494e442",
number: 97,
parentHash: "0x9ebc7e22b90a1e408526f94ab4b83d58ec44db65d59fce64dba2978dd8fec3fd",
receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
size: 537,
stateRoot: "0x8ca463aa29b64c0792c9fafc3dccea105d38e026eb16ebca30d2f4e2ad005005",
timestamp: 1619327092,
totalDifficulty: 12992131,
transactions: [],
transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
uncles: []
}
- 哪个地址挖掘的最新区块
> eth.getBlock(eth.blockNumber).miner
"0x2770212031615c2fe2e0a9d3ae40461f85c5ee61"
- 账户余额(eth为单位)
> web3.fromWei(eth.getBalance(eth.accounts[0]))
650
- BSC转账
// 得到当前节点拥有地址
> eth.accounts
["0x2770212031615c2fe2e0a9d3ae40461f85c5ee61", "0x6aa0d82340b93e2e24d8e075c843e752f5bb4422"]
// 解锁from地址
> personal.unlockAccount('0x2770212031615c2fe2e0a9d3ae40461f85c5ee61','123456',300)
true
// 转账1个BSC
> eth.sendTransaction({from: "0x2770212031615c2fe2e0a9d3ae40461f85c5ee61", to: "0x6aa0d82340b93e2e24d8e075c843e752f5bb4422", value: web3.toWei(1, "ether")})
"0x9ba8f559b502d278541641669b6e1a3612a6c0e7f93c16ff250cf1e128b3c0af"
// 查看to地址余额为1 BSC
> eth.getBalance("0x6aa0d82340b93e2e24d8e075c843e752f5bb4422")
1000000000000000000
- 停止挖矿
> miner.stop()
null
注:
如果开启挖矿,日志则会一直输出,此时你搭建的BSC私链的数据也会越来越大,所以在搭建前,应保证磁盘容量是否充足
四.rpc命令使用
- 查询指定用户的最新余额
注:服务一旦搭建完成,我们不可能每次进行交易或查询用户信息时,都进入到以太坊私链容器内进行操作,以下示例为在宿主机命令行或者任意一台与宿主机可通信的命令行内使用curl命令调用rpc远程调用协议进行相关操作
4.1当前燃气价格
# curl -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"eth_gasPrice","params":[],"id":73}' http://127.0.0.1:8545
{"jsonrpc":"2.0","id":73,"result":"0x0"}
4.2 返回客户端拥有的地址列表
# curl -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"eth_accounts","params":[],"id":1}' http://127.0.0.1:8545
{"jsonrpc":"2.0","id":1,"result":["0x2770212031615c2fe2e0a9d3ae40461f85c5ee61","0x6aa0d82340b93e2e24d8e075c843e752f5bb4422"]}
4.3 返回给定地址的帐户的余额 参数地址
# curl -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"eth_getBalance","params":["0x2770212031615c2fe2e0a9d3ae40461f85c5ee61", "latest"],"id":1}' http://127.0.0.1:8545
{"jsonrpc":"2.0","id":1,"result":"0x12aa4bba8112af40000"}
4.4 转账
# curl -H Content-Type:application/json -X POST --data '{"jsonrpc":"2.0","method":"eth_sendTransaction", "params":[{"from":"0x2770212031615c2fe2e0a9d3ae40461f85c5ee61","to":"0x6aa0d82340b93e2e24d8e075c843e752f5bb4422","gas":"0x5208","gasPrice":"0x1264c45600","value":"0xde0b6b3a7640000"}],"id":1}' http://127.0.0.1:8545
{"jsonrpc":"2.0","id":1,"result":"0xa9fe4dd246c7cd7bb4deb6ac2a2274d03f0a71016ba313a72df7af3aba58ab57"}
4.5 根据交易哈希查询交易信息
# curl -H Content-Type:application/json -X POST --data '{"jsonrpc":"2.0","method":"eth_getTransactionByHash","params":["0xa9fe4dd246c7cd7bb4deb6ac2a2274d03f0a71016ba313a72df7af3aba58ab57"],"id":1}' http://127.0.0.1:8545
{"jsonrpc":"2.0","id":1,"result":{"blockHash":"0xbda7dcb2a96bdb184b00e063b0b4cccbe0237dac50863760da18ea2e97878279","blockNumber":"0x517","from":"0x2770212031615c2fe2e0a9d3ae40461f85c5ee61","gas":"0x5208","gasPrice":"0x1264c45600","hash":"0xa9fe4dd246c7cd7bb4deb6ac2a2274d03f0a71016ba313a72df7af3aba58ab57","input":"0x","nonce":"0x1","to":"0x6aa0d82340b93e2e24d8e075c843e752f5bb4422","transactionIndex":"0x0","value":"0xde0b6b3a7640000","v":"0x823","r":"0x1e0f7ff1eaf10244475c7f136a4201532a1aa09ca3aa185beff4d8f8214239d","s":"0x6cdd9e6a7ff19ce51f71426a4efa182a2c8c01d7ca16f644aaa63b673f13b96f"}}
4.6 从这个地址发送的交易数量的整数
# curl -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"eth_getTransactionCount","params":["0x2770212031615c2fe2e0a9d3ae40461f85c5ee61", "latest"],"id":1}' http://127.0.0.1:8545
{"jsonrpc":"2.0","id":1,"result":"0x1"}
4.7 查询最新区块高度
# curl -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}' http://127.0.0.1:8545
{"jsonrpc":"2.0","id":1,"result":"0x46e"}
4.8 根据区块高度获取区块信息
# curl -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["0x46e", true],"id":1}' http://127.0.0.1:8545
{"jsonrpc":"2.0","id":1,"result":{"difficulty":"0x349dc","extraData":"0xd883010007846765746888676f312e31352e35856c696e7578","gasLimit":"0x7a1200","gasUsed":"0x0","hash":"0xd8b464d9cf8e28eeed6042480b54d07ad3425e6c1006873b2aedb022bd777392","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","miner":"0x2770212031615c2fe2e0a9d3ae40461f85c5ee61","mixHash":"0x0306855db6701a71b56e6f4f6c67348748a1520c8e2bbaf80bbb95c3cb1e61c8","nonce":"0x6e859dfc02c48678","number":"0x46e","parentHash":"0x3dc65f09bf60a16fa54cc2e44b03ad2ff1a528760aa8be4f022f19a9e6bd4785","receiptsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","size":"0x21a","stateRoot":"0xcf51398874cb0184bd9a4c7e4cc07e77ff75f562f1abf703c98af5d46ed02159","timestamp":"0x608507b6","totalDifficulty":"0xb820f5f","transactions":[],"transactionsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","uncles":[]}}
4.9 根据区块哈希获取区块信息
# curl -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"eth_getBlockByHash","params":["0xd8b464d9cf8e28eeed6042480b54d07ad3425e6c1006873b2aedb022bd777392", true],"id":1}' http://127.0.0.1:8545
{"jsonrpc":"2.0","id":1,"result":{"difficulty":"0x349dc","extraData":"0xd883010007846765746888676f312e31352e35856c696e7578","gasLimit":"0x7a1200","gasUsed":"0x0","hash":"0xd8b464d9cf8e28eeed6042480b54d07ad3425e6c1006873b2aedb022bd777392","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","miner":"0x2770212031615c2fe2e0a9d3ae40461f85c5ee61","mixHash":"0x0306855db6701a71b56e6f4f6c67348748a1520c8e2bbaf80bbb95c3cb1e61c8","nonce":"0x6e859dfc02c48678","number":"0x46e","parentHash":"0x3dc65f09bf60a16fa54cc2e44b03ad2ff1a528760aa8be4f022f19a9e6bd4785","receiptsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","size":"0x21a","stateRoot":"0xcf51398874cb0184bd9a4c7e4cc07e77ff75f562f1abf703c98af5d46ed02159","timestamp":"0x608507b6","totalDifficulty":"0xb820f5f","transactions":[],"transactionsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","uncles":[]}}
4.10 根据区块哈希查询当前区块里的交易数
# curl -H Content-Type:application/json -X POST --data '{"jsonrpc":"2.0","method":"eth_getBlockTransactionCountByHash","params":["0xbda7dcb2a96bdb184b00e063b0b4cccbe0237dac50863760da18ea2e97878279"],"id":1}' http://127.0.0.1:8545
{"jsonrpc":"2.0","id":1,"result":"0x1"}
4.11 根据区块高度查询当前区块里的交易数
# curl -H Content-Type:application/json -X POST --data '{"jsonrpc":"2.0","method":"eth_getBlockTransactionCountByNumber","params":["0x517"],"id":1}' http://127.0.0.1:8545
{"jsonrpc":"2.0","id":1,"result":"0x1"}
4.12 获取hashrate
# curl -H Content-Type:application/json -X POST --data '{"jsonrpc":"2.0","method":"eth_hashrate","params":[],"id":1}' http://127.0.0.1:8545
{"jsonrpc":"2.0","id":1,"result":"0xa775"}
以上是关于BSC(币安智能链)私有链部署的主要内容,如果未能解决你的问题,请参考以下文章
0基础教学_在BSC主链上部署智能合约并验证合约_BSC发币_币安发币
币安智能链BSC分红|通缩燃烧|回滚流动池|慈善钱包 合约部署