搭建比特币测试网络testnet

搭建比特币测试网络testnet

  • 下载bitcoincore

    https://bitcoin.org/zh_CN/download

     #下载页面
     #https://bitcoin.org/zh_CN/download
    
    cd /opt/
    wget https://bitcoin.org/bin/bitcoin-core-0.17.1/bitcoin-0.17.1-x86_64-linux-gnu.tar.gz
    
     #解压, 将客户端程序和服务端程序复制到  /usr/local/bin/目录下
    tar zxf bitcoin-0.17.1-x86_64-linux-gnu.tar.gz
    cd  bitcoin-0.17.1
    sudo cp bitcoin-cli bitcoind  /usr/local/bin/
    
    
  • 启动testnet

    编辑 ~/.bitcoin/bitcoin.conf

    testnet=1
    dbcache=10240
    txindex=1
    rpcuser=btc
    rpcpassword=btc2018
    daemon=1
    server=1
    rest=1
    rpcbind=0.0.0.0:18332
    rpcallowip=0.0.0.0/0
    deprecatedrpc=accounts
    

    直接启动 bitcoind

    启动之后, 会同步测试网络数据, 大概30G左右. (也可以使用轻节点方式)

  • 测试RPC

    bitcoin-cli getblockchaininfo     #查看区块链同步情况
    bitcoin-cli getmininginfo
    
    
    #curl访问rpc测试
    curl -s -X POST --user btc:btc2018  \
      -H 'content-type: text/plain;' http://127.0.0.1:18332/ \
      --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getmininginfo", "params": [] }' 
    
    #查询最新区块高度
    curl -s -X POST --user btc:btc2018  \
      -H 'content-type: text/plain;' http://127.0.0.1:18332/ \
      --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getblockcount", "params": [] }' \
      |awk -F '[:,]' '{print $3}'
    

使用C++编写代码进行RPC调用实现转账交易

  • 创建接受地址

    bitcoin-cli getaccountaddress "yqq"    //获取账户地址, 如果账户不存在则创建, 用于接收 
    GetNewAddress
    
  • 获取测试币

    https://coinfaucet.eu/en/btc-testnet/

  • 安装依赖库

    https://github.com/cinemast/libjson-rpc-cpp
    https://github.com/minium/bitcoin-api-cpp
    
  • 转账代码

    #include <bitcoinapi/bitcoinapi.h>
    
    int main()
    {
        std::string username = "btc";   //bitcoin.conf 中设置rpc用户
        std::string password = "btc2018"; //bitcoin.conf 中设置的rpc密码
        //std::string address = "192.168.10.199";
        std::string address = "192.168.10.56";
        int port = 18332;
    
        try
        {
            /* Constructor to connect to the bitcoin daemon */
            BitcoinAPI btc(username, password, address, port);
    
    		btc.walletpassphrase("123456"/*这里是加密钱包时设置的密码*/, 5);  
    
    		//转账
    		const std::string strDstAddr = "mv4rnyY3Su5gjcDNzbMLKBQkBicCtHUtFB";
    		const std::string strComment = "yqq";
    		const std::string strComment_to = "hello";
    		std::string strTxid = btc.sendtoaddress(strDstAddr, 0.001, strComment, strComment_to);
    
    		std::cout << "txid: " << strTxid << std::endl;
    
        }
        catch(BitcoinException e)
        {
            std::cerr << e.getMessage() << std::endl;
        }
    }
    
  • makefile

    test:
    	g++  test_testnet_transaction.cpp -lbitcoinapi
    

转账效果

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

附录

  • Window下启动 testnet: 在命令行中 使用 bitcoin.exe -testnet 即可启动测试网络
  • linux也可以使用 bitcoin-qt -testnet 启动测试网络

参考

  • https://www.cnblogs.com/elvi/p/10203927.html
  • https://blog.csdn.net/yzpbright/article/details/81017807
  • 9
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值