@@ -6,16 +6,30 @@ import backendInfo from '../api/backend-info';
6
6
import logger from '../logger' ;
7
7
import { SocksProxyAgent } from 'socks-proxy-agent' ;
8
8
import * as https from 'https' ;
9
+ import { Common } from '../api/common' ;
9
10
10
11
/**
11
12
* Maintain the most recent version of pools-v2.json
12
13
*/
13
14
class PoolsUpdater {
15
+ tag = 'PoolsUpdater' ;
16
+
14
17
lastRun : number = 0 ;
15
18
currentSha : string | null = null ;
16
19
poolsUrl : string = config . MEMPOOL . POOLS_JSON_URL ;
17
20
treeUrl : string = config . MEMPOOL . POOLS_JSON_TREE_URL ;
18
21
22
+ public async $startService ( ) : Promise < void > {
23
+ while ( 'Bitcoin is still alive' ) {
24
+ try {
25
+ await this . updatePoolsJson ( ) ;
26
+ } catch ( e : any ) {
27
+ logger . info ( `Exception ${ e } in PoolsUpdater::$startService. Code: ${ e . code } . Message: ${ e . message } ` , this . tag ) ;
28
+ }
29
+ await Common . sleep$ ( 10000 ) ;
30
+ }
31
+ }
32
+
19
33
public async updatePoolsJson ( ) : Promise < void > {
20
34
if ( [ 'mainnet' , 'testnet' , 'signet' ] . includes ( config . MEMPOOL . NETWORK ) === false ||
21
35
config . MEMPOOL . ENABLED === false
@@ -40,7 +54,7 @@ class PoolsUpdater {
40
54
this . currentSha = await this . getShaFromDb ( ) ;
41
55
}
42
56
43
- logger . debug ( `pools-v2.json sha | Current: ${ this . currentSha } | Github: ${ githubSha } ` ) ;
57
+ logger . debug ( `pools-v2.json sha | Current: ${ this . currentSha } | Github: ${ githubSha } ` , this . tag ) ;
44
58
if ( this . currentSha !== null && this . currentSha === githubSha ) {
45
59
return ;
46
60
}
@@ -50,16 +64,16 @@ class PoolsUpdater {
50
64
config . MEMPOOL . AUTOMATIC_POOLS_UPDATE !== true && // Automatic pools update is disabled
51
65
! process . env . npm_config_update_pools // We're not manually updating mining pool
52
66
) {
53
- logger . warn ( `Updated mining pools data is available (${ githubSha } ) but AUTOMATIC_POOLS_UPDATE is disabled` ) ;
54
- logger . info ( `You can update your mining pools using the --update-pools command flag. You may want to clear your nginx cache as well if applicable` ) ;
67
+ logger . warn ( `Updated mining pools data is available (${ githubSha } ) but AUTOMATIC_POOLS_UPDATE is disabled` , this . tag ) ;
68
+ logger . info ( `You can update your mining pools using the --update-pools command flag. You may want to clear your nginx cache as well if applicable` , this . tag ) ;
55
69
return ;
56
70
}
57
71
58
72
const network = config . SOCKS5PROXY . ENABLED ? 'tor' : 'clearnet' ;
59
73
if ( this . currentSha === null ) {
60
- logger . info ( `Downloading pools-v2.json for the first time from ${ this . poolsUrl } over ${ network } ` , logger . tags . mining ) ;
74
+ logger . info ( `Downloading pools-v2.json for the first time from ${ this . poolsUrl } over ${ network } ` , this . tag ) ;
61
75
} else {
62
- logger . warn ( `pools-v2.json is outdated, fetching latest from ${ this . poolsUrl } over ${ network } ` , logger . tags . mining ) ;
76
+ logger . warn ( `pools-v2.json is outdated, fetching latest from ${ this . poolsUrl } over ${ network } ` , this . tag ) ;
63
77
}
64
78
const poolsJson = await this . query ( this . poolsUrl ) ;
65
79
if ( poolsJson === undefined ) {
@@ -68,7 +82,7 @@ class PoolsUpdater {
68
82
poolsParser . setMiningPools ( poolsJson ) ;
69
83
70
84
if ( config . DATABASE . ENABLED === false ) { // Don't run db operations
71
- logger . info ( `Mining pools-v2.json (${ githubSha } ) import completed (no database)` ) ;
85
+ logger . info ( `Mining pools-v2.json (${ githubSha } ) import completed (no database)` , this . tag ) ;
72
86
return ;
73
87
}
74
88
@@ -78,14 +92,14 @@ class PoolsUpdater {
78
92
await this . updateDBSha ( githubSha ) ;
79
93
await DB . query ( 'COMMIT;' ) ;
80
94
} catch ( e ) {
81
- logger . err ( `Could not migrate mining pools, rolling back. Exception: ${ JSON . stringify ( e ) } ` , logger . tags . mining ) ;
95
+ logger . err ( `Could not migrate mining pools, rolling back. Exception: ${ JSON . stringify ( e ) } ` , this . tag ) ;
82
96
await DB . query ( 'ROLLBACK;' ) ;
83
97
}
84
- logger . info ( `Mining pools-v2.json (${ githubSha } ) import completed` ) ;
98
+ logger . info ( `Mining pools-v2.json (${ githubSha } ) import completed` , this . tag ) ;
85
99
86
100
} catch ( e ) {
87
101
this . lastRun = now - 600 ; // Try again in 10 minutes
88
- logger . err ( `PoolsUpdater failed. Will try again in 10 minutes. Exception: ${ JSON . stringify ( e ) } ` , logger . tags . mining ) ;
102
+ logger . err ( `PoolsUpdater failed. Will try again in 10 minutes. Exception: ${ JSON . stringify ( e ) } ` , this . tag ) ;
89
103
}
90
104
}
91
105
@@ -99,7 +113,7 @@ class PoolsUpdater {
99
113
await DB . query ( 'DELETE FROM state where name="pools_json_sha"' ) ;
100
114
await DB . query ( `INSERT INTO state VALUES('pools_json_sha', NULL, '${ githubSha } ')` ) ;
101
115
} catch ( e ) {
102
- logger . err ( 'Cannot save github pools-v2.json sha into the db. Reason: ' + ( e instanceof Error ? e . message : e ) , logger . tags . mining ) ;
116
+ logger . err ( 'Cannot save github pools-v2.json sha into the db. Reason: ' + ( e instanceof Error ? e . message : e ) , this . tag ) ;
103
117
}
104
118
}
105
119
}
@@ -112,7 +126,7 @@ class PoolsUpdater {
112
126
const [ rows ] : any [ ] = await DB . query ( 'SELECT string FROM state WHERE name="pools_json_sha"' ) ;
113
127
return ( rows . length > 0 ? rows [ 0 ] . string : null ) ;
114
128
} catch ( e ) {
115
- logger . err ( 'Cannot fetch pools-v2.json sha from db. Reason: ' + ( e instanceof Error ? e . message : e ) , logger . tags . mining ) ;
129
+ logger . err ( 'Cannot fetch pools-v2.json sha from db. Reason: ' + ( e instanceof Error ? e . message : e ) , this . tag ) ;
116
130
return null ;
117
131
}
118
132
}
@@ -131,7 +145,7 @@ class PoolsUpdater {
131
145
}
132
146
}
133
147
134
- logger . err ( `Cannot find "pools-v2.json" in git tree (${ this . treeUrl } )` , logger . tags . mining ) ;
148
+ logger . err ( `Cannot find "pools-v2.json" in git tree (${ this . treeUrl } )` , this . tag ) ;
135
149
return null ;
136
150
}
137
151
@@ -183,7 +197,7 @@ class PoolsUpdater {
183
197
}
184
198
return data . data ;
185
199
} catch ( e ) {
186
- logger . err ( 'Could not connect to Github. Reason: ' + ( e instanceof Error ? e . message : e ) ) ;
200
+ logger . err ( 'Could not connect to Github. Reason: ' + ( e instanceof Error ? e . message : e ) , this . tag ) ;
187
201
retry ++ ;
188
202
}
189
203
await setDelay ( config . MEMPOOL . EXTERNAL_RETRY_INTERVAL ) ;
0 commit comments