Пример #1
0
        //public bool InsertBasecoin(Transaction basecoinTrans)
        //{
        //    try
        //    {   // one block only has one basecoin
        //        if(!this.lstPoolTx.Contains(basecoinTrans))
        //        {
        //            this.lstPoolTx.Insert(0, basecoinTrans);
        //            return true;

        //        }
        //        else
        //        {
        //            return false;
        //        }

        //    }
        //    catch (Exception ex)
        //    {
        //        LogHelper.WriteErrorLog(ex.Message);
        //        return false;
        //    }
        //}

        public string WriteLastblock(Block block)
        {
            if (block == null)
            {
                return("Invalid block");
            }
            string jsonblock = JsonHelper.Serializer <Block>(block);

            LogHelper.WriteInfoLog(jsonblock);
            string strRet = LeveldbOperator.OpenDB(AppSettings.XXPDBFolder);

            if (strRet != ConstHelper.BC_OK)
            {
                return("Open DB fail");

                LogHelper.WriteInfoLog("Open DB fail");
            }
            strRet = LeveldbOperator.PutKeyValue(block.Hash, jsonblock);
            strRet = LeveldbOperator.PutKeyValue(ConstHelper.BC_LastKey, jsonblock);
            LeveldbOperator.CloseDB();
            if (strRet != ConstHelper.BC_OK)
            {
                return("Write KeyValue fail");

                LogHelper.WriteInfoLog("Write KeyValue fail");
            }

            return(ConstHelper.BC_OK);
        }
Пример #2
0
        private string GetDBfileInfo()
        {
            try
            {
                LogHelper.WriteMethodLog(true);
                string tempzip = Path.Combine(AppSettings.XXPTempFolder, ConstHelper.BC_DBZipName);
                if (File.Exists(tempzip))
                {
                    FileIOHelper.DeleteFile(tempzip);
                }
                ZipHelper.Zip(AppSettings.XXPDBFolder, tempzip);
                FileInfo tempzipInfo = new FileInfo(tempzip);

                LeveldbOperator.OpenDB(AppSettings.XXPDBFolder);
                string strLastblock = LeveldbOperator.GetValue(ConstHelper.BC_LastKey);
                Block  block        = JsonHelper.Deserialize <Block>(strLastblock);
                LeveldbOperator.CloseDB();

                DBFileInfo dbinfo = new DBFileInfo(tempzipInfo.Length, block.Header.Height);
                return(JsonHelper.Serializer <DBFileInfo>(dbinfo));;
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog(ex.Message);
                return(string.Empty);
            }
            finally
            {
                LogHelper.WriteMethodLog(false);
            }
        }
Пример #3
0
        public string CreatUTPoolFromDB(string strDBpath)
        {
            try
            {
                string strRet = LeveldbOperator.OpenDB(strDBpath);
                if (strRet != ConstHelper.BC_OK)
                {
                    return("open db fail");
                }
                strRet = LeveldbOperator.GetfirstKey();
                if (string.IsNullOrEmpty(strRet))
                {
                    return("empty DB");
                }
                string blockValue = LeveldbOperator.GetValue(strRet);
                Block  tempBlock  = JsonHelper.Deserialize <Block>(blockValue);
                this.BlockData2UTXOPool(true, tempBlock);

                while (true)
                {
                    strRet = LeveldbOperator.GetNextKey();
                    if (string.IsNullOrEmpty(strRet))
                    {
                        break;
                    }
                    blockValue = LeveldbOperator.GetValue(strRet);
                    tempBlock  = JsonHelper.Deserialize <Block>(blockValue);
                    this.BlockData2UTXOPool(true, tempBlock);
                }

                if (this.tempUTXOList.Count != 0)
                {
                    foreach (UTXO utxo in this.tempUTXOList)
                    {
                        if (this.CommitedUtxoPool.contains(utxo))
                        {
                            this.CommitedUtxoPool.removeUTXO(utxo);
                        }
                    }
                }

                return(ConstHelper.BC_OK);
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog(ex.Message);
                return("exception");
            }
            finally
            {
                LeveldbOperator.CloseDB();
            }
        }
Пример #4
0
        public static string PrintAlldb()
        {
            try
            {
                string strRet = LeveldbOperator.OpenDB(AppSettings.XXPDBFolder);
                if (strRet != ConstHelper.BC_OK)
                {
                    return("open db fail");
                }
                strRet = LeveldbOperator.GetfirstKey();
                if (string.IsNullOrEmpty(strRet))
                {
                    return("empty DB");
                }
                string blockValue = LeveldbOperator.GetValue(strRet);
                string lastblock  = string.Empty;
                LogHelper.WriteInfoLog(blockValue);


                while (true)
                {
                    strRet = LeveldbOperator.GetNextKey();
                    if (string.IsNullOrEmpty(strRet))
                    {
                        break;
                    }
                    else if (strRet == ConstHelper.BC_LastKey)
                    {
                        lastblock = LeveldbOperator.GetValue(strRet);
                    }
                    else
                    {
                        blockValue = LeveldbOperator.GetValue(strRet);
                        LogHelper.WriteInfoLog(blockValue);
                    }
                }
                LogHelper.WriteInfoLog("LashBlock: " + lastblock);
                return(ConstHelper.BC_OK);
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog(ex.Message);
                return("exception");
            }
            finally
            {
                LeveldbOperator.CloseDB();
            }
        }
Пример #5
0
        public Block GetLastBlock()
        {
            string strRet = LeveldbOperator.OpenDB(AppSettings.XXPDBFolder);

            if (strRet == ConstHelper.BC_OK)
            {
                string strlastBlock = LeveldbOperator.GetValue(ConstHelper.BC_LastKey);
                if (!string.IsNullOrEmpty(strlastBlock))
                {
                    this.mLastBlock = JsonHelper.Deserialize <Block>(strlastBlock);
                }
            }
            LeveldbOperator.CloseDB();
            return(mLastBlock);
        }
Пример #6
0
        public bool CreatBaseCoin(string sBaseCoinScript)
        {
            Block block = new Block();

            // mutex todo 181215
            Transaction basecoinTrans = this.CreatCoinBaseTX(sBaseCoinScript);

            this.AddTx2hsPool(basecoinTrans);
            //this.HashsetPool2list();
            block.listTransactions = this.GetlstPoolTx();
            this.ClearTxPool();
            block.SetTransInfo();

            block.SetBlockHeader("0000000000000000000000000000000000000000000000000000000000000000", -1);

            block.SetNonce("");
            //add by fdp 190114 set hash all 0 temporary for computing block size,
            block.Hash = ConstHelper.BC_BaseCoinInputTxHash;
            string jsonBlock = JsonHelper.Serializer <Block>(block);

            block.SetBlockSize(jsonBlock.Length);

            block.SetBlockHash();

            string jsonblock = JsonHelper.Serializer <Block>(block);

            LogHelper.WriteInfoLog(jsonblock);

            string strRet = LeveldbOperator.OpenDB(AppSettings.XXPDBFolder);

            strRet = LeveldbOperator.PutKeyValue(block.Hash, jsonblock);
            strRet = LeveldbOperator.PutKeyValue(ConstHelper.BC_LastKey, jsonblock);
            bool   breadOK = false;
            string readout = LeveldbOperator.GetValue(block.Hash);

            LogHelper.WriteInfoLog(readout);
            LeveldbOperator.CloseDB();

            if (!string.IsNullOrEmpty(readout))
            {
                breadOK = true;
            }
            return(breadOK);
        }
Пример #7
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));
        }
Пример #8
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");
            }
        }