兄弟连区块链教程btcpool矿池源码分析BlockMaker模块解析,2018年下半年,区块链行业正逐渐褪去发展之初的浮躁、回归理性,表面上看相关人才需求与身价似乎正在回落。但事实上,正是初期泡沫的渐退,让人们更多的关注点放在了区块链真正的技术之上。
# btcpool矿池-BlockMaker模块解析
## 核心机制总结
* blkmaker可以连多个bitcoind节点
* blkmaker监听和接收4类消息:RAWGBT、STRATUM_JOB、SOLVED_SHARE和NMC_SOLVED_SHARE
* 监听RAWGBT目的为获取gbtHash/交易列表,用于构建Block,gbtHash和vtxs写入rawGbtMap_
* rawGbtMap_保存最近100条gbtHash/vtxs对
* 监听STRATUM_JOB目的为获取jobId_/gbtHash,jobId_和gbtHash写入jobId2GbtHash_
* jobId2GbtHash_保存最近120条jobId_/gbtHash对
* 监听SOLVED_SHARE目的为获取BlockHeader和coinbaseTx
* BlockHeader+coinbaseTx+vtxs构造Block
* 构造好的Block会提交连接的所有bitcoind节点
* 构造好的Block入库,入库字段包括:
* puid、worker_id、worker_full_name、job_id、height、hash
* rewards(即coinbaseValue)、size(即blksize)、prev_hash、bits、version、created_at
* created_at为入库时间非爆块时间
## BlockMaker命令使用
BlockMaker用于监听kafka获取新的比特币区块和域名币区块,同时监听kafka获取Gbt消息和Job消息以构造交易列表,最后拼装完整区块提交给比特币节点。
```shell
blkmaker -c blkmaker.cfg -l log_dir
#-c指定blkmaker配置文件
#-l指定日志目录
```
## blkmaker.cfg配置文件
```shell
//比特币节点
bitcoinds = (
{
rpc_addr = ""; //rpc地址
rpc_userpwd = ""; //rpc权限,格式如username:password
}
);
//kafka集群
kafka = {
brokers = "1.1.1.1:9092,2.2.2.2:9092,3.3.3.3:9092";
};
//mysql配置,用于存储已发现的块
pooldb = {
host = "";
port = 3306;
username = "dbusername";
password = "dbpassword";
dbname = "";
};
```
## found_blocks数据库结构
-- MySQL dump 10.13 Distrib 5.7.20, for Linux (x86_64)
--
-- Host: 172.17.228.162 Database: bpool_local_db
-- ------------------------------------------------------
-- Server version 5.7.18-16-log
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `found_blocks`
--
DROP TABLE IF EXISTS `found_blocks`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `found_blocks` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`puid` int(11) NOT NULL,
`worker_id` bigint(20) NOT NULL,
`worker_full_name` varchar(50) NOT NULL,
`job_id` bigint(20) unsigned NOT NULL,
`height` int(11) NOT NULL,
`is_orphaned` tinyint(4) NOT NULL DEFAULT '0',
`hash` char(64) NOT NULL,
`rewards` bigint(20) NOT NULL,
`size` int(11) NOT NULL,
`prev_hash` char(64) NOT NULL,
`bits` int(10) unsigned NOT NULL,
`version` int(11) NOT NULL,
`created_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `hash` (`hash`),
KEY `height` (`height`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `found_nmc_blocks`
--
DROP TABLE IF EXISTS `found_nmc_blocks`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `found_nmc_blocks` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`bitcoin_block_hash` char(64) NOT NULL,
`aux_block_hash` char(64) NOT NULL,
`aux_pow` text NOT NULL,
`is_orphaned` tinyint(4) NOT NULL DEFAULT '0',
`created_at` datetime NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `aux_block_hash` (`aux_block_hash`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `mining_workers`
--
DROP TABLE IF EXISTS `mining_workers`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `mining_workers` (
`worker_id` bigint(20) NOT NULL,
`puid` int(11) NOT NULL,
`group_id` int(11) NOT NULL,
`worker_name` varchar(20) DEFAULT NULL,
`accept_1m` bigint(20) NOT NULL DEFAULT '0',
`accept_5m` bigint(20) NOT NULL DEFAULT '0',
`accept_15m` bigint(20) NOT NULL DEFAULT '0',
`reject_15m` bigint(20) NOT NULL DEFAULT '0',
`accept_1h` bigint(20) NOT NULL DEFAULT '0',
`reject_1h` bigint(20) NOT NULL DEFAULT '0',
`accept_count` int(11) NOT NULL DEFAULT '0',
`last_share_ip` char(16) NOT NULL DEFAULT '0.0.0.0',
`last_share_time` timestamp NOT NULL DEFAULT '1970-01-01 00:00:01',
`miner_agent` varchar(30) DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
UNIQUE KEY `puid_worker_id` (`puid`,`worker_id`),
KEY `puid_group_id` (`puid`,`group_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `mining_workers_tmp`
--
DROP TABLE IF EXISTS `mining_workers_tmp`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `mining_workers_tmp` (
`worker_id` bigint(20) NOT NULL,
`puid` int(11) NOT NULL,
`group_id` int(11) NOT NULL,
`worker_name` varchar(20) DEFAULT NULL,
`accept_1m` bigint(20) NOT NULL DEFAULT '0',
`accept_5m` bigint(20) NOT NULL DEFAULT '0',
`accept_15m` bigint(20) NOT NULL DEFAULT '0',
`reject_15m` bigint(20) NOT NULL DEFAULT '0',
`accept_1h` bigint(20) NOT NULL DEFAULT '0',
`reject_1h` bigint(20) NOT NULL DEFAULT '0',
`accept_count` int(11) NOT NULL DEFAULT '0',
`last_share_ip` char(16) NOT NULL DEFAULT '0.0.0.0',
`last_share_time` timestamp NOT NULL DEFAULT '1970-01-01 00:00:01',
`miner_agent` varchar(30) DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
UNIQUE KEY `puid_worker_id` (`puid`,`worker_id`),
KEY `puid_group_id` (`puid`,`group_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2018-02-22 14:02:44
```
CREATE TABLE `found_blocks` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`puid` int(11) NOT NULL,
`worker_id` bigint(20) NOT NULL,
`worker_full_name` varchar(50) NOT NULL,
`job_id` bigint(20) unsigned NOT NULL,
`height` int(11) NOT NULL,
`is_orphaned` tinyint(4) NOT NULL DEFAULT '0',
`hash` char(64) NOT NULL,
`rewards` bigint(20) NOT NULL,
`size` int(11) NOT NULL,
`prev_hash` char(64) NOT NULL,
`bits` int(10) unsigned NOT NULL,
`version` int(11) NOT NULL,
`created_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `hash` (`hash`),
KEY `height` (`height`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
```
区块链行业高薪缺人,
时代的红利你要不要分一杯羹!
今晚19点30分,清华尹成团队带你10天夯实区块链开发基础,深入解读golang的基本数据类型与流程控制
听课地址:https://ke.qq.com/course/334561
有疑问加站长微信联系(非本文作者)