-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlock.cpp
More file actions
57 lines (45 loc) · 1.45 KB
/
Block.cpp
File metadata and controls
57 lines (45 loc) · 1.45 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
#include "Block.h"
#include "sha256.h"
#include <ctime>
#include <sstream>
using namespace std;
namespace myCoin{
Transaction::Transaction(const std::string& name,double amount)
: name(name),amount(amount){
time_t currTime = time(nullptr);
timestamp = ctime(&currTime);
}
ostream& operator<<(ostream& os, const Transaction& trans){
os << " Name: " << trans.name << endl;
os << " Amount: " << trans.amount << endl;
os << " TimeStamp:" << trans.timestamp << endl;
return os;
}
Block::Block(size_t index, const string& previousHash, const string& name,double amount)
: index(index), previousHash(previousHash), nonce(0), data(name,amount),
blockHash(calculateHash()){}
const string Block::calculateHash() const {
stringstream blockInfo;
blockInfo << index << data.amount << data.name
<< data.timestamp << nonce;
return sha256(blockInfo.str());
}
void Block::mineBlock(size_t difficulty){
string subString(difficulty, '0');
while(blockHash.substr(0,difficulty) != subString){
blockHash = calculateHash();
nonce++;
}
}
ostream& operator<<(ostream& os, const Block& currBlock){
//check for genesis block
if(currBlock.index != 0){
os << "Block Number: " << currBlock.index << endl;
os << "Block Hash: " << currBlock.blockHash << endl;
os << "Previous Hash: " << currBlock.previousHash << endl;
os << "Nonce: " << currBlock.nonce << endl;
os << " TRANSACTION \n" << currBlock.data;
}
return os;
}
}