Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <string.h>
- #include "blockchain.h"
- #include "hashset.h"
- #include "hash.h"
- // compile code with:
- // gcc main.c blockchain.c hashset.c vector.c hash.c
- // run as:
- // a.out
- VectorMapFunction mapFn(void * pt, void * auxData){
- Block * tmp1 = (Block*) pt;
- custom * tmp2 = (custom*) auxData;
- if(tmp2->isFirst == 1){
- tmp2->isFirst = 0;
- } else {
- if(BlockHash(tmp2->block) != tmp1->previousHash){
- tmp2->answ = 0;
- } else {
- tmp2->block = tmp1;
- }
- }
- }
- void addBlock(BlockChain *bc, Block *b) {
- VectorAppend(bc, b);
- }
- // returns 1 if valid else 0
- // block's hash is calculated by BlockHash function from hash.h
- // the first block's previousHash value has no meaning!
- // don't use loop, use map!
- int isValid(BlockChain *bc) {
- custom * tmp = malloc(sizeof(custom));
- tmp->block = VectorNth(bc, 0);
- tmp->isFirst = 1;
- tmp->answ = 1;
- VectorMap(bc, mapFn, tmp);
- return tmp->answ;
- }
- typedef struct {
- int amount;
- char *name;
- } hashElem;
- // use for hashset!
- static const signed long kHashMultiplier = -1664117991L;
- static int StringHash(const void *elem, int numBuckets)
- {
- const char *s = ((hashElem *)elem)->name;
- unsigned long hashcode = 0;
- for (int i = 0; i < strlen(s); i++)
- hashcode = hashcode * kHashMultiplier + tolower(s[i]);
- return hashcode % numBuckets;
- }
- int compFn(const void * elem1, const void * elem2){
- hashElem * tmp1 = (hashElem*) elem1;
- hashElem * tmp2 = (hashElem*) elem2;
- return strcmp(tmp1->name, tmp2->name);
- }
- HashSetMapFunction hashMapFn(void * pt, void * auxData){
- hashElem * tmp1 = (hashElem*)pt;
- hashElem * tmp2 = (hashElem*)auxData;
- if(strcmp(tmp2->name, "lashiko") == 0 && tmp2->amount == -999){
- tmp2->name = tmp1->name;
- tmp2->amount = tmp1->amount;
- } else {
- if(tmp1->amount > tmp2->amount){
- tmp2->name = tmp1->name;
- tmp2->amount = tmp1->amount;
- }
- }
- }
- // you can use loop here!
- char * getMaxBalance(BlockChain *bc) {
- hashset * hash = malloc(sizeof(hashset));
- HashSetNew(hash, sizeof(hashElem), 1000, StringHash, compFn, NULL);
- char * temporary;
- for(int i = 0; i<VectorLength(bc); i++){
- Block * bl = VectorNth(bc, i);
- vector * vec = bl->transactions;
- for(int j = 0; j<VectorLength(vec); j++){
- Transaction * tr = VectorNth(vec, j);
- temporary = tr->from;
- hashElem * hsh1 = malloc(sizeof(hashElem));
- hsh1->name = tr->from;
- hsh1->amount = 0;
- hashElem * hsh2 = malloc(sizeof(hashElem));
- hsh2->name = tr->to;
- hsh2->amount = 0;
- hashElem * search1 = HashSetLookup(hash, hsh1);
- hashElem * search2 = HashSetLookup(hash, hsh2);
- if(search1 == NULL){
- hsh1->amount -= tr->amount;
- HashSetEnter(hash, hsh1);
- } else {
- search1->amount -= tr->amount;
- }
- if(search2 == NULL){
- hsh2->amount += tr->amount;
- HashSetEnter(hash, hsh2);
- } else {
- search2->amount += tr->amount;
- }
- }
- }
- hashElem * result = malloc(sizeof(hashElem));
- result->name = "lashiko";
- result->amount = -999;
- HashSetMap(hash, hashMapFn, result);
- return result->name;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement