-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchain.hpp
More file actions
56 lines (42 loc) · 1.61 KB
/
chain.hpp
File metadata and controls
56 lines (42 loc) · 1.61 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
// SPDX-License-Identifier: GPLv3-or-later
// Copyright © 2020 Or Toledano
#pragma once
#include "block.hpp"
#include "../hash/uhash.hpp"
#include <iostream>
#include <list>
#include <memory>
using std::list;
using tensorcoin::hash::UHash;
namespace tensorcoin::blockchain {
class Chain {
private:
const int init_target;
int target; // Number of required zeros at the start of the hash, >=1
const int blocksToIncTarget; // After blocksToIncTarget>=1 blocks we
// should increase the target by 1
const int version = 4;
list<Block> blockList; // Mined blocks
// Hardware/implementation polymorphism
std::unique_ptr<UHash> uHash = UHash::make_uhash();
friend std::ostream &operator<<(std::ostream &os, const Chain &c);
friend class AuthWallet;
public:
// Does the string start with target leading zeros
static bool isGoldenHash(const string &s, int target) noexcept;
Chain(int target, int blocksToIncTarget);
[[nodiscard]] int getTarget() const;
[[nodiscard]] int getBlocksToChangeTarget() const;
void mineAddBlock(string data, string claimer);
// Add a block to the chain, WITHOUT verifying it
template<typename B>
requires std::is_same_v<Block,
std::remove_cv_t<std::remove_reference_t<B>>>
void addBlock(B &&b) {
blockList.emplace_back(std::forward<B>(b));
}
[[nodiscard]] size_t size() const;
// Use the chain-specific hash on the block
[[nodiscard]] string hashBlock(const Block &s) const;
};
}