Repository navigation
Expand file tree
/
Copy pathBlockchain.hpp
More file actions
123 lines (97 loc) · 3.84 KB
/
Copy pathBlockchain.hpp
File metadata and controls
123 lines (97 loc) · 3.84 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#ifndef BLOCKCHAIN_HPP_
#define BLOCKCHAIN_HPP_
#include "Transaction.hpp"
#include "Block.hpp"
#include "nlohmann/json.hpp"
#include <filesystem>
#include <fstream>
class Blockchain
{
private:
std::uint32_t difficulty = 3;
std::uint32_t maxTransactionsCount = 2;
std::vector<Block> chain;
Block createGenesis()
{
return Block(0, sha256("Genesis"), difficulty, maxTransactionsCount,{0.0, "Genesis", "Genesis"});
}
public:
Blockchain(std::uint32_t difficulty, std::uint32_t maxTransactionsCount)
{
this->difficulty = difficulty;
this->maxTransactionsCount = maxTransactionsCount;
chain.push_back(createGenesis());
}
Blockchain(const std::string filename = "blockchain.json")
{
load(filename);
}
void push_transaction(Transaction transaction)
{
if(chain.back().push_transaction(transaction))
return;
chain.back().mine(difficulty);
chain.push_back({chain.size(), chain.back().calculateHash(), difficulty, maxTransactionsCount, transaction});
}
bool validate()
{
std::vector<Block>::iterator it;
for(it = chain.begin(); it != chain.end(); ++it)
{
Block & currentBlock = *it;
if(it == (chain.end() - 1))
continue;
if(!currentBlock.validate())
{
std::cout << "Block " << currentBlock << " is invalid\n";
return false;
}
if(chain.size() > 1 && it != chain.begin())
{
Block previousBlock = *(it-1);
if(currentBlock.previousHash() != previousBlock.calculateHash())
{
std::cout << "Block " << currentBlock << " is invalid\n";
return false;
}
}
}
return true;
}
std::string dump(std::int16_t indent = 4) const
{
//std::for_each(chain.begin(), chain.end(), std::bind(std::mem_fn(&Block::dump),std::placeholders::_1));
nlohmann::json output_json = {{"difficulty", difficulty}, {"maxTransactionsCount",maxTransactionsCount}, {"blocks", nlohmann::json::array({})}};
std::for_each(chain.begin(), chain.end(), std::bind(std::mem_fn(&Block::dump), std::placeholders::_1, std::ref(output_json)));
return output_json.dump(indent);
}
void save(const std::string output_filename = "blockchain.json", const std::int16_t indent = 4)
{
std::filesystem::path output_path = std::filesystem::path(std::filesystem::current_path() / "blockchain" / output_filename);
if(!std::filesystem::exists(output_path.parent_path()))
std::filesystem::create_directories(output_path.parent_path());
std::ofstream output_file(output_path);
output_file << dump();
}
void save(const std::int16_t indent, const std::string output_filename = "blockchain.json")
{
save(output_filename, indent);
}
void load(const std::string input_filename = "blockchain.json")
{
std::filesystem::path input_path = std::filesystem::path(std::filesystem::current_path() / "blockchain" / input_filename);
if(!std::filesystem::exists(input_path))
{
std::cerr << "Blockchain file does not exists!" << std::endl;
return;
}
nlohmann::json input_json;
std::ifstream input_file(input_path);
input_file >> input_json;
this->difficulty = input_json["difficulty"];
this->maxTransactionsCount = input_json["maxTransactionsCount"];
for(const auto & block_json : input_json["blocks"])
chain.push_back(Block(block_json));
}
};
#endif