-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock.hpp
63 lines (58 loc) · 1.7 KB
/
block.hpp
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
#pragma once
#include <vector>
#include <map>
#include "transaction.hpp"
#include "constants.hpp"
#include "common.hpp"
#include "crypto.hpp"
#include "helpers.hpp"
#include "merkle_tree.hpp"
using namespace std;
struct BlockHeader {
uint32_t id;
uint64_t timestamp;
uint32_t difficulty;
uint32_t numTransactions;
SHA256Hash lastBlockHash;
SHA256Hash merkleRoot;
SHA256Hash nonce;
};
#define BLOCKHEADER_BUFFER_SIZE 116
BlockHeader blockHeaderFromBuffer(const char* buffer);
void blockHeaderToBuffer(BlockHeader& t, char* buffer);
class Block {
public:
Block();
Block(json data);
Block(const Block& b);
Block(const BlockHeader&b, vector<Transaction>& transactions);
BlockHeader serialize();
json toJson();
void addTransaction(Transaction t);
void setNonce(SHA256Hash s);
void setMerkleRoot(SHA256Hash s);
void setTimestamp(uint64_t t);
void setId(uint32_t id);
void setDifficulty(uint8_t d);
SHA256Hash getHash();
SHA256Hash getNonce();
SHA256Hash getMerkleRoot();
SHA256Hash getLastBlockHash();
void setLastBlockHash(SHA256Hash hash);
uint64_t getTimestamp();
uint32_t getDifficulty() const;
vector<Transaction>& getTransactions();
uint32_t getId();
bool verifyNonce();
// protected:
uint32_t id;
uint64_t timestamp;
uint32_t difficulty;
vector<Transaction> transactions;
SHA256Hash merkleRoot;
SHA256Hash lastBlockHash;
SHA256Hash nonce;
private:
friend bool operator==(const Block& a, const Block& b);
};
bool operator==(const Block& a, const Block& b);