From c69b3a2fb69c9b4a4b8ace5d561b803c4da06306 Mon Sep 17 00:00:00 2001 From: sgaragagghu Date: Wed, 9 May 2018 04:49:10 +0200 Subject: [PATCH 01/11] comments (#106) * Update pool.c * Update pool.c * Update pool.c * Update pool.c * Update pool.c --- client/pool.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/client/pool.c b/client/pool.c index 57b66f96..234d4ba1 100644 --- a/client/pool.c +++ b/client/pool.c @@ -87,8 +87,8 @@ struct connection_pool_data { uint8_t data_size; uint8_t block_size; struct pollfd connection_descriptor; - struct miner_pool_data *miner; - int balance_sent; + struct miner_pool_data *miner; // More than one connection may lead to the same miner, it is needed to track + int balance_sent; // this behaviour to avoid potential exploit of the service. uint32_t shares_count; }; @@ -481,6 +481,17 @@ static void close_connection(connection_list_element *connection, int index, con ip & 0xff, ip >> 8 & 0xff, ip >> 16 & 0xff, ip >> 24 & 0xff, ntohs(port), message); } + + +/* @method :- calculate_nopaid_shares ++ @param :- ++ struct connection_pool_data* connection data :- miner's side data ++ struct xdag_pool_task* task data :- pool's side data ++ xdag_hash_t hash :- is a digest computed by the pool starting from the ++ miner's data 'conn_data->data' and pool side context task->ctx0 ++ @return :- void ++ @description :- calculate nopaid share */ + static void calculate_nopaid_shares(struct connection_pool_data *conn_data, struct xdag_pool_task *task, xdag_hash_t hash) { const xdag_time_t task_time = task->task_time; @@ -517,6 +528,7 @@ static void calculate_nopaid_shares(struct connection_pool_data *conn_data, stru diff = 46 - log(diff); // thus it's the same as the most difficult hash), it is probably a bug. Let's consider an "almost easiest" hash // like hash[3]=FFFFFFFFFFFFFFFF and hash[2]<=FFFFFFFFFFFFFBFF, in this case we have 46-log(FFFFFFFFFFFFFFFF)=46-19=27. // At this point diff seems to have a range [46;27], where higher value is higher difficulty. + // Adding share for connection if(conn_data->task_time < task_time) { conn_data->task_time = task_time; @@ -530,7 +542,7 @@ static void calculate_nopaid_shares(struct connection_pool_data *conn_data, stru } else if(diff > conn_data->maxdiff[i]) { conn_data->maxdiff[i] = diff; } - + // Adding share for miner if(conn_data->miner && conn_data->miner->task_time < task_time) { conn_data->miner->task_time = task_time; From 390aeb8a145f9ca1f94e531cd4878a2ca9c5f5de Mon Sep 17 00:00:00 2001 From: Ryan Kung Date: Sat, 12 May 2018 15:00:51 +0800 Subject: [PATCH 02/11] modified makefile for macos (#129) --- README.md | 14 ++++++++++++++ client/Makefile | 24 ++++++++++++++++++++---- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 4f405ce8..f643b337 100644 --- a/README.md +++ b/README.md @@ -120,6 +120,20 @@ Then run below command xdag> xfer [amount] [address] +Build (Mac Os): +----------------------- +- Install dependencies: + + $ brew install openssl + $ ln -s /usr/local/opt/openssl/include/openssl /usr/local/include + $ ln -s /usr/local/opt/openssl/lib/libssl.1.0.0.dylib /usr/local/lib/ + +- Make + + $ cd xdag/client + $ make + + Main chain idea: --------------- diff --git a/client/Makefile b/client/Makefile index c917698b..109b434f 100644 --- a/client/Makefile +++ b/client/Makefile @@ -7,6 +7,17 @@ dfslib = $(dfstools)/dfslib dar = ../dus/programs/dar/source ldusinc = ../ldus/source/include/ldus utils = ./utils +OS := $(shell uname) + +ifeq ($(OS), Darwin) + asm_src = \ + sha256-mb-x86_64-mac.s \ + x86_64cpuid-mac.s +else + asm_src = \ + sha256-mb-x86_64.s \ + x86_64cpuid.s +endif sources = \ address.c \ @@ -21,14 +32,12 @@ sources = \ miner.c \ mining_common.c \ sha256.c \ - sha256-mb-x86_64.s \ storage.c \ sync.c \ transport.c \ wallet.c \ commands.c \ terminal.c \ - x86_64cpuid.s \ $(dnet)/dnet_crypt.c \ $(dnet)/dnet_database.c \ $(dnet)/dnet_main.c \ @@ -103,13 +112,20 @@ headers = \ ./json-rpc/rpc_wrapper.h \ -flags = -std=gnu11 -O3 -DDFSTOOLS -DCHEATCOIN -DNDEBUG -g -lpthread -lcrypto -lssl -lm -Wall -Wmissing-prototypes -Wno-unused-result -Wl,--export-dynamic + +ifeq ($(OS), Darwin) + + flags = -std=gnu11 -O3 -DDFSTOOLS -DCHEATCOIN -DNDEBUG -g -lpthread -lcrypto -lssl -lm -Wall -Wmissing-prototypes -Wno-unused-result +else + flags = -std=gnu11 -O3 -DDFSTOOLS -DCHEATCOIN -DNDEBUG -g -lpthread -lcrypto -lssl -lm -Wall -Wmissing-prototypes -Wno-unused-result -Wl,--export-dynamic + +endif all: xdag xdag: $(sources) $(headers) Makefile - gcc -o xdag $(sources) -DSHA256_USE_OPENSSL_TXFM -DSHA256_OPENSSL_MBLOCK -I$(SRCROOT) -I$(utils) $(flags) + gcc -o xdag $(sources) $(asm_src) -DSHA256_USE_OPENSSL_TXFM -DSHA256_OPENSSL_MBLOCK -I$(SRCROOT) -I$(utils) $(flags) clean: rm xdag From 73cbe41e99bcdf150087e8a66e4a089715252ef1 Mon Sep 17 00:00:00 2001 From: reymondtu Date: Sun, 13 May 2018 00:19:45 +0800 Subject: [PATCH 03/11] Translate Protocol.md to Chinese (#135) * Translate Protocol.md to Chinese move to develop and optimization translate * update format update format * update dagger era update dagger era 2018.1.5 --- Protocol-cn.md | 167 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 Protocol-cn.md diff --git a/Protocol-cn.md b/Protocol-cn.md new file mode 100644 index 00000000..e55e88a0 --- /dev/null +++ b/Protocol-cn.md @@ -0,0 +1,167 @@ +# XDAG加密货币协议 +#### Version 0.3.1 Apirl 6, 2018 + +## 1.名词解释 + +**区快地址** - 区块hash截取后的base64编码. + +**金额** - XDAG的数量,以Cheato为单位,类似比特币的Santoshi. + +**区块** - Dagger的基本结构, 每个区块定长512字节, 结构定义在第二部分介绍. + +**链** - 一个块的序列,其中每个块被前一个块引用. + +**Cheato** - 一个XDAG包括2^32个Cheato. + +**Dagger元年** - Unix格式:0x5A500000 ,也就是2018年1月5日. + +**区块难度** - (2^128-1)/(hash_little / 2^160), hash_little = 小端字节序(hash(block)), hash_little大小为256 bits(32 bytes). + +**链难度** - 链上区块的难度之和. + +**Hash** - 采用双sha256哈希算法, hash(block) = sha256(sha256(block)). + +**主块** - 主链上的任何区块. + +**主链** - 难度最大的链称为主链. + +**链接** - 如果A具有类型2或3的字段,其中包含块B的截断散列,请参第二章. + +**引用** - 如果从区块A到区块B是一段连续的区块组成,则称区块B被区块A引用. + +**i-引用** - 如果区块A的第i个域连接到区块C,并且区块C引用区块B,则称区块B是区块A的i-引用. + +**分离链** - 链上的区块属于不同的时间片. + +**最短链** - 一个从区块A到区块B的链,每个区块都是i-引用B。i越小,链越短. + +**时间片** - 每64秒一个时间片,时间为Unix格式,低6bits等于0. + +**区块时间** - 区块时间记录在区块头信息里,从1970年1月1号开始,单位是1/1024秒. + +**交易** - 交易即区块. + +**hash截断** - hash小端字节序表示,取最低24个字节. + + +## 2.区块 + +一个区块有16个字段,每个字段32个字节,每个字段type可能为0到15,一般区块,第0个字段type必须是1;伪块,第0个字段必须是类型0。有关传输用的伪块,详看第5章 + +字段的类型0-15: + + 0 随机数, 32字节. + + 1 区块头. 结构如下: + - 64-bit 传输层头, 在计算区块散列的时候,这个头信息要全部设置为0; + - 64-bit 小端字节序, 包括字段类型0..15. 每个类型4 bits. 字段0的类型编码为低4位. + - 64-bit 小端字节序, 区块的时间. + - 64-bit 小端字节序, 交易的手续费. + + 2 交易的输入. 连接到另外一个区块B. + 结构如下: + - 192-bit 区块B的hash截断. + - 64-bit 小端字节序, 区块被应用后的金额. + + + 3 交易的输出. 连接到其他区块. + 结构如下: + - 192-bit 区块B的hash截断. + - 64-bit 小端字节序, 区块被应用后的金额. + + 4 输入签名的一半. 输入签名由两个type为4的字段组成. + 第一个字段是r,第二个字段是s,这两个值都是从ECDSA的签名算法获得. + 通过以下摘要得出的ECDSA签名, 并通过相应的私钥进行签名. + 摘要是: + hash(modified_block # key_prefix_byte # public_key) + + 其中: + - # 是连接符号 + - modified_block 是去除块中的输入签名与输出签名字段,由零填充. + - key_prefix_byte 如果公钥是偶数,0x02, 如果公钥是奇数,0x03. + - public_key - 公钥, 32 bytes. + + 5 输出签名的一半. 结构与输入签名一样. 参考第3章 . 输入与输出签名的区别. + + 6 偶数公钥. 该字段包含ECDSA的编号x公共密钥,如果数字y是偶数. + + 7 奇数公钥. 该字段包含ECDSA的编号x公共密钥,如果数字y是奇数. + + 8 - 15 留给未来的使用. + + +## 3.算法 + +每一个区块都是一个交易. 一个交易个可能有多个输入、输出、公钥、 输入和输出的签名, 还有手续费. 如果满足以下条件,则区块是有效的: + +- 区块的产生时间不小于Dagger元年(2018-01-06 22:45:20); +- 区块的输入、输出时间小于区块A的产生时间; +- 区块的每个输入或者输出必须是合法的区块; +- 区块的所有输入金额之和必须小于2的64次方; +- 区块的所有输出金额加上手续费必须小于2的64次方; +- 如果至少有一个输入大于所有输入的和,则必须不少于所有输出之和加上手续费;否则,所有输出的和必须为零; +- 对于区块A的每个输入B,在块A中有公钥K和输入或输出签名S,在块B中有输出签名T,使得使用密钥K,从块A获得签名S,使用相同密钥K,从块B获得签名T(非正式描述:只有块B的所有者才能从它提取资金) +- 输出签名字段的数目必须是偶数,而输入签名字段的数目可能是奇数;在这种情况下,最后一个输入签名字段可以被用作随机数,可以在不重新签名的情况下修改该字段. + +任何时刻,只有一个主链 (看 [1.名词解释](#1.名词解释)). 主链上的区块称为主块. 每个主块属于不同的时间片. 也就是说,在一个时间片内,有1个或0个主块. 每个主块被挖出来时,会奖被奖励. 在第一个四年,奖励1024个XDAG。第二个四年,奖励512个XDAG, 之后每四年,奖励减半,XDAG的产出总量大约40亿 + +区块按照下面的规则排序: + +1. 如果区块A被某个主块M引用,但是区块B没被主块M引用,则区块A优先区块B. +2. 如果区块A和区块B相等,并且区块M是最小的被引用区块,并且区块C是M到A或者M到B的最小链上的公共块,如果A是C的i-引用,B是C的j-引用,并且i小于j的话,区块A优先区块B. +3. 如果区块A和区块B在条件1/2下都相等,并且区块A被区块B引用,区块A优先区块B,交易使用上述规则进行。如果上述规则无法适用,该交易无效. + +交易使用上述规则进行, 如果上述规则无法适用,该交易无效. +每个区块拥有自己的XDAG金额.一开始的情况下是0, 但是在如下情况下,金额会变化: + +1. 如果区块A是被挖出的区块, 区块A的金额会加上主块奖励. +2. 如果区块A作为区块B的输入,区块A的金额会减去区块B的相应的输出金额. +3. 如果区块A作为区块B的输出, 在这种情况下区块A的金额会增加区块B的相应的输入金额 +4. 如果区块A是区块B连接中的最小的区块(按照上述规则),则区块A的金额会增加区块B的手续费. +5. 如果区块A的输入总和大于输出总额以及费用,则区块A的金额是两者的差额. + +一个交易能合法验证,必须满足以下条件: + +1. 对于区块A中的所有输入,要小于对于区块金额. +2. 区块A中所有输入加区块A本身的金额的大小必须大于所有输出以及手续费之和. + +因为每个区块有金额,所以本身也就是一个账户.用户如果拥有区块A中的输出的签名的私钥,则该用户拥有该区块A的账户.用户可以通过地址访问他的账户.区块地址是针对区块截断散列的base64编码。地址是32字节的字符串,包括字符:A-Z, a-z, 0-9, /, +.用户可以在系统中从他自己的账户中提取金额并且转账给一个有效的区块. + +## 4.加密与安全 + +签名算法,使用openssl实现中的标准ECDSA签名.椭圆曲线是Secp256k1.私钥长度为32个字节,公钥的长度为32字节(椭圆曲线上的点的x坐标)加上(椭圆曲线上的点的y坐标).签名是64个字节,由两个字段组成.这两个字段别是ECDSA签名算法中的r和s. +私钥存在wallet.dat文件中,这个文件和xdag可执行程序在同一目录.每个私钥长度32个字节, 公钥不保存,因为公钥可以从私钥计算得出.当用户第一次启动xdag程序时,要求用户提供一串随机字符.这些字符会作为随机生成器的种子.这些字符存在dnet_key.dat文件中.每次xdaga启动时,这些字符会参与随机数生成, 所有的私钥用这些字符生成。 + +## 5.传输层 + +Dagger节点利用传输协议进行区块的交换.区块的前8个字节会用做传输协议特定的信息. 需要提到的是,这个字段在计算区块散列的时候要清零.xdag程序默认使用dnet作为网络传输层.传输过程中的区块信息是加密的.使用作者的半对称算法和临时密钥对每个传输的块进行编码,并使用相同的密钥对另一侧进行解码.临时密钥由发送方生成,然后使用存储在文件dnet_key.dat中的发送方的私钥进行编码,然后使用发送方的公钥将其解码. 加密算法为长度为8192bits的RSA算法. + +节点间不仅能传输区块,还能传输伪区块.伪区块是一些需要对方答复的请求.对于伪区块,字段0的类型必须是0,字段1的类型必须是消息类型. + +伪区块的消息类型如下: + + 0 请求某个时间段内的所有区块信息.起始时间在区块头的时间字段中,结束时间在区块头的金额字段中. + 1 对消息类型0的回复.这个回复必须在所有请求的区块之后. + 2 请求某个时间段内的所有区块的汇总信息.起始时间在区块头的时间字段中,结束时间在区块头的金额字段中. + 3 对消息类型2的回复.回复是一个区块,其中的后256个字节是由16个汇总结构组成.每个汇总将总的时间段划分成16等份. + 每个汇总信息包括两个内容: + - 第一个64-bit(小端字节序),是该子区间中所有块的所有64-bit之和 + - 第二个64-bit(小端字节序),是从这个子区间开始的所有块的长度之和. + +每个请求在字段1里有自己的ID.每个回复必须在字段1里拥有相同的ID.每个伪区块消息在字段2,字段3以及字段4的开头存放统计结构 + +统计结构如下: + + - 发送方的主链难度, 16个字节; + - 网络主链上已知的主链最大难度, 16字节; + - 发送方的有效区块个数, 8个字节; + - 网络中的最大的有效区块个数, 8个字节; + - 发送方的主块个数, 8个字节; + - 网络中的最大的主块个数, 8个字节; + - 发送方已知的节点个数, 4个字节; + - 网络中已知的节点个数, 4个字节; + +伪区块中的其他字段填写发送方的节点地址信息.节点地址信息是6个字节,IP(4 字节, 大端字节序)和 端口(小端字节序) + +## 翻译 +本白皮书由reymondtu翻译并校对.(reymontu@gmx.com,XDAG:+09AAQmaOlLswDbHjHlJ+MFScwG+wfMB) From 75d49b395430c899732ccc021b2861cd0cdac2c9 Mon Sep 17 00:00:00 2001 From: Frozen Date: Fri, 18 May 2018 11:25:29 +0800 Subject: [PATCH 04/11] merge from master (#149) * Update netdb-white.txt * Update netdb.txt * State is fixed. --- client/block.c | 14 +++++++------- client/netdb-white.txt | 3 +++ client/netdb.txt | 3 +++ 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/client/block.c b/client/block.c index 60b41512..9d6df8ad 100644 --- a/client/block.c +++ b/client/block.c @@ -1342,13 +1342,13 @@ static int bi_compar(const void *l, const void *r) static const char* xdag_get_block_state_info(struct block_internal *block) { - if(block->flags & (BI_REF | BI_MAIN_REF)) { - if(block->flags & BI_MAIN) { - return "Main"; - } - if(block->flags & BI_APPLIED) { - return "Accepted"; - } + if(block->flags == (BI_REF | BI_MAIN_REF | BI_APPLIED | BI_MAIN | BI_MAIN_CHAIN)) { //1F + return "Main"; + } + if(block->flags == (BI_REF | BI_MAIN_REF | BI_APPLIED)) { //1C + return "Accepted"; + } + if(block->flags == (BI_REF | BI_MAIN_REF)) { //18 return "Rejected"; } return "Pending"; diff --git a/client/netdb-white.txt b/client/netdb-white.txt index b7a116cf..d9ccd721 100755 --- a/client/netdb-white.txt +++ b/client/netdb-white.txt @@ -1,6 +1,7 @@ 14.152.83.121:13655 24.234.35.55:31333 45.76.37.252:13654 +45.77.175.144:13654 47.91.197.90:13654 52.69.99.30:13655 52.83.132.76:13655 @@ -22,3 +23,5 @@ 172.105.216.53:3356 175.155.35.5:19999 183.236.166.17:13655 +195.201.168.17:16775 +195.201.168.17:17775 diff --git a/client/netdb.txt b/client/netdb.txt index b7a116cf..d9ccd721 100755 --- a/client/netdb.txt +++ b/client/netdb.txt @@ -1,6 +1,7 @@ 14.152.83.121:13655 24.234.35.55:31333 45.76.37.252:13654 +45.77.175.144:13654 47.91.197.90:13654 52.69.99.30:13655 52.83.132.76:13655 @@ -22,3 +23,5 @@ 172.105.216.53:3356 175.155.35.5:19999 183.236.166.17:13655 +195.201.168.17:16775 +195.201.168.17:17775 From 68fee0b4f4e2b826a282c780061b89832e751a45 Mon Sep 17 00:00:00 2001 From: Evgeniy Date: Mon, 21 May 2018 00:28:07 +0300 Subject: [PATCH 05/11] Check for miner's address was added. Project platform version was updated. --- client/pool.c | 65 ++++++++++++++----------------- win/xdag/xdag.vcxproj | 2 +- win/xdaglib/xdaglib.vcxproj | 2 +- win/xdagwallet/xdagwallet.vcxproj | 4 +- 4 files changed, 34 insertions(+), 39 deletions(-) diff --git a/client/pool.c b/client/pool.c index 98a766ca..76de76d3 100644 --- a/client/pool.c +++ b/client/pool.c @@ -110,9 +110,9 @@ struct payment_data { xdag_hash_t g_xdag_mined_hashes[CONFIRMATIONS_COUNT], g_xdag_mined_nonce[CONFIRMATIONS_COUNT]; -static int g_max_connections_count = START_MINERS_COUNT, g_max_miner_ip_count = START_MINERS_IP_COUNT; -static int g_connections_per_miner_limit = DEFAUL_CONNECTIONS_PER_MINER_LIMIT; -static int g_connections_count = 0; +static uint32_t g_max_connections_count = START_MINERS_COUNT, g_max_miner_ip_count = START_MINERS_IP_COUNT; +static uint32_t g_connections_per_miner_limit = DEFAUL_CONNECTIONS_PER_MINER_LIMIT; +static uint32_t g_connections_count = 0; static double g_pool_fee = 0, g_pool_reward = 0, g_pool_direct = 0, g_pool_fund = 0; static struct xdag_block *g_firstb = 0, *g_lastb = 0; @@ -193,20 +193,22 @@ int xdag_pool_set_config(const char *pool_config) pool_config = strtok_r(buf, " \t\r\n:", &lasts); if(pool_config) { + int max_connection_count_input; int open_max = (int)sysconf(_SC_OPEN_MAX); - sscanf(pool_config, "%d", &g_max_connections_count); + sscanf(pool_config, "%d", &max_connection_count_input); - if(g_max_connections_count < 0) { - g_max_connections_count = 0; + if(max_connection_count_input < 0) { + max_connection_count_input = 0; xdag_warn("pool: wrong connections count"); - } else if(g_max_connections_count > MAX_CONNECTIONS_COUNT) { - g_max_connections_count = MAX_CONNECTIONS_COUNT; + } else if(max_connection_count_input > MAX_CONNECTIONS_COUNT) { + max_connection_count_input = MAX_CONNECTIONS_COUNT; xdag_warn("pool: exceed max connections count %d", MAX_CONNECTIONS_COUNT); - } else if(g_max_connections_count > open_max - 64) { - g_max_connections_count = open_max - 64; + } else if(max_connection_count_input > open_max - 64) { + max_connection_count_input = open_max - 64; xdag_warn("pool: exceed max open files %d", open_max - 64); } + g_max_connections_count = max_connection_count_input; } pool_config = strtok_r(0, " \t\r\n:", &lasts); @@ -354,8 +356,8 @@ static int open_pool_connection(const char *pool_arg) static int connection_can_be_accepted(int sock, struct sockaddr_in *peeraddr) { connection_list_element *elt; - int count; - int count_accept; + uint32_t count; + uint32_t count_accept; //firstly we check that total count of connection did not exceed max count of connection LL_COUNT(g_connection_list_head, elt, count); @@ -396,18 +398,6 @@ static int connection_can_be_accepted(int sock, struct sockaddr_in *peeraddr) return 1; } -//static void rebuild_descriptors_array() -//{ -// connection_list_element *elt; -// int index = 0; -// LL_FOREACH(g_connection_list_head, elt) -// { -// memcpy(g_fds + index, &elt->connection_data.connection_descriptor, sizeof(struct pollfd)); -// ++index; -// } -// g_connections_count = index; -//} - void *pool_net_thread(void *arg) { const char *pool_arg = (const char*)arg; @@ -593,6 +583,13 @@ static int register_new_miner(connection_list_element *connection) miner_list_element *elt; struct connection_pool_data *conn_data = &connection->connection_data; + xdag_time_t time; + const int64_t position = xdag_get_block_pos(conn_data->data, &time); + if(position < 0) { + close_connection(connection, "Miner's address is unknown"); + return 0; + } + int exists = 0; pthread_mutex_lock(&g_descriptors_mutex); LL_FOREACH(g_miner_list_head, elt) @@ -723,7 +720,7 @@ static int recieve_data_from_connection(connection_list_element *connection) } } else { //share is received - uint64_t task_index = g_xdag_pool_task_index; + const uint64_t task_index = g_xdag_pool_task_index; struct xdag_pool_task *task = &g_xdag_pool_task[task_index & 1]; if (++conn_data->shares_count > SHARES_PER_TASK_LIMIT) { //if shares count limit is exceded it is considered as spamming and current connection is disconnected @@ -886,9 +883,9 @@ void *pool_block_thread(void *arg) for(;;) { int processed = 0; - uint64_t task_index = g_xdag_pool_task_index; + const uint64_t task_index = g_xdag_pool_task_index; struct xdag_pool_task *task = &g_xdag_pool_task[task_index & 1]; - xdag_time_t current_task_time = task->task_time; + const xdag_time_t current_task_time = task->task_time; if(current_task_time > prev_task_time) { uint64_t *hash = g_xdag_mined_hashes[(current_task_time - CONFIRMATIONS_COUNT + 1) & (CONFIRMATIONS_COUNT - 1)]; @@ -1104,9 +1101,7 @@ static void do_payments(uint64_t *hash, int fields_count, struct payment_data *d int pay_miners(xdag_time_t time) { - int64_t pos; - int key, defkey, fields_count; - double *diff, *prev_diff; + int defkey; struct payment_data data; miner_list_element *elt; @@ -1129,23 +1124,23 @@ int pay_miners(xdag_time_t time) data.pay = data.balance - (xdag_amount_t)(g_pool_fee * data.balance); if(!data.pay) return -3; - key = xdag_get_key(hash); + int key = xdag_get_key(hash); if(key < 0) return -4; if(!xdag_wallet_default_key(&defkey)) return -5; - fields_count = (key == defkey ? 12 : 10); + int fields_count = (key == defkey ? 12 : 10); - pos = xdag_get_block_pos(hash, &time); + int64_t pos = xdag_get_block_pos(hash, &time); if(pos < 0) return -6; struct xdag_block buf; struct xdag_block *block = xdag_storage_load(hash, time, pos, &buf); if(!block) return -7; - diff = malloc(2 * miners_count * sizeof(double)); + double *diff = malloc(2 * miners_count * sizeof(double)); if(!diff) return -8; - prev_diff = diff + miners_count; + double *prev_diff = diff + miners_count; if(!precalculate_payments(hash, confirmation_index, &data, diff, prev_diff, nonce)) { free(diff); diff --git a/win/xdag/xdag.vcxproj b/win/xdag/xdag.vcxproj index f84ad15e..212e2dbf 100644 --- a/win/xdag/xdag.vcxproj +++ b/win/xdag/xdag.vcxproj @@ -33,7 +33,7 @@ {8F05F6FF-AA25-4D79-AB2D-886BA8976393} Win32Proj xdag - 10.0.16299.0 + 10.0.17134.0 xdag diff --git a/win/xdaglib/xdaglib.vcxproj b/win/xdaglib/xdaglib.vcxproj index ca28ef3e..5859edc7 100644 --- a/win/xdaglib/xdaglib.vcxproj +++ b/win/xdaglib/xdaglib.vcxproj @@ -135,7 +135,7 @@ {49C6CE86-04E0-4431-B55F-A6CE88268996} Win32Proj xdaglib - 10.0.16299.0 + 10.0.17134.0 diff --git a/win/xdagwallet/xdagwallet.vcxproj b/win/xdagwallet/xdagwallet.vcxproj index 76f8c6f3..97ea501e 100644 --- a/win/xdagwallet/xdagwallet.vcxproj +++ b/win/xdagwallet/xdagwallet.vcxproj @@ -23,7 +23,7 @@ {EDACF749-CA11-48F7-806A-D39748389856} MFCProj xdagwallet - 10.0.16299.0 + 10.0.17134.0 xdagwallet @@ -255,4 +255,4 @@ copy $(SolutionDir)dependencies\x86\msvcr100.dll "$(OutDir)msvcr100.dll" /Y - + \ No newline at end of file From 7781b88be774ead71ee18e276f537967dfb2c01d Mon Sep 17 00:00:00 2001 From: Evgeniy Date: Mon, 21 May 2018 02:20:04 +0300 Subject: [PATCH 06/11] xdag_hash2address is made thread-safe --- client/address.c | 20 ++++++---------- client/address.h | 2 +- client/block.c | 27 ++++++++++++++------- client/commands.c | 41 +++++++++++++++++++++----------- client/json-rpc/rpc_procedures.c | 12 +++++++--- client/pool.c | 27 ++++++++++++++------- 6 files changed, 82 insertions(+), 47 deletions(-) diff --git a/client/address.c b/client/address.c index 4e19df27..87ee056d 100644 --- a/client/address.c +++ b/client/address.c @@ -25,9 +25,9 @@ int xdag_address_init(void) int xdag_address2hash(const char *address, xdag_hash_t hash) { uint8_t *fld = (uint8_t*)hash; - int i, c, d, e, n; + int i, c, d, n; - for (e = n = i = 0; i < 32; ++i) { + for (int e = n = i = 0; i < 32; ++i) { do { if (!(c = (uint8_t)*address++)) return -1; @@ -51,25 +51,19 @@ int xdag_address2hash(const char *address, xdag_hash_t hash) } // converts hash to address -const char *xdag_hash2address(const xdag_hash_t hash) +void xdag_hash2address(const xdag_hash_t hash, char *address) { - static char bufs[4][33]; - static int k = 0; - char *buf = &bufs[k++ & 3][0], *ptr = buf; - int i, c, d; + int c, d; const uint8_t *fld = (const uint8_t*)hash; - for (i = c = d = 0; i < 32; ++i) { + for (int i = c = d = 0; i < 32; ++i) { if (d < 6) { d += 8; c <<= 8; c |= *fld++; } d -= 6; - *ptr++ = bits2mime[c >> d & 0x3F]; + *address++ = bits2mime[c >> d & 0x3F]; } - - *ptr = 0; - - return buf; + *address = 0; } diff --git a/client/address.h b/client/address.h index a070828c..ebd94e84 100644 --- a/client/address.h +++ b/client/address.h @@ -12,6 +12,6 @@ extern int xdag_address_init(void); extern int xdag_address2hash(const char *address, xdag_hash_t hash); /* converts hash to address */ -extern const char *xdag_hash2address(const xdag_hash_t hash); +extern void xdag_hash2address(const xdag_hash_t hash, char *address); #endif diff --git a/client/block.c b/client/block.c index 9d6df8ad..a80a7611 100644 --- a/client/block.c +++ b/client/block.c @@ -1359,6 +1359,7 @@ int xdag_print_block_info(xdag_hash_t hash, FILE *out) { struct tm tm; char tbuf[64]; + char address[33]; int i; pthread_mutex_lock(&block_mutex); @@ -1381,17 +1382,25 @@ int xdag_print_block_info(xdag_hash_t hash, FILE *out) fprintf(out, " hash: %016llx%016llx%016llx%016llx\n", (unsigned long long)h[3], (unsigned long long)h[2], (unsigned long long)h[1], (unsigned long long)h[0]); fprintf(out, "difficulty: %llx%016llx\n", xdag_diff_args(bi->difficulty)); - fprintf(out, " balance: %s %10u.%09u\n", xdag_hash2address(h), pramount(bi->amount)); + xdag_hash2address(h, address); + fprintf(out, " balance: %s %10u.%09u\n", address, pramount(bi->amount)); fprintf(out, "-------------------------------------------------------------------------------------------\n"); fprintf(out, " block as transaction: details\n"); fprintf(out, " direction address amount\n"); fprintf(out, "-------------------------------------------------------------------------------------------\n"); - fprintf(out, " fee: %s %10u.%09u\n", (bi->ref ? xdag_hash2address(bi->ref->hash) : "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"), + if(bi->ref) { + xdag_hash2address(bi->ref->hash, address); + } + else { + strcpy(address, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); + } + fprintf(out, " fee: %s %10u.%09u\n", address, pramount(bi->fee)); for (i = 0; i < bi->nlinks; ++i) { + xdag_hash2address(bi->link[i]->hash, address); fprintf(out, " %6s: %s %10u.%09u\n", (1 << i & bi->in_mask ? " input" : "output"), - xdag_hash2address(bi->link[i]->hash), pramount(bi->linkamount[i])); + address, pramount(bi->linkamount[i])); } fprintf(out, "-------------------------------------------------------------------------------------------\n"); @@ -1400,7 +1409,8 @@ int xdag_print_block_info(xdag_hash_t hash, FILE *out) fprintf(out, "-------------------------------------------------------------------------------------------\n"); if (bi->flags & BI_MAIN) { - fprintf(out, " earning: %s %10u.%09u %s.%03d\n", xdag_hash2address(h), + xdag_hash2address(h, address); + fprintf(out, " earning: %s %10u.%09u %s.%03d\n", address, pramount(MAIN_START_AMOUNT >> ((MAIN_TIME(bi->time) - MAIN_TIME(XDAG_ERA)) >> MAIN_BIG_PERIOD_LOG)), tbuf, (int)((bi->time & 0x3ff) * 1000) >> 10); } @@ -1445,13 +1455,14 @@ int xdag_print_block_info(xdag_hash_t hash, FILE *out) struct block_internal *ri = ba[i]; if (ri->flags & BI_APPLIED) { for (int j = 0; j < ri->nlinks; j++) { - if (ri->link[j] == bi && ri->linkamount[j]) { + if(ri->link[j] == bi && ri->linkamount[j]) { t = ri->time >> 10; localtime_r(&t, &tm); strftime(tbuf, 64, "%Y-%m-%d %H:%M:%S", &tm); + xdag_hash2address(ri->hash, address); fprintf(out, " %6s: %s %10u.%09u %s.%03d\n", - (1 << j & ri->in_mask ? "output" : " input"), xdag_hash2address(ri->hash), - pramount(ri->linkamount[j]), tbuf, (int)((ri->time & 0x3ff) * 1000) >> 10); + (1 << j & ri->in_mask ? "output" : " input"), address, + pramount(ri->linkamount[j]), tbuf, (int)((ri->time & 0x3ff) * 1000) >> 10); } } } @@ -1469,7 +1480,7 @@ int xdagGetLastMainBlocks(int count, char** addressArray) int i = 0; for (struct block_internal *b = top_main_chain; b && i < count; b = b->link[b->max_diff_link]) { if (b->flags & BI_MAIN) { - strcpy(addressArray[i], xdag_hash2address(b->hash)); + xdag_hash2address(b->hash, addressArray[i]); ++i; } } diff --git a/client/commands.c b/client/commands.c index 8fa8d785..8a8e094c 100644 --- a/client/commands.c +++ b/client/commands.c @@ -394,25 +394,31 @@ long double amount2xdags(xdag_amount_t amount) int account_callback(void *data, xdag_hash_t hash, xdag_amount_t amount, xdag_time_t time, int n_our_key) { + char address[33]; struct account_callback_data *d = (struct account_callback_data *)data; - if(!d->count--) return -1; + if(!d->count--) { + return -1; + } + xdag_hash2address(hash, address); if(g_xdag_state < XDAG_STATE_XFER) - fprintf(d->out, "%s key %d\n", xdag_hash2address(hash), n_our_key); + fprintf(d->out, "%s key %d\n", address, n_our_key); else - fprintf(d->out, "%s %20.9Lf key %d\n", xdag_hash2address(hash), amount2xdags(amount), n_our_key); + fprintf(d->out, "%s %20.9Lf key %d\n", address, amount2xdags(amount), n_our_key); return 0; } static int make_transaction_block(struct xfer_callback_data *xferData) { + char address[33]; if(xferData->fieldsCount != XFER_MAX_IN) { memcpy(xferData->fields + xferData->fieldsCount, xferData->fields + XFER_MAX_IN, sizeof(xdag_hashlow_t)); } xferData->fields[xferData->fieldsCount].amount = xferData->todo; int res = xdag_create_block(xferData->fields, xferData->fieldsCount, 1, 0, 0, xferData->transactionBlockHash); if(res) { + xdag_hash2address(xferData->fields[xferData->fieldsCount].hash, address); xdag_err("FAILED: to %s xfer %.9Lf %s, error %d", - xdag_hash2address(xferData->fields[xferData->fieldsCount].hash), amount2xdags(xferData->todo), g_coinname, res); + address, amount2xdags(xferData->todo), g_coinname, res); return -1; } xferData->done += xferData->todo; @@ -425,6 +431,7 @@ static int make_transaction_block(struct xfer_callback_data *xferData) int xdag_do_xfer(void *outv, const char *amount, const char *address, int isGui) { + char address_buf[33]; struct xfer_callback_data xfer; FILE *out = (FILE *)outv; @@ -459,10 +466,10 @@ int xdag_do_xfer(void *outv, const char *amount, const char *address, int isGui) g_xdag_xfer_last = time(0); xdag_traverse_our_blocks(&xfer, &xfer_callback); if(out) { - fprintf(out, "Xfer: transferred %.9Lf %s to the address %s.\n", - amount2xdags(xfer.done), g_coinname, xdag_hash2address(xfer.fields[XFER_MAX_IN].hash)); - fprintf(out, "Transaction address is %s, it will take several minutes to complete the transaction.\n", - xdag_hash2address(xfer.transactionBlockHash)); + xdag_hash2address(xfer.fields[XFER_MAX_IN].hash, address_buf); + fprintf(out, "Xfer: transferred %.9Lf %s to the address %s.\n", amount2xdags(xfer.done), g_coinname, address_buf); + xdag_hash2address(xfer.transactionBlockHash, address_buf); + fprintf(out, "Transaction address is %s, it will take several minutes to complete the transaction.\n", address_buf); } return 0; } @@ -519,8 +526,10 @@ int xfer_callback(void *data, xdag_hash_t hash, xdag_amount_t amount, xdag_time_ void xdag_log_xfer(xdag_hash_t from, xdag_hash_t to, xdag_amount_t amount) { - xdag_mess("Xfer : from %s to %s xfer %.9Lf %s", - xdag_hash2address(from), xdag_hash2address(to), amount2xdags(amount), g_coinname); + char address_from[33], address_to[33]; + xdag_hash2address(from, address_from); + xdag_hash2address(to, address_to); + xdag_mess("Xfer : from %s to %s xfer %.9Lf %s", address_from, address_to, amount2xdags(amount), g_coinname); } static int out_balances_callback(void *data, xdag_hash_t hash, xdag_amount_t amount, xdag_time_t time) @@ -543,8 +552,10 @@ static int out_balances_callback(void *data, xdag_hash_t hash, xdag_amount_t amo static int out_sort_callback(const void *l, const void *r) { - return strcmp(xdag_hash2address(((struct xdag_field *)l)->data), - xdag_hash2address(((struct xdag_field *)r)->data)); + char address_l[33], address_r[33]; + xdag_hash2address(((struct xdag_field *)l)->data, address_l); + xdag_hash2address(((struct xdag_field *)r)->data, address_r); + return strcmp(address_l, address_r); } static void *add_block_callback(void *block, void *data) @@ -557,6 +568,7 @@ static void *add_block_callback(void *block, void *data) int out_balances() { + char address[33]; struct out_balances_data d; unsigned i = 0; xdag_set_log_level(0); @@ -567,7 +579,8 @@ int out_balances() xdag_traverse_all_blocks(&d, out_balances_callback); qsort(d.blocks, d.blocksCount, sizeof(struct xdag_field), out_sort_callback); for(i = 0; i < d.blocksCount; ++i) { - printf("%s %20.9Lf\n", xdag_hash2address(d.blocks[i].data), amount2xdags(d.blocks[i].amount)); + xdag_hash2address(d.blocks[i].data, address); + printf("%s %20.9Lf\n", address, amount2xdags(d.blocks[i].amount)); } return 0; } @@ -586,7 +599,7 @@ int xdag_show_state(xdag_hash_t hash) if(!hash) { strcpy(address, "Not ready"); } else { - strcpy(address, xdag_hash2address(hash)); + xdag_hash2address(hash, address); } strcpy(state, get_state()); return (*g_xdag_show_state)(state, balance, address); diff --git a/client/json-rpc/rpc_procedures.c b/client/json-rpc/rpc_procedures.c index a07d801f..4dca3a22 100644 --- a/client/json-rpc/rpc_procedures.c +++ b/client/json-rpc/rpc_procedures.c @@ -106,7 +106,10 @@ int rpc_account_callback(void *data, xdag_hash_t hash, xdag_amount_t amount, xda struct rpc_account_callback_data *d = (struct rpc_account_callback_data*)data; if(d->count-- <=0) return -1; - cJSON* address = cJSON_CreateString(xdag_hash2address(hash)); + char address_buf[33]; + xdag_hash2address(hash, address_buf); + + cJSON* address = cJSON_CreateString(address_buf); char str[128] = {0}; sprintf(str, "%.9Lf", amount2xdags(amount)); cJSON* balance = cJSON_CreateString(str); @@ -301,10 +304,13 @@ cJSON * method_xdag_do_xfer(struct xdag_rpc_context * ctx, cJSON * params, cJSON g_xdag_state = XDAG_STATE_XFER; g_xdag_xfer_last = time(0); xdag_traverse_our_blocks(&xfer, &xfer_callback); - + + char address_buf[33]; + xdag_hash2address(xfer.transactionBlockHash, address_buf); + cJSON * ret = NULL; cJSON* item = cJSON_CreateObject(); - cJSON_AddItemToObject(item, "block", cJSON_CreateString(xdag_hash2address(xfer.transactionBlockHash))); + cJSON_AddItemToObject(item, "block", cJSON_CreateString(address_buf)); ret = cJSON_CreateArray(); cJSON_AddItemToArray(ret, item); diff --git a/client/pool.c b/client/pool.c index 98a766ca..4bf9a700 100644 --- a/client/pool.c +++ b/client/pool.c @@ -495,13 +495,16 @@ static void close_connection(connection_list_element *connection, const char *me uint32_t ip = conn_data->ip; uint16_t port = conn_data->port; - + + char address_buf[33]; + xdag_hash2address(conn_data->miner->id.data, address_buf); + if(conn_data->miner) { - xdag_info("Pool: miner %s disconnected from %u.%u.%u.%u:%u by %s", xdag_hash2address(conn_data->miner->id.data), - ip & 0xff, ip >> 8 & 0xff, ip >> 16 & 0xff, ip >> 24 & 0xff, ntohs(port), message); + xdag_info("Pool: miner %s disconnected from %u.%u.%u.%u:%u by %s", address_buf, + ip & 0xff, ip >> 8 & 0xff, ip >> 16 & 0xff, ip >> 24 & 0xff, ntohs(port), message); } else { - xdag_info("Pool: disconnected from %u.%u.%u.%u:%u by %s", - ip & 0xff, ip >> 8 & 0xff, ip >> 16 & 0xff, ip >> 24 & 0xff, ntohs(port), message); + xdag_info("Pool: disconnected from %u.%u.%u.%u:%u by %s", + ip & 0xff, ip >> 8 & 0xff, ip >> 16 & 0xff, ip >> 24 & 0xff, ntohs(port), message); } free(connection); @@ -1162,12 +1165,13 @@ int pay_miners(xdag_time_t time) void remove_inactive_miners(void) { miner_list_element *elt, *eltmp; + char address[33]; pthread_mutex_lock(&g_descriptors_mutex); LL_FOREACH_SAFE(g_miner_list_head, elt, eltmp) { if(elt->miner_data.state == MINER_ARCHIVE && miner_calculate_unpaid_shares(&elt->miner_data) == 0.0) { - const char *address = xdag_hash2address(elt->miner_data.id.data); + xdag_hash2address(elt->miner_data.id.data, address); LL_DELETE(g_miner_list_head, elt); clear_nonces_hashtable(&elt->miner_data); @@ -1206,8 +1210,10 @@ static const char* connection_state_to_string(int connection_state) static int print_miner(FILE *out, int index, struct miner_pool_data *miner, int print_connections) { char ip_port_str[32], in_out_str[64]; + char address_buf[33]; + xdag_hash2address(miner->id.data, address_buf); - fprintf(out, "%3d. %s %s %-21s %-16s %lf\n", index, xdag_hash2address(miner->id.data), + fprintf(out, "%3d. %s %s %-21s %-16s %lf\n", index, address_buf, miner_state_to_string(miner->state), "-", "-", miner_calculate_unpaid_shares(miner)); if(print_connections) { @@ -1257,7 +1263,12 @@ static void print_connection(FILE *out, int index, struct connection_pool_data * sprintf(in_out_str, "%llu/%llu", (unsigned long long)conn_data->nfield_in * sizeof(struct xdag_field), (unsigned long long)conn_data->nfield_out * sizeof(struct xdag_field)); - strcpy(address, (conn_data->miner ? xdag_hash2address(conn_data->miner->id.data) : "- ")); + if(conn_data->miner) { + xdag_hash2address(conn_data->miner->id.data, address); + } + else { + strcpy(address, "- "); + } fprintf(out, "%3d. %s %s %-21s %-16s %lf\n", index, address, connection_state_to_string(conn_data->state), ip_port_str, in_out_str, connection_calculate_unpaid_shares(conn_data)); } From 4592533c6682d093fee76a7a183f7e0cd9d84a59 Mon Sep 17 00:00:00 2001 From: Evgeniy Date: Mon, 21 May 2018 22:30:02 +0300 Subject: [PATCH 07/11] Version is updated to 0.2.2. List.h is translated --- client/version.h | 15 ++++++++++----- ldus/source/include/ldus/list.h | 22 +++++++++++++--------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/client/version.h b/client/version.h index 5a2b6068..17624a59 100644 --- a/client/version.h +++ b/client/version.h @@ -8,17 +8,22 @@ #define VERSION_MAJOR 0 #define VERSION_MINOR 2 -#define VERSION_REVISION 2 +#define VERSION_REVISION 3 #define XDAG_VERSION STRINGIZE(VERSION_MAJOR) "." STRINGIZE(VERSION_MINOR) "." STRINGIZE(VERSION_REVISION) +/* version history + +0.2.3 bug with payments is fixed + +0.2.2 pool logic is refactored + verifying of shares from miner + initial version of JSON-RPC +*/ + //version history in russian /* история -0.2.2 refatory pool logic - verfiy share from miner - initial version of JSON-RPC - T13.895 более аккуратное сокращение времени прохождения транзакции (132 сек максимум); статистика теперь усредняется по 4-часовому интервалу; более аккуратное устранение утечки памяти; исправлены падения по сигналам 15, 28 diff --git a/ldus/source/include/ldus/list.h b/ldus/source/include/ldus/list.h index e1eeafc5..79b78785 100644 --- a/ldus/source/include/ldus/list.h +++ b/ldus/source/include/ldus/list.h @@ -9,33 +9,37 @@ #define container_of(ptr, type, member) ((type *)((char *)(ptr) - (size_t)offsetof(type, member))) //#define container_of(ptr, type, member) ((type *)((char *)(ptr) - (size_t)&((type *)0)->member)) -/* циклический список */ +/* circular list */ struct list { struct list *prev; struct list *next; }; -/* создать пустой список с данной головой */ +/* create an empty list with specified head */ static inline void list_init(struct list *head) { head->prev = head->next = head; } -/* добавить узел в список */ +/* adds the node to the list */ static inline void list_insert(struct list *head, struct list *node) { - node->prev = head, node->next = head->next; + node->prev = head; + node->next = head->next; node->next->prev = head->next = node; } -/* добавить узел в список с конца */ +/* adds the node to the end of the list */ static inline void list_insert_before(struct list *head, struct list *node) { - node->next = head, node->prev = head->prev; + node->next = head; + node->prev = head->prev; node->prev->next = head->prev = node; } -/* удалить узел из списка */ +/* remove the node from the list */ static inline void list_remove(struct list *node) { - struct list *prev = node->prev, *next = node->next; - prev->next = next, next->prev = prev; + struct list *prev = node->prev; + struct list *next = node->next; + prev->next = next; + next->prev = prev; node->prev = node->next = node; } From c832eb548cb476c134bdec3c94c49cdc07a978f5 Mon Sep 17 00:00:00 2001 From: trueserve Date: Mon, 21 May 2018 12:46:47 -0700 Subject: [PATCH 08/11] Fix the translation for accuracy --- ldus/source/include/ldus/list.h | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/ldus/source/include/ldus/list.h b/ldus/source/include/ldus/list.h index 79b78785..71287bd4 100644 --- a/ldus/source/include/ldus/list.h +++ b/ldus/source/include/ldus/list.h @@ -2,32 +2,32 @@ #ifndef LDUS_LIST_H_INCLUDED #define LDUS_LIST_H_INCLUDED - -#include - -// get rid of access memory 0x0000000000000000 runtime error which will cause dnet restart. -#define container_of(ptr, type, member) ((type *)((char *)(ptr) - (size_t)offsetof(type, member))) -//#define container_of(ptr, type, member) ((type *)((char *)(ptr) - (size_t)&((type *)0)->member)) - -/* circular list */ + +#include + +// get rid of access memory 0x0000000000000000 runtime error which will cause dnet restart. +#define container_of(ptr, type, member) ((type *)((char *)(ptr) - (size_t)offsetof(type, member))) +//#define container_of(ptr, type, member) ((type *)((char *)(ptr) - (size_t)&((type *)0)->member)) + +/* doubly linked list */ struct list { struct list *prev; struct list *next; }; -/* create an empty list with specified head */ +/* initialize an empty list */ static inline void list_init(struct list *head) { head->prev = head->next = head; } -/* adds the node to the list */ +/* adds a node to the end of the list */ static inline void list_insert(struct list *head, struct list *node) { node->prev = head; node->next = head->next; node->next->prev = head->next = node; } -/* adds the node to the end of the list */ +/* adds the node to the beginning of the list */ static inline void list_insert_before(struct list *head, struct list *node) { node->next = head; node->prev = head->prev; From 602780ffe0aab21ad45a3354317e62cbc3142eb4 Mon Sep 17 00:00:00 2001 From: Evgeniy Date: Tue, 22 May 2018 00:33:03 +0300 Subject: [PATCH 09/11] Null reference error is fixed. --- client/pool.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/client/pool.c b/client/pool.c index 3f1b64f8..89b88169 100644 --- a/client/pool.c +++ b/client/pool.c @@ -486,10 +486,9 @@ static void close_connection(connection_list_element *connection, const char *me uint32_t ip = conn_data->ip; uint16_t port = conn_data->port; - char address_buf[33]; - xdag_hash2address(conn_data->miner->id.data, address_buf); - if(conn_data->miner) { + char address_buf[33]; + xdag_hash2address(conn_data->miner->id.data, address_buf); xdag_info("Pool: miner %s disconnected from %u.%u.%u.%u:%u by %s", address_buf, ip & 0xff, ip >> 8 & 0xff, ip >> 16 & 0xff, ip >> 24 & 0xff, ntohs(port), message); } else { @@ -501,7 +500,6 @@ static void close_connection(connection_list_element *connection, const char *me } - /* @method :- calculate_nopaid_shares + @param :- + struct connection_pool_data* connection data :- miner's side data @@ -587,9 +585,13 @@ static int register_new_miner(connection_list_element *connection) struct connection_pool_data *conn_data = &connection->connection_data; xdag_time_t time; - const int64_t position = xdag_get_block_pos(conn_data->data, &time); + const int64_t position = xdag_get_block_pos((const uint64_t*)conn_data->data, &time); if(position < 0) { - close_connection(connection, "Miner's address is unknown"); + char address_buf[33]; + char message[100]; + xdag_hash2address((const uint64_t*)conn_data->data, address_buf); + sprintf(message, "Miner's address is unknown (%s)", address_buf); + close_connection(connection, message); return 0; } @@ -807,8 +809,6 @@ void *pool_main_thread(void *arg) for(;;) { pthread_mutex_lock(&g_descriptors_mutex); -// const int miners_count = g_connections_count; -// int res = poll(g_fds, miners_count, 1000); // move accept connection to g_connection_list_head. LL_FOREACH_SAFE(g_accept_connection_list_head, elt, eltmp) From 459d2ebabf160a944c14a6b030e770d447ecf9dd Mon Sep 17 00:00:00 2001 From: Frozen Date: Tue, 22 May 2018 14:35:28 +0800 Subject: [PATCH 10/11] optimize pool connection --- client/pool.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/client/pool.c b/client/pool.c index 89b88169..765bfef2 100644 --- a/client/pool.c +++ b/client/pool.c @@ -123,6 +123,7 @@ static struct pollfd *g_fds; static connection_list_element *g_connection_list_head = NULL; static connection_list_element *g_accept_connection_list_head = NULL; static miner_list_element *g_miner_list_head = NULL; +static uint32_t g_connection_changed = 0; static pthread_mutex_t g_descriptors_mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t g_pool_mutex = PTHREAD_MUTEX_INITIALIZER; @@ -468,6 +469,7 @@ static void close_connection(connection_list_element *connection, const char *me pthread_mutex_lock(&g_descriptors_mutex); LL_DELETE(g_connection_list_head, connection); --g_connections_count; + g_connection_changed = 1; close(conn_data->connection_descriptor.fd); @@ -815,17 +817,20 @@ void *pool_main_thread(void *arg) { LL_DELETE(g_accept_connection_list_head, elt); LL_APPEND(g_connection_list_head, elt); + g_connection_changed = 1; } - int index = 0; - LL_FOREACH(g_connection_list_head, elt) - { - memcpy(g_fds + index, &elt->connection_data.connection_descriptor, sizeof(struct pollfd)); - ++index; + if(g_connection_changed) { + g_connection_changed = 0; + LL_FOREACH(g_connection_list_head, elt) + { + memcpy(g_fds + index, &elt->connection_data.connection_descriptor, sizeof(struct pollfd)); + ++index; + } } - pthread_mutex_unlock(&g_descriptors_mutex); - int res = poll(g_fds, index, 1000); + int res = poll(g_fds, g_connections_count, 1000); + pthread_mutex_unlock(&g_descriptors_mutex); if(!res) continue; From 3b45d33d474cf233a48eb83fb143f62838995699 Mon Sep 17 00:00:00 2001 From: Frozen Date: Wed, 23 May 2018 00:48:40 +0800 Subject: [PATCH 11/11] use temporal variable to avoid mutex on poll. --- client/pool.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/client/pool.c b/client/pool.c index 765bfef2..c6a84f9b 100644 --- a/client/pool.c +++ b/client/pool.c @@ -829,9 +829,11 @@ void *pool_main_thread(void *arg) } } - int res = poll(g_fds, g_connections_count, 1000); + int connections_count = g_connections_count; pthread_mutex_unlock(&g_descriptors_mutex); + int res = poll(g_fds, connections_count, 1000); + if(!res) continue; // int index = 0;