普拉多VX

人生一路,不问来时,不知归期

0%

ubuntu下编译安装Bitcoin核心

配置

ubuntu | 16.04
disk | 500GB
net | 2M
cpu | 4
mem | 8G

初始化服务器

1
2
3
sudo apt-get upgrade
sudo apt-get install lrzsz git wget
mkdir /work

安装环境依赖

执行以下命令安装环境依赖包

1
2
3
4
5
6
7
8

sudo apt-get install build-essential libtool autotools-dev automake pkg-config bsdmainutils python3 libssl-dev

sudo apt-get install libssl-dev libboost-all-dev libevent-dev

sudo apt-get install libqt5gui5 libqt5core5a libqt5dbus5 qttools5-dev qttools5-dev-tools libprotobuf-dev protobuf-compiler

sudo apt-get install libqrencode-dev libminiupnpc-dev

下载bitcoin核心

1
2
3
cd /work
wget https://github.com/bitcoin/bitcoin/archive/v0.19.1.tar.gz
tar zxf v0.19.1.tar.gz

安装berkeley-db

因我项目中核心需要钱包功能,所以需要安装berkeley-db,如果不需要可以编译bitcoin时使用./configure --disable-wallet

方法一

1
2
cd bitcoin-0.19.1
./contrib/install_db4.sh `pwd`

如果执行失败,出现国内服务器无法访问https://gist.githubusercontent.com的情况下可手动下载clang.patch进行打补丁

1
2
3
4
5
https://gist.githubusercontent.com/LnL7/5153b251fd525fe15de69b67e63a6075/raw/7778e9364679093a32dec2908656738e16b6bdcb/clang.patch

#编辑./contrib/install_db4.sh第70行屏蔽掉下载

#http_get "${CLANG_CXX11_PATCH_URL}" clang.patch "${CLANG_CXX11_PATCH_HASH}"

可手动复制以下文件为clang.patch到/work/bitcoin-0.19.1/db4/db-4.8.30.NC/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
diff --git a/src/dbinc/atomic.h b/src/dbinc/atomic.h
index 6a858f7..9f338dc 100644
--- a/src/dbinc/atomic.h
+++ b/src/dbinc/atomic.h
@@ -70,7 +70,7 @@ typedef struct {
* These have no memory barriers; the caller must include them when necessary.
*/
#define atomic_read(p) ((p)->value)
-#define atomic_init(p, val) ((p)->value = (val))
+#define atomic_init_db(p, val) ((p)->value = (val))

#ifdef HAVE_ATOMIC_SUPPORT

@@ -144,7 +144,7 @@ typedef LONG volatile *interlocked_val;
#define atomic_inc(env, p) __atomic_inc(p)
#define atomic_dec(env, p) __atomic_dec(p)
#define atomic_compare_exchange(env, p, o, n) \
- __atomic_compare_exchange((p), (o), (n))
+ __atomic_compare_exchange_db((p), (o), (n))
static inline int __atomic_inc(db_atomic_t *p)
{
int temp;
@@ -176,7 +176,7 @@ static inline int __atomic_dec(db_atomic_t *p)
* http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Atomic-Builtins.html
* which configure could be changed to use.
*/
-static inline int __atomic_compare_exchange(
+static inline int __atomic_compare_exchange_db(
db_atomic_t *p, atomic_value_t oldval, atomic_value_t newval)
{
atomic_value_t was;
@@ -206,7 +206,7 @@ static inline int __atomic_compare_exchange(
#define atomic_dec(env, p) (--(p)->value)
#define atomic_compare_exchange(env, p, oldval, newval) \
(DB_ASSERT(env, atomic_read(p) == (oldval)), \
- atomic_init(p, (newval)), 1)
+ atomic_init_db(p, (newval)), 1)
#else
#define atomic_inc(env, p) __atomic_inc(env, p)
#define atomic_dec(env, p) __atomic_dec(env, p)
diff --git a/src/mp/mp_fget.c b/src/mp/mp_fget.c
index 16de695..d0dcc29 100644
--- a/src/mp/mp_fget.c
+++ b/src/mp/mp_fget.c
@@ -649,7 +649,7 @@ alloc: /* Allocate a new buffer header and data space. */

/* Initialize enough so we can call __memp_bhfree. */
alloc_bhp->flags = 0;
- atomic_init(&alloc_bhp->ref, 1);
+ atomic_init_db(&alloc_bhp->ref, 1);
#ifdef DIAGNOSTIC
if ((uintptr_t)alloc_bhp->buf & (sizeof(size_t) - 1)) {
__db_errx(env, DB_STR("3025",
@@ -955,7 +955,7 @@ alloc: /* Allocate a new buffer header and data space. */
MVCC_MPROTECT(bhp->buf, mfp->pagesize,
PROT_READ);

- atomic_init(&alloc_bhp->ref, 1);
+ atomic_init_db(&alloc_bhp->ref, 1);
MUTEX_LOCK(env, alloc_bhp->mtx_buf);
alloc_bhp->priority = bhp->priority;
alloc_bhp->pgno = bhp->pgno;
diff --git a/src/mp/mp_mvcc.c b/src/mp/mp_mvcc.c
index 770bad8..e28cce0 100644
--- a/src/mp/mp_mvcc.c
+++ b/src/mp/mp_mvcc.c
@@ -276,7 +276,7 @@ __memp_bh_freeze(dbmp, infop, hp, bhp, need_frozenp)
#else
memcpy(frozen_bhp, bhp, SSZA(BH, buf));
#endif
- atomic_init(&frozen_bhp->ref, 0);
+ atomic_init_db(&frozen_bhp->ref, 0);
if (mutex != MUTEX_INVALID)
frozen_bhp->mtx_buf = mutex;
else if ((ret = __mutex_alloc(env, MTX_MPOOL_BH,
@@ -428,7 +428,7 @@ __memp_bh_thaw(dbmp, infop, hp, frozen_bhp, alloc_bhp)
#endif
alloc_bhp->mtx_buf = mutex;
MUTEX_LOCK(env, alloc_bhp->mtx_buf);
- atomic_init(&alloc_bhp->ref, 1);
+ atomic_init_db(&alloc_bhp->ref, 1);
F_CLR(alloc_bhp, BH_FROZEN);
}

diff --git a/src/mp/mp_region.c b/src/mp/mp_region.c
index 4952030..47645f8 100644
--- a/src/mp/mp_region.c
+++ b/src/mp/mp_region.c
@@ -245,7 +245,7 @@ __memp_init(env, dbmp, reginfo_off, htab_buckets, max_nreg)
MTX_MPOOL_FILE_BUCKET, 0, &htab[i].mtx_hash)) != 0)
return (ret);
SH_TAILQ_INIT(&htab[i].hash_bucket);
- atomic_init(&htab[i].hash_page_dirty, 0);
+ atomic_init_db(&htab[i].hash_page_dirty, 0);
}

/*
@@ -302,7 +302,7 @@ no_prealloc:
} else
hp->mtx_hash = mtx_base + (i % dbenv->mp_mtxcount);
SH_TAILQ_INIT(&hp->hash_bucket);
- atomic_init(&hp->hash_page_dirty, 0);
+ atomic_init_db(&hp->hash_page_dirty, 0);
#ifdef HAVE_STATISTICS
hp->hash_io_wait = 0;
hp->hash_frozen = hp->hash_thawed = hp->hash_frozen_freed = 0;
diff --git a/src/mutex/mut_method.c b/src/mutex/mut_method.c
index 09353b0..177353c 100644
--- a/src/mutex/mut_method.c
+++ b/src/mutex/mut_method.c
@@ -474,7 +474,7 @@ atomic_compare_exchange(env, v, oldval, newval)
MUTEX_LOCK(env, mtx);
ret = atomic_read(v) == oldval;
if (ret)
- atomic_init(v, newval);
+ atomic_init_db(v, newval);
MUTEX_UNLOCK(env, mtx);

return (ret);
diff --git a/src/mutex/mut_tas.c b/src/mutex/mut_tas.c
index 106b161..fc4de9d 100644
--- a/src/mutex/mut_tas.c
+++ b/src/mutex/mut_tas.c
@@ -47,7 +47,7 @@ __db_tas_mutex_init(env, mutex, flags)

#ifdef HAVE_SHARED_LATCHES
if (F_ISSET(mutexp, DB_MUTEX_SHARED))
- atomic_init(&mutexp->sharecount, 0);
+ atomic_init_db(&mutexp->sharecount, 0);
else
#endif
if (MUTEX_INIT(&mutexp->tas)) {
@@ -536,7 +536,7 @@ __db_tas_mutex_unlock(env, mutex)
F_CLR(mutexp, DB_MUTEX_LOCKED);
/* Flush flag update before zeroing count */
MEMBAR_EXIT();
- atomic_init(&mutexp->sharecount, 0);
+ atomic_init_db(&mutexp->sharecount, 0);
} else {
DB_ASSERT(env, sharecount > 0);
MEMBAR_EXIT();

方法二

1
2
3
4
5
6
7
8
9
10
wget http://download.oracle.com/berkeley-db/db-4.8.30.NC.tar.gz
tar -xzvf db-4.8.30.NC.tar.gz

cd db-4.8.30.NC/build_unix/

../dist/configure --enable-cxx --disable-shared --with-pic --prefix=/work/bitcoin-0.19.1/db4

make

sudo make install

编译安装

1
2
3
4
5
./configure LDFLAGS="-L/work/bitcoin-0.19.1/db4/lib/" CPPFLAGS="-I/work/bitcoin-0.19.1/db4/include/"

sudo make

sudo make install

默认安装到/usr/local/bin/目录下

1
2
3
4
5
-rwxr-xr-x  1 root root 140376160 May 29 14:08 bench_bitcoin*
-rwxr-xr-x 1 root root 8032176 May 29 14:08 bitcoin-cli*
-rwxr-xr-x 1 root root 126259248 May 29 14:08 bitcoind*
-rwxr-xr-x 1 root root 19538256 May 29 14:08 bitcoin-tx*
-rwxr-xr-x 1 root root 58618744 May 29 14:08 bitcoin-wallet*

创建配置文件

1
2
mkdir /root/.bitcoin
vim /root/.bitcoin/bitcoin.conf

启动测试网络

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
daemon=1  #是否后台运行
server=1 #允许接收JSON-RPC commands
rpcuser=bitcoinrpc #rpc用户名
# generate random rpc password:
# $ strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 30 | tr -d '\n'; echo
rpcpassword=zvGzMyGKczjImkKMpD85WoGyUukhwf #rpc用户密码
rpcthreads=4

datadir=/work/blockdata #指定区块数据存放目录

testnet=1 #为1代表使用测试网络 为0表示正式网络


rpcallowip=172.16.0.0/12
rpcallowip=192.168.0.0/16
rpcallowip=10.0.0.0/8

# use 1G memory for utxo, depends on your machine's memory
dbcache=1000

# set maximum BIP141 block weight
blockmaxweight=4000000

启动

1
2
bitcoind    
#会提示Bitcoin Core starting

启动后会在指定目录生成如下区块数据

1
2
3
4
5
6
7
8
9
10
11
12
total 524
drwxr-xr-x 5 root root 4096 May 29 15:14 ./
drwxr-xr-x 3 root root 4096 May 29 15:13 ../
-rw------- 1 root root 37 May 29 15:13 banlist.dat
drwx------ 3 root root 4096 May 29 15:14 blocks/
drwx------ 2 root root 4096 May 29 15:13 chainstate/
-rw------- 1 root root 10073 May 29 15:14 debug.log
-rw------- 1 root root 247985 May 29 15:14 fee_estimates.dat
-rw------- 1 root root 0 May 29 15:13 .lock
-rw------- 1 root root 17 May 29 15:14 mempool.dat
-rw------- 1 root root 244776 May 29 15:14 peers.dat
drwxr-xr-x 2 root root 4096 May 29 15:14 wallets/

通过 tailf debug.log 查看区块数据同步进度

停止

1
bitcoin-cli  stop

启动正式网络

bitcoin.conf配置文件如下

1
将testnet=1改为testnet=0即可

正式网络同步区块数据较长,取决于网络和节点。截至当前区块数据达300GB。

参考资料: