Beispiel #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);
        }
Beispiel #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));
        }
Beispiel #3
0
        public string GetNewBlocks(string ip, Block lastBlock)
        {
            LogHelper.WriteMethodLog(true);
            RequestBlock ReqBkInfo = new RequestBlock();

            ReqBkInfo.RequestType     = BlockRequestType.GetNewBlocks;
            ReqBkInfo.LastBlockHash   = lastBlock.Hash;
            ReqBkInfo.LastBlockHeight = lastBlock.Header.Height;


            XXPSocketsModel sendMod = new XXPSocketsModel();

            sendMod.Type  = XXPCoinMsgType.SyncBlocks;
            sendMod.Value = JsonHelper.Serializer <RequestBlock>(ReqBkInfo);

            XXPSocketsModel RetMod = this.SocketsHelp.XXPSendMessage(ip, sendMod, AppSettings.XXPCommport);

            LogHelper.WriteInfoLog("GetNewBlocks return: " + RetMod.Value);
            return(RetMod.Value);
        }
Beispiel #4
0
        public string StartSendBlocks(RequestBlock ReqBkInfo)
        {
            try
            {
                LogHelper.WriteMethodLog(true);
                List <Block> lstTobeSendBlocks = new List <Block>();
                LeveldbOperator.OpenDB(AppSettings.XXPDBFolder);
                string strReqBlock = LeveldbOperator.GetValue(ReqBkInfo.LastBlockHash);
                if (!string.IsNullOrEmpty(strReqBlock))
                {
                    string strblock = LeveldbOperator.GetValue(ConstHelper.BC_LastKey);
                    Block  block    = JsonHelper.Deserialize <Block>(strblock);

                    while (block.Hash != ReqBkInfo.LastBlockHash)
                    {
                        lstTobeSendBlocks.Add(block);
                        strblock = LeveldbOperator.GetValue(block.Header.PreHash);
                        block    = JsonHelper.Deserialize <Block>(strblock);
                    }
                }
                LeveldbOperator.CloseDB();

                Task.Run(() => {
                    //foreach (var item in tobeSendBlocks)
                    for (int i = lstTobeSendBlocks.Count; i > 0; i--)
                    {
                        string str = SendNewBlock(ReqBkInfo.IP, lstTobeSendBlocks[i - 1]);
                        LogHelper.WriteInfoLog(str);
                    }
                });
                LogHelper.WriteMethodLog(false);
                return(ConstHelper.BC_OK);
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog(ex.Message);
                return("exception");
            }
        }
Beispiel #5
0
        private string handleSyncBlocks(XXPSocketsModel socketMod)
        {
            LogHelper.WriteMethodLog(true);
            RequestBlock ReqBkInfo = JsonHelper.Deserialize <RequestBlock>(socketMod.Value);

            ReqBkInfo.IP = socketMod.IpAddress;
            string strRet = string.Empty;

            switch (ReqBkInfo.RequestType)
            {
            case BlockRequestType.RequestBlockInfo:
                strRet = CheckBlock(ReqBkInfo);
                break;

            case BlockRequestType.GetNewBlocks:
                strRet = StartSendBlocks(ReqBkInfo);
                break;

            default:
                break;
            }
            LogHelper.WriteInfoLog("handleSyncBlocks ret: " + strRet);
            return(strRet);
        }