work2d.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 char * const extranonce2, 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 (extranonce2)
  53. hex2bin(p, extranonce2, 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. }