Skip to content

Commit

Permalink
cmpctblock: prep spv for bip-158
Browse files Browse the repository at this point in the history
-adds psuedo blockencoding class in blockencoding.h
-adds chacha20 psuedo class
-adds fast_random_context psuedo class
  • Loading branch information
xanimo committed Dec 28, 2023
1 parent f4af4e5 commit 2d863f2
Show file tree
Hide file tree
Showing 22 changed files with 777 additions and 93 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,10 @@ TARGET_SOURCES(${LIBDOGECOIN_NAME} PRIVATE
src/bip39.c
src/bip44.c
src/block.c
src/blockencoding.c
src/bloom.c
src/buffer.c
src/chacha20.c
src/cstr.c
src/ctaes.c
src/ecc.c
Expand Down
5 changes: 5 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ noinst_HEADERS = \
include/dogecoin/bip44.h \
include/dogecoin/block.h \
include/dogecoin/blockchain.h \
include/dogecoin/blockencoding.h \
include/dogecoin/bloom.h \
include/dogecoin/buffer.h \
include/dogecoin/byteswap.h \
include/dogecoin/chacha20.h \
include/dogecoin/chainparams.h \
include/dogecoin/common.h \
include/dogecoin/cstr.h \
Expand Down Expand Up @@ -81,8 +83,10 @@ libdogecoin_la_SOURCES = \
src/bip39.c \
src/bip44.c \
src/block.c \
src/blockencoding.c \
src/bloom.c \
src/buffer.c \
src/chacha20.c \
src/chainparams.c \
src/cstr.c \
src/ctaes.c \
Expand Down Expand Up @@ -134,6 +138,7 @@ tests_SOURCES = \
test/block_tests.c \
test/bloom_tests.c \
test/buffer_tests.c \
test/chacha20_tests.c \
test/cstr_tests.c \
test/ecc_tests.c \
test/hash_tests.c \
Expand Down
5 changes: 5 additions & 0 deletions include/dogecoin/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ typedef struct dogecoin_auxpow_block_ {
dogecoin_block_header* parent_header;
} dogecoin_auxpow_block;

typedef struct dogecoin_block_ {
dogecoin_auxpow_block block;
vector vtx;
} dogecoin_block;

LIBDOGECOIN_API dogecoin_block_header* dogecoin_block_header_new();
LIBDOGECOIN_API void dogecoin_block_header_free(dogecoin_block_header* header);
LIBDOGECOIN_API dogecoin_auxpow_block* dogecoin_auxpow_block_new();
Expand Down
87 changes: 87 additions & 0 deletions include/dogecoin/blockencoding.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
The MIT License (MIT)
Copyright (c) 2016 The Bitcoin Core developers
Copyright (c) 2023 bluezr
Copyright (c) 2023 The Dogecoin Foundation
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 __LIBDOGECOIN_BLOCK_ENCODING_H__
#define __LIBDOGECOIN_BLOCK_ENCODING_H__

#include <dogecoin/dogecoin.h>

LIBDOGECOIN_BEGIN_DECL

#include <dogecoin/block.h>
#include <dogecoin/serialize.h>

struct dogecoin_tx_mempool {
dogecoin_tx* tx;
};

struct dogecoin_block_tx_request {
uint256 blockhash;
vector indexes;
};

struct dogecoin_block_txns {
uint256 blockhash;
vector txn;
};

struct dogecoin_prefilled_tx {
uint16_t index;
dogecoin_tx tx;
};

typedef enum read_status_ {
READ_STATUS_OK,
READ_STATUS_INVALID, // Invalid object, peer is sending bogus crap
READ_STATUS_FAILED, // Failed to process object
} read_status;

struct dogecoin_block_header_and_short_txids {
uint64_t short_txid_k0, short_txid_k1;
uint64_t nonce;
void (*fill_short_txid_selector)(struct dogecoin_block_header_and_short_txids);
const int short_txids_length;
vector short_txids;
vector prefilled_txns;
dogecoin_block_header header;
uint64_t (*get_short_id)(struct dogecoin_block_header_and_short_txids, const uint256* txhash);
size_t (*block_tx_count)(struct dogecoin_block_header_and_short_txids);
};

struct partially_downloaded_block {
vector tx_available;
struct dogecoin_tx_mempool* pool;
dogecoin_block_header header;
read_status (*init_data)(struct dogecoin_block_header_and_short_txids* cmpctblock);
dogecoin_bool (*is_tx_available)(size_t index);
read_status (*fill_block)(dogecoin_auxpow_block* block, vector* vtx_missing);
};

LIBDOGECOIN_END_DECL

#endif // __LIBDOGECOIN_BLOCK_ENCODING_H__
3 changes: 3 additions & 0 deletions include/dogecoin/byteswap.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,13 @@ static unsigned char bit_swap_table[256] =
// Non-Mac OS X / non-Darwin

#if HAVE_DECL_BSWAP_8 == 0
DISABLE_WARNING_PUSH
DISABLE_WARNING(-Wunused-function)
LIBDOGECOIN_API static uint8_t bswap_8(uint8_t x)
{
return bit_swap_table[x];
}
DISABLE_WARNING_POP
#endif

#if HAVE_DECL_BSWAP_16 == 0
Expand Down
54 changes: 54 additions & 0 deletions include/dogecoin/chacha20.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
The MIT License (MIT)
Copyright (c) 2017 The Bitcoin Core developers
Copyright (c) 2023 bluezr
Copyright (c) 2023 The Dogecoin Foundation
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 __LIBDOGECOIN_CHACHA20_H__
#define __LIBDOGECOIN_CHACHA20_H__

#include <dogecoin/dogecoin.h>

LIBDOGECOIN_BEGIN_DECL

typedef struct chacha20 {
uint32_t input[16];
void (*setkey)(struct chacha20* this, const unsigned char* key, size_t keylen);
void (*setiv)(struct chacha20* this, uint64_t iv);
void (*seek)(struct chacha20* this, uint64_t pos);
void (*output)(struct chacha20* this, unsigned char* c, size_t bytes);
} chacha20;

struct chacha20* chacha20_new();
struct chacha20* chacha20_init(const unsigned char* key, size_t keylen);
void chacha20_set_key(struct chacha20* this, const unsigned char* key, size_t keylen);
void chacha20_set_iv(struct chacha20* this, uint64_t iv);
void chacha20_seek(struct chacha20* this, uint64_t pos);
void chacha20_output(struct chacha20* this, unsigned char* c, size_t bytes);
void chacha20_free(struct chacha20* this);

LIBDOGECOIN_END_DECL

#endif // __LIBDOGECOIN_CHACHA20_H__
27 changes: 27 additions & 0 deletions include/dogecoin/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ LIBDOGECOIN_API static inline uint64_t read_le64(const unsigned char* ptr)
return le64toh(x);
}

LIBDOGECOIN_API static inline void write_le8(unsigned char* ptr, uint8_t x)
{
uint8_t v = htole8(x);
memcpy_safe(ptr, (char*)&v, 1);
}

LIBDOGECOIN_API static inline void write_le16(unsigned char* ptr, uint16_t x)
{
uint16_t v = htole16(x);
Expand Down Expand Up @@ -85,6 +91,27 @@ LIBDOGECOIN_API static inline void write_be64(unsigned char* ptr, uint64_t x)
memcpy_safe(ptr, (char*)&v, 8);
}

/** Return the smallest number n such that (x >> n) == 0 (or 64 if the highest bit in x is set. */
LIBDOGECOIN_API static inline uint64_t count_bits(uint64_t x)
{
#if HAVE_BUILTIN_CLZL
if (sizeof(unsigned long) >= sizeof(uint64_t)) {
return x ? 8 * sizeof(unsigned long) - __builtin_clzl(x) : 0;
}
#endif
#if HAVE_BUILTIN_CLZLL
if (sizeof(unsigned long long) >= sizeof(uint64_t)) {
return x ? 8 * sizeof(unsigned long long) - __builtin_clzll(x) : 0;
}
#endif
int ret = 0;
while (x) {
x >>= 1;
++ret;
}
return ret;
}

LIBDOGECOIN_END_DECL

#endif // __LIBDOGECOIN_COMMON_H__
3 changes: 1 addition & 2 deletions include/dogecoin/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ LIBDOGECOIN_API void remove_all_hashes();
/* map functions */
typedef struct map {
int index;
int count;
hash *hashes;
void* data;
UT_hash_handle hh;
} map;

Expand Down
16 changes: 8 additions & 8 deletions include/dogecoin/portable_endian.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@

LIBDOGECOIN_BEGIN_DECL

# if __BYTE_ORDER == __LITTLE_ENDIAN
# define htole8(x) (x)
# elif __BYTE_ORDER == __BIG_ENDIAN
# define htole8(x) bswap_8(x)
#else
#error UNKNOWN BYTE ORDER
#endif

#if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__)

#define __WINDOWS__
Expand All @@ -24,14 +32,6 @@ LIBDOGECOIN_BEGIN_DECL

#include <endian.h>

# if __BYTE_ORDER == __LITTLE_ENDIAN
# define htole8(x) (x)
# elif __BYTE_ORDER == __BIG_ENDIAN
# define htole8(x) bswap_8(x)
#else
#error UNKNOWN BYTE ORDER
#endif

#elif defined(__APPLE__)

#include <libkern/OSByteOrder.h>
Expand Down
29 changes: 29 additions & 0 deletions include/dogecoin/random.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,35 @@

LIBDOGECOIN_BEGIN_DECL

#include <dogecoin/chacha20.h>
#include <stdint.h>

typedef struct fast_random_context {
dogecoin_bool requires_seed;
chacha20* rng;
unsigned char bytebuf[64];
int bytebuf_size;
uint64_t bitbuf;
int bitbuf_size;
void (*random_seed)(struct fast_random_context* this);
void (*fill_byte_buffer)(struct fast_random_context* this);
void (*fill_bit_buffer)(struct fast_random_context* this);
uint256* (*rand256)(struct fast_random_context* this);
uint64_t (*rand64)(struct fast_random_context* this);
uint64_t (*randbits)(struct fast_random_context* this, int bits);
uint32_t (*rand32)(struct fast_random_context* this);
dogecoin_bool (*randbool)(struct fast_random_context* this);
} fast_random_context;

struct fast_random_context* init_fast_random_context(dogecoin_bool f_deterministic, const uint256* seed);
uint256* rand256(struct fast_random_context* this);
uint64_t rand64(struct fast_random_context* this);
void free_fast_random_context(struct fast_random_context* this);

static const ssize_t NUM_OS_RANDOM_BYTES = 32;
void get_os_rand(unsigned char* ent32);
void random_sanity_check();

typedef struct dogecoin_rnd_mapper_ {
void (*dogecoin_random_init)(void);
dogecoin_bool (*dogecoin_random_bytes)(uint8_t* buf, uint32_t len, const uint8_t update_seed);
Expand Down
Loading

0 comments on commit 2d863f2

Please sign in to comment.