forked from DigiByte-Core/digibytewallet-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BRMerkleBlock.h
142 lines (119 loc) · 5.74 KB
/
BRMerkleBlock.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
//
// BRMerkleBlock.h
//
// Created by Aaron Voisine on 8/6/15.
// Copyright (c) 2015 breadwallet LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#ifndef BRMerkleBlock_h
#define BRMerkleBlock_h
#include "BRInt.h"
#include <stddef.h>
#include <inttypes.h>
#if defined(TARGET_OS_MAC)
#include <Foundation/Foundation.h>
#define digi_log(...) NSLog(__VA_ARGS__)
#elif defined(__ANDROID__)
#include <android/log.h>
#define digi_log(...) __android_log_print(ANDROID_LOG_ERROR, "digi", __VA_ARGS__)
#else
#include <stdio.h>
#define digi_log(...) printf(__VA_ARGS__)
#endif
#ifdef __cplusplus
extern "C" {
#endif
#define BLOCK_DIFFICULTY_INTERVAL 1 // number of blocks between difficulty target adjustments
#define BLOCK_UNKNOWN_HEIGHT INT32_MAX
#define BLOCK_MAX_TIME_DRIFT (2*60*60) // the furthest in the future a block is allowed to be timestamped
typedef struct {
UInt256 blockHash;
UInt256 powHash;
uint32_t version;
UInt256 prevBlock;
UInt256 merkleRoot;
uint32_t timestamp; // time interval since unix epoch
uint32_t target;
uint32_t nonce;
uint32_t totalTx;
UInt256 *hashes;
size_t hashesCount;
uint8_t *flags;
size_t flagsLen;
uint32_t height;
} BRMerkleBlock;
// Taken from https://github.com/digibyte/digibyte/blob/ce4e150f6d77abdd533a3b289ffd9f19fe8af277/src/primitives/block.h
typedef enum {
// primary version
BLOCK_VERSION_DEFAULT = 2,
// algo
BLOCK_VERSION_ALGO = (15 << 8),
BLOCK_VERSION_SCRYPT = (0 << 8),
BLOCK_VERSION_SHA256D = (2 << 8), // 512
BLOCK_VERSION_GROESTL = (4 << 8), // 1024
BLOCK_VERSION_SKEIN = (6 << 8), // 1536
BLOCK_VERSION_QUBIT = (8 << 8), // 2048
//BLOCK_VERSION_EQUIHASH = (10 << 8),
//BLOCK_VERSION_ETHASH = (12 << 8),
BLOCK_VERSION_ODO = (14 << 8), // 3584
} BLOCKHASH_ALGO;
#define BR_MERKLE_BLOCK_NONE\
((BRMerkleBlock) { UINT256_ZERO, 0, UINT256_ZERO, UINT256_ZERO, 0, 0, 0, 0, NULL, 0, NULL, 0, 0 })
// returns a newly allocated merkle block struct that must be freed by calling BRMerkleBlockFree()
BRMerkleBlock *BRMerkleBlockNew(void);
// returns a deep copy of block and that must be freed by calling BRMerkleBlockFree()
BRMerkleBlock *BRMerkleBlockCopy(const BRMerkleBlock *block);
// buf must contain either a serialized merkleblock or header
// returns a merkle block struct that must be freed by calling BRMerkleBlockFree()
BRMerkleBlock *BRMerkleBlockParse(const uint8_t *buf, size_t bufLen);
// returns number of bytes written to buf, or total bufLen needed if buf is NULL (block->height is not serialized)
size_t BRMerkleBlockSerialize(const BRMerkleBlock *block, uint8_t *buf, size_t bufLen);
// populates txHashes with the matched tx hashes in the block
// returns number of tx hashes written, or the total hashesCount needed if txHashes is NULL
size_t BRMerkleBlockTxHashes(const BRMerkleBlock *block, UInt256 *txHashes, size_t hashesCount);
// sets the hashes and flags fields for a block created with BRMerkleBlockNew()
void BRMerkleBlockSetTxHashes(BRMerkleBlock *block, const UInt256 hashes[], size_t hashesCount,
const uint8_t *flags, size_t flagsLen);
// true if merkle tree and timestamp are valid, and proof-of-work matches the stated difficulty target
// NOTE: this only checks if the block difficulty matches the difficulty target in the header, it does not check if the
// target is correct for the block's height in the chain - use BRMerkleBlockVerifyDifficulty() for that
int BRMerkleBlockIsValid(const BRMerkleBlock *block, uint32_t currentTime);
// true if the given tx hash is known to be included in the block
int BRMerkleBlockContainsTxHash(const BRMerkleBlock *block, UInt256 txHash);
// verifies the block difficulty target is correct for the block's position in the chain
// transitionTime is the timestamp of the block at the previous difficulty transition
// transitionTime may be 0 if block->height is not a multiple of BLOCK_DIFFICULTY_INTERVAL
int BRMerkleBlockVerifyDifficulty(const BRMerkleBlock *block, const BRMerkleBlock *previous, uint32_t transitionTime);
// returns a hash value for block suitable for use in a hashtable
inline static size_t BRMerkleBlockHash(const void *block)
{
return (size_t)((const BRMerkleBlock *)block)->blockHash.u32[0];
}
// true if block and otherBlock have equal blockHash values
inline static int BRMerkleBlockEq(const void *block, const void *otherBlock)
{
return (block == otherBlock ||
UInt256Eq(((const BRMerkleBlock *)block)->blockHash, ((const BRMerkleBlock *)otherBlock)->blockHash));
}
// frees memory allocated for block
void BRMerkleBlockFree(BRMerkleBlock *block);
#ifdef __cplusplus
}
#endif
#endif // BRMerkleBlock_h