在本机使用比特币Regtest测试网络

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/yzpbright/article/details/81017807

Regtest代表“回归测试(Regression Testing)”,是Bitcoin Core的一项特性,允许您创建本地区块链用于测试。 与testnet3(它是一个公共和共享的测试区块链)不同,regtest区块链旨在作为本地测试用的封闭运行系统。 您从头开始(from scratch)启动regtest区块链,创建一个本地的创世区块。 您可以将其他节点添加到网络中,或者仅仅运行单个节点来测试Bitcoin Core软件。

一、编译安装比特币核心客户端

参照 Ubuntu下编译安装比特币Bitcoin 编译安装即可

二、启动 regtest 网络

y@ubuntu:~$ bitcoind -regtest -txindex -daemon 
Bitcoin server starting
y@ubuntu:~$ 

说明:
-regtest 表示启动的是 regtest 网络;不同网络之间的区别在于创世区块不同。
-txindex 表示对所有的交易进行索引;否则默认只对与钱包地址有关的交易索引
-daemon 表示在后台运行

也可以通过将参数写入配置文件 ~/.bitcoin/bitcoin.conf 的方式 :

regtest=1 

rpcbind=127.0.0.1 
rpcallowip=127.0.0.1 
rpcuser=test 
rpcpassword=test 

server=1 
daemon=1 
txindex=1

然后直接执行命令,不用带参数:

bitcoind

即可。

基本命令使用

要使用命令,需要带上 regtest 标志。

挖矿

y@ubuntu:~$ bitcoin-cli -regtest generate 500

注意: 挖矿所得的coinbase交易中的奖励必须要100个区块的深度之后才能花费

查看区块信息

y@ubuntu:~$ bitcoin-cli -regtest getblockchaininfo
{
  "chain": "regtest",
  "blocks": 500,
  "headers": 500,
  "bestblockhash": "0139f2493a3d10dec105df453510b7104e8155e9a536558b2760d2563c5c0f2b",
  "difficulty": 4.656542373906925e-10,
  "mediantime": 1531385979,
  "verificationprogress": 1,
  "initialblockdownload": false,
  "chainwork": "00000000000000000000000000000000000000000000000000000000000003ea",
  "size_on_disk": 156650,
  "pruned": false,
  "softforks": [
    {
      "id": "bip34",
      "version": 2,
      "reject": {
        "status": false
      }
    },
    {
      "id": "bip66",
      "version": 3,
      "reject": {
        "status": false
      }
    },
    {
      "id": "bip65",
      "version": 4,
      "reject": {
        "status": false
      }
    }
  ],
  "bip9_softforks": {
    "csv": {
      "status": "active",
      "startTime": 0,
      "timeout": 9223372036854775807,
      "since": 432
    },
    "segwit": {
      "status": "active",
      "startTime": -1,
      "timeout": 9223372036854775807,
      "since": 0
    }
  },
  "warnings": "This is a pre-release test build - use at your own risk - do not use for mining or merchant applications"
}

查看挖矿奖励

y@ubuntu:~$ bitcoin-cli -regtest getbalance
12462.50000000

查看钱包默认账户的地址

y@ubuntu:~$ bitcoin-cli -regtest getaccountaddress
error code: -32
error message:
getaccountaddress is deprecated and will be removed in V0.18. To use this command, start bitcoind with -deprecatedrpc=accounts.

注意getaccountaddress命令在V0.18.版本已经被移除了。

分配一个新地址

y@ubuntu:~$ bitcoin-cli -regtest getnewaddress
2MvZH9rH1cQN6NSR6sY1Xkdku2e4hqU9aqX

向新地址中转账,这里转100个比特币

y@ubuntu:~$ bitcoin-cli -regtest sendtoaddress 2MvZH9rH1cQN6NSR6sY1Xkdku2e4hqU9aqX 100
053753fb919378974b864c8baec3bdd172ad75dd20676b137575be60630fa961

输出的结果是该交易的hash

查看该交易详细信息

y@ubuntu:~$ bitcoin-cli -regtest getrawtransaction  053753fb919378974b864c8baec3bdd172ad75dd20676b137575be60630fa961
02000000037c2a40e3ff3517d4a4bb66901bb69e37730286909cf1c701dfe755aa2120c1870000000048473044022023a74fa5e2132cf3964202a039c5083cf961637c7ae6f424a2fa6aa8d01dbb7902201ea16f1ecdb3e81edaff01d42559253b5d5d9df3cb711055a60d28dcd45f878a01feffffffeb92121885ca4faadb9855182ec03ce240893576b9f6b57bfd27b09db5ccca950000000049483045022100d58a738b0c9b1611dffbeeafc64b4e7aa2c38bdffe343a72bfb1a9354bf54eaf02207bebbdf3488384e1363c8f974dc49e5d1e479ebc235f1f020004acc361a598ad01feffffff3532dfdd72151e042154ca5745076a6ab20eace399d33e520309a86fb95b70d60000000048473044022064c0d1cbfb3182ee850893b5d9db4b90d21d9c23f79187d77a421ac71a8b84ae02207d521dcb2440f28945676aa040d3dc268ffe4aceea5d2061a1f1a5f23eac36f001feffffff02005c814a0000000017a91486d4ff0b24241ca3fd9260e6addad9bb902d45018700e40b540200000017a914245281b1f0e91afa30736ab9c0f6d9f9536a4a1f87f4010000
y@ubuntu:~$ bitcoin-cli -regtest decoderawtransaction 02000000037c2a40e3ff3517d4a4bb66901bb69e37730286909cf1c701dfe755aa2120c1870000000048473044022023a74fa5e2132cf3964202a039c5083cf961637c7ae6f424a2fa6aa8d01dbb7902201ea16f1ecdb3e81edaff01d42559253b5d5d9df3cb711055a60d28dcd45f878a01feffffffeb92121885ca4faadb9855182ec03ce240893576b9f6b57bfd27b09db5ccca950000000049483045022100d58a738b0c9b1611dffbeeafc64b4e7aa2c38bdffe343a72bfb1a9354bf54eaf02207bebbdf3488384e1363c8f974dc49e5d1e479ebc235f1f020004acc361a598ad01feffffff3532dfdd72151e042154ca5745076a6ab20eace399d33e520309a86fb95b70d60000000048473044022064c0d1cbfb3182ee850893b5d9db4b90d21d9c23f79187d77a421ac71a8b84ae02207d521dcb2440f28945676aa040d3dc268ffe4aceea5d2061a1f1a5f23eac36f001feffffff02005c814a0000000017a91486d4ff0b24241ca3fd9260e6addad9bb902d45018700e40b540200000017a914245281b1f0e91afa30736ab9c0f6d9f9536a4a1f87f4010000
{
  "txid": "053753fb919378974b864c8baec3bdd172ad75dd20676b137575be60630fa961",
  "hash": "053753fb919378974b864c8baec3bdd172ad75dd20676b137575be60630fa961",
  "version": 2,
  "size": 414,
  "vsize": 414,
  "weight": 1656,
  "locktime": 500,
  "vin": [
    {
      "txid": "87c12021aa55e7df01c7f19c90860273379eb61b9066bba4d41735ffe3402a7c",
      "vout": 0,
      "scriptSig": {
        "asm": "3044022023a74fa5e2132cf3964202a039c5083cf961637c7ae6f424a2fa6aa8d01dbb7902201ea16f1ecdb3e81edaff01d42559253b5d5d9df3cb711055a60d28dcd45f878a[ALL]",
        "hex": "473044022023a74fa5e2132cf3964202a039c5083cf961637c7ae6f424a2fa6aa8d01dbb7902201ea16f1ecdb3e81edaff01d42559253b5d5d9df3cb711055a60d28dcd45f878a01"
      },
      "sequence": 4294967294
    },
    {
      "txid": "95caccb59db027fd7bb5f6b976358940e23cc02e185598dbaa4fca85181292eb",
      "vout": 0,
      "scriptSig": {
        "asm": "3045022100d58a738b0c9b1611dffbeeafc64b4e7aa2c38bdffe343a72bfb1a9354bf54eaf02207bebbdf3488384e1363c8f974dc49e5d1e479ebc235f1f020004acc361a598ad[ALL]",
        "hex": "483045022100d58a738b0c9b1611dffbeeafc64b4e7aa2c38bdffe343a72bfb1a9354bf54eaf02207bebbdf3488384e1363c8f974dc49e5d1e479ebc235f1f020004acc361a598ad01"
      },
      "sequence": 4294967294
    },
    {
      "txid": "d6705bb96fa80903523ed399e3ac0eb26a6a074557ca5421041e1572dddf3235",
      "vout": 0,
      "scriptSig": {
        "asm": "3044022064c0d1cbfb3182ee850893b5d9db4b90d21d9c23f79187d77a421ac71a8b84ae02207d521dcb2440f28945676aa040d3dc268ffe4aceea5d2061a1f1a5f23eac36f0[ALL]",
        "hex": "473044022064c0d1cbfb3182ee850893b5d9db4b90d21d9c23f79187d77a421ac71a8b84ae02207d521dcb2440f28945676aa040d3dc268ffe4aceea5d2061a1f1a5f23eac36f001"
      },
      "sequence": 4294967294
    }
  ],
  "vout": [
    {
      "value": 12.49991680,
      "n": 0,
      "scriptPubKey": {
        "asm": "OP_HASH160 86d4ff0b24241ca3fd9260e6addad9bb902d4501 OP_EQUAL",
        "hex": "a91486d4ff0b24241ca3fd9260e6addad9bb902d450187",
        "reqSigs": 1,
        "type": "scripthash",
        "addresses": [
          "2N5Y9hEEtKKW9T6jEhcN4Fs7jvXCVaVA1bS"
        ]
      }
    },
    {
      "value": 100.00000000,
      "n": 1,
      "scriptPubKey": {
        "asm": "OP_HASH160 245281b1f0e91afa30736ab9c0f6d9f9536a4a1f OP_EQUAL",
        "hex": "a914245281b1f0e91afa30736ab9c0f6d9f9536a4a1f87",
        "reqSigs": 1,
        "type": "scripthash",
        "addresses": [
          "2MvZH9rH1cQN6NSR6sY1Xkdku2e4hqU9aqX"
        ]
      }
    }
  ]
}

生成一个区块,使得交易得到确认

y@ubuntu:~$ bitcoin-cli -regtest generate 1 
[
  "223012d674962e92a73b76f2e37277ab723935a0a8d0d68b5d6211997ecb2d06"
]

查看收到的比特币

y@ubuntu:~$ bitcoin-cli -regtest listreceivedbyaddress
[
  {
    "address": "2MvZH9rH1cQN6NSR6sY1Xkdku2e4hqU9aqX",
    "account": "",
    "amount": 100.00000000,
    "confirmations": 1,
    "label": "",
    "txids": [
      "053753fb919378974b864c8baec3bdd172ad75dd20676b137575be60630fa961"
    ]
  }
]

参考:
《精通比特币》第九章 区块链 9.9.3 Regtest–本地区块链
自建 BTC RegTest 测试网络

展开阅读全文

没有更多推荐了,返回首页