Skip to content
This repository was archived by the owner on Feb 15, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion external/secp256k1
3 changes: 2 additions & 1 deletion include/ccoin/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* file COPYING or http://www.opensource.org/licenses/mit-license.php.
*/

#include <stdbool.h>
#include <stdbool.h> // for bool
#include <stddef.h> // for size_t

#ifdef __cplusplus
extern "C" {
Expand Down
20 changes: 11 additions & 9 deletions include/ccoin/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
* file COPYING or http://www.opensource.org/licenses/mit-license.php.
*/

#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <ccoin/buffer.h>
#include <ccoin/buint.h>
#include <ccoin/coredefs.h>
#include <ccoin/hashtab.h>
#include <ccoin/cstr.h>
#include <ccoin/parr.h>
#include <ccoin/buffer.h> // for const_buffer
#include <ccoin/buint.h> // for bu256_t, bu256_equal, etc
#include <ccoin/coredefs.h> // for ::COIN
#include <ccoin/cstr.h> // for cstring
#include <ccoin/hashtab.h> // for bitc_hashtab_get, etc
#include <ccoin/parr.h> // for parr, parr_idx

#include <stdbool.h> // for bool, false, true
#include <stdint.h> // for uint32_t, int64_t, uint16_t, etc
#include <string.h> // for memcpy, memset, NULL

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -116,6 +117,7 @@ static inline void bp_outpt_copy(struct bp_outpt *dest,

struct bp_txin {
struct bp_outpt prevout;
parr* scriptWitness;
cstring *scriptSig;
uint32_t nSequence;
};
Expand Down
15 changes: 12 additions & 3 deletions include/ccoin/coredefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,23 @@ extern "C" {
#endif

enum {
//! BIP 0031, pong message, is enabled for all versions AFTER this one
BIP0031_VERSION = 60000,
CADDR_TIME_VERSION = 31402,

MIN_PROTO_VERSION = 209,
//! nTime field added to CAddress, starting with this version;
//! if possible, avoid requesting addresses nodes older than this
CADDR_TIME_VERSION = 31402,

MAX_BLOCK_SIZE = 1000000,
//! initial proto version, to be increased after version/verack negotiation
INIT_PROTO_VERSION = 209,

/** The maximum allowed weight for a block, see BIP 141 (network rule) */
MAX_BLOCK_WEIGHT = 4000000,

/** Coinbase transaction outputs can only be spent after this number of new blocks (network rule) */
COINBASE_MATURITY = 100,

WITNESS_SCALE_FACTOR = 4,
};

enum {
Expand Down
121 changes: 100 additions & 21 deletions include/ccoin/script.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
* file COPYING or http://www.opensource.org/licenses/mit-license.php.
*/

#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <ccoin/buffer.h>
#include <ccoin/core.h>
#include <ccoin/clist.h>
#include <ccoin/buint.h>
#include <ccoin/key.h>
#include <ccoin/parr.h>
#include <ccoin/buffer.h> // for const_buffer
#include <ccoin/buint.h> // for bu256_t
#include <ccoin/clist.h> // for clist
#include <ccoin/core.h> // for bp_tx
#include <ccoin/cstr.h> // for cstring, cstr_append_buf
#include <ccoin/key.h> // for bp_keystore
#include <ccoin/parr.h> // for parr

#include <stdbool.h> // for bool, false
#include <stddef.h> // for size_t
#include <stdint.h> // for uint8_t, int64_t, uint64_t

#ifdef __cplusplus
extern "C" {
Expand All @@ -31,6 +33,9 @@ static const int MAX_PUBKEYS_PER_MULTISIG = 20;
// Maximum script length in bytes
static const int MAX_SCRIPT_SIZE = 10000;

// Maximum number of values on script interpreter stack
static const int MAX_STACK_SIZE = 1000;

// Threshold for nLockTime: below this value it is interpreted as block number,
// otherwise as UNIX timestamp.
static const unsigned int LOCKTIME_THRESHOLD = 500000000; // Tue Nov 5 00:53:20 1985 UTC
Expand All @@ -45,20 +50,88 @@ enum
};

/** Script verification flags */
enum
{
enum {
SCRIPT_VERIFY_NONE = 0,

// Evaluate P2SH subscripts (softfork safe, BIP16).
SCRIPT_VERIFY_P2SH = (1U << 0),

// Passing a non-strict-DER signature or one with undefined hashtype to a checksig operation causes script failure.
// Evaluating a pubkey that is not (0x04 + 64 bytes) or (0x02 or 0x03 + 32 bytes) by checksig causes script failure.
// (softfork safe, but not used or intended as a consensus rule).
SCRIPT_VERIFY_STRICTENC = (1U << 1),

// Passing a non-strict-DER signature to a checksig operation causes script failure (softfork safe, BIP62 rule 1)
SCRIPT_VERIFY_DERSIG = (1U << 2),

// Passing a non-strict-DER signature or one with S > order/2 to a checksig operation causes script failure
// (softfork safe, BIP62 rule 5).
SCRIPT_VERIFY_LOW_S = (1U << 3),

// verify dummy stack item consumed by CHECKMULTISIG is of zero-length (softfork safe, BIP62 rule 7).
SCRIPT_VERIFY_NULLDUMMY = (1U << 4),

// Using a non-push operator in the scriptSig causes script failure (softfork safe, BIP62 rule 2).
SCRIPT_VERIFY_SIGPUSHONLY = (1U << 5),

// Require minimal encodings for all push operations (OP_0... OP_16, OP_1NEGATE where possible, direct
// pushes up to 75 bytes, OP_PUSHDATA up to 255 bytes, OP_PUSHDATA2 for anything larger). Evaluating
// any other push causes the script to fail (BIP62 rule 3).
// In addition, whenever a stack element is interpreted as a number, it must be of minimal length (BIP62 rule 4).
// (softfork safe)
SCRIPT_VERIFY_MINIMALDATA = (1U << 6),
SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS = (1U << 7),

// Discourage use of NOPs reserved for upgrades (NOP1-10)
//
// Provided so that nodes can avoid accepting or mining transactions
// containing executed NOP's whose meaning may change after a soft-fork,
// thus rendering the script invalid; with this flag set executing
// discouraged NOPs fails the script. This verification flag will never be
// a mandatory flag applied to scripts in a block. NOPs that are not
// executed, e.g. within an unexecuted IF ENDIF block, are *not* rejected.
SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS = (1U << 7),

// Require that only a single stack element remains after evaluation. This changes the success criterion from
// "At least one stack element must remain, and when interpreted as a boolean, it must be true" to
// "Exactly one stack element must remain, and when interpreted as a boolean, it must be true".
// (softfork safe, BIP62 rule 6)
// Note: CLEANSTACK should never be used without P2SH or WITNESS.
SCRIPT_VERIFY_CLEANSTACK = (1U << 8),

// Verify CHECKLOCKTIMEVERIFY
//
// See BIP65 for details.
SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY = (1U << 9),

// support CHECKSEQUENCEVERIFY opcode
//
// See BIP112 for details
SCRIPT_VERIFY_CHECKSEQUENCEVERIFY = (1U << 10),

// Support segregated witness
//
SCRIPT_VERIFY_WITNESS = (1U << 11),

// Making v1-v16 witness program non-standard
//
SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM = (1U << 12),

// Segwit script only: Require the argument of OP_IF/NOTIF to be exactly 0x01 or empty vector
//
SCRIPT_VERIFY_MINIMALIF = (1U << 13),

// Signature(s) must be empty vector if an CHECK(MULTI)SIG operation failed
//
SCRIPT_VERIFY_NULLFAIL = (1U << 14),

// Public keys in segregated witness scripts must be compressed
//
SCRIPT_VERIFY_WITNESS_PUBKEYTYPE = (1U << 15),
};

enum SigVersion {
SIGVERSION_BASE = 0,
SIGVERSION_WITNESS_V0 = 1,
};

enum txnouttype
Expand Down Expand Up @@ -209,7 +282,6 @@ enum opcodetype
OP_NOP10 = 0xb9,



// template matching params
OP_SMALLINTEGER = 0xfa,
OP_PUBKEYS = 0xfb,
Expand Down Expand Up @@ -244,11 +316,13 @@ extern enum opcodetype GetOpType(const char *opname);
*/

extern bool bsp_getop(struct bscript_op *op, struct bscript_parser *bp);
extern unsigned int bsp_get_sigopcount(struct const_buffer* buf, bool fAccurate);
extern parr *bsp_parse_all(const void *data_, size_t data_len);
extern enum txnouttype bsp_classify(parr *ops);
extern bool bsp_addr_parse(struct bscript_addr *addr,
const void *data, size_t data_len);
extern void bsp_addr_free(struct bscript_addr *addr);
extern bool is_bsp_witnessprogram(const cstring* s, int* version, cstring* program);
extern bool is_bsp_pushonly(struct const_buffer *buf);
extern bool is_bsp_pubkey(parr *ops);
extern bool is_bsp_pubkeyhash(parr *ops);
Expand All @@ -275,6 +349,12 @@ static inline bool is_bsp_p2sh_str(const cstring *s)
return is_bsp_p2sh(&buf);
}

static inline bool is_bsp_p2wsh(struct const_buffer* buf)
{
const unsigned char* vch = (const unsigned char*)(buf->p);
return (buf->len == 34 && vch[0] == OP_0 && vch[1] == 0x20);
}

static inline void bsp_start(struct bscript_parser *bp,
struct const_buffer *buf)
{
Expand All @@ -286,14 +366,13 @@ static inline void bsp_start(struct bscript_parser *bp,
* script validation and signing
*/

extern void bp_tx_sighash(bu256_t *hash, const cstring *scriptCode,
const struct bp_tx *txTo, unsigned int nIn,
int nHashType);
extern bool bp_script_verify(const cstring *scriptSig, const cstring *scriptPubKey,
const struct bp_tx *txTo, unsigned int nIn,
unsigned int flags, int nHashType);
extern bool bp_verify_sig(const struct bp_utxo *txFrom, const struct bp_tx *txTo,
unsigned int nIn, unsigned int flags, int nHashType);
extern void bp_tx_sighash(bu256_t* hash, const cstring* scriptCode, const struct bp_tx* txTo,
unsigned int nIn, int nHashType, int64_t amount, enum SigVersion sigversion);
extern bool bp_script_verify(const cstring* scriptSig, const cstring* scriptPubKey,
parr** witness, const struct bp_tx* txTo, unsigned int nIn,
unsigned int flags, int nHashType, int64_t amount);
extern bool bp_verify_sig(const struct bp_utxo* txFrom,const struct bp_tx* txTo,
unsigned int nIn, unsigned int flags, int nHashType, int64_t amount);

extern bool bp_script_sign(struct bp_keystore *ks, const cstring *fromPubKey,
const struct bp_tx *txTo, unsigned int nIn,
Expand Down
20 changes: 13 additions & 7 deletions include/ccoin/serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
* file COPYING or http://www.opensource.org/licenses/mit-license.php.
*/

#include <stdint.h>
#include <stdbool.h>
#include <gmp.h>
#include <ccoin/buffer.h>
#include <ccoin/buint.h>
#include <ccoin/cstr.h>
#include <ccoin/parr.h>
#include <ccoin/buffer.h> // for const_buffer
#include <ccoin/buint.h> // for bu256_t
#include <ccoin/cstr.h> // for cstring
#include <ccoin/parr.h> // for parr

#include <gmp.h> // for mpz_t

#include <stdbool.h> // for bool
#include <stddef.h> // for size_t
#include <stdint.h> // for uint32_t, uint64_t, int64_t, etc


#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -43,6 +47,7 @@ static inline void ser_s64(cstring *s, int64_t v_)
}

extern void ser_u256_array(cstring *s, parr *arr);
extern void ser_varlen_array(cstring *s, parr *arr);

extern bool deser_skip(struct const_buffer *buf, size_t len);
extern bool deser_bytes(void *po, struct const_buffer *buf, size_t len);
Expand All @@ -66,6 +71,7 @@ static inline bool deser_s64(int64_t *vo, struct const_buffer *buf)
}

extern bool deser_u256_array(parr **ao, struct const_buffer *buf);
extern bool deser_varlen_array(parr **ao, struct const_buffer *buf);

extern void u256_from_compact(mpz_t vo, uint32_t c);

Expand Down
2 changes: 1 addition & 1 deletion lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ AM_CPPFLAGS = -I$(top_srcdir)/include \

lib_LTLIBRARIES= libccoin.la

libccoin_la_LIBADD= -lm \
libccoin_la_LIBADD = @MATH_LIBS@ \
$(top_builddir)/external/secp256k1/libsecp256k1.la

libccoin_la_SOURCES= \
Expand Down
28 changes: 19 additions & 9 deletions lib/block.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@
*/
#include "picocoin-config.h"

#include <string.h>
#include <time.h>
#include <ccoin/core.h>
#include <ccoin/util.h>
#include <ccoin/parr.h>
#include <ccoin/coredefs.h>
#include <ccoin/serialize.h>
#include <ccoin/buint.h> // for bu256_t, bu256_new, etc
#include <ccoin/core.h> // for bitc_block, bitc_tx, etc
#include <ccoin/coredefs.h> // for ::MAX_BLOCK_WEIGHT, etc
#include <ccoin/cstr.h> // for cstring
#include <ccoin/parr.h> // for parr, parr_idx, parr_add, etc
#include <ccoin/serialize.h> // for u256_from_compact
#include <ccoin/util.h> // for bu_Hash_, MIN

#include <gmp.h> // for mpz_clear, mpz_init, mpz_t, etc

#include <stdbool.h> // for false, bool, true
#include <stdint.h> // for int64_t
#include <string.h> // for NULL, memset
#include <time.h> // for time, time_t

static bool bp_has_dup_inputs(const struct bp_tx *tx)
{
Expand Down Expand Up @@ -46,7 +53,7 @@ bool bp_tx_valid(const struct bp_tx *tx)
return false;

// Size limits
if (bp_tx_ser_size(tx) > MAX_BLOCK_SIZE)
if (bp_tx_ser_size(tx) * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT)
return false;

// Check for negative or overflow output values
Expand Down Expand Up @@ -215,7 +222,10 @@ bool bp_block_valid(struct bp_block *block)
if (!block->vtx || !block->vtx->len)
return false;

if (bp_block_ser_size(block) > MAX_BLOCK_SIZE)
if (block->vtx->len * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT)
return false;

if (bp_block_ser_size(block) * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT)
return false;

if (!bp_block_valid_target(block)) return false;
Expand Down
6 changes: 6 additions & 0 deletions lib/checkpoints.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ static const struct bp_checkpoint bp_ck_main[] = {
{134444, "0x00000000000005b12ffd4cd315cd34ffd4a594f430ac814c91184a0d42d2b0fe"},
{168000, "0x000000000000099e61ea72015e79632f216fe6cb33d7899acb35b75c8303b763"},
{193000, "0x000000000000059f452a5f7340de6682a977387c17010ff6e6c3bd83ca8b1317"},
{210000, "0x000000000000048b95347e83192f69cf0366076336c639f9b7228e9ba171342e"},
{216116, "0x00000000000001b4f4b433e81ee46494af945cf96014816a4e2370f11b23df4e"},
{225430, "0x00000000000001c108384350f74090433e7fcf79a606b8e797f065b130575932"},
{250000, "0x000000000000003887df1f29024b06fc2200b55f8af8f35453d7be294df2d214"},
{279000, "0x0000000000000001ae8c72a0b0c301f67e3afca10e819efa9041e458e9bd7e40"},
{295000, "0x00000000000000004d9b4ef50f0f9d686fd69db2e03af35a100370c64632a983"},
};

static const struct bp_checkpoint bp_ck_testnet3[] = {
Expand Down
Loading