work2d.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #include "work2d.h"
  16. #define MAX_DIVISIONS WORK2D_MAX_DIVISIONS
  17. static bool work2d_reserved[MAX_DIVISIONS + 1] = { true };
  18. int work2d_xnonce1sz;
  19. int work2d_xnonce2sz;
  20. void work2d_init()
  21. {
  22. RUNONCE();
  23. for (uint64_t n = MAX_DIVISIONS; n; n >>= 8)
  24. ++work2d_xnonce1sz;
  25. work2d_xnonce2sz = 2;
  26. }
  27. bool reserve_work2d_(uint32_t * const xnonce1_p)
  28. {
  29. uint32_t xnonce1;
  30. for (xnonce1 = MAX_DIVISIONS; work2d_reserved[xnonce1]; --xnonce1)
  31. if (!xnonce1)
  32. return false;
  33. work2d_reserved[xnonce1] = true;
  34. *xnonce1_p = htole32(xnonce1);
  35. return true;
  36. }
  37. void release_work2d_(uint32_t xnonce1)
  38. {
  39. xnonce1 = le32toh(xnonce1);
  40. work2d_reserved[xnonce1] = false;
  41. }
  42. int work2d_pad_xnonce_size(const struct stratum_work * const swork)
  43. {
  44. return swork->n2size - work2d_xnonce1sz - work2d_xnonce2sz;
  45. }
  46. void *work2d_pad_xnonce(void * const buf_, const struct stratum_work * const swork, const bool hex)
  47. {
  48. uint8_t * const buf = buf_;
  49. int pad = work2d_pad_xnonce_size(swork);
  50. if (pad < 0)
  51. return NULL;
  52. if (hex)
  53. {
  54. pad *= 2;
  55. memset(buf, 'b', pad);
  56. }
  57. else
  58. memset(buf, '\xbb', pad);
  59. return &buf[pad];
  60. }
  61. 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)
  62. {
  63. uint8_t *p, *s;
  64. *work = (struct work){
  65. .pool = swork->pool,
  66. .work_restart_id = swork->work_restart_id,
  67. .tv_staged = *tvp_prepared,
  68. };
  69. bytes_resize(&work->nonce2, swork->n2size);
  70. s = bytes_buf(&work->nonce2);
  71. p = &s[swork->n2size - work2d_xnonce2sz];
  72. if (xnonce2)
  73. memcpy(p, xnonce2, work2d_xnonce2sz);
  74. #ifndef __OPTIMIZE__
  75. else
  76. memset(p, '\0', work2d_xnonce2sz);
  77. #endif
  78. p -= work2d_xnonce1sz;
  79. memcpy(p, &xnonce1, work2d_xnonce1sz);
  80. work2d_pad_xnonce(s, swork, false);
  81. gen_stratum_work2(work, swork);
  82. }
  83. 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)
  84. {
  85. struct work _work, *work;
  86. bool rv;
  87. // Generate dummy work
  88. work = &_work;
  89. work2d_gen_dummy_work(work, swork, tvp_prepared, xnonce2, xnonce1);
  90. *(uint32_t *)&work->data[68] = htobe32(ntime);
  91. work->nonce_diff = nonce_diff;
  92. // Check if it's stale, if desired
  93. if (out_is_stale)
  94. *out_is_stale = stale_work(work, true);
  95. // Submit nonce
  96. rv = submit_nonce(thr, work, nonce);
  97. clean_work(work);
  98. return rv;
  99. }