work2d.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. int work2d_pad_xnonce_size(const struct stratum_work * const swork)
  42. {
  43. return swork->n2size - work2d_xnonce1sz - work2d_xnonce2sz;
  44. }
  45. void *work2d_pad_xnonce(void * const buf_, const struct stratum_work * const swork, const bool hex)
  46. {
  47. uint8_t * const buf = buf_;
  48. int pad = work2d_pad_xnonce_size(swork);
  49. if (pad < 0)
  50. return NULL;
  51. if (hex)
  52. {
  53. pad *= 2;
  54. memset(buf, 'b', pad);
  55. }
  56. else
  57. memset(buf, '\xbb', pad);
  58. return &buf[pad];
  59. }
  60. 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)
  61. {
  62. uint8_t *p, *s;
  63. *work = (struct work){
  64. .pool = swork->pool,
  65. .work_restart_id = swork->work_restart_id,
  66. .tv_staged = *tvp_prepared,
  67. };
  68. bytes_resize(&work->nonce2, swork->n2size);
  69. s = bytes_buf(&work->nonce2);
  70. p = &s[swork->n2size - work2d_xnonce2sz];
  71. if (xnonce2)
  72. memcpy(p, xnonce2, work2d_xnonce2sz);
  73. #ifndef __OPTIMIZE__
  74. else
  75. memset(p, '\0', work2d_xnonce2sz);
  76. #endif
  77. p -= work2d_xnonce1sz;
  78. memcpy(p, &xnonce1, work2d_xnonce1sz);
  79. work2d_pad_xnonce(s, swork, false);
  80. gen_stratum_work2(work, swork);
  81. }
  82. 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, const float nonce_diff)
  83. {
  84. struct work _work, *work;
  85. bool rv;
  86. // Generate dummy work
  87. work = &_work;
  88. work2d_gen_dummy_work(work, swork, tvp_prepared, xnonce2, xnonce1);
  89. *(uint32_t *)&work->data[68] = htobe32(ntime);
  90. work->nonce_diff = nonce_diff;
  91. // Check if it's stale, if desired
  92. if (out_is_stale)
  93. *out_is_stale = stale_work(work, true);
  94. // Submit nonce
  95. rv = submit_nonce(thr, work, nonce);
  96. clean_work(work);
  97. return rv;
  98. }