diff --git a/CMakeLists.txt b/CMakeLists.txt index 91d52042073d26dfac631cf2587e7f280b18a02e..a01ff25cded580e9fe14ef44262887a534468cfa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,32 +4,38 @@ project (dap-chain) set(DAP_CHAIN_SRCS dap_chain_common.c dap_chain_block.c - dap_chain_block_section.c - dap_chain_block_roots.c + dap_chain_block_txs.c + dap_chain_section.c + dap_chain_section_roots.c dap_chain.c dap_chain_coin.c dap_chain_srv.c - dap_chain_tx.c + dap_chain_section_tx.c dap_hash.c dap_hash_fusion.c dap_hash_keccak.c dap_hash_slow.c - + dap_chain_wallet.c ) set(DAP_CHAIN_HEADERS dap_chain_block.h - dap_chain_block_section.h - dap_chain_block_roots.h dap_chain.h - dap_chain_common.h + dap_chain_internal.h + dap_chain_common.h dap_chain_coin.h dap_chain_srv.h - dap_chain_tx.h + dap_chain_section.h + dap_chain_section_roots.h + dap_chain_section_tx.h + dap_chain_section_tx_in.h + dap_chain_section_tx_out.h dap_hash.h dap_hash_fusion.h dap_hash_keccak.h dap_hash_slow.h + dap_chain_wallet.h + dap_chain_wallet_internal.h ) add_subdirectory(monero_crypto) diff --git a/dap_chain.c b/dap_chain.c index 8e797a7989ff8e39b3af2577ae7f566fd076e621..4bb2fb49fd42a7cd225d18e4b1b8a82d7db4be07 100644 --- a/dap_chain.c +++ b/dap_chain.c @@ -19,6 +19,7 @@ */ +#include "dap_chain_internal.h" #include "dap_chain.h" #define LOG_TAG "dap_chain" @@ -47,9 +48,15 @@ void dap_chain_deinit() * @param a_file_name * @return */ -dap_chain_t * dap_chain_open(const char * a_file_name) +dap_chain_t * dap_chain_open(const char * a_file_cache,const char * a_file_storage ) { + dap_chain_t * l_chain = DAP_NEW_Z(dap_chain_t); + DAP_CHAIN_INTERNAL_LOCAL_NEW(l_chain); + l_chain_internal->file_storage_type = 0x0000; // TODO compressed format + l_chain_internal->file_storage blockhain_file = fopen(a_file_name,"a+"); + + return l_chain; } /** diff --git a/dap_chain.h b/dap_chain.h index 33c4e3efec0af91be5451fe5ed9d4bdf1bd96be6..3260fd79efc6ab54504438398a75a3d55d0fe45c 100644 --- a/dap_chain.h +++ b/dap_chain.h @@ -18,9 +18,7 @@ along with any DAP based project. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef _DAP_CHAIN_H_ -#define _DAP_CHAIN_H_ - +#pragma once #include "dap_chain_block.h" typedef struct dap_chain{ @@ -42,5 +40,3 @@ void dap_chain_save(dap_chain_t * a_chain); void dap_chain_info_dump_log(dap_chain_t * a_chain); void dap_chain_close(dap_chain_t * a_chain); - -#endif diff --git a/dap_chain_block.c b/dap_chain_block.c index 9c3800732b9b71ba9f16e208171368660304704f..cd160264b0a412fb1e8767c475d1582d3ff9f96c 100644 --- a/dap_chain_block.c +++ b/dap_chain_block.c @@ -19,7 +19,7 @@ */ #include "dap_common.h" -#include "dap_chain_block_roots.h" +#include "dap_chain_section_roots.h" #include "dap_chain_block.h" diff --git a/dap_chain_block.h b/dap_chain_block.h index 25ab478c192b5ca9a922e0d67d28057f18c51468..f257726a8d7b660497e85669dd412d2b748c68d8 100644 --- a/dap_chain_block.h +++ b/dap_chain_block.h @@ -17,16 +17,15 @@ You should have received a copy of the GNU General Public License along with any DAP based project. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef _DAP_CHAIN_BLOCK_H_ -#define _DAP_CHAIN_BLOCK_H_ +#pragma once #include <stdalign.h> #include <stdint.h> #include <stddef.h> #include "dap_common.h" #include "dap_math_ops.h" #include "dap_chain_common.h" -#include "dap_chain_block_section.h" -#include "dap_chain_block_roots.h" +#include "dap_chain_section.h" +#include "dap_chain_section_roots.h" #define DAP_CHAIN_BLOCK_SIGNATURE 0xDA05BF8E @@ -39,11 +38,10 @@ typedef struct dap_chain_block{ uint32_t signature; /// @param signature @brief Magic number, always equels to DAP_CHAIN_BLOCK_SIGNATURE int32_t version; /// @param version @brief block version (be carefull, signed value, as Bitcoin has) dap_chain_hash_t prev_block; /// @param prev_block Hash of the previous block - dap_chain_hash_t root_main;/// @param root_main Main tree's root for all block's hashes uint64_t timestamp; /// @param timestamp @brief Block create time timestamp uint64_t difficulty; /// difficulty level uint64_t nonce; /// Nonce value to allow header variation for mining - uint32_t section_size; /// @param secion_size Size of section[] array + dap_chain_hash_t root_sections;/// @param root_main Main tree's root for all sections's hashes } header; dap_chain_block_section_t section[]; } DAP_ALIGN_PACKED dap_chain_block_t; @@ -51,4 +49,3 @@ typedef struct dap_chain_block{ int dap_chain_block_init(); void dap_chain_block_deinit(); -#endif diff --git a/dap_chain_block_roots.c b/dap_chain_block_roots.c deleted file mode 100644 index 1b2d6b05b473757e74a38166e35a64a99b5bc85e..0000000000000000000000000000000000000000 --- a/dap_chain_block_roots.c +++ /dev/null @@ -1,4 +0,0 @@ -#include "dap_common.h" -#include "dap_chain_block_roots.h" - -#define LOG_TAG "dap_chain_block_roots" diff --git a/dap_chain_block_roots.h b/dap_chain_block_roots.h deleted file mode 100644 index d1e8acfab92bbac253a8bed2300eda19b4bda7e6..0000000000000000000000000000000000000000 --- a/dap_chain_block_roots.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef _DAP_CHAIN_BLOCK_ROOTS_H_ -#define _DAP_CHAIN_BLOCK_ROOTS_H_ - -#include "dap_common.h" -#include "dap_chain_common.h" - -/** - * @struct dap_chain_block_roots_v1 - * @brief Hash tree roots for block, version 1 - */ -typedef struct dap_chain_block_roots_v1{ - dap_chain_hash_t main; -} DAP_ALIGN_PACKED dap_chain_block_roots_v1_t; - -/** - * @struct dap_chain_block_roots_v2 - * @brief Hash tree roots for block, version 2 - */ -typedef struct dap_chain_block_roots_v2{ - dap_chain_hash_t main; - dap_chain_hash_t txs; -} DAP_ALIGN_PACKED dap_chain_block_roots_v2_t; - -typedef dap_chain_block_roots_v2_t dap_chain_block_roots_t; - -#endif - diff --git a/dap_chain_block_section.c b/dap_chain_block_section.c deleted file mode 100644 index 64604b205036c4920de8ea7563c91612431e48cb..0000000000000000000000000000000000000000 --- a/dap_chain_block_section.c +++ /dev/null @@ -1,3 +0,0 @@ -#include "dap_common.h" - -#define LOG_TAG "dap_chain_block_secion" diff --git a/dap_chain_block_section.h b/dap_chain_block_section.h deleted file mode 100644 index a76e44090cd760014c609706147e1141e4473f47..0000000000000000000000000000000000000000 --- a/dap_chain_block_section.h +++ /dev/null @@ -1,48 +0,0 @@ -/** - * @struct dap_chain_block_section - * @brief section inside the block - */ -#ifndef _DAP_CHAIN_BLOCK_SECTION_H_ -#define _DAP_CHAIN_BLOCK_SECTION_H_ -#include <stdint.h> -#include "dap_common.h" -#include "dap_math_ops.h" -#include "dap_chain_common.h" - -/// First section that must be in any block, with hash tree roots -#define DAP_CHAIN_BLOCK_SECTION_ROOTS 0xffff - -/// End section, means all the rest of the block is empty -#define DAP_CHAIN_BLOCK_SECTION_END 0x0000 - -/// Transaction section -#define DAP_CHAIN_BLOCK_SECTION_TX 0x0100 - -/// Smart contract: EVM code section -#define DAP_CHAIN_BLOCK_SECTION_EVM_CODE 0x0200 - -/// Smart contract: EVM data section -#define DAP_CHAIN_BLOCK_SECTION_EVM_DATA 0x0201 - -/// Public key -#define DAP_CHAIN_BLOCK_SECTION_PKEY 0x0300 - -/// Coin: gold -#define DAP_CHAIN_BLOCK_SECTION_COIN_GOLD 0xff00 - -/// Coin: copper -#define DAP_CHAIN_BLOCK_SECTION_COIN_COPPER 0xff01 - -/// Coin: silver -#define DAP_CHAIN_BLOCK_SECTION_COIN_SILVER 0xff02 - - -typedef struct dap_chain_block_section{ - struct{ - uint16_t type; // Section type - uint32_t size; // section size - } header; - uint8_t data[]; // data -} DAP_ALIGN_PACKED dap_chain_block_section_t; - -#endif diff --git a/dap_wallet.c b/dap_chain_block_txs.c similarity index 100% rename from dap_wallet.c rename to dap_chain_block_txs.c diff --git a/dap_wallet.h b/dap_chain_block_txs.h similarity index 100% rename from dap_wallet.h rename to dap_chain_block_txs.h diff --git a/dap_chain_common.h b/dap_chain_common.h index 34a67094ba8f359761ee7404298dbebc21061a4f..df72b8e518937a62689fa62b53c4e83bfd42b1da 100644 --- a/dap_chain_common.h +++ b/dap_chain_common.h @@ -5,10 +5,18 @@ #include "dap_common.h" #include "dap_math_ops.h" -#define DAP_CHAIN_HASH_SIZE 32 +#define DAP_CHAIN_HASH_SIZE 64 typedef union dap_chain_hash{ uint8_t data[DAP_CHAIN_HASH_SIZE]; } dap_chain_hash_t; +typedef union dap_chain_sig_type{ + uint16_t raw; + enum { + SIG_TYPE_NEWHOPE = 0x0000, + SIG_TYPE_MULTI = 0xffff /// @brief Has inside subset of different signatures and sign composed with all of them + } type; +} dap_chain_sig_type_t; + #endif diff --git a/dap_chain_internal.h b/dap_chain_internal.h new file mode 100644 index 0000000000000000000000000000000000000000..720e15bb85bd17210407e94c1c2f7d415c265bed --- /dev/null +++ b/dap_chain_internal.h @@ -0,0 +1,52 @@ +/* + Copyright (c) 2017-2018 (c) Project "DeM Labs Inc" https://github.com/demlabsinc + All rights reserved. + + This file is part of DAP (Deus Applications Prototypes) the open source project + + DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + DAP is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with any DAP based project. If not, see <http://www.gnu.org/licenses/>. +*/ +#pragma once +#include <stdio.h> +#include <stdint.h> +#include "dap_common.h" +#include "dap_chain.h" + +#define DAP_CHAIN_FILE_SIGNATURE 0xfa340bef153eba48 + +typedef struct dap_chain_file_header +{ + uint64_t signature; + uint32_t version; + uint8_t type; + uint32_t chain_id; +} dap_chain_file_header_t; + +/** + * @struct dap_chain_internal + * @brief Internal blochain data, mostly aggregated +typedef struct dap_chain_internal +{ + FILE * file_cache_idx_blocks; /// @param file_cache @brief Index for blocks + FILE * file_cache_idx_txs; /// @param file_cache @brief Index for cache + FILE * file_cache; /// @param file_cache @brief Cache for raw blocks + FILE * file_storage; /// @param file_cache @brief Cache for raw blocks + uint8_t file_storage_type; /// @param file_storage_type @brief Is file_storage is raw, compressed or smth else +} dap_chain_internal_t; + +#define DAP_CHAIN_INTERNAL(a) ((dap_chain_internal_t *) a->_inheritor ) + +#define DAP_CHAIN_INTERNAL_LOCAL(a) dap_chain_internal_t * l_chain_internal = DAP_CHAIN_INTERNAL(a) + +#define DAP_CHAIN_INTERNAL_LOCAL_NEW(a) dap_chain_internal_t * l_chain_internal = DAP_NEW_Z(dap_chain_internal_t); a->_inheritor = l_chain_internal diff --git a/dap_chain_tx.h b/dap_chain_section.c similarity index 85% rename from dap_chain_tx.h rename to dap_chain_section.c index 74bd78ed3f175ca812569f2e5896ed4358c3f3e1..8264ee3210e2141da933c6cfdcbfbb6e6e7b80d5 100644 --- a/dap_chain_tx.h +++ b/dap_chain_section.c @@ -17,13 +17,7 @@ You should have received a copy of the GNU General Public License along with any DAP based project. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef _DAP_CHAIN_TX_H_ -#define _DAP_CHAIN_TX_H_ -#include "dap_chain_common.h" - -int dap_chain_tx_init(); -void dap_chain_tx_deinit(); - -#endif +#include "dap_common.h" +#define LOG_TAG "dap_chain_block_secion" diff --git a/dap_chain_section.h b/dap_chain_section.h new file mode 100644 index 0000000000000000000000000000000000000000..49e3fcff9aa8b39249428376dd352bf1cd0166d8 --- /dev/null +++ b/dap_chain_section.h @@ -0,0 +1,70 @@ +/* + Copyright (c) 2017-2018 (c) Project "DeM Labs Inc" https://github.com/demlabsinc + All rights reserved. + + This file is part of DAP (Deus Applications Prototypes) the open source project + + DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + DAP is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with any DAP based project. If not, see <http://www.gnu.org/licenses/>. +*/ + + +/** + * @struct dap_chain_block_section + * @brief section inside the block + */ +#pragma once +#include <stdint.h> +#include "dap_common.h" +#include "dap_math_ops.h" +#include "dap_chain_common.h" + + +/// End section, means all the rest of the block is empty +#define DAP_CHAIN_SECTION_END 0x0000 + +/// Transaction header section +#define DAP_CHAIN_SECTION_TX 0x0100 + +/// Transaction request section +#define DAP_CHAIN_SECTION_TX_REQUEST 0x0300 + +/// Smart contract: DVM code section +#define DAP_CHAIN_SECTION_DVM_CODE 0x0900 +/// Smart contract: DVM code section +#define DAP_CHAIN_SECTION_DVM_DATA 0x0901 + +/// Smart contract: EVM code section +#define DAP_CHAIN_SECTION_EVM_CODE 0x0910 + +/// Smart contract: EVM data section +#define DAP_CHAIN_SECTION_EVM_CODE 0x0911 + +/// Pub key section, with sign and address +#define DAP_CHAIN_SECTION_PKEY 0x0c00 + +/// Section with additional roots, for example transaction roots +#define DAP_CHAIN_SECTION_ROOTS 0xf000 + +/// Coin +#define DAP_CHAIN_SECTION_COIN 0xffff + + + + +typedef struct dap_chain_block_section{ + uint16_t type; // Section type + uint8_t data[]; // data +} DAP_ALIGN_PACKED dap_chain_block_section_t; + +inline uint32_t diff --git a/dap_chain_tx.c b/dap_chain_section_roots.c similarity index 72% rename from dap_chain_tx.c rename to dap_chain_section_roots.c index a6c03c8818c556f011bb58d9f9c528db1dc55375..56382e008f38f894a2d4eee1f68e88f6592d6403 100644 --- a/dap_chain_tx.c +++ b/dap_chain_section_roots.c @@ -19,24 +19,6 @@ */ #include "dap_common.h" -#include "dap_chain_tx.h" - -#define LOG_TAG "dap_chain_tx" -/** - * @brief dap_chain_tx_init - * @return - */ -int dap_chain_tx_init() -{ - log_it(L_INFO,"DapChain Transactions module init"); - return 0; -} - -/** - * @brief dap_chain_tx_deinit - */ -void dap_chain_tx_deinit() -{ - log_it(L_INFO,"DapChain Transactions module deinit"); -} +#include "dap_chain_section_roots.h" +#define LOG_TAG "dap_chain_section_roots" diff --git a/dap_chain_section_roots.h b/dap_chain_section_roots.h new file mode 100644 index 0000000000000000000000000000000000000000..d0eb6320aa4c7f2cd20996a8f0cdf6e67d859f5a --- /dev/null +++ b/dap_chain_section_roots.h @@ -0,0 +1,43 @@ +/* + Copyright (c) 2017-2018 (c) Project "DeM Labs Inc" https://github.com/demlabsinc + All rights reserved. + + This file is part of DAP (Deus Applications Prototypes) the open source project + + DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + DAP is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with any DAP based project. If not, see <http://www.gnu.org/licenses/>. +*/ +#pragma once + +#include "dap_common.h" +#include "dap_chain_common.h" + +/** + * @struct dap_chain_section_roots_v1 + * @brief Hash tree roots for block, version 1 + */ +typedef struct dap_chain_section_roots_v1{ + dap_chain_hash_t main; +} DAP_ALIGN_PACKED dap_chain_block_roots_v1_t; + +/** + * @struct dap_chain_section_roots_v2 + * @brief Hash tree roots for block, version 2 + */ +typedef struct dap_chain_section_roots_v2{ + dap_chain_hash_t main; + dap_chain_hash_t txs; +} DAP_ALIGN_PACKED dap_chain_section_roots_v2_t; + +typedef dap_chain_section_roots_v2_t dap_chain_section_roots_t; + diff --git a/dap_chain_section_tx.c b/dap_chain_section_tx.c new file mode 100644 index 0000000000000000000000000000000000000000..741070145892bfd052fef11b8e5cf73d33dea71b --- /dev/null +++ b/dap_chain_section_tx.c @@ -0,0 +1,4 @@ +#include "dap_common.h" +#include "dap_chain_section_tx.h" + +#define LOG_TAG "dap_chain_section_tx" diff --git a/dap_chain_section_tx.h b/dap_chain_section_tx.h new file mode 100644 index 0000000000000000000000000000000000000000..10bfc026f3d51546a0977a52ce2961ae889a03a0 --- /dev/null +++ b/dap_chain_section_tx.h @@ -0,0 +1,40 @@ +/* + Copyright (c) 2017-2018 (c) Project "DeM Labs Inc" https://github.com/demlabsinc + All rights reserved. + + This file is part of DAP (Deus Applications Prototypes) the open source project + + DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + DAP is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with any DAP based project. If not, see <http://www.gnu.org/licenses/>. +*/ +#pragma once +#include "dap_chain_common.h" +#include "dap_chain_section.h" + +typedef enum dap_chain_tx_item_type{ + TX_ITEM_TYPE_IN = 0x00, + TX_ITEM_TYPE_OUT = 0x10, +} dap_chain_tx_item_type_t; + +/** + * @struct dap_chain_section_tx + * @brief Transaction section, consists from lot of tx_items + */ +typedef struct dap_chain_section_tx{ + struct { + uint64_t lock_time; + uint32_t tx_items_size; // size of next sequencly lying tx_item sections would be decided to belong this transaction + } DAP_ALIGN_PACKED header; + uint8_t tx_items[]; +} DAP_ALIGN_PACKED dap_chain_section_tx_t; + diff --git a/dap_chain_section_tx_in.h b/dap_chain_section_tx_in.h new file mode 100644 index 0000000000000000000000000000000000000000..c2484bacfed09e9c53dd7c70850d64474c12002a --- /dev/null +++ b/dap_chain_section_tx_in.h @@ -0,0 +1,22 @@ +#pragma once + +#include <stdint.h> +#include "dap_common.h" +#include "dap_chain_common.h" +#include "dap_chain_section_tx.h" +/** + * @struct dap_chain_tx_item + * @brief Sections belongs to heading tx section, with inputs, outputs and others tx relatated items + */ + +typedef struct dap_chain_tx_in{ + struct { + dap_chain_tx_item_type_t type:8; /// @param type @brief Transaction item type + dap_chain_hash_t tx_prev_hash; /// @param tx_prev_hash @brief Hash of the previous transaction + uint32_t tx_out_prev_idx; /// @param tx_prev_idx @brief Previous tx_out index + dap_chain_sig_type_t sig_type:16; /// Signature type + uint32_t sig_size; /// Signature size + } header; /// Only header's hash is used for verification + uint32_t seq_no; /// Sequence number, out of the header so could be changed during reorganization + uint8_t sig[]; /// @param sig @brief raw signatura dat +} DAP_ALIGN_PACKED dap_chain_tx_in_t; diff --git a/dap_chain_section_tx_out.h b/dap_chain_section_tx_out.h new file mode 100644 index 0000000000000000000000000000000000000000..e014f19e4df7258111c936f4284632a31a152568 --- /dev/null +++ b/dap_chain_section_tx_out.h @@ -0,0 +1,20 @@ +#pragma once + +#include <stdint.h> +#include "dap_common.h" +#include "dap_chain_common.h" +#include "dap_chain_section_tx.h" + +/** + * @struct dap_chain_tx_out + * @brief Transaction item outout + */ +typedef struct dap_chain_tx_out{ + struct { + dap_chain_tx_item_type_t type:8; /// @param type @brief Transaction item type + uint64_t value; /// @param value @brief Number of Datoshis ( DAP/10^9 ) to be transfered + dap_chain_sig_type_t sig_type:16; /// @param sig_type @brief Signature type + uint32_t sig_size; /// @param sig_size @brief Signature size + } header; /// Only header's hash is used for verification + uint8_t sig[]; /// @param sig @brief raw signatura dat +} DAP_ALIGN_PACKED dap_chain_tx_out_t; diff --git a/dap_chain_wallet.c b/dap_chain_wallet.c new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/dap_chain_wallet.h b/dap_chain_wallet.h new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/dap_chain_wallet_internal.h b/dap_chain_wallet_internal.h new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391