work2d.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright 2013-2014 Luke Dashjr
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include "config.h"
  10. #include <stdbool.h>
  11. #include <stdint.h>
  12. #include <string.h>
  13. #include "miner.h"
  14. #include "util.h"
  15. #define MAX_DIVISIONS 255
  16. static bool work2d_reserved[MAX_DIVISIONS + 1] = { true };
  17. int work2d_xnonce1sz;
  18. int work2d_xnonce2sz;
  19. void work2d_init()
  20. {
  21. RUNONCE();
  22. for (uint64_t n = MAX_DIVISIONS; n; n >>= 8)
  23. ++work2d_xnonce1sz;
  24. work2d_xnonce2sz = 2;
  25. }
  26. bool reserve_work2d_(uint32_t * const xnonce1_p)
  27. {
  28. uint32_t xnonce1;
  29. for (xnonce1 = MAX_DIVISIONS; work2d_reserved[xnonce1]; --xnonce1)
  30. if (!xnonce1)
  31. return false;
  32. work2d_reserved[xnonce1] = true;
  33. *xnonce1_p = htole32(xnonce1);
  34. return true;
  35. }
  36. void release_work2d_(uint32_t xnonce1)
  37. {
  38. xnonce1 = le32toh(xnonce1);
  39. work2d_reserved[xnonce1] = false;
  40. }
  41. void work2d_gen_dummy_work(struct work * const work, struct stratum_work * const swork, const struct timeval * const tvp_prepared, const void * const xnonce2, const uint32_t xnonce1)
  42. {
  43. uint8_t *p, *s;
  44. *work = (struct work){
  45. .pool = swork->pool,
  46. .work_restart_id = swork->work_restart_id,
  47. .tv_staged = *tvp_prepared,
  48. };
  49. bytes_resize(&work->nonce2, swork->n2size);
  50. s = bytes_buf(&work->nonce2);
  51. p = &s[swork->n2size - work2d_xnonce2sz];
  52. if (xnonce2)
  53. memcpy(p, xnonce2, work2d_xnonce2sz);
  54. #ifndef __OPTIMIZE__
  55. else
  56. memset(p, '\0', work2d_xnonce2sz);
  57. #endif
  58. p -= work2d_xnonce1sz;
  59. memcpy(p, &xnonce1, work2d_xnonce1sz);
  60. if (p != s)
  61. memset(s, '\xbb', p - s);
  62. gen_stratum_work2(work, swork);
  63. }
  64. bool work2d_submit_nonce(struct thr_info * const thr, struct stratum_work * const swork, const struct timeval * const tvp_prepared, const void * const xnonce2, const uint32_t xnonce1, const uint32_t nonce, const uint32_t ntime, bool * const out_is_stale)
  65. {
  66. struct work _work, *work;
  67. bool rv;
  68. // Generate dummy work
  69. work = &_work;
  70. work2d_gen_dummy_work(work, swork, tvp_prepared, xnonce2, xnonce1);
  71. *(uint32_t *)&work->data[68] = htobe32(ntime);
  72. // Check if it's stale, if desired
  73. if (out_is_stale)
  74. *out_is_stale = stale_work(work, true);
  75. // Submit nonce
  76. rv = submit_nonce(thr, work, nonce);
  77. clean_work(work);
  78. return rv;
  79. }