btcpool礦池原始碼分析(3)-BlockMaker模組解析
# 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
--
-- 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;
```
## BlockMaker模組流程圖
### 參考文件
暫無
網址:http://www.qukuailianxueyuan.io/
欲領取造幣技術與全套虛擬機器資料
區塊鏈技術交流QQ群:756146052 備註:CSDN
尹成學院微信:備註:CSDN
網址:http://www.qukuailianxueyuan.io/
欲領取造幣技術與全套虛擬機器資料
區塊鏈技術交流QQ群:756146052 備註:CSDN
尹成學院微信:備註:CSDN
相關文章
- btcpool礦池原始碼分析(5)-JobMaker模組解析TCP原始碼
- btcpool礦池原始碼分析(6)-nmcauxmaker模組解析TCP原始碼UX
- btcpool礦池原始碼分析(9)-statshttpd模組解析TCP原始碼httpd
- btcpool礦池原始碼分析(10)-StratumServer模組解析TCP原始碼Server
- btcpool礦池原始碼分析(4)-GbtMaker模組解析TCP原始碼
- btcpool礦池原始碼分析(6)-PoolWatcher模組解析TCP原始碼
- btcpool礦池原始碼分析(7)-sharelogger模組解析TCP原始碼
- btcpool礦池原始碼分析(8)-slparserTCP原始碼
- btcpool礦池原始碼分析(1)環境搭建TCP原始碼
- btcpool礦池原始碼分析(2)-核心機制總結及優化思考TCP原始碼優化
- open-ethereum-pool以太坊礦池原始碼分析(3)payouts模組原始碼
- open-ethereum-pool以太坊礦池原始碼分析(4)-policy模組原始碼
- open-ethereum-pool以太坊礦池原始碼分析(5)proxy模組原始碼
- open-ethereum-pool以太坊礦池原始碼分析(6)-redis模組原始碼Redis
- open-ethereum-pool以太坊礦池原始碼分析(7)unlocker模組原始碼
- go-ethereum原始碼解析-miner挖礦部分原始碼分析CPU挖礦Go原始碼
- Django(49)drf解析模組原始碼分析Django原始碼
- open-ethereum-pool以太坊礦池原始碼分析(2)API分析原始碼API
- (一) Mybatis原始碼分析-解析器模組MyBatis原始碼
- 比特幣原始碼分析:VersionBits模組解析比特幣原始碼
- open-ethereum-pool以太坊礦池原始碼分析(1)-main入口分析原始碼AI
- 以太坊原始碼分析(42)miner挖礦部分原始碼分析CPU挖礦原始碼
- Swoole 原始碼分析——記憶體模組之記憶體池原始碼記憶體
- OkHttp3原始碼解析(三)——連線池複用HTTP原始碼
- OkHttp3原始碼分析[複用連線池]HTTP原始碼
- 以太坊交易池原始碼解析原始碼
- open-ethereum-pool以太坊礦池原始碼分析(1)環境安裝原始碼
- 以太坊原始碼分析(16)挖礦分析原始碼
- MOSN 原始碼解析 - 連線池原始碼
- webpack核心模組tapable原始碼解析Web原始碼
- Spark原始碼解析之Storage模組Spark原始碼
- 比特幣挖礦與原始碼解析比特幣原始碼
- 執行緒池原始碼分析執行緒原始碼
- 以太坊交易池原始碼分析原始碼
- 集合原始碼分析[3]-ArrayList 原始碼分析原始碼
- 以太坊原始碼分析(26)core-txpool交易池原始碼分析原始碼
- SOFARegistry 原始碼|資料同步模組解析原始碼
- dubbo原始碼-執行緒池分析原始碼執行緒