-
Notifications
You must be signed in to change notification settings - Fork 1
/
Block.hpp
129 lines (102 loc) · 4.88 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
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
124
125
126
127
128
129
/*
* Copyright (c) (2023) SPHINX_ORG
* Authors:
* - (C kusuma) <[email protected]>
* GitHub: (https://github.com/chykusuma)
* Contributors:
* - (Contributor 1) <[email protected]>
* Github: (https://github.com/yourgit)
* - (Contributor 2) <[email protected]>
* Github: (https://github.com/yourgit)
*/
#ifndef SPHINXBLOCK_HPP
#define SPHINXBLOCK_HPP
#pragma once
#include <cstdint>
#include <iostream>
#include <unordered_map>
#include <stdexcept>
#include <fstream>
#include <iostream>
#include <ctime>
#include <string>
#include <vector>
#include <array>
#include "json.hpp"
#include "Params.hpp"
#include "MerkleBlock.hpp"
using json = nlohmann::json;
// Add the appropriate namespace for the MerkleBlock
using namespace SPHINXMerkleBlock;
// Forward declarations
namespace SPHINXTrx {
class Transaction; // Forward declaration of the Transaction class
}
// Forward declarations
namespace SPHINXChain {
class Chain; // Forward declaration of the Chain class
}
// Forward declarations
namespace SPHINXDb {
class DistributedDb; // Forward declaration of the DistributedDb class
}
namespace SPHINXBlock {
class Block {
private:
// Private member variables
std::string previousHash_; // The hash of the previous block in the blockchain
std::string merkleRoot_; // The Merkle root hash of the transactions in the block
std::string signature_; // The signature of the block
uint32_t blockHeight_; // The position of the block within the blockchain
std::time_t timestamp_; // The time when the block was created
uint32_t nonce_; // A random value used in the mining process to find a valid block hash
uint32_t difficulty_; // A measure of how hard it is to find a valid block hash (mining difficulty)
std::vector<std::string> transactions_; // The list of transactions included in the block
SPHINXChain::Chain* blockchain_; // A pointer to the blockchain (assuming SPHINXChain::Chain is a class)
const std::vector<std::string>& checkpointBlocks_; // Reference to the list of checkpoint blocks
public:
static const uint32_t MAX_BLOCK_SIZE; // Maximum allowed block size in number of transactions
static const uint32_t MAX_TIMESTAMP_OFFSET; // Maximum allowed timestamp difference from current time
// Constructor without checkpointBlocks parameter
Block(const std::string& previousHash);
// Constructor with the addition of checkpointBlocks parameter
Block(const std::string& previousHash, const std::vector<std::string>& checkpointBlocks);
// Function to calculate the hash of the block
std::string calculateBlockHash() const;
// Add a transaction to the list of transactions in the block
void addTransaction(const std::string& transaction);
// Calculate and return the Merkle root of the transactions
std::string calculateMerkleRoot() const;
// Function to sign the Merkle root with SPHINCS+ private key
std::string signMerkleRoot(const SPHINXPrivKey& privateKey, const std::string& merkleRoot);
// Function to store the Merkle root and signature in the header of the block
void storeMerkleRootAndSignature(const std::string& merkleRoot, const std::string& signature);
// Get the hash of the block by calling the calculateBlockHash() function
std::string getBlockHash() const;
// Verify the block's signature and Merkle root
bool verifyBlock(const SPHINXMerkleBlock::SPHINXPubKey& publicKey) const;
// Setters and getters for the remaining member variables
void setMerkleRoot(const std::string& merkleRoot);
void setSignature(const std::string& signature);
void setBlockHeight(uint32_t blockHeight);
void setNonce(uint32_t nonce);
void setDifficulty(uint32_t difficulty);
void setTransactions(const std::vector<std::string>& transactions);
std::string getPreviousHash() const;
std::string getMerkleRoot() const;
std::string getSignature() const;
uint32_t getBlockHeight() const;
std::time_t getTimestamp() const;
uint32_t getNonce() const;
uint32_t getDifficulty() const;
std::vector<std::string> getTransactions() const;
// Block headers
nlohmann::json toJson() const;
void fromJson(const nlohmann::json& blockJson);
bool save(const std::string& filename) const;
static Block load(const std::string& filename);
bool saveToDatabase(SPHINXDb::DistributedDb& distributedDb) const;
static Block loadFromDatabase(const std::string& blockId, SPHINXDb::DistributedDb& distributedDb);
};
} // namespace SPHINXBlock
#endif // SPHINX_BLOCK_HPP