Skip to content

Commit

Permalink
consensus: add arith_uint256, auxpow, pow, uint256 and validation files
Browse files Browse the repository at this point in the history
  • Loading branch information
xanimo committed Jun 17, 2023
1 parent c62536e commit 277168d
Show file tree
Hide file tree
Showing 23 changed files with 1,563 additions and 11 deletions.
10 changes: 10 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ noinst_HEADERS = \
include/bip39/index.h \
include/dogecoin/address.h \
include/dogecoin/aes.h \
include/dogecoin/arith_uint256.h \
include/dogecoin/auxpow.h \
include/dogecoin/base58.h \
include/dogecoin/bip32.h \
include/dogecoin/bip39.h \
Expand All @@ -33,6 +35,7 @@ noinst_HEADERS = \
include/dogecoin/byteswap.h \
include/dogecoin/chainparams.h \
include/dogecoin/common.h \
include/dogecoin/consensus.h \
include/dogecoin/cstr.h \
include/dogecoin/ctaes.h \
include/dogecoin/ecc.h \
Expand All @@ -44,6 +47,7 @@ noinst_HEADERS = \
include/dogecoin/mem.h \
include/dogecoin/moon.h \
include/dogecoin/portable_endian.h \
include/dogecoin/pow.h \
include/qr/png.h \
include/qr/qr.h \
include/qr/jpeg.h \
Expand All @@ -58,6 +62,7 @@ noinst_HEADERS = \
include/dogecoin/transaction.h \
include/dogecoin/tx.h \
include/dogecoin/utils.h \
include/dogecoin/validation.h \
include/dogecoin/vector.h \
include/dogecoin/wow.h

Expand All @@ -67,13 +72,16 @@ pkgconfig_DATA = libdogecoin.pc
libdogecoin_la_SOURCES = \
src/address.c \
src/aes.c \
src/arith_uint256.c \
src/auxpow.c \
src/base58.c \
src/bip32.c \
src/bip39.c \
src/bip44.c \
src/block.c \
src/buffer.c \
src/chainparams.c \
src/consensus.c \
src/cstr.c \
src/ctaes.c \
src/ecc.c \
Expand All @@ -83,6 +91,7 @@ libdogecoin_la_SOURCES = \
src/map.c \
src/mem.c \
src/moon.c \
src/pow.c \
src/png.c \
src/jpeg.c \
src/qrengine.c \
Expand All @@ -98,6 +107,7 @@ libdogecoin_la_SOURCES = \
src/transaction.c \
src/tx.c \
src/utils.c \
src/validation.c \
src/vector.c

if USE_SSE2
Expand Down
60 changes: 60 additions & 0 deletions include/dogecoin/arith_uint256.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
The MIT License (MIT)
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_ARITH_UINT256_H__
#define __LIBDOGECOIN_ARITH_UINT256_H__

#include <assert.h>
#include <limits.h>
#include <stdint.h>
#include <string.h>

#include <dogecoin/dogecoin.h>

LIBDOGECOIN_BEGIN_DECL

struct uint_err {
const char* str;
};

static unsigned int BITS;

typedef struct base_uint_ {
int WIDTH; // BITS / 32
uint32_t pn[]; // pn[WIDTH]
} base_uint_;

typedef base_uint_ arith_uint256;

arith_uint256 set_compact(arith_uint256 hash, uint32_t compact, dogecoin_bool *pf_negative, dogecoin_bool *pf_overflow);
uint256* arith_to_uint256(const arith_uint256 a);
arith_uint256 uint_to_arith(const uint256* a);
uint64_t get_low64(arith_uint256 pn);

LIBDOGECOIN_END_DECL

#endif // __LIBDOGECOIN_ARITH_UINT256_H__
49 changes: 49 additions & 0 deletions include/dogecoin/auxpow.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
The MIT License (MIT)
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_AUXPOW__
#define __LIBDOGECOIN_AUXPOW__

#include <dogecoin/block.h>
#include <dogecoin/dogecoin.h>
#include <dogecoin/tx.h>
#include <dogecoin/vector.h>

LIBDOGECOIN_BEGIN_DECL

#define BLOCK_VERSION_AUXPOW_BIT 0x100

/** Header for merge-mining data in the coinbase. */
static const unsigned char pchMergedMiningHeader[] = { 0xfa, 0xbe, 'm', 'm' };

int get_expected_index(uint32_t nNonce, int nChainId, unsigned h);
uint256* check_merkle_branch(uint256 hash, const vector* parent_coinbase_merkle, int n_index);
void init_aux_pow(dogecoin_block_header* block);

LIBDOGECOIN_END_DECL

#endif // __LIBDOGECOIN_AUXPOW__
15 changes: 14 additions & 1 deletion include/dogecoin/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,39 @@ LIBDOGECOIN_BEGIN_DECL
#include <dogecoin/map.h>
#include <dogecoin/tx.h>

typedef struct _auxpow {
dogecoin_bool is;
dogecoin_bool (*check)(void* ctx, uint256* hash, uint32_t chainid, dogecoin_chainparams* params);
void *ctx;
} auxpow;

typedef struct dogecoin_block_header_ {
int32_t version;
uint32_t version;
uint256 prev_block;
uint256 merkle_root;
uint32_t timestamp;
uint32_t bits;
uint32_t nonce;
auxpow auxpow[1];
} dogecoin_block_header;

typedef struct dogecoin_auxpow_block_ {
dogecoin_block_header* header;
dogecoin_tx* parent_coinbase;
uint256 parent_hash;
uint8_t parent_merkle_count;
uint256* parent_coinbase_merkle;
uint32_t parent_merkle_index;
uint8_t aux_merkle_count;
uint32_t aux_merkle_index;
dogecoin_block_header* parent_header;
} dogecoin_auxpow_block;

typedef struct dogecoin_block_ {
dogecoin_auxpow_block* block;
vector* vtx; // vector of transactions
} 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
3 changes: 3 additions & 0 deletions include/dogecoin/chainparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ typedef struct dogecoin_chainparams_ {
uint256 genesisblockhash;
int default_port;
dogecoin_dns_seed dnsseeds[8];
dogecoin_bool strict_id;
dogecoin_bool auxpow_id;
uint256 pow_limit;
} dogecoin_chainparams;

typedef struct dogecoin_checkpoint_ {
Expand Down
171 changes: 171 additions & 0 deletions include/dogecoin/consensus.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/*
The MIT License (MIT)
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_CONSENSUS_H__
#define __LIBDOGECOIN_CONSENSUS_H__

#include <stdint.h>

#include <dogecoin/block.h>
#include <dogecoin/dogecoin.h>
#include <dogecoin/tx.h>
#include <dogecoin/utils.h>
#include <dogecoin/vector.h>

LIBDOGECOIN_BEGIN_DECL

/** The maximum allowed size for a serialized block, in bytes (only for buffer size limits) */
static const unsigned int MAX_BLOCK_SERIALIZED_SIZE = 4000000;
/** The maximum allowed weight for a block, see BIP 141 (network rule) */
static const unsigned int MAX_BLOCK_WEIGHT = 4000000;
/** The maximum allowed size for a block excluding witness data, in bytes (network rule) */
static const unsigned int MAX_BLOCK_BASE_SIZE = 1000000;
/** The maximum allowed number of signature check operations in a block (network rule) */
static const int64_t MAX_BLOCK_SIGOPS_COST = 80000;

/** Flags for nSequence and nLockTime locks */
enum {
/* Interpret sequence numbers as relative lock-time constraints. */
LOCKTIME_VERIFY_SEQUENCE = (1 << 0),

/* Use GetMedianTimePast() instead of nTime for end point timestamp. */
LOCKTIME_MEDIAN_TIME_PAST = (1 << 1),
};

/** Merkle */
uint256* compute_merkle_root(const vector* leaves, dogecoin_bool* mutated);
vector* compute_merkle_branch(const vector* leaves, uint32_t position);
uint256* compute_merkle_root_from_branch(const uint256* leaf, const vector* branch, uint32_t position);

/*
* Compute the Merkle root of the transactions in a block.
* *mutated is set to true if a duplicated subtree was found.
*/
uint256* block_merkle_root(const dogecoin_block* block, dogecoin_bool* mutated);

/*
* Compute the Merkle branch for the tree of transactions in a block, for a
* given position.
* This can be verified using compute_merkle_root_from_branch.
*/
vector* block_merkle_branch(const dogecoin_block* block, uint32_t position);

/** Validation */
/** "reject" message codes */
static const unsigned char REJECT_MALFORMED = 0x01;
static const unsigned char REJECT_INVALID = 0x10;
static const unsigned char REJECT_OBSOLETE = 0x11;
static const unsigned char REJECT_DUPLICATE = 0x12;
static const unsigned char REJECT_NONSTANDARD = 0x40;
static const unsigned char REJECT_DUST = 0x41;
static const unsigned char REJECT_INSUFFICIENTFEE = 0x42;
static const unsigned char REJECT_CHECKPOINT = 0x43;

static enum {
MODE_VALID, //!< everything ok
MODE_INVALID, //!< network rule violation (DoS value may be set)
MODE_ERROR, //!< run-time error
} mode;

typedef struct validation_state {
int mode;
int n_dos;
char* str_reject_reason;
unsigned int ch_reject_code;
dogecoin_bool corruption_possible;
char* str_debug_message;
void (*DoS)(struct validation_state* state, int level, bool ret,
unsigned int ch_reject_code_in, const char* str_reject_reason_in,
bool corruption_in, const char* str_debug_message_in);
dogecoin_bool (*invalid)(struct validation_state* state, bool ret,
unsigned int _ch_reject_code, const char* _str_reject_reason,
const char* _str_debug_message);
dogecoin_bool (*err)(struct validation_state* state, const char* str_reject_reason_in);
} validation_state;

dogecoin_bool DoS(struct validation_state* state, int level, bool ret,
unsigned int ch_reject_code_in, const char* str_reject_reason_in,
bool corruption_in, const char* str_debug_message_in) {
state->ch_reject_code = ch_reject_code_in;
state->str_reject_reason = (char*)str_reject_reason_in;
state->corruption_possible = corruption_in;
state->str_debug_message = (char*)str_debug_message_in;
if (state->mode == MODE_ERROR)
return ret;
state->n_dos += level;
state->mode = MODE_INVALID;
return ret;
}
dogecoin_bool invalid(struct validation_state* state, bool ret,
unsigned int _ch_reject_code, const char* _str_reject_reason,
const char* _str_debug_message) {
return DoS(state, 0, ret, _ch_reject_code, _str_reject_reason, false, _str_debug_message);
}
dogecoin_bool err(struct validation_state* state, const char* str_reject_reason_in) {
if (state->mode == MODE_VALID)
state->str_reject_reason = (char*)str_reject_reason_in;
state->mode = MODE_ERROR;
return false;
}
const dogecoin_bool is_valid(struct validation_state* state) {
return state->mode == MODE_VALID;
}
const dogecoin_bool return_invalid(struct validation_state* state) {
return state->mode == MODE_INVALID;
}
const dogecoin_bool is_err(struct validation_state* state) {
return state->mode == MODE_ERROR;
}
const dogecoin_bool is_invalid(struct validation_state* state, int *n_dos_out) {
if (return_invalid(state)) {
n_dos_out = state->n_dos;
return true;
}
return false;
}
const dogecoin_bool corruption_possible(struct validation_state* state) {
return state->corruption_possible;
}
void set_corruption_possible(struct validation_state* state) {
state->corruption_possible = true;
}
const unsigned int get_reject_code(struct validation_state* state) { return state->ch_reject_code; }
const char* get_reject_reason(struct validation_state* state) { return state->str_reject_reason; }
const char* get_debug_message(struct validation_state* state) { return state->str_debug_message; }

validation_state* init_validation_state() {
validation_state* state = dogecoin_calloc(1, sizeof(*state));
state->mode = MODE_VALID;
state->n_dos = 0;
state->ch_reject_code = 0;
state->corruption_possible = false;
return state;
}

LIBDOGECOIN_END_DECL

#endif // __LIBDOGECOIN_CONSENSUS_H__
Loading

0 comments on commit 277168d

Please sign in to comment.