实验 1 熟悉 Bitcoin Core 的基本配置方法

1.1 熟悉比特币客户端的配置方法

本实验在Linux下完成配置

在Ubuntu中依次执行:

mkdir /wallet
cd /wallet
wget https://bitcoincore.org/bin/bitcoin-core-0.15.2/bitcoin-0.15.2-x86_64-linux-gnu.tar.gz
cd bitcoin-0.15.2/bin
ln -s /home/skye/Desktop/wallet/bitcoin-0.15.2/bin/bitcoind /usr/bin/bitcoind
ln -s /home/skye/Desktop/wallet/bitcoin-0.15.2/bin/bitcoin-cli /usr/bin/bitcoin-cli
ln -s /home/skye/Desktop/wallet/bitcoin-0.15.2/bin/bitcoin-qt /usr/bin/bitcoin-qt

成功安装好:

image-20220929151435603

在根目录下新建.bitcoin,新建bitcoin.conf文件,文件内容如下(这里是做完后续实验的截图,regtest文件夹是下一步生成的):

image-20220929151634079

运行bitcoindregtest文件夹中内容如下:

image-20220929151811517

打开日志文件debug.log

2022-09-28 12:09:23 Bitcoin version v0.15.2
2022-09-28 12:09:23 InitParameterInteraction: parameter interaction: -whitelistforcerelay=1 -> setting -whitelistrelay=1
2022-09-28 12:09:23 Validating signatures for all blocks.
2022-09-28 12:09:23 Setting nMinimumChainWork=0000000000000000000000000000000000000000000000000000000000000000
2022-09-28 12:09:23 Using the 'standard' SHA256 implementation
2022-09-28 12:09:23 Using RdRand as an additional entropy source
2022-09-28 12:09:23 Default data directory /root/.bitcoin
2022-09-28 12:09:23 Using data directory /root/.bitcoin/regtest
2022-09-28 12:09:23 Using config file /root/.bitcoin/bitcoin.conf
2022-09-28 12:09:23 Using at most 125 automatic connections (1024 file descriptors available)
2022-09-28 12:09:23 Using 16 MiB out of 32/2 requested for signature cache, able to store 524288 elements
2022-09-28 12:09:23 Using 16 MiB out of 32/2 requested for script execution cache, able to store 524288 elements
2022-09-28 12:09:23 Using 4 threads for script verification
2022-09-28 12:09:23 scheduler thread start
2022-09-28 12:09:23 HTTP: creating work queue of depth 16
2022-09-28 12:09:23 No rpcpassword set - using random cookie authentication
2022-09-28 12:09:23 Generated RPC authentication cookie /root/.bitcoin/regtest/.cookie
2022-09-28 12:09:23 HTTP: starting 4 worker threads
2022-09-28 12:09:23 init message: Verifying wallet(s)...
2022-09-28 12:09:23 Using BerkeleyDB version Berkeley DB 4.8.30: (April 9, 2010)
2022-09-28 12:09:23 Using wallet wallet.dat
2022-09-28 12:09:23 CDBEnv::Open: LogDir=/root/.bitcoin/regtest/database ErrorFile=/root/.bitcoin/regtest/db.log
2022-09-28 12:09:23 Cache configuration:
2022-09-28 12:09:23 * Using 2.0MiB for block index database
2022-09-28 12:09:23 * Using 8.0MiB for chain state database
2022-09-28 12:09:23 * Using 440.0MiB for in-memory UTXO set (plus up to 286.1MiB of unused mempool space)
2022-09-28 12:09:23 init message: Loading block index...
2022-09-28 12:09:23 Opening LevelDB in /root/.bitcoin/regtest/blocks/index
2022-09-28 12:09:23 Opened LevelDB successfully
2022-09-28 12:09:23 Using obfuscation key for /root/.bitcoin/regtest/blocks/index: 0000000000000000
2022-09-28 12:09:23 LoadBlockIndexDB: last block file = 0
2022-09-28 12:09:23 LoadBlockIndexDB: last block file info: CBlockFileInfo(blocks=0, size=0, heights=0...0, time=1970-01-01...1970-01-01)
2022-09-28 12:09:23 Checking all blk files are present...
2022-09-28 12:09:23 LoadBlockIndexDB: transaction index disabled
2022-09-28 12:09:23 Initializing databases...a
2022-09-28 12:09:23 Pre-allocating up to position 0x1000000 in blk00000.dat
2022-09-28 12:09:23 Opening LevelDB in /root/.bitcoin/regtest/chainstate
2022-09-28 12:09:23 Opened LevelDB successfully
2022-09-28 12:09:23 Using obfuscation key for /root/.bitcoin/regtest/chainstate: 358c63173439fcc5
2022-09-28 12:09:23 init message: Rewinding blocks...
2022-09-28 12:09:23 block index 20ms
2022-09-28 12:09:23 init message: Loading wallet...
2022-09-28 12:09:23 nFileVersion = 150200
2022-09-28 12:09:23 Keys: 2002 plaintext, 0 encrypted, 2002 w/ metadata, 2002 total
2022-09-28 12:09:23 wallet 51ms
2022-09-28 12:09:23 setKeyPool.size() = 2000
2022-09-28 12:09:23 mapWallet.size() = 0
2022-09-28 12:09:23 mapAddressBook.size() = 1
2022-09-28 12:09:23 UpdateTip: new best=0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206 height=0 version=0x00000001 log2_work=1 tx=1 date='2011-02-02 23:16:42' progress=1.000000 cache=0.0MiB(0txo)
2022-09-28 12:09:23 Failed to open mempool file from disk. Continuing anyway.
2022-09-28 12:09:23 mapBlockIndex.size() = 1
2022-09-28 12:09:23 nBestHeight = 0
2022-09-28 12:09:23 torcontrol thread start
2022-09-28 12:09:23 Bound to [::]:18444
2022-09-28 12:09:23 Bound to 0.0.0.0:18444
2022-09-28 12:09:23 init message: Loading P2P addresses...
2022-09-28 12:09:23 Loaded 0 addresses from peers.dat 0ms
2022-09-28 12:09:23 init message: Loading banlist...
2022-09-28 12:09:23 init message: Starting network threads...
2022-09-28 12:09:23 net thread start
2022-09-28 12:09:23 dnsseed thread start
2022-09-28 12:09:23 Loading addresses from DNS seeds (could take a while)
2022-09-28 12:09:23 0 addresses found from DNS seeds
2022-09-28 12:09:23 dnsseed thread exit
2022-09-28 12:09:23 addcon thread start
2022-09-28 12:09:23 opencon thread start
2022-09-28 12:09:23 init message: Done loading
2022-09-28 12:09:23 msghand thread start
2022-09-28 12:10:24 Adding fixed seed nodes as DNS doesn't seem to be available.
  • 测试中为存储链上交易状态初始化的数据空间是多少?

    2022-09-28 12:09:23 * Using 2.0MiB for block index database
    2022-09-28 12:09:23 * Using 8.0MiB for chain state database

    可以看到是8.0MIB

  • 初始化过程中,节点钱包密钥池最终保存了多少对密钥?

    2022-09-28 12:09:23 setKeyPool.size() = 2000

    2000对

  • 简述回归测试模式下,程序添加 P2P 节点的步骤

    在区块链中增加新的区块并与ip端口绑定,之后加载P2P地址,重建peers.dat和banlist.dat;启动网线程,完成加载,然后开启其他线程,寻找新鲜的更新信息,最后接受版本信息。

1.2 学习 bitcoin.conf 的配置方法

  • .bitcoin下创建alice、bob、network三个文件夹

  • 对应文件夹下配置bitcoin.conf文件,具体配置如下:

    regtest=1
    port=22222
    rpcport=18332
    addnode=127.0.0.1:22224
    addnode=127.0.0.1:22226

    rpcuser=alice

    # rpc访问的password
    rpcpassword=8PPL3gL3gAM967mies3E= #设置rpc接口的访问密码
    regtest=1
    port=22224
    rpcport=18334
    addnode=127.0.0.1:22222
    addnode=127.0.0.1:22226

    rpcuser=bob

    # rpc访问的password
    rpcpassword=8PPL3gL3gAM967mies3E= #设置rpc接口的访问密码
    regtest=1
    port=22226
    rpcport=18336
    addnode=127.0.0.1:22222
    addnode=127.0.0.1:22224

    rpcuser=network

    rpcpassword=8PPL3gL3gAM967mies3E= #设置rpc接口的访问密码
  • 同时启动三个终端运行:

    bitcoind -datadir=/root/.bitcoin/alice
    bitcoind -datadir=/root/.bitcoin/bob
    bitcoind -datadir=/root/.bitcoin/network
  • 查看debug日志

    image-20220929153403522

    可以看到节点成功连接

实验 2 掌握常用 RPC 指令,利用回归测试网络实现挖矿与交易

linux中命令简写:

alias alice-d='bitcoind -regtest -conf=/root/.bitcoin/alice/bitcoin.conf -datadir=/root/.bitcoin/alice $*'
alias bob-d='bitcoind -regtest -datadir=/root/.bitcoin/bob $*'
alias network-d='bitcoind -regtest -datadir=/root/.bitcoin/network $*'

alias alice-cli='bitcoin-cli -regtest -conf=/root/.bitcoin/alice/bitcoin.conf -datadir=/root/.bitcoin/alice $*'
alias bob-cli='bitcoin-cli -regtest -conf=/root/.bitcoin/bob/bitcoin.conf -datadir=/root/.bitcoin/bob $*'
alias network-cli='bitcoin-cli -regtest -conf=/root/.bitcoin/network/bitcoin.conf -datadir=/root/.bitcoin/network $*'

alias alice-qt=bitcoin-qt -regtest -datadir=/root/.bitcoin/alice $*
alias bob-qt=bitcoin-qt -regtest -datadir=/root/.bitcoin/bob $*
alias network-qt=bitcoin-qt -regtest -datadir=/root/.bitcoin/network $*

注意-conf后面对应的是刚刚配置文件的路径,要读取rpcuser rpcpassword,找不到就会报错

同时启动四个终端:

image-20220929153942127

进行交易的步骤:

  1. alice生成余额:

    root@skye-vm:/home/skye# alice-cli generate 150
    [
    ....这里有150条
    ]
    root@skye-vm:/home/skye# alice-cli getbalance
    2500.00000000

    image-20220929154151389

  2. 生成交易,由 alice 分别支付给 bob 2.5BTC,network 1.5BTC,并使交易入块获得确认;

    分别查询network和bob的地址,支付给他们,bob再挖一块确认,最后查看余额

    root@skye-vm:/home/skye# alice-cli getnewaddress
    mgQnJdZhzPw3y5jfzoibM1kMA8Rc3huyyt
    root@skye-vm:/home/skye# bob-cli getnewaddress
    mobmsBsg7NMXVRzHsisp4gtBUtorNZY1yk
    root@skye-vm:/home/skye# network-cli getnewaddress
    mvo34utYwAKmRY2sPiwE6mc8yBPaSocUYu
    root@skye-vm:/home/skye# alice-cli sendtoaddress "mobmsBsg7NMXVRzHsisp4gtBUtorNZY1yk" 1.5
    6be8d1fb847daaf2f668ff41f99168173a9af8eb7ce5635e9921a6a9c7ac40d4
    root@skye-vm:/home/skye# alice-cli sendtoaddress "mvo34utYwAKmRY2sPiwE6mc8yBPaSocUYu" 2.5
    0c691ad0b9134650eea5dd6ee20dba25c3120acc81b1b9a798522d0e4da571c9
    root@skye-vm:/home/skye# bob-cli generate 1
    [
    "7f3ec2a1e50f138eb8311b0dfc1d48287a64246545ad2fd2c3c17488e3fef828"
    ]
    root@skye-vm:/home/skye# bob-cli listaccounts
    {
    "": 1.50000000
    }
    root@skye-vm:/home/skye# network-cli listaccounts
    {
    "": 2.50000000
    }

    image-20220929154511317

实验

输入命令:

image-20220929155216433

可以看到两个交易的基本情况如图,包括交易id,交易序号,输出包含交易值,scrippubkey等,第一个交易数据量大小为0.10000000,第二个为0.09890000

image-20220929155503017