-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockchain.cpp
More file actions
81 lines (67 loc) · 1.84 KB
/
Blockchain.cpp
File metadata and controls
81 lines (67 loc) · 1.84 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "Blockchain.h"
Blockchain::Blockchain(Block genericBlock)
{
//Если создаём блокчейн с нуля
if(genericBlock.index() == -1)
{
genericBlock.setIndex(0);
genericBlock.setNonce(0);
genericBlock.setData("Hello im first block");
genericBlock.setPrevHash(QByteArray());//Эквивалент нулю
m_blockChain.insert(genericBlock.index(),genericBlock);
m_lastIndex = genericBlock.index();
}
else//Случай когда используем создание не с нулевого блока
{
}
}
Blockchain::~Blockchain()
{
}
int Blockchain::size() const
{
return m_blockChain.size();
}
int Blockchain::lastBlockIndex() const
{
return m_lastIndex;
}
Block Blockchain::genericBlock() const
{
int genericBlock = 0;
return m_blockChain.value(genericBlock);
}
QByteArray Blockchain::lastBlockHash() const
{
return m_blockChain.value(m_lastIndex).hash();
}
Block Blockchain::lastBlock() const
{
return m_blockChain.value(m_lastIndex);
}
Block Blockchain::blockAt(int index) const
{
return m_blockChain.value(index);
}
//Добавляет блок по индексу
void Blockchain::append(const Block &block)
{
//checkBlock фнукция проверки блока на удовлетворение условиям
if(!m_blockChain.contains(block.index()))
{
//проверка что предыдущий хэш нового блока совпадает с хэшем последнего блока
if (lastBlockHash() == block.prevHash())
{
m_blockChain.insert(block.index(),block);
m_lastIndex = block.index();
}
}
}
void Blockchain::setDifficulty(int difficulty)
{
m_difficulty = difficulty;
}
int Blockchain::difficulty() const
{
return m_difficulty;
}