Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "blockchain.h"
  4. #include "hashset.h"
  5. #include "hash.h"
  6.  
  7. // compile code with:
  8. // gcc main.c blockchain.c hashset.c vector.c hash.c
  9. // run as:
  10. // a.out
  11.  
  12.  
  13.  
  14. VectorMapFunction mapFn(void * pt, void * auxData){
  15. Block * tmp1 = (Block*) pt;
  16. custom * tmp2 = (custom*) auxData;
  17. if(tmp2->isFirst == 1){
  18. tmp2->isFirst = 0;
  19. } else {
  20. if(BlockHash(tmp2->block) != tmp1->previousHash){
  21. tmp2->answ = 0;
  22. } else {
  23. tmp2->block = tmp1;
  24. }
  25. }
  26. }
  27.  
  28. void addBlock(BlockChain *bc, Block *b) {
  29. VectorAppend(bc, b);
  30. }
  31.  
  32. // returns 1 if valid else 0
  33. // block's hash is calculated by BlockHash function from hash.h
  34. // the first block's previousHash value has no meaning!
  35. // don't use loop, use map!
  36. int isValid(BlockChain *bc) {
  37. custom * tmp = malloc(sizeof(custom));
  38. tmp->block = VectorNth(bc, 0);
  39. tmp->isFirst = 1;
  40. tmp->answ = 1;
  41. VectorMap(bc, mapFn, tmp);
  42. return tmp->answ;
  43. }
  44.  
  45. typedef struct {
  46. int amount;
  47. char *name;
  48. } hashElem;
  49.  
  50. // use for hashset!
  51. static const signed long kHashMultiplier = -1664117991L;
  52. static int StringHash(const void *elem, int numBuckets)
  53. {
  54. const char *s = ((hashElem *)elem)->name;
  55. unsigned long hashcode = 0;
  56. for (int i = 0; i < strlen(s); i++)
  57. hashcode = hashcode * kHashMultiplier + tolower(s[i]);
  58. return hashcode % numBuckets;
  59. }
  60.  
  61. int compFn(const void * elem1, const void * elem2){
  62. hashElem * tmp1 = (hashElem*) elem1;
  63. hashElem * tmp2 = (hashElem*) elem2;
  64. return strcmp(tmp1->name, tmp2->name);
  65. }
  66.  
  67. HashSetMapFunction hashMapFn(void * pt, void * auxData){
  68. hashElem * tmp1 = (hashElem*)pt;
  69. hashElem * tmp2 = (hashElem*)auxData;
  70.  
  71. if(strcmp(tmp2->name, "lashiko") == 0 && tmp2->amount == -999){
  72. tmp2->name = tmp1->name;
  73. tmp2->amount = tmp1->amount;
  74. } else {
  75. if(tmp1->amount > tmp2->amount){
  76. tmp2->name = tmp1->name;
  77. tmp2->amount = tmp1->amount;
  78. }
  79. }
  80. }
  81.  
  82. // you can use loop here!
  83. char * getMaxBalance(BlockChain *bc) {
  84. hashset * hash = malloc(sizeof(hashset));
  85. HashSetNew(hash, sizeof(hashElem), 1000, StringHash, compFn, NULL);
  86. char * temporary;
  87. for(int i = 0; i<VectorLength(bc); i++){
  88. Block * bl = VectorNth(bc, i);
  89. vector * vec = bl->transactions;
  90. for(int j = 0; j<VectorLength(vec); j++){
  91. Transaction * tr = VectorNth(vec, j);
  92. temporary = tr->from;
  93. hashElem * hsh1 = malloc(sizeof(hashElem));
  94. hsh1->name = tr->from;
  95. hsh1->amount = 0;
  96. hashElem * hsh2 = malloc(sizeof(hashElem));
  97. hsh2->name = tr->to;
  98. hsh2->amount = 0;
  99. hashElem * search1 = HashSetLookup(hash, hsh1);
  100. hashElem * search2 = HashSetLookup(hash, hsh2);
  101. if(search1 == NULL){
  102. hsh1->amount -= tr->amount;
  103. HashSetEnter(hash, hsh1);
  104. } else {
  105. search1->amount -= tr->amount;
  106. }
  107. if(search2 == NULL){
  108. hsh2->amount += tr->amount;
  109. HashSetEnter(hash, hsh2);
  110. } else {
  111. search2->amount += tr->amount;
  112. }
  113. }
  114. }
  115. hashElem * result = malloc(sizeof(hashElem));
  116. result->name = "lashiko";
  117. result->amount = -999;
  118. HashSetMap(hash, hashMapFn, result);
  119. return result->name;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement