示例#1
0
        public ResponseBlock RequestNewBlockInfo(Block lastBlock)
        {
            LogHelper.WriteMethodLog(true);
            RequestBlock ReqBkInfo = new RequestBlock();

            ReqBkInfo.RequestType     = BlockRequestType.RequestBlockInfo;
            ReqBkInfo.LastBlockHash   = lastBlock.Hash;
            ReqBkInfo.LastBlockHeight = lastBlock.Header.Height;
            string strValue = JsonHelper.Serializer <LastBlockInfo>(ReqBkInfo);

            XXPSocketsModel sendMod = new XXPSocketsModel();

            sendMod.Type  = XXPCoinMsgType.SyncBlocks;
            sendMod.Value = strValue;

            ResponseBlock            ResponseBkInfo = new ResponseBlock();
            List <ResponseBlock>     lstResponse    = new List <ResponseBlock>();
            Dictionary <string, int> dicAddPool     = new Dictionary <string, int>();

            dicAddPool = this.dicAddressesPool;
            foreach (var item in dicAddPool)
            {
                XXPSocketsModel RetMod = this.SocketsHelp.XXPSendMessage(item.Key, sendMod, AppSettings.XXPCommport);
                if (RetMod.Type == XXPCoinMsgType.Exception)
                {
                    int lostCount = item.Value;
                    //this.dicAddressesPool[item.Key] = lostCount++;

                    return(ResponseBkInfo);
                }
                else if (!string.IsNullOrEmpty(RetMod.Value))
                {
                    ResponseBlock bkInfo = JsonHelper.Deserialize <ResponseBlock>(RetMod.Value);
                    bkInfo.IP = RetMod.IpAddress;
                    lstResponse.Add(bkInfo);
                }
            }



            int OrphanCount = lstResponse.Count(x => x.BlockResult == BlockResultType.OrphanBlock);

            if (OrphanCount > lstResponse.Count / 2)
            {
                ResponseBkInfo.BlockResult = BlockResultType.OrphanBlock;
            }
            else
            {
                int iHighest = lstResponse.Max(x => x.LastBlockHeight);
                ResponseBkInfo = lstResponse.FirstOrDefault(x => x.LastBlockHeight == iHighest);
            }

            LogHelper.WriteInfoLog("RequestNewBlockInfo ret: " + ResponseBkInfo.BlockResult);
            return(ResponseBkInfo);
        }
示例#2
0
        private string CheckBlock(RequestBlock ReqBkInfo)
        {
            LogHelper.WriteMethodLog(true);
            LeveldbOperator.OpenDB(AppSettings.XXPDBFolder);
            string strLastblock = LeveldbOperator.GetValue(ConstHelper.BC_LastKey);
            Block  block        = JsonHelper.Deserialize <Block>(strLastblock);


            ResponseBlock RetBkInfo = new ResponseBlock();

            RetBkInfo.LastBlockHash   = block.Hash;
            RetBkInfo.LastBlockHeight = block.Header.Height;

            if (ReqBkInfo.LastBlockHeight > block.Header.Height)
            {
                RetBkInfo.BlockResult = BlockResultType.Higher;
                // request new block todo 181213
            }
            else
            {
                string strBlock = LeveldbOperator.GetValue(ReqBkInfo.LastBlockHash);
                if (string.IsNullOrEmpty(strBlock))
                {
                    RetBkInfo.BlockResult = BlockResultType.OrphanBlock;
                }
                else
                {
                    if (ReqBkInfo.LastBlockHeight == block.Header.Height)
                    {
                        RetBkInfo.BlockResult = BlockResultType.Sameheight;
                    }
                    else
                    {
                        RetBkInfo.BlockResult = BlockResultType.Lower;
                    }
                }
            }

            LeveldbOperator.CloseDB();
            LogHelper.WriteMethodLog(false);
            return(JsonHelper.Serializer <ResponseBlock>(RetBkInfo));
        }