Repository navigation
Expand file tree
/
Copy pathblock.h
More file actions
46 lines (38 loc) · 1.49 KB
/
Copy pathblock.h
File metadata and controls
46 lines (38 loc) · 1.49 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
#pragma once
#include <string>
#include <vector>
#include <memory>
#include "transaction.h"
#include "merkletree.h"
#include <nlohmann/json.hpp>
class Block {
public:
Block(int index, const std::vector<Transaction>& transactions, const std::string& previousHash);
Block(const nlohmann::json& json);
std::string calculateHash() const;
void mineBlock(int difficulty);
bool isValid() const;
// Getters
int getIndex() const { return index_; }
std::string getTimestamp() const { return timestamp_; }
std::string getPreviousHash() const { return previousHash_; }
std::string getHash() const { return hash_; }
int getNonce() const { return nonce_; }
std::string getMerkleRoot() const { return merkleRoot_; }
const std::vector<Transaction>& getTransactions() const { return transactions_; }
const std::map<std::string, double>& getBalanceChanges() const { return balanceChanges_; }
void setBalanceChanges(const std::map<std::string, double>& balanceChanges) { balanceChanges_ = balanceChanges; }
// 将区块转换为 JSON 字符串
std::string toJson() const;
bool verifyDifficulty(int difficulty) const;
private:
std::map<std::string, double> balanceChanges_; // 记录每个地址的余额变更
int index_;
std::string timestamp_;
std::vector<Transaction> transactions_;
std::string previousHash_;
std::string hash_;
int nonce_;
std::string merkleRoot_;
static std::string sha256(const std::string& str);
};