-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockchain.hpp
More file actions
69 lines (61 loc) · 2.01 KB
/
Blockchain.hpp
File metadata and controls
69 lines (61 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#pragma once
#include<iostream>
#include <iomanip>
#include<vector>
#include<string>
#include "block.hpp"
#include<fstream>
#include<time.h>
#include"utils.hpp"
#include"mining.hpp"
using namespace std;
/*
BLOCK:
long long index;
string date_time;
string last_hash;
string hash_;
string data;
->in txt:
1 index date_time last_hash hash data last
*/
void create_block(string ndata){
//first reads the hole file to find the last
ifstream chain("chain.txt");
string index,date_time,last_hash = string(64,'0'),hash_,data,last;
string last_idx="-1";
string last_block_hash = string(64,'0');
vector<string>lines;
//Implementation of earn by mining funcion
cout <<"Write your address: ";
string miner_add;
cin >> miner_add;
int amount = 20; //needs to be variable
string old_miner_add; // Variable to read old miner addresses from file
int old_amount;
while(chain >> index >> date_time >> last_hash >> hash_ >> old_miner_add >> old_amount >> data >> last){
string line = index+" "+date_time+" "+last_hash+" "+hash_+" "+old_miner_add+" "+to_string(old_amount)+" "+data+" ";
if(last == "1"){
line += "0";
last_block_hash = hash_;
last_idx = index;
} else {
line += "0";
}
lines.push_back(line);
}
chain.close();
// Create and mine the new block
long long new_index = stoi(last_idx) + 1;
date_time = to_string(time(0));
Block temp_block(new_index, date_time, last_block_hash, "", ndata,0);
pair<long long, string> mining_result = mine_block(temp_block);
string new_hash = mining_result.second;
string new_block = to_string(new_index)+" "+date_time+" "+last_block_hash+" "+new_hash+" "+miner_add+" "+to_string(amount)+" "+ndata+" "+"1";
lines.push_back(new_block);
//Writes the lines in the file
ofstream chain1("chain.txt");
for(string line : lines){
chain1 << line<<'\n';
}chain1.close();
}