forked from melpon/wandbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlockChainHash
86 lines (78 loc) · 3.22 KB
/
BlockChainHash
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
// This file is a simple demonstration on how blockchain works
#include <iostream>
#include <string>
#include <memory>
#include <ostream>
class Block
{
private:
std::string transaction;
std::size_t hashTrans;
public:
Block()
{
std::hash<std::string> cnstrctHsh;
hashTrans = cnstrctHsh("hello");
}
Block(const std::string& trans) : transaction(trans)
{
std::hash<std::string> cnstrctHsh;
hashTrans = cnstrctHsh(transaction);
}
Block(const std::string& trans, const Block& blck) : transaction(trans)
{
std::hash<std::string> cnstrctHsh;
transaction += blck.getTransact(); //Append the previous transaction to allow chaining
hashTrans = (cnstrctHsh(transaction))|blck.getHash();
}
Block(const Block& blck)
{
std::cout<<"Copy constructor called "<<std::endl;
this->transaction = blck.getTransact();
std::hash<std::string> cnstrctHsh;
hashTrans = (cnstrctHsh(transaction))^blck.getHash();
}
std::size_t getHash() const
{
return(this->hashTrans);
}
std::string getTransact() const
{
return(this->transaction);
}
void printHash(void)
{
std::cout<<"The Transaction is: "<<transaction<<std::endl;
std::cout<<"The Hash number is: "<<hashTrans<<std::endl;
}
friend std::ostream& operator <<(std::ostream& out, const Block& inBclk); //Remember ostream always comes first in the operand signatures
};
std::ostream& operator<<(std::ostream& out, const Block& inBclk)
{
std::cout<<"\n\n";
out<<"The Transaction is: "<<inBclk.getTransact()<<"\n";
out<<"The Hash number is: "<<inBclk.getHash()<<std::endl;
return out;
}
int main()
{
std::cout << "Hello, Wandbox time to blockchain" << std::endl;
std::string trans0 {"block0"};
std::string trans1 {"block1"};
std::string trans2 {"block2"};
std::string trans3 {"block3"};
std::string trans4 {"block4"};
std::string trans5 {"block5"};
std::unique_ptr<Block> uPtr0 = std::make_unique<Block>(trans0);
std::unique_ptr<Block> uPtr1 = std::make_unique<Block>(trans1, *uPtr0) ;
std::unique_ptr<Block> uPtr2 = std::make_unique<Block>(trans2, *uPtr1);
std::unique_ptr<Block> uPtr3 = std::make_unique<Block>(trans3, *uPtr2);
std::unique_ptr<Block> uPtr4 = std::make_unique<Block>(trans4, *uPtr3);
std::unique_ptr<Block> uPtr5 = std::make_unique<Block>(trans5, *uPtr4);
std::unique_ptr<Block*> uPtr6 = std::make_unique<Block*>(uPtr5.release()); //potential memory leak
Block * memLk = *uPtr6; //memory leak detected
std::cout<<"\nPrinting object: "<< *memLk << std::endl;
if(*uPtr6 != 0)
(*(*uPtr6)).printHash();
delete memLk;
}