forked from UNOMP/node-multi-hashing
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathblock.h
157 lines (133 loc) · 4.06 KB
/
block.h
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#pragma once
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstdlib>
#include <vector>
#include "transaction.h"
#include "utilstrencodings.h"
#include "serialize.h"
#include "uint256.h"
namespace energi {
struct BlockHeader
{
int32_t nVersion;
uint256 hashPrevBlock;
uint256 hashMerkleRoot;
uint32_t nTime;
uint32_t nBits;
uint32_t nHeight;
uint256 hashMix;
uint64_t nNonce;
BlockHeader()
{
SetNull();
}
ADD_SERIALIZE_METHODS
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
{
READWRITE(this->nVersion);
nVersion = this->nVersion;
READWRITE(hashPrevBlock);
READWRITE(hashMerkleRoot);
READWRITE(nTime);
READWRITE(nBits);
READWRITE(nHeight);
READWRITE(hashMix);
READWRITE(nNonce);
}
void SetNull()
{
nVersion = 0;
hashPrevBlock.SetNull();
hashMerkleRoot.SetNull();
nTime = 0;
nHeight = 0;
hashMix.SetNull();
nNonce = 0;
}
friend std::ostream& operator << (std::ostream& os, const BlockHeader& header)
{
os << "version: " << header.nVersion << " \n"
<< "hashPrevBlock: " << header.hashPrevBlock.ToString() << " \n"
<<" hashMerkleRoot: " << header.hashMerkleRoot.ToString() << " \n"
<< "Time: " << header.nTime << " \n"
<< "Bits: " << header.nBits << " \n"
<< "Height: " << header.nHeight << " \n"
<< "hashMix: " << header.hashMix.ToString() << " \n"
<< "Nonce: " << header.nNonce << " \n";
return os;
}
};
struct Block : public BlockHeader
{
std::vector<CTransaction> vtx;
CTxOut txoutBackbone; // Energibackbone payment
CTxOut txoutMasternode; // masternode payment
std::vector<CTxOut> voutSuperblock; //superblock payment
Block()
{
SetNull();
}
Block(const BlockHeader& header)
{
SetNull();
*((BlockHeader*)this) = header;
}
ADD_SERIALIZE_METHODS
template<typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
{
READWRITE(*(BlockHeader*)this);
READWRITE(vtx);
}
void SetNull()
{
BlockHeader::SetNull();
vtx.clear();
txoutBackbone = CTxOut();
txoutMasternode = CTxOut();
voutSuperblock.clear();
}
};
#pragma pack(push, 1)
struct CBlockHeaderTruncatedLE
{
int32_t nVersion;
char hashPrevBlock[65];
char hashMerkleRoot[65];
uint32_t nTime;
uint32_t nBits;
uint32_t nHeight;
CBlockHeaderTruncatedLE(const BlockHeader& header)
: nVersion(htole32(header.nVersion))
, hashPrevBlock{0}
, hashMerkleRoot{0}
, nTime(htole32(header.nTime))
, nBits(htole32(header.nBits))
, nHeight(htole32(header.nHeight))
{
auto prevHash = header.hashPrevBlock.ToString();
memcpy(hashPrevBlock, prevHash.c_str(), (std::min)(prevHash.size(), sizeof(hashPrevBlock)));
auto merkleRoot = header.hashMerkleRoot.ToString();
memcpy(hashMerkleRoot, merkleRoot.c_str(), (std::min)(merkleRoot.size(), sizeof(hashMerkleRoot)));
}
};
static_assert(sizeof(CBlockHeaderTruncatedLE) == 146, "CBlockHeaderTruncatedLE has incorrect size");
struct CBlockHeaderFullLE : public CBlockHeaderTruncatedLE
{
uint64_t nNonce;
char hashMix[65];
CBlockHeaderFullLE(BlockHeader const & h)
: CBlockHeaderTruncatedLE(h)
, nNonce(h.nNonce)
, hashMix{0}
{
auto mixString = h.hashMix.ToString();
memcpy(hashMix, mixString.c_str(), (std::min)(mixString.size(), sizeof(hashMix)));
}
};
static_assert(sizeof(CBlockHeaderFullLE) == 219, "CBlockHeaderFullLE has incorrect size");
#pragma pack(pop)
} // namespace energi