Guest User

Untitled

a guest
Dec 9th, 2019
109
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <list>
  3. #include <vector>
  4. #include <ctime>
  5.  
  6. using namespace std;
  7.  
  8. string zeros = "00000000000000";
  9.  
  10. struct block {
  11. size_t id;
  12. string timestamp;
  13. string data;
  14. size_t nonce;
  15. size_t previous_hash;
  16. };
  17.  
  18. void find_nonce (block &a_block, int difficulty){
  19. size_t hr = hash_combined(a_block);
  20.  
  21. while (true){
  22. string hrs = to_string(hr);
  23. if (hrs.compare(hrs.size() - difficulty, difficulty, zeros, 0, difficulty) == 0 )
  24. break;
  25. a_block.nonce++;
  26. hr = hash_combined(a_block);
  27. }
  28. }
  29.  
  30. void check_valid_blockchain (list <block>, &chain){
  31.  
  32.  
  33. }
  34.  
  35. int main(){
  36. list <block> blockchain;
  37. int difficulty = 7;
  38.  
  39. vector<string> texts = {"A pays B 10",
  40. "B pays C 20",
  41. "C pays D 30",
  42. };
  43.  
  44. time_t now = time(0);
  45.  
  46. struct tm *timeinfo = localtime(&now);
  47. char buffer[80];
  48. strftime(buffer, 80, "%F%T", timeinfo);
  49. block genesis_block = {0, buffer, "GENESIS BLOCK", 0, 0};
  50. find_nonce(genesis_block, difficulty);
  51. blockchain.push_back(genesis_block);
  52.  
  53. block previous_block = genesis_block;
  54.  
  55. for (int i=0; i < texts.size(); i++ ){
  56. block a_block;
  57. //now?
  58. now = time(0);
  59. timeinfo = localtime(&now);
  60. strftime(buffer, 80, "%F%T", timeinfo);
  61. a_block.timestamp = buffer;
  62. a_block.data = texts[i];
  63. a_block.previous_hash = hash_combined(previous_block);
  64. find_nonce(a_block, difficulty);
  65. blockchain.push_back(a_block);
  66. print_block(a_block);
  67. }
  68. }
RAW Paste Data