noncedup.c.bak 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright 2014 Andrew Smith
  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 "miner.h"
  10. #include "klist.h"
  11. // Nonce
  12. typedef struct nitem {
  13. uint32_t work_id;
  14. uint32_t nonce;
  15. struct timeval when;
  16. } NITEM;
  17. #define DATAN(_item) ((NITEM *)(_item->data))
  18. struct dupdata {
  19. int timelimit;
  20. K_LIST *nfree_list;
  21. K_STORE *nonce_list;
  22. uint64_t checked;
  23. uint64_t dup;
  24. };
  25. void dupalloc(struct cgpu_info *cgpu, int timelimit)
  26. {
  27. struct dupdata *dup;
  28. dup = calloc(1, sizeof(*dup));
  29. if (unlikely(!dup))
  30. quithere(1, "Failed to calloc dupdata");
  31. dup->timelimit = timelimit;
  32. dup->nfree_list = k_new_list("Nonces", sizeof(NITEM), 1024, 0, true);
  33. dup->nonce_list = k_new_store(dup->nfree_list);
  34. cgpu->dup_data = dup;
  35. }
  36. bool isdupnonce(struct cgpu_info *cgpu, struct work *work, uint32_t nonce)
  37. {
  38. struct dupdata *dup = (struct dupdata *)(cgpu->dup_data);
  39. struct timeval now;
  40. bool unique = true;
  41. K_ITEM *item;
  42. if (!dup)
  43. return false;
  44. cgtime(&now);
  45. dup->checked++;
  46. K_WLOCK(dup->nfree_list);
  47. item = dup->nonce_list->tail;
  48. while (unique && item) {
  49. if (DATAN(item)->work_id == work->id && DATAN(item)->nonce == nonce) {
  50. unique = false;
  51. applog(LOG_WARNING, "%s%d: Duplicate nonce %08x",
  52. cgpu->drv->name, cgpu->device_id, nonce);
  53. } else
  54. item = item->prev;
  55. }
  56. if (unique) {
  57. item = k_unlink_head(dup->nfree_list);
  58. DATAN(item)->work_id = work->id;
  59. DATAN(item)->nonce = nonce;
  60. memcpy(&(DATAN(item)->when), &now, sizeof(now));
  61. k_add_head(dup->nonce_list, item);
  62. }
  63. item = dup->nonce_list->tail;
  64. while (item && tdiff(&(DATAN(item)->when), &now) > dup->timelimit) {
  65. item = k_unlink_tail(dup->nonce_list);
  66. k_add_head(dup->nfree_list, item);
  67. item = dup->nonce_list->tail;
  68. }
  69. K_WUNLOCK(dup->nfree_list);
  70. if (!unique)
  71. dup->dup++;
  72. return !unique;
  73. }