From 6adf4ac6429b5441f195dae25562add092c84db8 Mon Sep 17 00:00:00 2001 From: "roman.khlopkov" <roman.khlopkov@demlabs.net> Date: Fri, 8 Dec 2023 12:25:38 +0300 Subject: [PATCH] [*] Doubles & warnings fix --- 3rdparty/crc32c_adler/CMakeLists.txt | 21 - 3rdparty/crc32c_adler/README | 14 + 3rdparty/crc32c_adler/crc32c_adler.c | 504 ++++++++++++++++++ 3rdparty/crc32c_adler/crc32c_adler.h | 72 +++ 3rdparty/crc32c_adler/example-dht.c | 163 ++++++ 3rdparty/crc32c_adler/example-stdin.c | 46 ++ CMakeLists.txt | 4 - cmake/OS_Detection.cmake | 235 -------- dap-sdk | 2 +- .../consensus/esbocs/dap_chain_cs_esbocs.c | 16 +- modules/net/dap_chain_ledger.c | 2 +- modules/net/dap_chain_node_cli_cmd.c | 6 +- .../dap_chain_net_srv_stake_pos_delegate.c | 15 + .../xchange/dap_chain_net_srv_xchange.c | 2 +- modules/type/dag/dap_chain_cs_dag_event.c | 9 +- modules/wallet/CMakeLists.txt | 12 +- 16 files changed, 845 insertions(+), 278 deletions(-) delete mode 100755 3rdparty/crc32c_adler/CMakeLists.txt create mode 100755 3rdparty/crc32c_adler/README create mode 100755 3rdparty/crc32c_adler/crc32c_adler.c create mode 100755 3rdparty/crc32c_adler/crc32c_adler.h create mode 100755 3rdparty/crc32c_adler/example-dht.c create mode 100755 3rdparty/crc32c_adler/example-stdin.c delete mode 100644 cmake/OS_Detection.cmake diff --git a/3rdparty/crc32c_adler/CMakeLists.txt b/3rdparty/crc32c_adler/CMakeLists.txt deleted file mode 100755 index b444a2b87a..0000000000 --- a/3rdparty/crc32c_adler/CMakeLists.txt +++ /dev/null @@ -1,21 +0,0 @@ -# -# DESCRIPTION: A miminal cmake script to be used to produce CRC32 Addler static library -# -# AUTHOR: Ruslan R. Laishev -# -# CREATION DATE: 14-NOV-2022 -# -# MODIFICATION HISTORY: -# -cmake_minimum_required(VERSION 3.10) -project(crc32c_adler) - -set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall" ) -set( SRCS crc32c_adler.c crc32c_adler.h) -add_library(crc32c_adler STATIC ${SRCS}) - -target_link_libraries(crc32c_adler) - -if(NOT ANDROID) - set(CELLFRAME_LIBS ${CELLFRAME_LIBS} pthread) -endif() diff --git a/3rdparty/crc32c_adler/README b/3rdparty/crc32c_adler/README new file mode 100755 index 0000000000..23b609d0e8 --- /dev/null +++ b/3rdparty/crc32c_adler/README @@ -0,0 +1,14 @@ + +Extended version of CRC32C implementation by Mark Adler, providing both hardware (SSE 4.2) and software algorithms with auto-detection. + +Source: +https://stackoverflow.com/a/17646775 + +According to Ferry Toth, Adler's implementation is 'highly optimized': +https://stackoverflow.com/a/39862799 +https://github.com/htot/crc32c + +Revised software algorithm by Robert Važan: +https://stackoverflow.com/a/21915223 +https://crc32c.machinezoo.com/ +https://bitbucket.org/robertvazan/crc32c-hw/src diff --git a/3rdparty/crc32c_adler/crc32c_adler.c b/3rdparty/crc32c_adler/crc32c_adler.c new file mode 100755 index 0000000000..68e90941aa --- /dev/null +++ b/3rdparty/crc32c_adler/crc32c_adler.c @@ -0,0 +1,504 @@ +/* + CRC32C_ADLER -- Computes CRC32C Checksums + Version 1.2, Date 05/21/18 + Copyright (C) 2013 Mark Adler <madler@alumni.caltech.edu> + Copyright (C) 2018 Fonic <https://github.com/fonic> + + Provides both a hardware-accelerated algorithm (*) and a software algorithm. + Note that this computes CRC32C checksums, not CRC32 (without 'C') checksums + used by Ethernet, gzip, etc. + + (*) CRC instruction on Intel SSE 4.2 processors. SSE 4.2 was first supported + by Nehalem processors introduced in November, 2008. + + Version history: + 1.0 10 Feb 2013 First version + 1.1 1 Aug 2013 Correct comments on why three crc instructions in parallel + 1.2 21 May 2018 Add header file, revise hardware support check, eliminate + pthreads, restructure code, revise comments and description + + Version 1.1 by Mark Adler was originally published here: + https://stackoverflow.com/a/17646775 + + This software is provided 'as-is', without any express or implied + warranty. In no event will the author be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +#include "crc32c_adler.h" + +/* CRC32C (iSCSI) polynomial in reversed bit order. */ +#define POLY 0x82f63b78 + + +/****************************************************************************** + * * + * Software Algorithm (1) - Table-driven, 8 Bytes / Iteration * + * * + ******************************************************************************/ + +/* Table for software algorithm. */ +static uint32_t crc32c_table[8][256]; + +/* Flag to indicate if crc32c_init_sw() has been called. */ +static int crc32c_sw_initialized = 0; + +/* Initialize table for software algorithm. */ +static void crc32c_init_sw(void) +{ + uint32_t n, crc, k; + + for (n = 0; n < 256; n++) { + crc = n; + crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1; + crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1; + crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1; + crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1; + crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1; + crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1; + crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1; + crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1; + crc32c_table[0][n] = crc; + } + for (n = 0; n < 256; n++) { + crc = crc32c_table[0][n]; + for (k = 1; k < 8; k++) { + crc = crc32c_table[0][crc & 0xff] ^ (crc >> 8); + crc32c_table[k][n] = crc; + } + } + crc32c_sw_initialized = 1; +} + +/* Compute CRC32C checksum. */ +uint32_t crc32c_sw(uint32_t crci, const void *buf, size_t len) +{ + const unsigned char *next = buf; + uint64_t crc; + + if (!crc32c_sw_initialized) + crc32c_init_sw(); + + crc = crci ^ 0xffffffff; + while (len && ((uintptr_t)next & 7) != 0) { + crc = crc32c_table[0][(crc ^ *next++) & 0xff] ^ (crc >> 8); + len--; + } + while (len >= 8) { + crc ^= *(uint64_t *)next; + crc = crc32c_table[7][crc & 0xff] ^ + crc32c_table[6][(crc >> 8) & 0xff] ^ + crc32c_table[5][(crc >> 16) & 0xff] ^ + crc32c_table[4][(crc >> 24) & 0xff] ^ + crc32c_table[3][(crc >> 32) & 0xff] ^ + crc32c_table[2][(crc >> 40) & 0xff] ^ + crc32c_table[1][(crc >> 48) & 0xff] ^ + crc32c_table[0][crc >> 56]; + next += 8; + len -= 8; + } + while (len) { + crc = crc32c_table[0][(crc ^ *next++) & 0xff] ^ (crc >> 8); + len--; + } + return (uint32_t)crc ^ 0xffffffff; +} + + +/****************************************************************************** + * * + * Software Algorithm (2) - Table-driven, 16 Bytes / Iteration * + * * + ******************************************************************************/ + +/* Table for software algorithm. */ +static uint32_t crc32c_table2[16][256]; + +/* Flag to indicate if crc32c_init_sw2() has been called. */ +static int crc32c_table2_initialized = 0; + +/* Initialize table for software algorithm. */ +static void crc32c_init_sw2(void) +{ + for(int i = 0; i < 256; i++) + { + uint32_t res = (uint32_t)i; + for(int t = 0; t < 16; t++) { + for (int k = 0; k < 8; k++) res = (res & 1) == 1 ? POLY ^ (res >> 1) : (res >> 1); + crc32c_table2[t][i] = res; + } + } + crc32c_table2_initialized = 1; +} + +/* Compute CRC32C checksum. */ +uint32_t crc32c_sw2(uint32_t crci, const void *buf, size_t len) +{ + const unsigned char *next = buf; +#ifdef __x86_64__ + uint64_t crc; +#else + uint32_t crc; +#endif + + if(!crc32c_table2_initialized) + crc32c_init_sw2(); + + crc = crci ^ 0xffffffff; +#ifdef __x86_64__ + while (len && ((uintptr_t)next & 7) != 0) + { + crc = crc32c_table2[0][(crc ^ *next++) & 0xff] ^ (crc >> 8); + --len; + } + while (len >= 16) + { + crc ^= *(uint64_t *)next; + uint64_t high = *(uint64_t *)(next + 8); + crc = crc32c_table2[15][crc & 0xff] + ^ crc32c_table2[14][(crc >> 8) & 0xff] + ^ crc32c_table2[13][(crc >> 16) & 0xff] + ^ crc32c_table2[12][(crc >> 24) & 0xff] + ^ crc32c_table2[11][(crc >> 32) & 0xff] + ^ crc32c_table2[10][(crc >> 40) & 0xff] + ^ crc32c_table2[9][(crc >> 48) & 0xff] + ^ crc32c_table2[8][crc >> 56] + ^ crc32c_table2[7][high & 0xff] + ^ crc32c_table2[6][(high >> 8) & 0xff] + ^ crc32c_table2[5][(high >> 16) & 0xff] + ^ crc32c_table2[4][(high >> 24) & 0xff] + ^ crc32c_table2[3][(high >> 32) & 0xff] + ^ crc32c_table2[2][(high >> 40) & 0xff] + ^ crc32c_table2[1][(high >> 48) & 0xff] + ^ crc32c_table2[0][high >> 56]; + next += 16; + len -= 16; + } +#else + while (len && ((uintptr_t)next & 3) != 0) + { + crc = crc32c_table2[0][(crc ^ *next++) & 0xff] ^ (crc >> 8); + --len; + } + while (len >= 12) + { + crc ^= *(uint32_t *)next; + uint32_t high = *(uint32_t *)(next + 4); + uint32_t high2 = *(uint32_t *)(next + 8); + crc = crc32c_table2[11][crc & 0xff] + ^ crc32c_table2[10][(crc >> 8) & 0xff] + ^ crc32c_table2[9][(crc >> 16) & 0xff] + ^ crc32c_table2[8][crc >> 24] + ^ crc32c_table2[7][high & 0xff] + ^ crc32c_table2[6][(high >> 8) & 0xff] + ^ crc32c_table2[5][(high >> 16) & 0xff] + ^ crc32c_table2[4][high >> 24] + ^ crc32c_table2[3][high2 & 0xff] + ^ crc32c_table2[2][(high2 >> 8) & 0xff] + ^ crc32c_table2[1][(high2 >> 16) & 0xff] + ^ crc32c_table2[0][high2 >> 24]; + next += 12; + len -= 12; + } +#endif + while (len) + { + crc = crc32c_table2[0][(crc ^ *next++) & 0xff] ^ (crc >> 8); + --len; + } + return (uint32_t)crc ^ 0xffffffff; +} + + +/****************************************************************************** + * * + * Hardware Algorithm - SSE 4.2 * + * * + ******************************************************************************/ + +/* Multiply a matrix times a vector over the Galois field of two elements, + GF(2). Each element is a bit in an unsigned integer. mat must have at + least as many entries as the power of two for most significant one bit in + vec. */ +static inline uint32_t gf2_matrix_times(uint32_t *mat, uint32_t vec) +{ + uint32_t sum; + + sum = 0; + while (vec) { + if (vec & 1) + sum ^= *mat; + vec >>= 1; + mat++; + } + return sum; +} + +/* Multiply a matrix by itself over GF(2). Both mat and square must have 32 + rows. */ +static inline void gf2_matrix_square(uint32_t *square, uint32_t *mat) +{ + int n; + + for (n = 0; n < 32; n++) + square[n] = gf2_matrix_times(mat, mat[n]); +} + +/* Construct an operator to apply len zeros to a crc. len must be a power of + two. If len is not a power of two, then the result is the same as for the + largest power of two less than len. The result for len == 0 is the same as + for len == 1. A variant of this routine could be easily written for any + len, but that is not needed for this application. */ +static void crc32c_zeros_op(uint32_t *even, size_t len) +{ + int n; + uint32_t row; + uint32_t odd[32]; /* odd-power-of-two zeros operator */ + + /* Put operator for one zero bit in odd. */ + odd[0] = POLY; /* CRC32C polynomial */ + row = 1; + for (n = 1; n < 32; n++) { + odd[n] = row; + row <<= 1; + } + + /* Put operator for two zero bits in even. */ + gf2_matrix_square(even, odd); + + /* Put operator for four zero bits in odd. */ + gf2_matrix_square(odd, even); + + /* First square will put the operator for one zero byte (eight zero bits), + in even -- next square puts operator for two zero bytes in odd, and so + on, until len has been rotated down to zero. */ + do { + gf2_matrix_square(even, odd); + len >>= 1; + if (len == 0) + return; + gf2_matrix_square(odd, even); + len >>= 1; + } while (len); + + /* Answer ended up in odd -- copy to even. */ + for (n = 0; n < 32; n++) + even[n] = odd[n]; +} + +/* Take a length and build four lookup tables for applying the zeros operator + for that length, byte-by-byte on the operand. */ +static void crc32c_zeros(uint32_t zeros[][256], size_t len) +{ + uint32_t n; + uint32_t op[32]; + + crc32c_zeros_op(op, len); + for (n = 0; n < 256; n++) { + zeros[0][n] = gf2_matrix_times(op, n); + zeros[1][n] = gf2_matrix_times(op, n << 8); + zeros[2][n] = gf2_matrix_times(op, n << 16); + zeros[3][n] = gf2_matrix_times(op, n << 24); + } +} + +/* Apply the zeros operator table to crc. */ +static inline uint32_t crc32c_shift(uint32_t zeros[][256], uint32_t crc) +{ + return zeros[0][crc & 0xff] ^ zeros[1][(crc >> 8) & 0xff] ^ + zeros[2][(crc >> 16) & 0xff] ^ zeros[3][crc >> 24]; +} + +/* Block sizes for three-way parallel crc computation. LONG and SHORT must + both be powers of two. The associated string constants must be set + accordingly, for use in constructing the assembler instructions. */ +#define LONG 8192 +#define LONGx1 "8192" +#define LONGx2 "16384" +#define SHORT 256 +#define SHORTx1 "256" +#define SHORTx2 "512" + +/* Tables for hardware algorithm that shift a crc by LONG and SHORT zeros. */ +static uint32_t crc32c_long[4][256]; +static uint32_t crc32c_short[4][256]; + +/* Flag to indicate if crc32c_init_hw() has been called. */ +static int crc32c_hw_initialized = 0; + + + + +#if defined(__x86_64__) /* @RRL: to compile for ARM */ + + + +static void crc32c_init_hw(void) +{ + crc32c_zeros(crc32c_long, LONG); + crc32c_zeros(crc32c_short, SHORT); + crc32c_hw_initialized = 1; +} + +/* Compute CRC32C checksum. */ +uint32_t crc32c_hw(uint32_t crc, const void *buf, size_t len) +{ + const unsigned char *next = buf; + const unsigned char *end; + uint64_t crc0, crc1, crc2; /* need to be 64 bits for crc32q */ + + /* Populate shift tables the first time through. */ + if (!crc32c_hw_initialized) + crc32c_init_hw(); + + /* Pre-process the crc. */ + crc0 = crc ^ 0xffffffff; + + /* Compute the crc for up to seven leading bytes to bring the data pointer + to an eight-byte boundary. */ + while (len && ((uintptr_t)next & 7) != 0) { + __asm__("crc32b\t" "(%1), %0" + : "=r"(crc0) + : "r"(next), "0"(crc0)); + next++; + len--; + } + + /* Compute the crc on sets of LONG*3 bytes, executing three independent crc + instructions, each on LONG bytes -- this is optimized for the Nehalem, + Westmere, Sandy Bridge, and Ivy Bridge architectures, which have a + throughput of one crc per cycle, but a latency of three cycles. */ + while (len >= LONG*3) { + crc1 = 0; + crc2 = 0; + end = next + LONG; + do { + __asm__("crc32q\t" "(%3), %0\n\t" + "crc32q\t" LONGx1 "(%3), %1\n\t" + "crc32q\t" LONGx2 "(%3), %2" + : "=r"(crc0), "=r"(crc1), "=r"(crc2) + : "r"(next), "0"(crc0), "1"(crc1), "2"(crc2)); + next += 8; + } while (next < end); + crc0 = crc32c_shift(crc32c_long, crc0) ^ crc1; + crc0 = crc32c_shift(crc32c_long, crc0) ^ crc2; + next += LONG*2; + len -= LONG*3; + } + + /* Do the same thing, but now on SHORT*3 blocks for the remaining data less + than a LONG*3 block. */ + while (len >= SHORT*3) { + crc1 = 0; + crc2 = 0; + end = next + SHORT; + do { + __asm__("crc32q\t" "(%3), %0\n\t" + "crc32q\t" SHORTx1 "(%3), %1\n\t" + "crc32q\t" SHORTx2 "(%3), %2" + : "=r"(crc0), "=r"(crc1), "=r"(crc2) + : "r"(next), "0"(crc0), "1"(crc1), "2"(crc2)); + next += 8; + } while (next < end); + crc0 = crc32c_shift(crc32c_short, crc0) ^ crc1; + crc0 = crc32c_shift(crc32c_short, crc0) ^ crc2; + next += SHORT*2; + len -= SHORT*3; + } + + /* Compute the crc on the remaining eight-byte units less than a SHORT*3 + block. */ + end = next + (len - (len & 7)); + while (next < end) { + __asm__("crc32q\t" "(%1), %0" + : "=r"(crc0) + : "r"(next), "0"(crc0)); + next += 8; + } + len &= 7; + + /* Compute the crc for up to seven trailing bytes. */ + while (len) { + __asm__("crc32b\t" "(%1), %0" + : "=r"(crc0) + : "r"(next), "0"(crc0)); + next++; + len--; + } + + /* Return a post-processed crc. */ + return (uint32_t)crc0 ^ 0xffffffff; +} + + + +#endif /* @RRL: to compile for ARM */ + +/****************************************************************************** + * * + * Other Functions * + * * + ******************************************************************************/ + +/* Variables to store information on hardware support. */ +static int crc32c_hardware_support = 0; +static int crc32c_hardware_checked = 0; + +/* Check for hardware support (SSE 4.2). Note that this does not check for + the existence of the cpuid instruction itself, which was introduced on the + 486SL in 1992, so this will fail on earlier x86 processors. cpuid works + on all Pentium and later processors. */ +int crc32c_hw_support() +{ +#if defined(__x86_64__) /* @RRL: to compile for ARM */ + if (!crc32c_hardware_checked) { + do { + uint32_t eax, ecx; + eax = 1; + __asm__("cpuid" + : "=c"(ecx) + : "a"(eax) + : "%ebx", "%edx"); + (crc32c_hardware_support) = (ecx >> 20) & 1; + } while (0); + crc32c_hardware_checked = 1; + } + return crc32c_hardware_support; +#else + return 0; +#endif + +} + +/* Disable hardware algorithm even if supported by hardware. */ +void crc32c_hw_disable() +{ + crc32c_hardware_support = 0; + crc32c_hardware_checked = 1; +} + +/* Compute CRC32C checksum. Use hardware algorithm if supported, + fall back on software algorithm otherwise. */ +uint32_t crc32c(uint32_t crc, const void *buf, size_t len) +{ +#if defined(__x86_64__) /* @RRL: to compile for ARM */ + return crc32c_hw_support() ? crc32c_hw(crc, buf, len) : crc32c_sw(crc, buf, len); +#else + return crc32c_sw(crc, buf, len); +#endif +} diff --git a/3rdparty/crc32c_adler/crc32c_adler.h b/3rdparty/crc32c_adler/crc32c_adler.h new file mode 100755 index 0000000000..ce1f27b4d5 --- /dev/null +++ b/3rdparty/crc32c_adler/crc32c_adler.h @@ -0,0 +1,72 @@ +/* + CRC32C_ADLER -- Computes CRC32C Checksums + Version 1.2, Date 05/21/18 + Copyright (C) 2013 Mark Adler <madler@alumni.caltech.edu> + Copyright (C) 2018 Fonic <https://github.com/fonic> + + Provides both a hardware-accelerated algorithm (*) and a software algorithm. + Note that this computes CRC32C checksums, not CRC32 (without 'C') checksums + used by Ethernet, gzip, etc. + + (*) CRC instruction on Intel SSE 4.2 processors. SSE 4.2 was first supported + by Nehalem processors introduced in November, 2008. + + Version history: + 1.0 10 Feb 2013 First version + 1.1 1 Aug 2013 Correct comments on why three crc instructions in parallel + 1.2 21 May 2018 Add header file, revise hardware support check, eliminate + pthreads, restructure code, revise comments and description + + Version 1.1 by Mark Adler was originally published here: + https://stackoverflow.com/a/17646775 + + This software is provided 'as-is', without any express or implied + warranty. In no event will the author be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#ifndef CRC32C_ADLER_H +#define CRC32C_ADLER_H + +#include <stdint.h> + +#define CRC32C_INIT 0xEDB88320 + +#if defined(__cplusplus) +extern "C" { +#endif + +/* Compute CRC32C checksum using software algorithm (1). */ +uint32_t crc32c_sw(uint32_t crci, const void *buf, size_t len); + +/* Compute CRC32C checksum using software algorithm (2). */ +uint32_t crc32c_sw2(uint32_t crci, const void *buf, size_t len); + +/* Compute CRC32C checksum using hardware algorithm. */ +uint32_t crc32c_hw(uint32_t crc, const void *buf, size_t len); + +/* Check if hardware-support (i.e. SSE 4.2) is available. */ +int crc32c_hw_support(); + +/* Disable hardware algorithm even if supported by hardware. */ +void crc32c_hw_disable(); + +/* Compute CRC32C checksum. Use hardware algorithm if supported, + fall back on software algorithm otherwise. */ +uint32_t crc32c(uint32_t crc, const void *buf, size_t len); + +#if defined(__cplusplus) +} +#endif +#endif // CRC32C_ADLER_H diff --git a/3rdparty/crc32c_adler/example-dht.c b/3rdparty/crc32c_adler/example-dht.c new file mode 100755 index 0000000000..5bd05b1e6b --- /dev/null +++ b/3rdparty/crc32c_adler/example-dht.c @@ -0,0 +1,163 @@ +/* + TODO describe example +*/ +#define _GNU_SOURCE /* random, srandom*/ +#include <stdio.h> +#include <stdlib.h> +#include <stdint.h> +#include <unistd.h> +#include <fcntl.h> /* open, close etc. */ +#include <string.h> /* memcpy */ +#include <netinet/in.h> /* AF_INET, INET_ADDRSTRLEN etc. */ +#include <arpa/inet.h> /* inet_ntop */ + +#include "crc32c_adler.h" + +/* Convert id to hex string of formate used in BEP 42. */ +char id_hex_str[20*2+2+1]; /* 20 bytes x 2 characters + 2 x ' ' + '\0' */ +const char* +id_to_hex(const unsigned char *id) +{ + //const char* hex_chr = "0123456789ABCDEF"; + const char* hex_chr = "0123456789abcdef"; + for(int i=0,j=0; i < 20 && j < sizeof(id_hex_str)-2; i++) { + id_hex_str[j++] = hex_chr[ (id[i]>>4) & 0x0F ]; + id_hex_str[j++] = hex_chr[ id[i] & 0x0F ]; + if (i == 2 || i == 18) { + id_hex_str[j++] = ' '; + } + } + id_hex_str[sizeof(id_hex_str)-1] = '\0'; + return id_hex_str; +} + +/* Generate node ID from IP address + predefined rand using example algorithm + provided in BEP 42. + + Parameters: + ip IPv4 or IPv6 address (network byte order) + iplen number of octets to consider in ip (4 or 8) + id resulting node ID + rand predefined random value */ +void crc32c_id(const uint8_t* ip, int iplen, uint8_t id[20], uint32_t rand) +{ + uint8_t v4_mask[] = { 0x03, 0x0f, 0x3f, 0xff }; + uint8_t v6_mask[] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff }; + uint8_t* mask = iplen == 4 ? v4_mask : v6_mask; + + uint8_t ip_copy[8]; + memcpy(ip_copy, ip, iplen); + + for (int i = 0; i < iplen; ++i) + ip_copy[i] &= mask[i]; + + //uint32_t rand = random() & 0xff; + uint8_t r = rand & 0x7; + ip_copy[0] |= r << 5; + + uint32_t crc = 0; + crc = crc32c(crc, ip_copy, iplen); + + /* only take the top 21 bits from crc */ + id[0] = (crc >> 24) & 0xff; + id[1] = (crc >> 16) & 0xff; + id[2] = ((crc >> 8) & 0xf8) | (random() & 0x7); + for (int i = 3; i < 19; ++i) id[i] = random(); + id[19] = rand; +} + +/* Find how many bits two ids have in common. */ +int common_bits(const unsigned char *id1, const unsigned char *id2) +{ + int i, j; + unsigned char xor; + for(i = 0; i < 20; i++) { + if(id1[i] != id2[i]) + break; + } + + if(i == 20) + return 160; + + xor = id1[i] ^ id2[i]; + + j = 0; + while((xor & 0x80) == 0) { + xor <<= 1; + j++; + } + + return 8 * i + j; +} + +/* Check if a node ID is correct in the sense of BEP 42. */ +int check_id(const uint8_t id1[20], const uint8_t* ip, int iplen, uint32_t rand) +{ + /* Generate ID from IP + rand -> id2. */ + uint8_t id2[20]; + crc32c_id(ip, iplen, id2, rand); + + /* Compare id1 with id2: + - the first 21 bits must match + - the last byte must match rand */ + int cbits = common_bits(id1, id2); + if (cbits < 21) { + printf("Only the first %i bits match (expected: 21)\n", cbits); + return 0; + } + if (id1[19] != id2[19]) { + printf("Last byte does not match (expected: %u, got: %u)\n", id2[19], id1[19]); + return 0; + } + return 1; +} + +/* Main. */ +int main(int argc, char **argv) +{ + (void)argc; + (void)argv; + + /* Print which CRC32C algorithm is used. */ + printf("\nUsing %s algorithm.\n\n", crc32c_hw_support() ? "hardware-accelerated (SSE 4.2)" : "software"); + + /* Seed random. */ + int fd = open("/dev/urandom", O_RDONLY); + if(fd < 0) { + perror("open(random)"); + exit(1); + } + unsigned seed; + read(fd, &seed, sizeof(seed)); + srandom(seed); + close(fd); + + /* Example IP/rand combinations as used in BEP 42. */ + uint8_t ip[5][4] = { + { 124, 31, 75, 21 }, + { 21, 75, 31, 124 }, + { 65, 23, 51, 170 }, + { 84, 124, 73, 14 }, + { 43, 213, 53, 83 } + }; + uint32_t rand[] = { + 1, + 86, + 22, + 65, + 90 + }; + int iplen = 4; + uint8_t id[20]; + + printf("IP rand Node ID Ok?\n"); + printf("=============== ===== ========================================== ====\n"); + for (int i = 0; i < 5; ++i) { + crc32c_id(ip[i], iplen, id, rand[i]); + char ipstr[INET_ADDRSTRLEN]; + inet_ntop(AF_INET, ip[i], ipstr, sizeof(ipstr)); + printf("%-15s %2u %s %s\n", ipstr, rand[i], id_to_hex(id), (check_id(id, ip[i], 4, rand[i]) ? "yes" : "no")); + } + + return 0; +} diff --git a/3rdparty/crc32c_adler/example-stdin.c b/3rdparty/crc32c_adler/example-stdin.c new file mode 100755 index 0000000000..d64fdb6a8a --- /dev/null +++ b/3rdparty/crc32c_adler/example-stdin.c @@ -0,0 +1,46 @@ +/* + TODO describe example +*/ +#include <stdio.h> +#include <stdlib.h> +#include <stdint.h> +#include <unistd.h> + +#include "crc32c_adler.h" + +#define SIZE (262144*3) +#define CHUNK SIZE + +int main(int argc, char **argv) +{ + char *buf; + ssize_t got; + size_t off, n; + uint32_t crc; + + (void)argv; + crc = 0; + buf = malloc(SIZE); + if (buf == NULL) { + fputs("out of memory", stderr); + return 1; + } + while ((got = read(0, buf, SIZE)) > 0) { + off = 0; + do { + n = (size_t)got - off; + if (n > CHUNK) + n = CHUNK; + crc = argc > 1 ? crc32c_sw(crc, buf + off, n) : + crc32c(crc, buf + off, n); + off += n; + } while (off < (size_t)got); + } + free(buf); + if (got == -1) { + fputs("read error\n", stderr); + return 1; + } + printf("%08x\n", crc); + return 0; +} diff --git a/CMakeLists.txt b/CMakeLists.txt index c620d3a641..7e09be0832 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -192,8 +192,4 @@ if (DARWIN) set(CELLFRAME_LIBS ${CELLFRAME_LIBS} bz2) endif() -set(CELLFRAME_LIBS ${CELLFRAME_LIBS} crc32c_adler) - target_link_libraries(${PROJECT_NAME} ${CELLFRAME_LIBS}) - - diff --git a/cmake/OS_Detection.cmake b/cmake/OS_Detection.cmake deleted file mode 100644 index 16c323946d..0000000000 --- a/cmake/OS_Detection.cmake +++ /dev/null @@ -1,235 +0,0 @@ -include_guard(GLOBAL) - -if(${CMAKE_SYSTEM_NAME} MATCHES "Linux") - set(OS_TYPE_DESKTOP ON) - set(LINUX ON) - set(UNIX ON) - EXECUTE_PROCESS( COMMAND cat /etc/os-release COMMAND grep VERSION_CODENAME COMMAND sed s/VERSION_CODENAME=// COMMAND tr -d '\n' OUTPUT_VARIABLE L_DEBIAN_OS_NAME) - EXECUTE_PROCESS( COMMAND cat /etc/os-release COMMAND grep VERSION_ID COMMAND sed s/VERSION_ID=// COMMAND tr -d '\n' COMMAND sed s/\\x22// COMMAND sed s/\\x22// OUTPUT_VARIABLE L_DEBIAN_OS_VERSION) - SET(DEBIAN_OS_NAME "${L_DEBIAN_OS_NAME}") - SET(DEBIAN_OS_VERSION ${L_DEBIAN_OS_VERSION}) - message("[ ] Debian OS ${DEBIAN_OS_VERSION} (${DEBIAN_OS_NAME})") -# check if we're building natively on Android (TERMUX) - EXECUTE_PROCESS( COMMAND uname -o COMMAND tr -d '\n' OUTPUT_VARIABLE OPERATING_SYSTEM) - - execute_process ( - COMMAND bash -c "awk -F= '/^ID=/{print $2}' /etc/os-release |tr -d '\n' | tr -d '\"'" - OUTPUT_VARIABLE DEBIAN_OS_RELEASE_NAME - ) - -elseif(${CMAKE_SYSTEM_NAME} MATCHES "Android") - set(ANDROID ON) - set(UNIX ON) - set(LINUX OFF) - set(OS_TYPE_MOBILE ON) - message("[*] ANDROID build") - add_definitions(-DANDROID -DDAP_OS_ANDROID) -elseif(${CMAKE_SYSTEM_NAME} MATCHES "Win") - set(OS_TYPE_DESKTOP ON) -endif() - -if((CMAKE_BUILD_TYPE STREQUAL "Debug") OR (DAP_DEBUG)) - message("[!] Debug build") - SET(DAP_DEBUG ON) -else() - message("[!] Release build") - SET(DAP_RELEASE ON) - if((CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")) - message("[!] Debug symbols ON") - SET(DAP_DBG_INFO ON) - endif() -endif() - -if(CMAKE_SIZEOF_VOID_P EQUAL "8") - set(DEFAULT_BUILD_64 ON) -else() - set(DEFAULT_BUILD_64 OFF) -endif() -option(BUILD_64 "Build for 64-bit? 'OFF' builds for 32-bit." ${DEFAULT_BUILD_64}) - -if(BUILD_64) - set(ARCH_WIDTH "64") -else() - set(ARCH_WIDTH "32") -endif() -message(STATUS "[*] Building for a ${ARCH_WIDTH}-bit system") - -if(UNIX) - add_definitions ("-DDAP_OS_UNIX") - if (APPLE) - - EXECUTE_PROCESS( COMMAND whoami COMMAND tr -d '\n' OUTPUT_VARIABLE L_USER) - EXECUTE_PROCESS( COMMAND echo -n /Users/${L_USER} OUTPUT_VARIABLE L_USERDIR_PATH) - set (USERDIR_PATH "${L_USERDIR_PATH}") - add_definitions ("-DDAP_OS_DARWIN -DDARWIN -DDAP_OS_BSD") - set(DARWIN ON) - set(BSD ON) - if (${_CMAKE_OSX_SYSROOT_PATH} MATCHES "MacOS") - set(MACOS ON) - # on macOS "uname -m" returns the architecture (x86_64 or arm64) - if (NOT DEFINED MACOS_ARCH) - - execute_process( - COMMAND uname -m - RESULT_VARIABLE result - OUTPUT_VARIABLE MACOS_ARCH - OUTPUT_STRIP_TRAILING_WHITESPACE - ) - endif() - add_definitions("-DDAP_OS_MAC -DDAP_OS_MAC_ARCH=${MACOS_ARCH}") - elseif (${_CMAKE_OSX_SYSROOT_PATH} MATCHES "iOS") - set(IOS ON) - add_definitions("-DDAP_OS_IOS") - else() - set(MACOS ON) - add_definitions("-DDAP_OS_MAC -DDAP_OS_MAC_ARCH=${MACOS_ARCH}") - endif() - endif() - - if (${CMAKE_SYSTEM_NAME} MATCHES "BSD" ) - add_definitions ("-DDAP_OS_BSD") - set(BSD ON) - endif() - - if (${CMAKE_SYSTEM_NAME} MATCHES "Linux" ) - add_definitions ("-DDAP_OS_LINUX") - endif() - - set(CFLAGS_WARNINGS "-Wall -Wextra -Werror=sign-compare -Wno-unused-command-line-argument -Wno-deprecated-declarations -Wno-unused-local-typedefs -Wno-unused-function -Wno-implicit-fallthrough -Wno-unused-variable -Wno-unused-parameter") - if (LINUX) - set(CCOPT_SYSTEM "") - set(LDOPT_SYSTEM "") - if(DAP_DEBUG) - set(_CCOPT "-DDAP_DEBUG ${CFLAGS_WARNINGS} -pg -g3 -ggdb -fno-eliminate-unused-debug-symbols -fno-strict-aliasing") - set(_LOPT "-pg") - if (DEFINED ENV{DAP_ASAN}) - message("[!] Address Sanitizer enabled") - set(_CCOPT "${_CCOPT} -fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer -fno-common -O1") - set(_LOPT "${_LOPT} -fsanitize=address") - elseif(DEFINED ENV{DAP_MSAN}) - if (CMAKE_C_COMPILER_ID MATCHES ".*[Cc][Ll][Aa][Nn][Gg].*") - message("[!] Memory Sanitizer enabled") - set(_CCOPT "${_CCOPT} -fsanitize=memory -fPIE -pie -fno-omit-frame-pointer -O2") - set(_LOPT "${_LOPT} -fsanitize=memory -fPIE -pie") - else() - message("[!] Memory Sanitizer is not available on this compiler") - endif() - elseif(DEFINED ENV{DAP_TSAN}) - message("[!] Thread Sanitizer enabled") - set(_CCOPT "${_CCOPT} -fsanitize=thread") - set(_LOPT "${_LOPT} -fsanitize=thread") - elseif(DEFINED ENV{DAP_UBSAN}) - message("[!] Undefined behaviour Sanitizer enabled") - set(_CCOPT "${_CCOPT} -fsanitize=undefined -fsanitize=bounds -fno-omit-frame-pointer") - set(_LOPT "${_LOPT} -fsanitize=undefined") - endif() - SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -pg") - else() - set(_CCOPT "${CFLAGS_WARNINGS} -fPIC -fno-ident -ffast-math -ftree-vectorize -fno-asynchronous-unwind-tables -ffunction-sections -Wl,--gc-sections -std=gnu11") - if (DEFINED ENV{DAP_ASAN}) - message("[!] Address Sanitizer enabled") - set(_CCOPT "${_CCOPT} -fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer -fno-common -O2") - set(_LOPT "${_LOPT} -fsanitize=address") - elseif(DEFINED ENV{DAP_MSAN}) - if (CMAKE_C_COMPILER_ID MATCHES ".*[Cc][Ll][Aa][Nn][Gg].*") - message("[!] Memory Sanitizer enabled") - set(_CCOPT "${_CCOPT} -fsanitize=memory -fPIE -pie -fno-omit-frame-pointer -O2") - set(_LOPT "${_LOPT} -fsanitize=memory -fPIE -pie") - else() - message("[!] Memory Sanitizer is not available on this compiler") - endif() - elseif(DEFINED ENV{DAP_TSAN}) - message("[!] Thread Sanitizer enabled") - set(_CCOPT "${_CCOPT} -fsanitize=thread") - set(_LOPT "${_LOPT} -fsanitize=thread") - elseif(DEFINED ENV{DAP_UBSAN}) - message("[!] Undefined behaviour Sanitizer enabled") - set(_CCOPT "${_CCOPT} -fsanitize=undefined -fsanitize=bounds -fno-omit-frame-pointer") - set(_LOPT "${_LOPT} -fsanitize=undefined") - else() - set(_CCOPT "${_CCOPT} -O3") - if(NOT DAP_DBG_INFO) - set(_CCOPT "${_CCOPT} -Wl,--strip-all") - endif() - endif() - endif() - elseif (DARWIN) - set(CCOPT_SYSTEM "-L/usr/local/lib -L/opt/homebrew/lib -I/opt/homebrew/include -I/usr/local/include") - set(LDOPT_SYSTEM "-L/usr/local/lib -L/opt/homebrew/lib -lintl -flat_namespace") - set(CCFLAGS_COMMON "-std=c11 ${CFLAGS_WARNINGS}") - if(DAP_DEBUG) - set(_CCOPT "${CCOPT_SYSTEM} -DDAP_DEBUG ${CCFLAGS_COMMON} -g3 -ggdb -fno-eliminate-unused-debug-symbols -fno-strict-aliasing") - set(_LOPT "${LDOPT_SYSTEM}") - SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}") - else() - set(_CCOPT "${CCOPT_SYSTEM} ${CCFLAGS_COMMON} -O3 -fPIC -fno-strict-aliasing -fno-ident -ffast-math -ftree-vectorize -fno-asynchronous-unwind-tables -ffunction-sections") - set(_LOPT "${LDOPT_SYSTEM}") - SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}") - endif() - elseif(BSD) - set(CCOPT_SYSTEM "-L/usr/local/lib -I/usr/local/include") - set(LDOPT_SYSTEM "-L/usr/local/lib") - if(DAP_DEBUG) - set(_CCOPT "${CCOPT_SYSTEM} -DDAP_DEBUG ${CFLAGS_WARNINGS} -pg -g3 -ggdb -fno-eliminate-unused-debug-symbols -fno-strict-aliasing") - set(_LOPT "-pg ${LDOPT_SYSTEM} ") - SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -pg") - else() - set(_CCOPT "${CCOPT_SYSTEM} ${CFLAGS_WARNINGS} -O3 -fPIC -fno-strict-aliasing -fno-ident -ffast-math -ftree-vectorize -fno-asynchronous-unwind-tables -ffunction-sections -std=gnu11") - set(_LOPT "${LDOPT_SYSTEM} ") - SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}") - endif() - endif() - - if (ANDROID) - if (ARCH_WIDTH MATCHES "32") - set(_CCOPT "${_CCOPT} -std=gnu11") - else() - set(_CCOPT "${_CCOPT} -fforce-enable-int128 -std=gnu11") - endif() - add_definitions ("-DDAP_OS_ANDROID") - add_definitions ("-DDAP_OS_LINUX") - endif() - - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_CCOPT}") - set(CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} ${_LOPT}") - -endif() - -if(WIN32) - message(STATUS "[*] Building for Windows") - add_definitions ("-DDAP_OS_WINDOWS") - add_definitions ("-DUNDEBUG") - add_definitions ("-DWIN32") - add_definitions ("-D_WINDOWS") - add_definitions ("-D__WINDOWS__") - add_definitions ("-D_CRT_SECURE_NO_WARNINGS") - add_definitions ("-DCURL_STATICLIB") - add_definitions("-DHAVE_PREAD") - add_definitions("-DHAVE_MMAP") - add_definitions("-DHAVE_STRNDUP") - add_definitions("-DNGHTTP2_STATICLIB") - add_compile_definitions(WINVER=0x0600 _WIN32_WINNT=0x0600) - add_compile_definitions(__USE_MINGW_ANSI_STDIO=1) - - set(CCOPT_SYSTEM "") - set(LDOPT_SYSTEM "") - - if(DAP_DEBUG) - set(_CCOPT "-mconsole -static -std=gnu11 ${CFLAGS_WARNINGS} -g3 -ggdb -fno-strict-aliasing -fno-eliminate-unused-debug-symbols -pg") - set(_LOPT "-mconsole -static -pg") - else() - set(_CCOPT "-static -std=gnu11 ${CFLAGS_WARNINGS} -O3 -fPIC -fno-ident -ffast-math -fno-strict-aliasing -ftree-vectorize -mfpmath=sse -mmmx -msse2 -fno-asynchronous-unwind-tables -ffunction-sections -Wl,--gc-sections") - if(NOT DAP_DBG_INFO) - add_definitions ("-DNDEBUG") - set(_CCOPT "${_CCOPT} -Wl,--strip-all") - endif() - endif() - - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_CCOPT} ") - set(CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} ${_LOPT}") - - include_directories(../../dap-sdk/3rdparty/uthash/src/) - include_directories(../../dap-sdk/3rdparty/json-c) - include_directories(3rdparty/wepoll/) - #include_directories(libdap-chain-net-srv-vpn/) -endif() diff --git a/dap-sdk b/dap-sdk index 79146c12e8..d6ff75b22f 160000 --- a/dap-sdk +++ b/dap-sdk @@ -1 +1 @@ -Subproject commit 79146c12e8a59a9fe77e6ae54997a70791f8f036 +Subproject commit d6ff75b22f70b79af1b54ee8d5da03252c8d599a diff --git a/modules/consensus/esbocs/dap_chain_cs_esbocs.c b/modules/consensus/esbocs/dap_chain_cs_esbocs.c index 799426851a..9cb9695fea 100644 --- a/modules/consensus/esbocs/dap_chain_cs_esbocs.c +++ b/modules/consensus/esbocs/dap_chain_cs_esbocs.c @@ -2352,8 +2352,11 @@ static void s_message_send(dap_chain_esbocs_session_t *a_session, uint8_t a_mess { dap_chain_net_t *l_net = dap_chain_net_by_id(a_session->chain->net_id); size_t l_message_size = sizeof(dap_chain_esbocs_message_hdr_t) + a_data_size; - dap_chain_esbocs_message_t *l_message = - DAP_NEW_Z_SIZE(dap_chain_esbocs_message_t, l_message_size); + dap_chain_esbocs_message_t *l_message = DAP_NEW_Z_SIZE(dap_chain_esbocs_message_t, l_message_size); + if (!l_message) { + log_it(L_CRITICAL, "Memory allocation error"); + return; + } l_message->hdr.version = DAP_CHAIN_ESBOCS_PROTOCOL_VERSION; l_message->hdr.round_id = a_session->cur_round.id; l_message->hdr.attempt_num = a_session->cur_round.attempt_num; @@ -2370,6 +2373,10 @@ static void s_message_send(dap_chain_esbocs_session_t *a_session, uint8_t a_mess size_t l_sign_size = dap_sign_get_size(l_sign); l_message_size += l_sign_size; l_message = DAP_REALLOC(l_message, l_message_size); + if (!l_message) { + log_it(L_CRITICAL, "Memory allocation error"); + return; + } memcpy(l_message->msg_n_sign + a_data_size, l_sign, l_sign_size); DAP_DELETE(l_sign); l_message->hdr.sign_size = l_sign_size; @@ -2574,6 +2581,11 @@ static dap_chain_datum_decree_t *s_esbocs_decree_set_min_validators_count(dap_ch if (l_sign) { size_t l_sign_size = dap_sign_get_size(l_sign); l_decree = DAP_REALLOC(l_decree, sizeof(dap_chain_datum_decree_t) + l_cur_sign_offset + l_sign_size); + if (!l_decree) { + log_it(L_CRITICAL, "Memory allocation error"); + DAP_DELETE(l_sign); + return NULL; + } memcpy((byte_t*)l_decree->data_n_signs + l_cur_sign_offset, l_sign, l_sign_size); l_total_signs_size += l_sign_size; l_cur_sign_offset += l_sign_size; diff --git a/modules/net/dap_chain_ledger.c b/modules/net/dap_chain_ledger.c index 9e7b8576a9..366908e66e 100644 --- a/modules/net/dap_chain_ledger.c +++ b/modules/net/dap_chain_ledger.c @@ -3804,7 +3804,7 @@ int dap_ledger_tx_cache_check(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx debug_if(s_debug_more && !a_from_threshold, L_INFO, "Previous transaction was found for hash %s",l_tx_prev_hash_str); // 2. Check if out in previous transaction has spent - dap_hash_fast_t l_spender; + dap_hash_fast_t l_spender = {}; if (s_ledger_tx_hash_is_used_out_item(l_item_out, l_tx_prev_out_idx, &l_spender)) { l_err_num = DAP_LEDGER_TX_CHECK_OUT_ITEM_ALREADY_USED; char l_hash[DAP_CHAIN_HASH_FAST_STR_SIZE]; diff --git a/modules/net/dap_chain_node_cli_cmd.c b/modules/net/dap_chain_node_cli_cmd.c index 30da15a32b..a167020174 100644 --- a/modules/net/dap_chain_node_cli_cmd.c +++ b/modules/net/dap_chain_node_cli_cmd.c @@ -1088,7 +1088,9 @@ int com_node(int a_argc, char ** a_argv, char **a_str_reply) dap_chain_node_addr_t l_node_addr = { 0 }; dap_chain_node_addr_t l_link = { 0 }; dap_chain_node_info_t *l_node_info = NULL; - size_t l_node_info_size = sizeof(l_node_info->hdr) + sizeof(l_link); + + //TODO need to rework with new node info / alias /links concept + size_t l_node_info_size = sizeof(*l_node_info) + sizeof(l_link); if(cmd_num >= CMD_ADD && cmd_num <= CMD_DEL) { l_node_info = DAP_NEW_Z_SIZE(dap_chain_node_info_t, l_node_info_size); if (!l_node_info) { @@ -4498,7 +4500,7 @@ int com_mempool(int a_argc, char **a_argv, char **a_str_reply){ dap_chain_t *l_chain = NULL; const char *l_addr_b58 = NULL; enum _subcmd {SUBCMD_LIST, SUBCMD_PROC, SUBCMD_PROC_ALL, SUBCMD_DELETE, SUBCMD_ADD_CA, SUBCMD_CHECK, SUBCMD_DUMP}; - enum _subcmd l_cmd; + enum _subcmd l_cmd = 0; if (a_argv[1]) { char *lts = a_argv[1]; if (!dap_strcmp(lts, "list")) { diff --git a/modules/service/stake/dap_chain_net_srv_stake_pos_delegate.c b/modules/service/stake/dap_chain_net_srv_stake_pos_delegate.c index 6a902ca33d..055145bcba 100644 --- a/modules/service/stake/dap_chain_net_srv_stake_pos_delegate.c +++ b/modules/service/stake/dap_chain_net_srv_stake_pos_delegate.c @@ -663,6 +663,11 @@ dap_chain_datum_decree_t *dap_chain_net_srv_stake_decree_approve(dap_chain_net_t if (l_sign) { size_t l_sign_size = dap_sign_get_size(l_sign); l_decree = DAP_REALLOC(l_decree, sizeof(dap_chain_datum_decree_t) + l_cur_sign_offset + l_sign_size); + if (!l_decree) { + log_it(L_CRITICAL, "Memory allocation error"); + DAP_DELETE(l_sign); + return NULL; + } memcpy((byte_t*)l_decree->data_n_signs + l_cur_sign_offset, l_sign, l_sign_size); l_total_signs_size += l_sign_size; l_cur_sign_offset += l_sign_size; @@ -878,6 +883,11 @@ static dap_chain_datum_decree_t *s_stake_decree_invalidate(dap_chain_net_t *a_ne if (l_sign) { size_t l_sign_size = dap_sign_get_size(l_sign); l_decree = DAP_REALLOC(l_decree, sizeof(dap_chain_datum_decree_t) + l_cur_sign_offset + l_sign_size); + if (!l_decree) { + log_it(L_CRITICAL, "Memory allocation error"); + DAP_DELETE(l_sign); + return NULL; + } memcpy((byte_t*)l_decree->data_n_signs + l_cur_sign_offset, l_sign, l_sign_size); l_total_signs_size += l_sign_size; l_cur_sign_offset += l_sign_size; @@ -954,6 +964,11 @@ static dap_chain_datum_decree_t *s_stake_decree_set_min_stake(dap_chain_net_t *a if (l_sign) { size_t l_sign_size = dap_sign_get_size(l_sign); l_decree = DAP_REALLOC(l_decree, sizeof(dap_chain_datum_decree_t) + l_cur_sign_offset + l_sign_size); + if (!l_decree) { + log_it(L_CRITICAL, "Memory allocation error"); + DAP_DELETE(l_sign); + return NULL; + } memcpy((byte_t*)l_decree->data_n_signs + l_cur_sign_offset, l_sign, l_sign_size); l_total_signs_size += l_sign_size; l_cur_sign_offset += l_sign_size; diff --git a/modules/service/xchange/dap_chain_net_srv_xchange.c b/modules/service/xchange/dap_chain_net_srv_xchange.c index 97c2df6746..30344c5d64 100644 --- a/modules/service/xchange/dap_chain_net_srv_xchange.c +++ b/modules/service/xchange/dap_chain_net_srv_xchange.c @@ -509,7 +509,7 @@ static dap_chain_datum_tx_t *s_xchange_tx_create_exchange(dap_chain_net_srv_xcha bool l_net_fee_used = dap_chain_net_tx_get_fee(a_price->net->pub.id, &l_net_fee, &l_net_fee_addr); if (l_net_fee_used) SUM_256_256(l_net_fee, a_price->fee, &l_total_fee); - uint16_t l_service_fee_type; + uint16_t l_service_fee_type = 0; bool l_service_fee_used = s_srv_xchange_get_fee(a_price->net->pub.id, &l_service_fee, &l_service_fee_addr, &l_service_fee_type); if (l_service_fee_used) { switch (l_service_fee_type) { diff --git a/modules/type/dag/dap_chain_cs_dag_event.c b/modules/type/dag/dap_chain_cs_dag_event.c index 2150c23428..345524da61 100644 --- a/modules/type/dag/dap_chain_cs_dag_event.c +++ b/modules/type/dag/dap_chain_cs_dag_event.c @@ -71,10 +71,13 @@ dap_chain_cs_dag_event_t *dap_chain_cs_dag_event_new(dap_chain_id_t a_chain_id, dap_sign_t * l_sign = dap_sign_create(a_key, l_event_new, l_event_size, 0); if ( l_sign ){ size_t l_sign_size = dap_sign_get_size(l_sign); - l_event_new = (dap_chain_cs_dag_event_t *)DAP_REALLOC(l_event_new, l_event_size + l_sign_size ); - memcpy(l_event_new->hashes_n_datum_n_signs + l_hashes_size + l_datum_size, l_sign, l_sign_size); l_event_size += l_sign_size; - l_event_new = (dap_chain_cs_dag_event_t* )DAP_REALLOC(l_event_new, l_event_size); + l_event_new = (dap_chain_cs_dag_event_t *)DAP_REALLOC(l_event_new, l_event_size); + if (!l_event_new) { + log_it(L_CRITICAL, "Not enough memeory"); + DAP_DELETE(l_sign); + return NULL; + } memcpy(l_event_new->hashes_n_datum_n_signs + l_hashes_size + l_datum_size, l_sign, l_sign_size); l_event_new->header.signs_count++; log_it(L_INFO,"Created event size %zd, signed with sign size %zd", l_event_size, l_sign_size); diff --git a/modules/wallet/CMakeLists.txt b/modules/wallet/CMakeLists.txt index 0b4cc30c88..977a228473 100644 --- a/modules/wallet/CMakeLists.txt +++ b/modules/wallet/CMakeLists.txt @@ -1,17 +1,13 @@ cmake_minimum_required(VERSION 3.10) project (dap_chain_wallet) -file(GLOB DAP_CHAIN_WALLET_SRCS *.c) +file(GLOB DAP_CHAIN_WALLET_SRCS *.c ../../3rdparty/crc32c_adler/crc32c_adler.c) file(GLOB DAP_CHAIN_WALLET_HEADERS include/*.h) +include_directories(../../3rdparty/crc32c_adler) add_library(${PROJECT_NAME} STATIC ${DAP_CHAIN_WALLET_SRCS} ${DAP_CHAIN_WALLET_HEADERS}) -target_link_libraries(${PROJECT_NAME} dap_core dap_crypto dap_chain dap_chain_net crc32c_adler) -if (CELLFRAME_SDK_STANDALONE_BUILD) - target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../dap-sdk/3rdparty/crc32c_adler) -else() - target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../../dap-sdk/3rdparty/crc32c_adler) -endif() +target_link_libraries(${PROJECT_NAME} dap_core dap_crypto dap_chain dap_chain_net) target_include_directories(${PROJECT_NAME} INTERFACE .) target_include_directories(${PROJECT_NAME} PUBLIC include) @@ -22,4 +18,4 @@ INSTALL(TARGETS ${PROJECT_NAME} ARCHIVE DESTINATION lib/modules/wallet/ PUBLIC_HEADER DESTINATION include/modules/wallet/ ) -endif() \ No newline at end of file +endif() -- GitLab