aboutsummaryrefslogtreecommitdiff
path: root/src/util/gnunet-scrypt.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2016-04-30 08:17:37 +0000
committerChristian Grothoff <christian@grothoff.org>2016-04-30 08:17:37 +0000
commit95f9076a2139f5fb042b944a0658b6cda2fa35db (patch)
treeb0826a2a1dcf812e6b4450fe6b05d47cd53ae49d /src/util/gnunet-scrypt.c
parent7746f68db77b9ca3c4aaca24ab2ce5253461240b (diff)
downloadgnunet-95f9076a2139f5fb042b944a0658b6cda2fa35db.tar.gz
gnunet-95f9076a2139f5fb042b944a0658b6cda2fa35db.zip
implementing new scheduler shutdown semantics
Diffstat (limited to 'src/util/gnunet-scrypt.c')
-rw-r--r--src/util/gnunet-scrypt.c88
1 files changed, 54 insertions, 34 deletions
diff --git a/src/util/gnunet-scrypt.c b/src/util/gnunet-scrypt.c
index 7449d8042..6fd871cbe 100644
--- a/src/util/gnunet-scrypt.c
+++ b/src/util/gnunet-scrypt.c
@@ -40,7 +40,7 @@ static struct GNUNET_CRYPTO_EddsaPublicKey pub;
40 40
41static uint64_t proof; 41static uint64_t proof;
42 42
43static struct GNUNET_SCHEDULER_Task * proof_task; 43static struct GNUNET_SCHEDULER_Task *proof_task;
44 44
45static const struct GNUNET_CONFIGURATION_Handle *cfg; 45static const struct GNUNET_CONFIGURATION_Handle *cfg;
46 46
@@ -51,15 +51,21 @@ static char *pwfn;
51 51
52/** 52/**
53 * Write our current proof to disk. 53 * Write our current proof to disk.
54 *
55 * @param cls closure
54 */ 56 */
55static void 57static void
56write_proof () 58shutdown_task (void *cls)
57{ 59{
58 if (sizeof (proof) != 60 if (sizeof (proof) !=
59 GNUNET_DISK_fn_write (pwfn, &proof, sizeof (proof), 61 GNUNET_DISK_fn_write (pwfn,
62 &proof,
63 sizeof (proof),
60 GNUNET_DISK_PERM_USER_READ | 64 GNUNET_DISK_PERM_USER_READ |
61 GNUNET_DISK_PERM_USER_WRITE)) 65 GNUNET_DISK_PERM_USER_WRITE))
62 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", proof); 66 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
67 "write",
68 proof);
63} 69}
64 70
65 71
@@ -120,15 +126,10 @@ find_proof (void *cls)
120 unsigned int i; 126 unsigned int i;
121 struct GNUNET_TIME_Absolute timestamp; 127 struct GNUNET_TIME_Absolute timestamp;
122 struct GNUNET_TIME_Relative elapsed; 128 struct GNUNET_TIME_Relative elapsed;
123 const struct GNUNET_SCHEDULER_TaskContext *tc;
124 129
125 tc = GNUNET_SCHEDULER_get_task_context (); 130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
126 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason)) 131 "Got Proof of Work %llu\n",
127 { 132 proof);
128 write_proof ();
129 return;
130 }
131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got Proof of Work %llu\n", proof);
132 proof_task = NULL; 133 proof_task = NULL;
133 memcpy (&buf[sizeof (uint64_t)], &pub, 134 memcpy (&buf[sizeof (uint64_t)], &pub,
134 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)); 135 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey));
@@ -144,7 +145,7 @@ find_proof (void *cls)
144 proof = counter; 145 proof = counter;
145 FPRINTF (stdout, "Proof of work found: %llu!\n", 146 FPRINTF (stdout, "Proof of work found: %llu!\n",
146 (unsigned long long) proof); 147 (unsigned long long) proof);
147 write_proof (); 148 GNUNET_SCHEDULER_shutdown ();
148 return; 149 return;
149 } 150 }
150 counter++; 151 counter++;
@@ -162,16 +163,16 @@ find_proof (void *cls)
162 (unsigned long long) counter); 163 (unsigned long long) counter);
163 /* remember progress every 100 rounds */ 164 /* remember progress every 100 rounds */
164 proof = counter; 165 proof = counter;
165 write_proof (); 166 shutdown_task (NULL);
166 } 167 }
167 else 168 else
168 { 169 {
169 proof = counter; 170 proof = counter;
170 } 171 }
171 proof_task = 172 proof_task =
172 GNUNET_SCHEDULER_add_delayed_with_priority (proof_find_delay, 173 GNUNET_SCHEDULER_add_delayed_with_priority (proof_find_delay,
173 GNUNET_SCHEDULER_PRIORITY_IDLE, 174 GNUNET_SCHEDULER_PRIORITY_IDLE,
174 &find_proof, NULL); 175 &find_proof, NULL);
175} 176}
176 177
177 178
@@ -193,17 +194,18 @@ run (void *cls,
193 char *pids; 194 char *pids;
194 195
195 cfg = config; 196 cfg = config;
196
197 /* load proof of work */ 197 /* load proof of work */
198 if (NULL == pwfn) 198 if (NULL == pwfn)
199 { 199 {
200 if (GNUNET_OK != 200 if (GNUNET_OK !=
201 GNUNET_CONFIGURATION_get_value_filename (cfg, "NSE", 201 GNUNET_CONFIGURATION_get_value_filename (cfg,
202 "NSE",
202 "PROOFFILE", 203 "PROOFFILE",
203 &pwfn)) 204 &pwfn))
204 { 205 {
205 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 206 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
206 "NSE", "PROOFFILE"); 207 "NSE",
208 "PROOFFILE");
207 GNUNET_SCHEDULER_shutdown (); 209 GNUNET_SCHEDULER_shutdown ();
208 return; 210 return;
209 } 211 }
@@ -219,19 +221,26 @@ run (void *cls,
219 /* load private key */ 221 /* load private key */
220 if (NULL == pkfn) 222 if (NULL == pkfn)
221 { 223 {
222 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "PEER", 224 if (GNUNET_OK !=
223 "PRIVATE_KEY", 225 GNUNET_CONFIGURATION_get_value_filename (cfg,
224 &pkfn)) 226 "PEER",
227 "PRIVATE_KEY",
228 &pkfn))
225 { 229 {
226 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 230 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
227 "PEER", "PRIVATE_KEY"); 231 "PEER",
232 "PRIVATE_KEY");
228 return; 233 return;
229 } 234 }
230 } 235 }
231 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Private Key file: %s\n", pkfn); 236 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
237 "Private Key file: %s\n",
238 pkfn);
232 if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create_from_file (pkfn))) 239 if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create_from_file (pkfn)))
233 { 240 {
234 FPRINTF (stderr, _("Loading hostkey from `%s' failed.\n"), pkfn); 241 FPRINTF (stderr,
242 _("Loading hostkey from `%s' failed.\n"),
243 pkfn);
235 GNUNET_free (pkfn); 244 GNUNET_free (pkfn);
236 return; 245 return;
237 } 246 }
@@ -248,22 +257,28 @@ run (void *cls,
248 if (0 == nse_work_required) 257 if (0 == nse_work_required)
249 { 258 {
250 if (GNUNET_OK != 259 if (GNUNET_OK !=
251 GNUNET_CONFIGURATION_get_value_number (cfg, "NSE", "WORKBITS", 260 GNUNET_CONFIGURATION_get_value_number (cfg,
261 "NSE",
262 "WORKBITS",
252 &nse_work_required)) 263 &nse_work_required))
253 { 264 {
254 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "NSE", "WORKBITS"); 265 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
266 "NSE",
267 "WORKBITS");
255 GNUNET_SCHEDULER_shutdown (); 268 GNUNET_SCHEDULER_shutdown ();
256 return; 269 return;
257 } 270 }
258 if (nse_work_required >= sizeof (struct GNUNET_HashCode) * 8) 271 if (nse_work_required >= sizeof (struct GNUNET_HashCode) * 8)
259 { 272 {
260 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, "NSE", "WORKBITS", 273 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
274 "NSE",
275 "WORKBITS",
261 _("Value is too large.\n")); 276 _("Value is too large.\n"));
262 GNUNET_SCHEDULER_shutdown (); 277 GNUNET_SCHEDULER_shutdown ();
263 return; 278 return;
264 } else if (0 == nse_work_required) 279 }
280 else if (0 == nse_work_required)
265 { 281 {
266 write_proof ();
267 GNUNET_SCHEDULER_shutdown (); 282 GNUNET_SCHEDULER_shutdown ();
268 return; 283 return;
269 } 284 }
@@ -275,8 +290,12 @@ run (void *cls,
275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
276 "Delay between tries: %s\n", 291 "Delay between tries: %s\n",
277 GNUNET_STRINGS_relative_time_to_string (proof_find_delay, 1)); 292 GNUNET_STRINGS_relative_time_to_string (proof_find_delay, 1));
278 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE, 293 proof_task =
279 &find_proof, NULL); 294 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
295 &find_proof,
296 NULL);
297 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
298 NULL);
280} 299}
281 300
282 301
@@ -307,7 +326,8 @@ main (int argc, char *const *argv)
307 }; 326 };
308 int ret; 327 int ret;
309 328
310 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) 329 if (GNUNET_OK !=
330 GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
311 return 2; 331 return 2;
312 332
313 ret = (GNUNET_OK == 333 ret = (GNUNET_OK ==