├── .gitignore
├── LICENSE
├── README.md
├── config_example.json
├── init.js
├── lib
├── api.js
├── apiInterfaces.js
├── blockUnlocker.js
├── charts.js
├── chartsDataCollector.js
├── cli.js
├── configReader.js
├── exceptionWriter.js
├── logger.js
├── paymentProcessor.js
├── pool.js
└── utils.js
├── package.json
├── redisBlocksUpgrade.js
└── website
├── admin.html
├── config.js
├── custom.css
├── custom.js
├── index.html
├── pages
├── admin
│ ├── monitoring.html
│ ├── statistics.html
│ └── userslist.html
├── blockchain_block.html
├── blockchain_blocks.html
├── blockchain_transaction.html
├── getting_started.html
├── home.html
├── network.html
├── payments.html
├── pool_blocks.html
└── support.html
└── themes
├── deep-gray-dark-theme.css
├── default-theme.css
├── ease-way-light-theme.css
├── img
├── bg.jpg
└── bg2.jpg
├── motherboard-dark-theme.css
└── nightly-mining-dark-theme.css
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules/
2 | .idea/
3 | config.json
4 | logs/
5 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 2, June 1991
3 |
4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6 | Everyone is permitted to copy and distribute verbatim copies
7 | of this license document, but changing it is not allowed.
8 |
9 | Preamble
10 |
11 | The licenses for most software are designed to take away your
12 | freedom to share and change it. By contrast, the GNU General Public
13 | License is intended to guarantee your freedom to share and change free
14 | software--to make sure the software is free for all its users. This
15 | General Public License applies to most of the Free Software
16 | Foundation's software and to any other program whose authors commit to
17 | using it. (Some other Free Software Foundation software is covered by
18 | the GNU Lesser General Public License instead.) You can apply it to
19 | your programs, too.
20 |
21 | When we speak of free software, we are referring to freedom, not
22 | price. Our General Public Licenses are designed to make sure that you
23 | have the freedom to distribute copies of free software (and charge for
24 | this service if you wish), that you receive source code or can get it
25 | if you want it, that you can change the software or use pieces of it
26 | in new free programs; and that you know you can do these things.
27 |
28 | To protect your rights, we need to make restrictions that forbid
29 | anyone to deny you these rights or to ask you to surrender the rights.
30 | These restrictions translate to certain responsibilities for you if you
31 | distribute copies of the software, or if you modify it.
32 |
33 | For example, if you distribute copies of such a program, whether
34 | gratis or for a fee, you must give the recipients all the rights that
35 | you have. You must make sure that they, too, receive or can get the
36 | source code. And you must show them these terms so they know their
37 | rights.
38 |
39 | We protect your rights with two steps: (1) copyright the software, and
40 | (2) offer you this license which gives you legal permission to copy,
41 | distribute and/or modify the software.
42 |
43 | Also, for each author's protection and ours, we want to make certain
44 | that everyone understands that there is no warranty for this free
45 | software. If the software is modified by someone else and passed on, we
46 | want its recipients to know that what they have is not the original, so
47 | that any problems introduced by others will not reflect on the original
48 | authors' reputations.
49 |
50 | Finally, any free program is threatened constantly by software
51 | patents. We wish to avoid the danger that redistributors of a free
52 | program will individually obtain patent licenses, in effect making the
53 | program proprietary. To prevent this, we have made it clear that any
54 | patent must be licensed for everyone's free use or not licensed at all.
55 |
56 | The precise terms and conditions for copying, distribution and
57 | modification follow.
58 |
59 | GNU GENERAL PUBLIC LICENSE
60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61 |
62 | 0. This License applies to any program or other work which contains
63 | a notice placed by the copyright holder saying it may be distributed
64 | under the terms of this General Public License. The "Program", below,
65 | refers to any such program or work, and a "work based on the Program"
66 | means either the Program or any derivative work under copyright law:
67 | that is to say, a work containing the Program or a portion of it,
68 | either verbatim or with modifications and/or translated into another
69 | language. (Hereinafter, translation is included without limitation in
70 | the term "modification".) Each licensee is addressed as "you".
71 |
72 | Activities other than copying, distribution and modification are not
73 | covered by this License; they are outside its scope. The act of
74 | running the Program is not restricted, and the output from the Program
75 | is covered only if its contents constitute a work based on the
76 | Program (independent of having been made by running the Program).
77 | Whether that is true depends on what the Program does.
78 |
79 | 1. You may copy and distribute verbatim copies of the Program's
80 | source code as you receive it, in any medium, provided that you
81 | conspicuously and appropriately publish on each copy an appropriate
82 | copyright notice and disclaimer of warranty; keep intact all the
83 | notices that refer to this License and to the absence of any warranty;
84 | and give any other recipients of the Program a copy of this License
85 | along with the Program.
86 |
87 | You may charge a fee for the physical act of transferring a copy, and
88 | you may at your option offer warranty protection in exchange for a fee.
89 |
90 | 2. You may modify your copy or copies of the Program or any portion
91 | of it, thus forming a work based on the Program, and copy and
92 | distribute such modifications or work under the terms of Section 1
93 | above, provided that you also meet all of these conditions:
94 |
95 | a) You must cause the modified files to carry prominent notices
96 | stating that you changed the files and the date of any change.
97 |
98 | b) You must cause any work that you distribute or publish, that in
99 | whole or in part contains or is derived from the Program or any
100 | part thereof, to be licensed as a whole at no charge to all third
101 | parties under the terms of this License.
102 |
103 | c) If the modified program normally reads commands interactively
104 | when run, you must cause it, when started running for such
105 | interactive use in the most ordinary way, to print or display an
106 | announcement including an appropriate copyright notice and a
107 | notice that there is no warranty (or else, saying that you provide
108 | a warranty) and that users may redistribute the program under
109 | these conditions, and telling the user how to view a copy of this
110 | License. (Exception: if the Program itself is interactive but
111 | does not normally print such an announcement, your work based on
112 | the Program is not required to print an announcement.)
113 |
114 | These requirements apply to the modified work as a whole. If
115 | identifiable sections of that work are not derived from the Program,
116 | and can be reasonably considered independent and separate works in
117 | themselves, then this License, and its terms, do not apply to those
118 | sections when you distribute them as separate works. But when you
119 | distribute the same sections as part of a whole which is a work based
120 | on the Program, the distribution of the whole must be on the terms of
121 | this License, whose permissions for other licensees extend to the
122 | entire whole, and thus to each and every part regardless of who wrote it.
123 |
124 | Thus, it is not the intent of this section to claim rights or contest
125 | your rights to work written entirely by you; rather, the intent is to
126 | exercise the right to control the distribution of derivative or
127 | collective works based on the Program.
128 |
129 | In addition, mere aggregation of another work not based on the Program
130 | with the Program (or with a work based on the Program) on a volume of
131 | a storage or distribution medium does not bring the other work under
132 | the scope of this License.
133 |
134 | 3. You may copy and distribute the Program (or a work based on it,
135 | under Section 2) in object code or executable form under the terms of
136 | Sections 1 and 2 above provided that you also do one of the following:
137 |
138 | a) Accompany it with the complete corresponding machine-readable
139 | source code, which must be distributed under the terms of Sections
140 | 1 and 2 above on a medium customarily used for software interchange; or,
141 |
142 | b) Accompany it with a written offer, valid for at least three
143 | years, to give any third party, for a charge no more than your
144 | cost of physically performing source distribution, a complete
145 | machine-readable copy of the corresponding source code, to be
146 | distributed under the terms of Sections 1 and 2 above on a medium
147 | customarily used for software interchange; or,
148 |
149 | c) Accompany it with the information you received as to the offer
150 | to distribute corresponding source code. (This alternative is
151 | allowed only for noncommercial distribution and only if you
152 | received the program in object code or executable form with such
153 | an offer, in accord with Subsection b above.)
154 |
155 | The source code for a work means the preferred form of the work for
156 | making modifications to it. For an executable work, complete source
157 | code means all the source code for all modules it contains, plus any
158 | associated interface definition files, plus the scripts used to
159 | control compilation and installation of the executable. However, as a
160 | special exception, the source code distributed need not include
161 | anything that is normally distributed (in either source or binary
162 | form) with the major components (compiler, kernel, and so on) of the
163 | operating system on which the executable runs, unless that component
164 | itself accompanies the executable.
165 |
166 | If distribution of executable or object code is made by offering
167 | access to copy from a designated place, then offering equivalent
168 | access to copy the source code from the same place counts as
169 | distribution of the source code, even though third parties are not
170 | compelled to copy the source along with the object code.
171 |
172 | 4. You may not copy, modify, sublicense, or distribute the Program
173 | except as expressly provided under this License. Any attempt
174 | otherwise to copy, modify, sublicense or distribute the Program is
175 | void, and will automatically terminate your rights under this License.
176 | However, parties who have received copies, or rights, from you under
177 | this License will not have their licenses terminated so long as such
178 | parties remain in full compliance.
179 |
180 | 5. You are not required to accept this License, since you have not
181 | signed it. However, nothing else grants you permission to modify or
182 | distribute the Program or its derivative works. These actions are
183 | prohibited by law if you do not accept this License. Therefore, by
184 | modifying or distributing the Program (or any work based on the
185 | Program), you indicate your acceptance of this License to do so, and
186 | all its terms and conditions for copying, distributing or modifying
187 | the Program or works based on it.
188 |
189 | 6. Each time you redistribute the Program (or any work based on the
190 | Program), the recipient automatically receives a license from the
191 | original licensor to copy, distribute or modify the Program subject to
192 | these terms and conditions. You may not impose any further
193 | restrictions on the recipients' exercise of the rights granted herein.
194 | You are not responsible for enforcing compliance by third parties to
195 | this License.
196 |
197 | 7. If, as a consequence of a court judgment or allegation of patent
198 | infringement or for any other reason (not limited to patent issues),
199 | conditions are imposed on you (whether by court order, agreement or
200 | otherwise) that contradict the conditions of this License, they do not
201 | excuse you from the conditions of this License. If you cannot
202 | distribute so as to satisfy simultaneously your obligations under this
203 | License and any other pertinent obligations, then as a consequence you
204 | may not distribute the Program at all. For example, if a patent
205 | license would not permit royalty-free redistribution of the Program by
206 | all those who receive copies directly or indirectly through you, then
207 | the only way you could satisfy both it and this License would be to
208 | refrain entirely from distribution of the Program.
209 |
210 | If any portion of this section is held invalid or unenforceable under
211 | any particular circumstance, the balance of the section is intended to
212 | apply and the section as a whole is intended to apply in other
213 | circumstances.
214 |
215 | It is not the purpose of this section to induce you to infringe any
216 | patents or other property right claims or to contest validity of any
217 | such claims; this section has the sole purpose of protecting the
218 | integrity of the free software distribution system, which is
219 | implemented by public license practices. Many people have made
220 | generous contributions to the wide range of software distributed
221 | through that system in reliance on consistent application of that
222 | system; it is up to the author/donor to decide if he or she is willing
223 | to distribute software through any other system and a licensee cannot
224 | impose that choice.
225 |
226 | This section is intended to make thoroughly clear what is believed to
227 | be a consequence of the rest of this License.
228 |
229 | 8. If the distribution and/or use of the Program is restricted in
230 | certain countries either by patents or by copyrighted interfaces, the
231 | original copyright holder who places the Program under this License
232 | may add an explicit geographical distribution limitation excluding
233 | those countries, so that distribution is permitted only in or among
234 | countries not thus excluded. In such case, this License incorporates
235 | the limitation as if written in the body of this License.
236 |
237 | 9. The Free Software Foundation may publish revised and/or new versions
238 | of the General Public License from time to time. Such new versions will
239 | be similar in spirit to the present version, but may differ in detail to
240 | address new problems or concerns.
241 |
242 | Each version is given a distinguishing version number. If the Program
243 | specifies a version number of this License which applies to it and "any
244 | later version", you have the option of following the terms and conditions
245 | either of that version or of any later version published by the Free
246 | Software Foundation. If the Program does not specify a version number of
247 | this License, you may choose any version ever published by the Free Software
248 | Foundation.
249 |
250 | 10. If you wish to incorporate parts of the Program into other free
251 | programs whose distribution conditions are different, write to the author
252 | to ask for permission. For software which is copyrighted by the Free
253 | Software Foundation, write to the Free Software Foundation; we sometimes
254 | make exceptions for this. Our decision will be guided by the two goals
255 | of preserving the free status of all derivatives of our free software and
256 | of promoting the sharing and reuse of software generally.
257 |
258 | NO WARRANTY
259 |
260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
268 | REPAIR OR CORRECTION.
269 |
270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278 | POSSIBILITY OF SUCH DAMAGES.
279 |
280 | END OF TERMS AND CONDITIONS
281 |
282 | How to Apply These Terms to Your New Programs
283 |
284 | If you develop a new program, and you want it to be of the greatest
285 | possible use to the public, the best way to achieve this is to make it
286 | free software which everyone can redistribute and change under these terms.
287 |
288 | To do so, attach the following notices to the program. It is safest
289 | to attach them to the start of each source file to most effectively
290 | convey the exclusion of warranty; and each file should have at least
291 | the "copyright" line and a pointer to where the full notice is found.
292 |
293 | {description}
294 | Copyright (C) {year} {fullname}
295 |
296 | This program is free software; you can redistribute it and/or modify
297 | it under the terms of the GNU General Public License as published by
298 | the Free Software Foundation; either version 2 of the License, or
299 | (at your option) any later version.
300 |
301 | This program is distributed in the hope that it will be useful,
302 | but WITHOUT ANY WARRANTY; without even the implied warranty of
303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
304 | GNU General Public License for more details.
305 |
306 | You should have received a copy of the GNU General Public License along
307 | with this program; if not, write to the Free Software Foundation, Inc.,
308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
309 |
310 | Also add information on how to contact you by electronic and paper mail.
311 |
312 | If the program is interactive, make it output a short notice like this
313 | when it starts in an interactive mode:
314 |
315 | Gnomovision version 69, Copyright (C) year name of author
316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
317 | This is free software, and you are welcome to redistribute it
318 | under certain conditions; type `show c' for details.
319 |
320 | The hypothetical commands `show w' and `show c' should show the appropriate
321 | parts of the General Public License. Of course, the commands you use may
322 | be called something other than `show w' and `show c'; they could even be
323 | mouse-clicks or menu items--whatever suits your program.
324 |
325 | You should also get your employer (if you work as a programmer) or your
326 | school, if any, to sign a "copyright disclaimer" for the program, if
327 | necessary. Here is a sample; alter the names:
328 |
329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program
330 | `Gnomovision' (which makes passes at compilers) written by James Hacker.
331 |
332 | {signature of Ty Coon}, 1 April 1989
333 | Ty Coon, President of Vice
334 |
335 | This General Public License does not permit incorporating your program into
336 | proprietary programs. If your program is a subroutine library, you may
337 | consider it more useful to permit linking proprietary applications with the
338 | library. If this is what you want to do, use the GNU Lesser General
339 | Public License instead of this License.
--------------------------------------------------------------------------------
/config_example.json:
--------------------------------------------------------------------------------
1 | {
2 | "coin": "dashcoin",
3 | "symbol": "DSH",
4 | "coinUnits": 1000000000000,
5 | "coinDifficultyTarget": 120,
6 |
7 | "logging": {
8 | "files": {
9 | "level": "info",
10 | "directory": "logs",
11 | "flushInterval": 5
12 | },
13 | "console": {
14 | "level": "info",
15 | "colors": true
16 | }
17 | },
18 |
19 | "poolServer": {
20 | "enabled": true,
21 | "clusterForks": "auto",
22 | "poolAddress": "D6WLtrV1SBWV8HWQzQv8uuYuGy3uwZ8ah5iT5HovSqhTKMauquoTsKP8RBJzVqVesX87poYWQgkGWB4NWHJ6Ravv93v4BaE",
23 | "blockRefreshInterval": 1000,
24 | "minerTimeout": 900,
25 | "ports": [
26 | {
27 | "port": 3333,
28 | "difficulty": 100,
29 | "desc": "Low end hardware"
30 | },
31 | {
32 | "port": 5555,
33 | "difficulty": 2000,
34 | "desc": "Mid range hardware"
35 | },
36 | {
37 | "port": 7777,
38 | "difficulty": 10000,
39 | "desc": "High end hardware"
40 | },
41 | {
42 | "port": 8888,
43 | "difficulty": 10000,
44 | "desc": "Hidden port",
45 | "hidden": true
46 | }
47 | ],
48 | "varDiff": {
49 | "minDiff": 100,
50 | "maxDiff": 200000,
51 | "targetTime": 100,
52 | "retargetTime": 30,
53 | "variancePercent": 30,
54 | "maxJump": 100
55 | },
56 | "fixedDiff": {
57 | "enabled": true,
58 | "addressSeparator": "."
59 | },
60 | "shareTrust": {
61 | "enabled": true,
62 | "min": 10,
63 | "stepDown": 3,
64 | "threshold": 10,
65 | "penalty": 30
66 | },
67 | "banning": {
68 | "enabled": true,
69 | "time": 600,
70 | "invalidPercent": 25,
71 | "checkThreshold": 30
72 | },
73 | "slushMining": {
74 | "enabled": false,
75 | "weight": 120,
76 | "lastBlockCheckRate": 1
77 | }
78 | },
79 |
80 | "payments": {
81 | "enabled": true,
82 | "interval": 600,
83 | "maxAddresses": 50,
84 | "mixin": 3,
85 | "transferFee": 50000000,
86 | "minPayment": 1000000000,
87 | "maxTransactionAmount": 0,
88 | "denomination": 1000000000
89 | },
90 |
91 | "blockUnlocker": {
92 | "enabled": true,
93 | "interval": 30,
94 | "depth": 10,
95 | "poolFee": 2,
96 | "devDonation": 0.1,
97 | "coreDevDonation": 0.1,
98 | "extraFeaturesDevDonation":0.1
99 | },
100 |
101 | "api": {
102 | "enabled": true,
103 | "hashrateWindow": 600,
104 | "updateInterval": 5,
105 | "port": 8117,
106 | "blocks": 30,
107 | "payments": 30,
108 | "password": "your_password"
109 | },
110 |
111 | "daemon": {
112 | "host": "127.0.0.1",
113 | "port": 42081,
114 | "legacy": false
115 | },
116 |
117 | "wallet": {
118 | "host": "127.0.0.1",
119 | "port": 8082
120 | },
121 |
122 | "redis": {
123 | "host": "127.0.0.1",
124 | "port": 6379
125 | },
126 |
127 | "monitoring": {
128 | "daemon": {
129 | "checkInterval": 60,
130 | "rpcMethod": "getblockcount"
131 | },
132 | "wallet": {
133 | "checkInterval": 60,
134 | "rpcMethod": "getbalance"
135 | }
136 | },
137 |
138 | "charts": {
139 | "pool": {
140 | "hashrate": {
141 | "enabled": true,
142 | "updateInterval": 60,
143 | "stepInterval": 1800,
144 | "maximumPeriod": 86400
145 | },
146 | "workers": {
147 | "enabled": true,
148 | "updateInterval": 60,
149 | "stepInterval": 1800,
150 | "maximumPeriod": 86400
151 | },
152 | "difficulty": {
153 | "enabled": true,
154 | "updateInterval": 1800,
155 | "stepInterval": 10800,
156 | "maximumPeriod": 604800
157 | },
158 | "price": {
159 | "enabled": true,
160 | "updateInterval": 1800,
161 | "stepInterval": 10800,
162 | "maximumPeriod": 604800
163 | },
164 | "profit": {
165 | "enabled": true,
166 | "updateInterval": 1800,
167 | "stepInterval": 10800,
168 | "maximumPeriod": 604800
169 | }
170 | },
171 | "user": {
172 | "hashrate": {
173 | "enabled": true,
174 | "updateInterval": 180,
175 | "stepInterval": 1800,
176 | "maximumPeriod": 86400
177 | },
178 | "payments": {
179 | "enabled": true
180 | }
181 | }
182 | }
183 | }
184 |
--------------------------------------------------------------------------------
/init.js:
--------------------------------------------------------------------------------
1 | var fs = require('fs');
2 | var cluster = require('cluster');
3 | var os = require('os');
4 |
5 | var redis = require('redis');
6 |
7 |
8 | require('./lib/configReader.js');
9 |
10 | require('./lib/logger.js');
11 |
12 |
13 | global.redisClient = redis.createClient(config.redis.port, config.redis.host);
14 |
15 |
16 | if (cluster.isWorker){
17 | switch(process.env.workerType){
18 | case 'pool':
19 | require('./lib/pool.js');
20 | break;
21 | case 'blockUnlocker':
22 | require('./lib/blockUnlocker.js');
23 | break;
24 | case 'paymentProcessor':
25 | require('./lib/paymentProcessor.js');
26 | break;
27 | case 'api':
28 | require('./lib/api.js');
29 | break;
30 | case 'cli':
31 | require('./lib/cli.js');
32 | break
33 | case 'chartsDataCollector':
34 | require('./lib/chartsDataCollector.js');
35 | break
36 |
37 | }
38 | return;
39 | }
40 |
41 | var logSystem = 'master';
42 | require('./lib/exceptionWriter.js')(logSystem);
43 |
44 |
45 | var singleModule = (function(){
46 |
47 | var validModules = ['pool', 'api', 'unlocker', 'payments', 'chartsDataCollector'];
48 |
49 | for (var i = 0; i < process.argv.length; i++){
50 | if (process.argv[i].indexOf('-module=') === 0){
51 | var moduleName = process.argv[i].split('=')[1];
52 | if (validModules.indexOf(moduleName) > -1)
53 | return moduleName;
54 |
55 | log('error', logSystem, 'Invalid module "%s", valid modules: %s', [moduleName, validModules.join(', ')]);
56 | process.exit();
57 | }
58 | }
59 | })();
60 |
61 |
62 | (function init(){
63 |
64 | checkRedisVersion(function(){
65 |
66 | if (singleModule){
67 | log('info', logSystem, 'Running in single module mode: %s', [singleModule]);
68 |
69 | switch(singleModule){
70 | case 'pool':
71 | spawnPoolWorkers();
72 | break;
73 | case 'unlocker':
74 | spawnBlockUnlocker();
75 | break;
76 | case 'payments':
77 | spawnPaymentProcessor();
78 | break;
79 | case 'api':
80 | spawnApi();
81 | break;
82 | case 'chartsDataCollector':
83 | spawnChartsDataCollector();
84 | break;
85 | }
86 | }
87 | else{
88 | spawnPoolWorkers();
89 | spawnBlockUnlocker();
90 | spawnPaymentProcessor();
91 | spawnApi();
92 | spawnChartsDataCollector();
93 | }
94 |
95 | spawnCli();
96 |
97 | });
98 | })();
99 |
100 |
101 | function checkRedisVersion(callback){
102 |
103 | redisClient.info(function(error, response){
104 | if (error){
105 | log('error', logSystem, 'Redis version check failed');
106 | return;
107 | }
108 | var parts = response.split('\r\n');
109 | var version;
110 | var versionString;
111 | for (var i = 0; i < parts.length; i++){
112 | if (parts[i].indexOf(':') !== -1){
113 | var valParts = parts[i].split(':');
114 | if (valParts[0] === 'redis_version'){
115 | versionString = valParts[1];
116 | version = parseFloat(versionString);
117 | break;
118 | }
119 | }
120 | }
121 | if (!version){
122 | log('error', logSystem, 'Could not detect redis version - must be super old or broken');
123 | return;
124 | }
125 | else if (version < 2.6){
126 | log('error', logSystem, "You're using redis version %s the minimum required version is 2.6. Follow the damn usage instructions...", [versionString]);
127 | return;
128 | }
129 | callback();
130 | });
131 | }
132 |
133 | function spawnPoolWorkers(){
134 |
135 | if (!config.poolServer || !config.poolServer.enabled || !config.poolServer.ports || config.poolServer.ports.length === 0) return;
136 |
137 | if (config.poolServer.ports.length === 0){
138 | log('error', logSystem, 'Pool server enabled but no ports specified');
139 | return;
140 | }
141 |
142 |
143 | var numForks = (function(){
144 | if (!config.poolServer.clusterForks)
145 | return 1;
146 | if (config.poolServer.clusterForks === 'auto')
147 | return os.cpus().length;
148 | if (isNaN(config.poolServer.clusterForks))
149 | return 1;
150 | return config.poolServer.clusterForks;
151 | })();
152 |
153 | var poolWorkers = {};
154 |
155 | var createPoolWorker = function(forkId){
156 | var worker = cluster.fork({
157 | workerType: 'pool',
158 | forkId: forkId
159 | });
160 | worker.forkId = forkId;
161 | worker.type = 'pool';
162 | poolWorkers[forkId] = worker;
163 | worker.on('exit', function(code, signal){
164 | log('error', logSystem, 'Pool fork %s died, spawning replacement worker...', [forkId]);
165 | setTimeout(function(){
166 | createPoolWorker(forkId);
167 | }, 2000);
168 | }).on('message', function(msg){
169 | switch(msg.type){
170 | case 'banIP':
171 | Object.keys(cluster.workers).forEach(function(id) {
172 | if (cluster.workers[id].type === 'pool'){
173 | cluster.workers[id].send({type: 'banIP', ip: msg.ip});
174 | }
175 | });
176 | break;
177 | }
178 | });
179 | };
180 |
181 | var i = 1;
182 | var spawnInterval = setInterval(function(){
183 | createPoolWorker(i.toString());
184 | i++;
185 | if (i - 1 === numForks){
186 | clearInterval(spawnInterval);
187 | log('info', logSystem, 'Pool spawned on %d thread(s)', [numForks]);
188 | }
189 | }, 10);
190 | }
191 |
192 | function spawnBlockUnlocker(){
193 |
194 | if (!config.blockUnlocker || !config.blockUnlocker.enabled) return;
195 |
196 | var worker = cluster.fork({
197 | workerType: 'blockUnlocker'
198 | });
199 | worker.on('exit', function(code, signal){
200 | log('error', logSystem, 'Block unlocker died, spawning replacement...');
201 | setTimeout(function(){
202 | spawnBlockUnlocker();
203 | }, 2000);
204 | });
205 |
206 | }
207 |
208 | function spawnPaymentProcessor(){
209 |
210 | if (!config.payments || !config.payments.enabled) return;
211 |
212 | var worker = cluster.fork({
213 | workerType: 'paymentProcessor'
214 | });
215 | worker.on('exit', function(code, signal){
216 | log('error', logSystem, 'Payment processor died, spawning replacement...');
217 | setTimeout(function(){
218 | spawnPaymentProcessor();
219 | }, 2000);
220 | });
221 | }
222 |
223 | function spawnApi(){
224 | if (!config.api || !config.api.enabled) return;
225 |
226 | var worker = cluster.fork({
227 | workerType: 'api'
228 | });
229 | worker.on('exit', function(code, signal){
230 | log('error', logSystem, 'API died, spawning replacement...');
231 | setTimeout(function(){
232 | spawnApi();
233 | }, 2000);
234 | });
235 | }
236 |
237 | function spawnCli(){
238 |
239 | }
240 |
241 | function spawnChartsDataCollector(){
242 | if (!config.charts) return;
243 |
244 | var worker = cluster.fork({
245 | workerType: 'chartsDataCollector'
246 | });
247 | worker.on('exit', function(code, signal){
248 | log('error', logSystem, 'chartsDataCollector died, spawning replacement...');
249 | setTimeout(function(){
250 | spawnChartsDataCollector();
251 | }, 2000);
252 | });
253 | }
254 |
--------------------------------------------------------------------------------
/lib/apiInterfaces.js:
--------------------------------------------------------------------------------
1 | var http = require('http');
2 | var https = require('https');
3 |
4 | function jsonHttpRequest(host, port, data, callback, path){
5 | path = path || '/json_rpc';
6 |
7 | var options = {
8 | hostname: host,
9 | port: port,
10 | path: path,
11 | method: data ? 'POST' : 'GET',
12 | headers: {
13 | 'Content-Length': data.length,
14 | 'Content-Type': 'application/json',
15 | 'Accept': 'application/json'
16 | }
17 | };
18 |
19 | var req = (port == 443 ? https : http).request(options, function(res){
20 | var replyData = '';
21 | res.setEncoding('utf8');
22 | res.on('data', function(chunk){
23 | replyData += chunk;
24 | });
25 | res.on('end', function(){
26 | var replyJson;
27 | try{
28 | replyJson = JSON.parse(replyData);
29 | }
30 | catch(e){
31 | callback(e);
32 | return;
33 | }
34 | callback(null, replyJson);
35 | });
36 | });
37 |
38 | req.on('error', function(e){
39 | callback(e);
40 | });
41 |
42 | req.end(data);
43 | }
44 |
45 | function rpc(host, port, method, params, callback){
46 |
47 | var data = JSON.stringify({
48 | id: "0",
49 | jsonrpc: "2.0",
50 | method: method,
51 | params: params
52 | });
53 | jsonHttpRequest(host, port, data, function(error, replyJson){
54 | if (error){
55 | callback(error);
56 | return;
57 | }
58 | callback(replyJson.error, replyJson.result)
59 | });
60 | }
61 |
62 | function batchRpc(host, port, array, callback){
63 | var rpcArray = [];
64 | for (var i = 0; i < array.length; i++){
65 | rpcArray.push({
66 | id: i.toString(),
67 | jsonrpc: "2.0",
68 | method: array[i][0],
69 | params: array[i][1]
70 | });
71 | }
72 | var data = JSON.stringify(rpcArray);
73 | jsonHttpRequest(host, port, data, callback);
74 | }
75 |
76 |
77 | module.exports = function(daemonConfig, walletConfig, poolApiConfig){
78 | return {
79 | batchRpcDaemon: function(batchArray, callback){
80 | batchRpc(daemonConfig.host, daemonConfig.port, batchArray, callback);
81 | },
82 | rpcDaemon: function(method, params, callback){
83 | rpc(daemonConfig.host, daemonConfig.port, method, params, callback);
84 | },
85 | rpcWallet: function(method, params, callback){
86 | rpc(walletConfig.host, walletConfig.port, method, params, callback);
87 | },
88 | pool: function(method, callback){
89 | jsonHttpRequest('127.0.0.1', poolApiConfig.port, '', callback, method);
90 | },
91 | jsonHttpRequest: jsonHttpRequest
92 | }
93 | };
94 |
--------------------------------------------------------------------------------
/lib/blockUnlocker.js:
--------------------------------------------------------------------------------
1 | var async = require('async');
2 |
3 | var apiInterfaces = require('./apiInterfaces.js')(config.daemon, config.wallet, config.api);
4 |
5 | var logSystem = 'unlocker';
6 | require('./exceptionWriter.js')(logSystem);
7 |
8 | log('info', logSystem, 'Started');
9 |
10 | function runInterval(){
11 | async.waterfall([
12 |
13 | //Get all block candidates in redis
14 | function(callback){
15 | redisClient.zrange(config.coin + ':blocks:candidates', 0, -1, 'WITHSCORES', function(error, results){
16 | if (error){
17 | log('error', logSystem, 'Error trying to get pending blocks from redis %j', [error]);
18 | callback(true);
19 | return;
20 | }
21 | if (results.length === 0){
22 | log('info', logSystem, 'No blocks candidates in redis');
23 | callback(true);
24 | return;
25 | }
26 |
27 | var blocks = [];
28 |
29 | for (var i = 0; i < results.length; i += 2){
30 | var parts = results[i].split(':');
31 | blocks.push({
32 | serialized: results[i],
33 | height: parseInt(results[i + 1]),
34 | hash: parts[0],
35 | time: parts[1],
36 | difficulty: parts[2],
37 | shares: parts[3]
38 | });
39 | }
40 |
41 | callback(null, blocks);
42 | });
43 | },
44 |
45 | //Check if blocks are orphaned
46 | function(blocks, callback){
47 | async.filter(blocks, function(block, mapCback){
48 | var block_height = block.height + 1;
49 | if (typeof config.daemon.legacy !== 'undefined' && config.daemon.legacy === true) {
50 | var block_height = block.height
51 | }
52 | apiInterfaces.rpcDaemon('getblockheaderbyheight', {height: block_height}, function(error, result){
53 | if (error){
54 | log('error', logSystem, 'Error with getblockheaderbyheight RPC request for block %s - %j', [block.serialized, error]);
55 | block.unlocked = false;
56 | mapCback();
57 | return;
58 | }
59 | if (!result.block_header){
60 | log('error', logSystem, 'Error with getblockheaderbyheight, no details returned for %s - %j', [block.serialized, result]);
61 | block.unlocked = false;
62 | mapCback();
63 | return;
64 | }
65 | var blockHeader = result.block_header;
66 | block.orphaned = blockHeader.hash === block.hash ? 0 : 1;
67 | block.unlocked = blockHeader.depth >= config.blockUnlocker.depth;
68 | block.reward = blockHeader.reward;
69 | mapCback(block.unlocked);
70 | });
71 | }, function(unlockedBlocks){
72 |
73 | if (unlockedBlocks.length === 0){
74 | log('info', logSystem, 'No pending blocks are unlocked yet (%d pending)', [blocks.length]);
75 | callback(true);
76 | return;
77 | }
78 |
79 | callback(null, unlockedBlocks)
80 | })
81 | },
82 |
83 | //Get worker shares for each unlocked block
84 | function(blocks, callback){
85 |
86 | var redisCommands = blocks.map(function(block){
87 | return ['hgetall', config.coin + ':shares:round' + block.height];
88 | });
89 |
90 |
91 | redisClient.multi(redisCommands).exec(function(error, replies){
92 | if (error){
93 | log('error', logSystem, 'Error with getting round shares from redis %j', [error]);
94 | callback(true);
95 | return;
96 | }
97 | for (var i = 0; i < replies.length; i++){
98 | var workerShares = replies[i];
99 | blocks[i].workerShares = workerShares;
100 | }
101 | callback(null, blocks);
102 | });
103 | },
104 |
105 | //Handle orphaned blocks
106 | function(blocks, callback){
107 | var orphanCommands = [];
108 |
109 | blocks.forEach(function(block){
110 | if (!block.orphaned) return;
111 |
112 | orphanCommands.push(['del', config.coin + ':shares:round' + block.height]);
113 |
114 | orphanCommands.push(['zrem', config.coin + ':blocks:candidates', block.serialized]);
115 | orphanCommands.push(['zadd', config.coin + ':blocks:matured', block.height, [
116 | block.hash,
117 | block.time,
118 | block.difficulty,
119 | block.shares,
120 | block.orphaned
121 | ].join(':')]);
122 |
123 | if (block.workerShares) {
124 | var workerShares = block.workerShares;
125 | Object.keys(workerShares).forEach(function (worker) {
126 | orphanCommands.push(['hincrby', config.coin + ':shares:roundCurrent', worker, workerShares[worker]]);
127 | });
128 | }
129 | });
130 |
131 | if (orphanCommands.length > 0){
132 | redisClient.multi(orphanCommands).exec(function(error, replies){
133 | if (error){
134 | log('error', logSystem, 'Error with cleaning up data in redis for orphan block(s) %j', [error]);
135 | callback(true);
136 | return;
137 | }
138 | callback(null, blocks);
139 | });
140 | }
141 | else{
142 | callback(null, blocks);
143 | }
144 | },
145 |
146 | //Handle unlocked blocks
147 | function(blocks, callback){
148 | var unlockedBlocksCommands = [];
149 | var payments = {};
150 | var totalBlocksUnlocked = 0;
151 | blocks.forEach(function(block){
152 | if (block.orphaned) return;
153 | totalBlocksUnlocked++;
154 |
155 | unlockedBlocksCommands.push(['del', config.coin + ':shares:round' + block.height]);
156 | unlockedBlocksCommands.push(['zrem', config.coin + ':blocks:candidates', block.serialized]);
157 | unlockedBlocksCommands.push(['zadd', config.coin + ':blocks:matured', block.height, [
158 | block.hash,
159 | block.time,
160 | block.difficulty,
161 | block.shares,
162 | block.orphaned,
163 | block.reward
164 | ].join(':')]);
165 |
166 | var feePercent = config.blockUnlocker.poolFee / 100;
167 |
168 | if (Object.keys(donations).length) {
169 | for(var wallet in donations) {
170 | var percent = donations[wallet] / 100;
171 | feePercent += percent;
172 | payments[wallet] = Math.round(block.reward * percent);
173 | log('info', logSystem, 'Block %d donation to %s as %d percent of reward: %d', [block.height, wallet, percent, payments[wallet]]);
174 | }
175 | }
176 |
177 | var reward = Math.round(block.reward - (block.reward * feePercent));
178 |
179 | log('info', logSystem, 'Unlocked %d block with reward %d and donation fee %d. Miners reward: %d', [block.height, block.reward, feePercent, reward]);
180 |
181 | if (block.workerShares) {
182 | var totalShares = parseInt(block.shares);
183 | Object.keys(block.workerShares).forEach(function (worker) {
184 | var percent = block.workerShares[worker] / totalShares;
185 | var workerReward = Math.round(reward * percent);
186 | payments[worker] = (payments[worker] || 0) + workerReward;
187 | log('info', logSystem, 'Block %d payment to %s for %d shares: %d', [block.height, worker, totalShares, payments[worker]]);
188 | });
189 | }
190 | });
191 |
192 | for (var worker in payments) {
193 | var amount = parseInt(payments[worker]);
194 | if (amount <= 0){
195 | delete payments[worker];
196 | continue;
197 | }
198 | unlockedBlocksCommands.push(['hincrby', config.coin + ':workers:' + worker, 'balance', amount]);
199 | }
200 |
201 | if (unlockedBlocksCommands.length === 0){
202 | log('info', logSystem, 'No unlocked blocks yet (%d pending)', [blocks.length]);
203 | callback(true);
204 | return;
205 | }
206 |
207 | redisClient.multi(unlockedBlocksCommands).exec(function(error, replies){
208 | if (error){
209 | log('error', logSystem, 'Error with unlocking blocks %j', [error]);
210 | callback(true);
211 | return;
212 | }
213 | log('info', logSystem, 'Unlocked %d blocks and update balances for %d workers', [totalBlocksUnlocked, Object.keys(payments).length]);
214 | callback(null);
215 | });
216 | }
217 | ], function(error, result){
218 | setTimeout(runInterval, config.blockUnlocker.interval * 1000);
219 | })
220 | }
221 |
222 | runInterval();
223 |
--------------------------------------------------------------------------------
/lib/charts.js:
--------------------------------------------------------------------------------
1 | var fs = require('fs');
2 | var async = require('async');
3 | var http = require('http');
4 | var apiInterfaces = require('./apiInterfaces.js')(config.daemon, config.wallet, config.api);
5 |
6 | var logSystem = 'charts';
7 | require('./exceptionWriter.js')(logSystem);
8 |
9 | log('info', logSystem, 'Started');
10 |
11 | function startDataCollectors() {
12 | async.each(Object.keys(config.charts.pool), function(chartName) {
13 | var settings = config.charts.pool[chartName];
14 | if(settings.enabled) {
15 | setInterval(function() {
16 | collectPoolStatWithInterval(chartName, settings);
17 | }, settings.updateInterval * 1000);
18 | }
19 | });
20 |
21 | var settings = config.charts.user.hashrate;
22 | if(settings.enabled) {
23 | setInterval(function() {
24 | collectUsersHashrate('hashrate', settings);
25 | }, settings.updateInterval * 1000)
26 | }
27 | }
28 |
29 | function getChartDataFromRedis(chartName, callback) {
30 | redisClient.get(getStatsRedisKey(chartName), function(error, data) {
31 | callback(data ? JSON.parse(data) : []);
32 | });
33 | }
34 |
35 | function getUserHashrateChartData(address, callback) {
36 | getChartDataFromRedis('hashrate:' + address, callback);
37 | }
38 |
39 | function convertPaymentsDataToChart(paymentsData) {
40 | var data = [];
41 | if(paymentsData && paymentsData.length) {
42 | for(var i = 0; paymentsData[i]; i += 2) {
43 | data.unshift([+paymentsData[i + 1], paymentsData[i].split(':')[1]]);
44 | }
45 | }
46 | return data;
47 | }
48 |
49 | function getUserChartsData(address, paymentsData, callback) {
50 | var stats = {};
51 | var chartsFuncs = {
52 | hashrate: function(callback) {
53 | getUserHashrateChartData(address, function(data) {
54 | callback(null, data);
55 | });
56 | },
57 |
58 | payments: function(callback) {
59 | callback(null, convertPaymentsDataToChart(paymentsData));
60 | }
61 | };
62 | for(var chartName in chartsFuncs) {
63 | if(!config.charts.user[chartName].enabled) {
64 | delete chartsFuncs[chartName];
65 | }
66 | }
67 | async.parallel(chartsFuncs, callback);
68 | }
69 |
70 | function getStatsRedisKey(chartName) {
71 | return config.coin + ':charts:' + chartName;
72 | }
73 |
74 | var chartStatFuncs = {
75 | hashrate: getPoolHashrate,
76 | workers: getPoolWorkers,
77 | difficulty: getNetworkDifficulty,
78 | price: getCoinPrice,
79 | profit: getCoinProfit
80 | };
81 |
82 | var statValueHandler = {
83 | avg: function(set, value) {
84 | set[1] = (set[1] * set[2] + value) / (set[2] + 1);
85 | },
86 | avgRound: function(set, value) {
87 | statValueHandler.avg(set, value);
88 | set[1] = Math.round(set[1]);
89 | },
90 | max: function(set, value) {
91 | if(value > set[1]) {
92 | set[1] = value;
93 | }
94 | }
95 | };
96 |
97 | var preSaveFunctions = {
98 | hashrate: statValueHandler.avgRound,
99 | workers: statValueHandler.max,
100 | difficulty: statValueHandler.avgRound,
101 | price: statValueHandler.avg,
102 | profit: statValueHandler.avg
103 | };
104 |
105 | function storeCollectedValues(chartName, values, settings) {
106 | for(var i in values) {
107 | storeCollectedValue(chartName + ':' + i, values[i], settings);
108 | }
109 | }
110 |
111 | function storeCollectedValue(chartName, value, settings) {
112 | var now = new Date() / 1000 | 0;
113 | getChartDataFromRedis(chartName, function(sets) {
114 | var lastSet = sets[sets.length - 1]; // [time, avgValue, updatesCount]
115 | if(!lastSet || now - lastSet[0] > settings.stepInterval) {
116 | lastSet = [now, value, 1];
117 | sets.push(lastSet);
118 | while(now - sets[0][0] > settings.maximumPeriod) { // clear old sets
119 | sets.shift();
120 | }
121 | }
122 | else {
123 | preSaveFunctions[chartName]
124 | ? preSaveFunctions[chartName](lastSet, value)
125 | : statValueHandler.avgRound(lastSet, value);
126 | lastSet[2]++;
127 | }
128 | redisClient.set(getStatsRedisKey(chartName), JSON.stringify(sets));
129 | log('info', logSystem, chartName + ' chart collected value ' + value + '. Total sets count ' + sets.length);
130 | });
131 | }
132 |
133 | function collectPoolStatWithInterval(chartName, settings) {
134 | async.waterfall([
135 | chartStatFuncs[chartName],
136 | function(value, callback) {
137 | storeCollectedValue(chartName, value, settings, callback);
138 | }
139 | ]);
140 | }
141 |
142 | function getPoolStats(callback) {
143 | apiInterfaces.pool('/stats', callback);
144 | }
145 |
146 | function getPoolHashrate(callback) {
147 | getPoolStats(function(error, stats) {
148 | callback(error, stats.pool ? Math.round(stats.pool.hashrate) : null);
149 | });
150 | }
151 |
152 | function getPoolWorkers(callback) {
153 | getPoolStats(function(error, stats) {
154 | callback(error, stats.pool ? stats.pool.miners : null);
155 | });
156 | }
157 |
158 | function getNetworkDifficulty(callback) {
159 | getPoolStats(function(error, stats) {
160 | callback(error, stats.pool ? stats.network.difficulty : null);
161 | });
162 | }
163 |
164 | function getUsersHashrates(callback) {
165 | apiInterfaces.pool('/miners_hashrate', function(error, data) {
166 | callback(data.minersHashrate);
167 | });
168 | }
169 |
170 | function collectUsersHashrate(chartName, settings) {
171 | var redisBaseKey = getStatsRedisKey(chartName) + ':';
172 | redisClient.keys(redisBaseKey + '*', function(keys) {
173 | var hashrates = {};
174 | for(var i in keys) {
175 | hashrates[keys[i].substr(keys[i].length)] = 0;
176 | }
177 | getUsersHashrates(function(newHashrates) {
178 | for(var address in newHashrates) {
179 | hashrates[address] = newHashrates[address];
180 | }
181 | storeCollectedValues(chartName, hashrates, settings);
182 | });
183 | });
184 | }
185 |
186 | function getCoinPrice(callback) {
187 | apiInterfaces.jsonHttpRequest('api.cryptonator.com', 443, '', function(error, response) {
188 | callback(response.error ? response.error : error, response.success ? +response.ticker.price : null);
189 | }, '/api/ticker/' + config.symbol.toLowerCase() + '-usd');
190 | }
191 |
192 | function getCoinProfit(callback) {
193 | getCoinPrice(function(error, price) {
194 | if(error) {
195 | callback(error);
196 | return;
197 | }
198 | getPoolStats(function(error, stats) {
199 | if(error) {
200 | callback(error);
201 | return;
202 | }
203 | callback(null, stats.network.reward * price / stats.network.difficulty / config.coinUnits);
204 | });
205 | });
206 | }
207 |
208 | function getPoolChartsData(callback) {
209 | var chartsNames = [];
210 | var redisKeys = [];
211 | for(var chartName in config.charts.pool) {
212 | if(config.charts.pool[chartName].enabled) {
213 | chartsNames.push(chartName);
214 | redisKeys.push(getStatsRedisKey(chartName));
215 | }
216 | }
217 | if(redisKeys.length) {
218 | redisClient.mget(redisKeys, function(error, data) {
219 | var stats = {};
220 | if(data) {
221 | for(var i in data) {
222 | if(data[i]) {
223 | stats[chartsNames[i]] = JSON.parse(data[i]);
224 | }
225 | }
226 | }
227 | callback(error, stats);
228 | });
229 | }
230 | else {
231 | callback(null, {});
232 | }
233 | }
234 |
235 | module.exports = {
236 | startDataCollectors: startDataCollectors,
237 | getUserChartsData: getUserChartsData,
238 | getPoolChartsData: getPoolChartsData
239 | };
240 |
--------------------------------------------------------------------------------
/lib/chartsDataCollector.js:
--------------------------------------------------------------------------------
1 | var fs = require('fs');
2 | var async = require('async');
3 | var http = require('http');
4 | var charts = require('./charts.js');
5 |
6 | var logSystem = 'chartsDataCollector';
7 | require('./exceptionWriter.js')(logSystem);
8 |
9 | log('info', logSystem, 'Started');
10 |
11 | charts.startDataCollectors();
12 |
--------------------------------------------------------------------------------
/lib/cli.js:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/forknote/forknote-pool/7b796fadf0775d74ff420899fa3f1e70f13a9753/lib/cli.js
--------------------------------------------------------------------------------
/lib/configReader.js:
--------------------------------------------------------------------------------
1 | var fs = require('fs');
2 |
3 | var configFile = (function(){
4 | for (var i = 0; i < process.argv.length; i++){
5 | if (process.argv[i].indexOf('-config=') === 0)
6 | return process.argv[i].split('=')[1];
7 | }
8 | return 'config.json';
9 | })();
10 |
11 |
12 | try {
13 | global.config = JSON.parse(fs.readFileSync(configFile));
14 | }
15 | catch(e){
16 | console.error('Failed to read config file ' + configFile + '\n\n' + e);
17 | return;
18 | }
19 |
20 | var donationAddresses = {
21 | devDonation: {
22 | XMR: '45Jmf8PnJKziGyrLouJMeBFw2yVyX1QB52sKEQ4S1VSU2NVsaVGPNu4bWKkaHaeZ6tWCepP6iceZk8XhTLzDaEVa72QrtVh'
23 | },
24 | coreDevDonation: {
25 | BCN: '252m7ru3wT5McAUztrZDExJ9PgnmyJVgk2ayucQLt13dFrf5DE4SrSBVkbtVhvZbRj1Ty4cVWaE6MGDVArZLpuMhCkrvToA',
26 | BBR: '@zoidberg',
27 | XMR: '46BeWrHpwXmHDpDEUmZBWZfoQpdc6HaERCNmx1pEYL2rAcuwufPN9rXHHtyUA4QVy66qeFQkn6sfK8aHYjA3jk3o1Bv16em',
28 | QCN: '1R9wwAH68XNGHZBsSCbyPL5EBepCoqnPPYpUuYx7jyZtc1SqekoM5p4iiB4EnkHBMXUrkwg7vR35vcoC6h48t7AjKXqXWqX',
29 | FCN: '6ntYwFY2syKVha7K1KZrhD8Uyzc2VsCpAjGi8YptCVHmfeqkAyRWQSX8gV23uvnHZY2LssBMoidGfCrVAc9k1RSsN7WKSf2',
30 | AEON: 'WmsG9mv8iSeJ8w2U9J1jRXNbLT7bCUj9VShzGuBctrhEBMHWxLZ2noSYhs8WgM4RDR68mrMW7hm33NRiDV8bNVu52azJb6XoN',
31 | OEC: 'C9ouydpeiyT2gVr5MqPUjHVJ26nJ9b8ZmMtVYRRTpGqHXA973MvzUWiFFhFCapkLmdVivd1c8Fj5wKDNw3oao7K44bUeAqx',
32 | DSH: 'D3z2DDWygoZU4NniCNa4oMjjKi45dC2KHUWUyD1RZ1pfgnRgcHdfLVQgh5gmRv4jwEjCX5LoLERAf5PbjLS43Rkd8vFUM1m',
33 | KRB: 'Kdev1L9V5ow3cdKNqDpLcFFxZCqu5W2GE9xMKewsB2pUXWxcXvJaUWHcSrHuZw91eYfQFzRtGfTemReSSMN4kE445i6Etb3'
34 | },
35 | extraFeaturesDevDonation: {
36 | XDN: 'dddcPW8VjhJA9U2QDPunnkL8Yky83TdEBGThXsETtmAx8q7jvni7Kt4hn8pqS3ks7GhM2z9BeD22jgKewZ2kQhWK27ZaGfLHQ',
37 | BCN: '23emzdEQqoWAifE1ZQLTrGAmVkdzjami82xgX4zWoX2VbiuGuunMJ1oF14PPa1cVRgGFz8tUWevsSNzMcPqHiQmF7iSzS1g',
38 | BBR: '1Gy4NimzTgyhcZ22432N4ojP9HC6mHpML2g8SsmmjPZS4o8SYNFND99LAihRPHA2ddarf3okkJ3umTC2gLpysKBfLi4hfTF',
39 | XMR: '43JNtJkxnhX789pJnCV6W4DQt8BqAWVoV4pqjGiWZ9845ee3aTBRBibMBzSziQi9BD5aEEpegQjLwguUgsXecB5bCY5wrMe',
40 | QCN: '1MafhBsdrkW3ssQexQzHf8Q2VBEWE3DmrbySKJzXXNJFeHHTWahDkJ3iLkiKnAMMtzHPeLrsYVmkQJJ9DJx3ToodKUapV8p',
41 | FCN: '6iAu6xDGnSFekMxJj7S61aepNVXbyCrV7PgWWKSfsEPyXbUjHAxjgq3KwED3dWrgddCRRtwBrrYgmVLZR7vdBr3KLe9Cowd',
42 | MCN: 'Vdufe8Pjkp2apvJC2N3Q7KdqiDDnLR3cH8hPhB2hjBtdXRxnHmHdgESbCjAD5dv1oiFrA1jKJQqszHWELdNygCGW2Ri6qRH1B',
43 | AEON: 'Wmsfi4rDdUC4xQyMo7f4BkfuPtSooPZ3wfx8e7JFXaKyfFY5buDJF4UakwhLp3FXxKVwB3ZFLQ3bTUBksCoG3tVQ2tzkrCZDm',
44 | OEC: 'C53KdM3sWk2P27yvqihNDEVsCtDQH9koJTdmEX96ftqgTvSsuvg5HMJ2dLnynwcWr5d3oMvwzsQVKdVeYchG5iU6L5rNJjd',
45 | DSH: 'D9t1KjB9w2haRp29ueJ9jHfTyq1FSzpMqavKpfFuwa8yEdHKuryfmh4W6ZzbHC71JFLoRD4ny7HWm15jb52JYcye7bBjD62',
46 | INF8: 'inf8FtwGgmaATeXKvycUT7BfxiawxqhfRXaahPog7s4c7L8qreSvnebNDvwfDBXrip8ptgKgbToV73mS5M1cNviY5sbKhKitCd'
47 | }
48 | };
49 |
50 | global.donations = {};
51 |
52 | for(var configOption in donationAddresses) {
53 | var percent = config.blockUnlocker[configOption];
54 | var wallet = donationAddresses[configOption][config.symbol];
55 | if(percent && wallet) {
56 | global.donations[wallet] = percent;
57 | }
58 | }
59 |
60 | global.version = "v1.1.4";
61 |
--------------------------------------------------------------------------------
/lib/exceptionWriter.js:
--------------------------------------------------------------------------------
1 | var fs = require('fs');
2 | var cluster = require('cluster');
3 |
4 | var dateFormat = require('dateformat');
5 |
6 |
7 | module.exports = function(logSystem){
8 |
9 | process.on('uncaughtException', function(err) {
10 | console.log('\n' + err.stack + '\n');
11 | var time = dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss');
12 | fs.appendFile(config.logging.files.directory + '/' + logSystem + '_crash.log', time + '\n' + err.stack + '\n\n', function(err){
13 | if (cluster.isWorker)
14 | process.exit();
15 | });
16 | });
17 | };
--------------------------------------------------------------------------------
/lib/logger.js:
--------------------------------------------------------------------------------
1 | var fs = require('fs');
2 | var util = require('util');
3 |
4 | var dateFormat = require('dateformat');
5 | var clc = require('cli-color');
6 |
7 | var severityMap = {
8 | 'info': clc.blue,
9 | 'warn': clc.yellow,
10 | 'error': clc.red
11 | };
12 |
13 | var severityLevels = ['info', 'warn', 'error'];
14 |
15 |
16 | var logDir = config.logging.files.directory;
17 |
18 | if (!fs.existsSync(logDir)){
19 | try {
20 | fs.mkdirSync(logDir);
21 | }
22 | catch(e){
23 | throw e;
24 | }
25 | }
26 |
27 | var pendingWrites = {};
28 |
29 | setInterval(function(){
30 | for (var fileName in pendingWrites){
31 | var data = pendingWrites[fileName];
32 | fs.appendFile(fileName, data);
33 | delete pendingWrites[fileName];
34 | }
35 | }, config.logging.files.flushInterval * 1000);
36 |
37 | global.log = function(severity, system, text, data){
38 |
39 | var logConsole = severityLevels.indexOf(severity) >= severityLevels.indexOf(config.logging.console.level);
40 | var logFiles = severityLevels.indexOf(severity) >= severityLevels.indexOf(config.logging.files.level);
41 |
42 | if (!logConsole && !logFiles) return;
43 |
44 | var time = dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss');
45 | var formattedMessage = text;
46 |
47 | if (data) {
48 | data.unshift(text);
49 | formattedMessage = util.format.apply(null, data);
50 | }
51 |
52 | if (logConsole){
53 | if (config.logging.console.colors)
54 | console.log(severityMap[severity](time) + clc.white.bold(' [' + system + '] ') + formattedMessage);
55 | else
56 | console.log(time + ' [' + system + '] ' + formattedMessage);
57 | }
58 |
59 |
60 | if (logFiles) {
61 | var fileName = logDir + '/' + system + '_' + severity + '.log';
62 | var fileLine = time + ' ' + formattedMessage + '\n';
63 | pendingWrites[fileName] = (pendingWrites[fileName] || '') + fileLine;
64 | }
65 | };
--------------------------------------------------------------------------------
/lib/paymentProcessor.js:
--------------------------------------------------------------------------------
1 | var fs = require('fs');
2 |
3 | var async = require('async');
4 |
5 | var apiInterfaces = require('./apiInterfaces.js')(config.daemon, config.wallet, config.api);
6 |
7 |
8 | var logSystem = 'payments';
9 | require('./exceptionWriter.js')(logSystem);
10 |
11 |
12 | log('info', logSystem, 'Started');
13 |
14 |
15 | function runInterval(){
16 | async.waterfall([
17 |
18 | //Get worker keys
19 | function(callback){
20 | redisClient.keys(config.coin + ':workers:*', function(error, result) {
21 | if (error) {
22 | log('error', logSystem, 'Error trying to get worker balances from redis %j', [error]);
23 | callback(true);
24 | return;
25 | }
26 | callback(null, result);
27 | });
28 | },
29 |
30 | //Get worker balances
31 | function(keys, callback){
32 | var redisCommands = keys.map(function(k){
33 | return ['hget', k, 'balance'];
34 | });
35 | redisClient.multi(redisCommands).exec(function(error, replies){
36 | if (error){
37 | log('error', logSystem, 'Error with getting balances from redis %j', [error]);
38 | callback(true);
39 | return;
40 | }
41 | var balances = {};
42 | for (var i = 0; i < replies.length; i++){
43 | var parts = keys[i].split(':');
44 | var workerId = parts[parts.length - 1];
45 | balances[workerId] = parseInt(replies[i]) || 0
46 |
47 | }
48 | callback(null, balances);
49 | });
50 | },
51 |
52 | //Filter workers under balance threshold for payment
53 | function(balances, callback){
54 |
55 | var payments = {};
56 |
57 | for (var worker in balances){
58 | var balance = balances[worker];
59 | if (balance >= config.payments.minPayment){
60 | var remainder = balance % config.payments.denomination;
61 | var payout = balance - remainder;
62 | if (payout < 0) continue;
63 | payments[worker] = payout;
64 | }
65 | }
66 |
67 | if (Object.keys(payments).length === 0){
68 | log('info', logSystem, 'No workers\' balances reached the minimum payment threshold');
69 | callback(true);
70 | return;
71 | }
72 |
73 | var transferCommands = [];
74 | var addresses = 0;
75 | var commandAmount = 0;
76 | var commandIndex = 0;
77 |
78 | for (var worker in payments){
79 | var amount = parseInt(payments[worker]);
80 | if(config.payments.maxTransactionAmount && amount + commandAmount > config.payments.maxTransactionAmount) {
81 | amount = config.payments.maxTransactionAmount - commandAmount;
82 | }
83 |
84 | if(!transferCommands[commandIndex]) {
85 | transferCommands[commandIndex] = {
86 | redis: [],
87 | amount : 0,
88 | rpc: {
89 | destinations: [],
90 | fee: config.payments.transferFee,
91 | mixin: config.payments.mixin,
92 | unlock_time: 0
93 | }
94 | };
95 | }
96 |
97 | transferCommands[commandIndex].rpc.destinations.push({amount: amount, address: worker});
98 | transferCommands[commandIndex].redis.push(['hincrby', config.coin + ':workers:' + worker, 'balance', -amount]);
99 | transferCommands[commandIndex].redis.push(['hincrby', config.coin + ':workers:' + worker, 'paid', amount]);
100 | transferCommands[commandIndex].amount += amount;
101 |
102 | addresses++;
103 | commandAmount += amount;
104 | if (addresses >= config.payments.maxAddresses || ( config.payments.maxTransactionAmount && commandAmount >= config.payments.maxTransactionAmount)) {
105 | commandIndex++;
106 | addresses = 0;
107 | commandAmount = 0;
108 | }
109 | }
110 |
111 | var timeOffset = 0;
112 |
113 | async.filter(transferCommands, function(transferCmd, cback){
114 | apiInterfaces.rpcWallet('transfer', transferCmd.rpc, function(error, result){
115 | if (error){
116 | log('error', logSystem, 'Error with send_transaction RPC request to wallet daemon %j', [error]);
117 | log('error', logSystem, 'Payments failed to send to %j', transferCmd.rpc.destinations);
118 | cback(false);
119 | return;
120 | }
121 |
122 | var now = (timeOffset++) + Date.now() / 1000 | 0;
123 | var txHash = result.tx_hash;
124 |
125 |
126 | transferCmd.redis.push(['zadd', config.coin + ':payments:all', now, [
127 | txHash,
128 | transferCmd.amount,
129 | transferCmd.rpc.fee,
130 | transferCmd.rpc.mixin,
131 | Object.keys(transferCmd.rpc.destinations).length
132 | ].join(':')]);
133 |
134 |
135 | for (var i = 0; i < transferCmd.rpc.destinations.length; i++){
136 | var destination = transferCmd.rpc.destinations[i];
137 | transferCmd.redis.push(['zadd', config.coin + ':payments:' + destination.address, now, [
138 | txHash,
139 | destination.amount,
140 | transferCmd.rpc.fee,
141 | transferCmd.rpc.mixin
142 | ].join(':')]);
143 | }
144 |
145 |
146 | log('info', logSystem, 'Payments sent via wallet daemon %j', [result]);
147 | redisClient.multi(transferCmd.redis).exec(function(error, replies){
148 | if (error){
149 | log('error', logSystem, 'Super critical error! Payments sent yet failing to update balance in redis, double payouts likely to happen %j', [error]);
150 | log('error', logSystem, 'Double payments likely to be sent to %j', transferCmd.rpc.destinations);
151 | cback(false);
152 | return;
153 | }
154 | cback(true);
155 | });
156 | });
157 | }, function(succeeded){
158 | var failedAmount = transferCommands.length - succeeded.length;
159 | log('info', logSystem, 'Payments splintered and %d successfully sent, %d failed', [succeeded.length, failedAmount]);
160 | callback(null);
161 | });
162 |
163 | }
164 |
165 | ], function(error, result){
166 | setTimeout(runInterval, config.payments.interval * 1000);
167 | });
168 | }
169 |
170 | runInterval();
171 |
--------------------------------------------------------------------------------
/lib/utils.js:
--------------------------------------------------------------------------------
1 | var base58 = require('base58-native');
2 | var cnUtil = require('cryptonote-util');
3 |
4 | exports.uid = function(){
5 | var min = 100000000000000;
6 | var max = 999999999999999;
7 | var id = Math.floor(Math.random() * (max - min + 1)) + min;
8 | return id.toString();
9 | };
10 |
11 | exports.ringBuffer = function(maxSize){
12 | var data = [];
13 | var cursor = 0;
14 | var isFull = false;
15 |
16 | return {
17 | append: function(x){
18 | if (isFull){
19 | data[cursor] = x;
20 | cursor = (cursor + 1) % maxSize;
21 | }
22 | else{
23 | data.push(x);
24 | cursor++;
25 | if (data.length === maxSize){
26 | cursor = 0;
27 | isFull = true;
28 | }
29 | }
30 | },
31 | avg: function(plusOne){
32 | var sum = data.reduce(function(a, b){ return a + b }, plusOne || 0);
33 | return sum / ((isFull ? maxSize : cursor) + (plusOne ? 1 : 0));
34 | },
35 | size: function(){
36 | return isFull ? maxSize : cursor;
37 | },
38 | clear: function(){
39 | data = [];
40 | cursor = 0;
41 | isFull = false;
42 | }
43 | };
44 | };
45 |
46 | exports.varIntEncode = function(n){
47 |
48 | };
49 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "forknote-pool",
3 | "version": "0.0.1",
4 | "license": "GPL-2.0",
5 | "authors": "Matthew Little",
6 | "repository": {
7 | "type": "git",
8 | "url": "https://github.com/forknote/forknote-pool"
9 | },
10 | "dependencies": {
11 | "bignum": "*",
12 | "async": "1",
13 | "redis": "*",
14 | "cli-color": "*",
15 | "dateformat": "*",
16 | "base58-native": "*",
17 | "multi-hashing": "git://github.com/fancoder/node-multi-hashing.git",
18 | "cryptonote-util": "git://github.com/forknote/node-cryptonote-util.git"
19 | },
20 | "engines": {
21 | "node": ">=0.10"
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/redisBlocksUpgrade.js:
--------------------------------------------------------------------------------
1 |
2 |
3 | /*
4 |
5 | This script converts the block data in redis from the old format (v0.99.0.6 and earlier) to the new format
6 | used in v0.99.1+
7 |
8 | */
9 |
10 | var util = require('util');
11 |
12 | var async = require('async');
13 |
14 | var redis = require('redis');
15 |
16 | require('./lib/configReader.js');
17 |
18 | var apiInterfaces = require('./lib/apiInterfaces.js')(config.daemon, config.wallet);
19 |
20 |
21 | function log(severity, system, text, data){
22 |
23 | var formattedMessage = text;
24 |
25 | if (data) {
26 | data.unshift(text);
27 | formattedMessage = util.format.apply(null, data);
28 | }
29 |
30 | console.log(severity + ': ' + formattedMessage);
31 | }
32 |
33 |
34 | var logSystem = 'reward script';
35 |
36 | var redisClient = redis.createClient(config.redis.port, config.redis.host);
37 |
38 | function getTotalShares(height, callback){
39 |
40 | redisClient.hgetall(config.coin + ':shares:round' + height, function(err, workerShares){
41 |
42 | if (err) {
43 | callback(err);
44 | return;
45 | }
46 |
47 | var totalShares = Object.keys(workerShares).reduce(function(p, c){
48 | return p + parseInt(workerShares[c])
49 | }, 0);
50 |
51 | callback(null, totalShares);
52 |
53 | });
54 | }
55 |
56 |
57 | async.series([
58 | function(callback){
59 | redisClient.smembers(config.coin + ':blocksUnlocked', function(error, result){
60 | if (error){
61 | log('error', logSystem, 'Error trying to get unlocke blocks from redis %j', [error]);
62 | callback();
63 | return;
64 | }
65 | if (result.length === 0){
66 | log('info', logSystem, 'No unlocked blocks in redis');
67 | callback();
68 | return;
69 | }
70 |
71 | var blocks = result.map(function(item){
72 | var parts = item.split(':');
73 | return {
74 | height: parseInt(parts[0]),
75 | difficulty: parts[1],
76 | hash: parts[2],
77 | time: parts[3],
78 | shares: parts[4],
79 | orphaned: 0
80 | };
81 | });
82 |
83 | async.map(blocks, function(block, mapCback){
84 | apiInterfaces.rpcDaemon('getblockheaderbyheight', {height: block.height}, function(error, result){
85 | if (error){
86 | log('error', logSystem, 'Error with getblockheaderbyheight RPC request for block %s - %j', [block.serialized, error]);
87 | mapCback(null, block);
88 | return;
89 | }
90 | if (!result.block_header){
91 | log('error', logSystem, 'Error with getblockheaderbyheight, no details returned for %s - %j', [block.serialized, result]);
92 | mapCback(null, block);
93 | return;
94 | }
95 | var blockHeader = result.block_header;
96 | block.reward = blockHeader.reward;
97 | mapCback(null, block);
98 | });
99 | }, function(err, blocks){
100 |
101 | if (blocks.length === 0){
102 | log('info', logSystem, 'No unlocked blocks');
103 | callback();
104 | return;
105 | }
106 |
107 | var zaddCommands = [config.coin + ':blocks:matured'];
108 |
109 | for (var i = 0; i < blocks.length; i++){
110 | var block = blocks[i];
111 | zaddCommands.push(block.height);
112 | zaddCommands.push([
113 | block.hash,
114 | block.time,
115 | block.difficulty,
116 | block.shares,
117 | block.orphaned,
118 | block.reward
119 | ].join(':'));
120 | }
121 |
122 | redisClient.zadd(zaddCommands, function(err, result){
123 | if (err){
124 | console.log('failed zadd ' + JSON.stringify(err));
125 | callback();
126 | return;
127 | }
128 | console.log('successfully converted unlocked blocks to matured blocks');
129 | callback();
130 | });
131 |
132 |
133 | });
134 | });
135 | },
136 | function(callback){
137 | redisClient.smembers(config.coin + ':blocksPending', function(error, result) {
138 | if (error) {
139 | log('error', logSystem, 'Error trying to get pending blocks from redis %j', [error]);
140 | callback();
141 | return;
142 | }
143 | if (result.length === 0) {
144 | log('info', logSystem, 'No pending blocks in redis');
145 | callback();
146 | return;
147 | }
148 |
149 | async.map(result, function(item, mapCback){
150 | var parts = item.split(':');
151 | var block = {
152 | height: parseInt(parts[0]),
153 | difficulty: parts[1],
154 | hash: parts[2],
155 | time: parts[3],
156 | serialized: item
157 | };
158 | getTotalShares(block.height, function(err, shares){
159 | block.shares = shares;
160 | mapCback(null, block);
161 | });
162 | }, function(err, blocks){
163 |
164 | var zaddCommands = [config.coin + ':blocks:candidates'];
165 |
166 | for (var i = 0; i < blocks.length; i++){
167 | var block = blocks[i];
168 | zaddCommands.push(block.height);
169 | zaddCommands.push([
170 | block.hash,
171 | block.time,
172 | block.difficulty,
173 | block.shares
174 | ].join(':'));
175 | }
176 |
177 | redisClient.zadd(zaddCommands, function(err, result){
178 | if (err){
179 | console.log('failed zadd ' + JSON.stringify(err));
180 | return;
181 | }
182 | console.log('successfully converted pending blocks to block candidates');
183 | });
184 |
185 | });
186 |
187 | });
188 | }
189 | ], function(){
190 | process.exit();
191 | });
--------------------------------------------------------------------------------
/website/admin.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
69 | You can Download
70 | and run cryptonote-easy-miner
71 | which will automatically generate your wallet address and run CPUMiner with the proper parameters.
72 |