forked from OCEAN-xyz/datum_gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatum_utils.h
More file actions
163 lines (145 loc) · 5.37 KB
/
datum_utils.h
File metadata and controls
163 lines (145 loc) · 5.37 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
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
158
159
160
161
162
163
/*
*
* DATUM Gateway
* Decentralized Alternative Templates for Universal Mining
*
* This file is part of OCEAN's Bitcoin mining decentralization
* project, DATUM.
*
* https://ocean.xyz
*
* ---
*
* Copyright (c) 2024 Bitcoin Ocean, LLC & Jason Hughes
*
* 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 _DATUM_UTILS_H_
#define _DATUM_UTILS_H_
#include <stdint.h>
#include <stdbool.h>
#include "datum_logger.h"
void datum_utils_init(void);
uint64_t current_time_seconds(void);
uint64_t current_time_millis(void);
uint64_t current_time_micros(void);
uint64_t get_process_uptime_seconds(void);
unsigned char hex2bin_uchar(const char *in);
void build_hex_lookup(void);
bool my_sha256(void *digest, const void *buffer, size_t length);
void nbits_to_target(uint32_t nbits, uint8_t *target);
int compare_hashes(const uint8_t *hash1, const uint8_t *hash2);
unsigned long long block_reward(unsigned int block_height);
int append_bitcoin_varint_hex(uint64_t n, char *s);
int append_UNum_hex(uint64_t n, char *s);
void panic_from_thread(int a);
bool double_sha256(void *out, const void *in, size_t length);
void hex_to_bin_le(const char *hex, unsigned char *bin);
void hex_to_bin(const char *hex, unsigned char *bin);
void hash2hex(unsigned char *bytes, char *hexString);
void get_target_from_diff(unsigned char *result, uint64_t diff);
uint64_t roundDownToPowerOfTwo_64(uint64_t x);
int addr_2_output_script(const char *addr, unsigned char *script, int max_len);
int output_script_2_addr(const unsigned char *script, const int len, char *addr);
int base64_decode(const char *in, size_t inLen, unsigned char *out, size_t *outLen);
void uchar_to_hex(char *s, const unsigned char b);
int get_bitcoin_varint_len_bytes(uint64_t n);
bool strncpy_uachars(char *out, const char *in, size_t maxlen);
bool strncpy_workerchars(char *out, const char *in, size_t maxlen);
long double calc_network_difficulty(const char *bits_hex);
unsigned char floorPoT(uint64_t x);
uint64_t datum_siphash(const void *src, uint64_t sz, const unsigned char key[16]);
uint64_t datum_siphash_mod8(const void *src, uint64_t sz, const unsigned char key[16]);
int datum_atoi_strict(const char *s, size_t size);
static inline
uint8_t upk_u8(const void * const bufp, const int offset)
{
const uint8_t * const buf = bufp;
return buf[offset];
}
#define upk_u8le(buf, offset) upk_u8(buf, offset)
static inline
uint16_t upk_u16le(const void * const bufp, const int offset)
{
const uint8_t * const buf = bufp;
return (((uint16_t)buf[offset+0]) << 0)
| (((uint16_t)buf[offset+1]) << 8);
}
static inline
uint32_t upk_u32le(const void * const bufp, const int offset)
{
const uint8_t * const buf = bufp;
return (((uint32_t)buf[offset+0]) << 0)
| (((uint32_t)buf[offset+1]) << 8)
| (((uint32_t)buf[offset+2]) << 0x10)
| (((uint32_t)buf[offset+3]) << 0x18);
}
static inline
uint64_t upk_u64le(const void * const bufp, const int offset)
{
const uint8_t * const buf = bufp;
return (((uint64_t)buf[offset+0]) << 0)
| (((uint64_t)buf[offset+1]) << 8)
| (((uint64_t)buf[offset+2]) << 0x10)
| (((uint64_t)buf[offset+3]) << 0x18)
| (((uint64_t)buf[offset+4]) << 0x20)
| (((uint64_t)buf[offset+5]) << 0x28)
| (((uint64_t)buf[offset+6]) << 0x30)
| (((uint64_t)buf[offset+7]) << 0x38);
}
static inline
void pk_u8(void * const bufp, const int offset, const uint8_t nv)
{
uint8_t * const buf = bufp;
buf[offset] = nv;
}
#define pk_u8le(buf, offset, nv) pk_u8(buf, offset, nv)
static inline
void pk_u16le(void * const bufp, const int offset, const uint16_t nv)
{
uint8_t * const buf = bufp;
buf[offset+0] = (nv >> 0) & 0xff;
buf[offset+1] = (nv >> 8) & 0xff;
}
static inline
void pk_u32le(void * const bufp, const int offset, const uint32_t nv)
{
uint8_t * const buf = bufp;
buf[offset+0] = (nv >> 0) & 0xff;
buf[offset+1] = (nv >> 8) & 0xff;
buf[offset+2] = (nv >> 0x10) & 0xff;
buf[offset+3] = (nv >> 0x18) & 0xff;
}
static inline
void pk_u64le(void * const bufp, const int offset, const uint64_t nv)
{
uint8_t * const buf = bufp;
buf[offset+0] = (nv >> 0) & 0xff;
buf[offset+1] = (nv >> 8) & 0xff;
buf[offset+2] = (nv >> 0x10) & 0xff;
buf[offset+3] = (nv >> 0x18) & 0xff;
buf[offset+4] = (nv >> 0x20) & 0xff;
buf[offset+5] = (nv >> 0x28) & 0xff;
buf[offset+6] = (nv >> 0x30) & 0xff;
buf[offset+7] = (nv >> 0x38) & 0xff;
}
extern volatile int panic_mode;
#endif