예제 #1
0
파일: TxHandler.cs 프로젝트: v-Paul/Budding
 public TxHandler()
 {
     // IMPLEMENT THIS
     this.CommitedUtxoPool   = new UTXOPool();
     this.UnCommitedUtxoPool = new UTXOPool();
     this.tempUTXOList       = new List <UTXO>();
 }
예제 #2
0
        public string CheckBalance(string strKey, double dPaytoAmount, UTXOPool utxopool)
        {
            LogHelper.WriteMethodLog(true);
            string strRet = ConstHelper.BC_OK;

            if (strKey != ConstHelper.BC_All)
            {
                double dMyval = this.GetValue(true, strKey, utxopool);
                if (dMyval < dPaytoAmount)
                {
                    // check all
                    dMyval = this.GetValue(true, ConstHelper.BC_All, utxopool);
                    if (dMyval < dPaytoAmount)
                    {
                        strRet = "not sufficient funds";
                    }
                    else
                    {
                        strRet = @"Current key not sufficient funds,pls select key <All>";
                    }
                }
            }
            else
            {
                double dMyval = this.GetValue(true, strKey, utxopool);
                if (dMyval < dPaytoAmount)
                {
                    strRet = "not sufficient funds";
                }
            }
            LogHelper.WriteInfoLog("CheckBalance result: " + strRet);
            return(strRet);
        }
예제 #3
0
        public double GetValue(bool bCommited, string key, UTXOPool utxopool)
        {
            LogHelper.WriteMethodLog(true);
            double dVal = 0;

            if (key == ConstHelper.BC_All)
            {
                Dictionary <string, double> dickeyValue = GetDicKeyValue(bCommited, utxopool);
                foreach (var dicItem in dickeyValue)
                {
                    dVal += dicItem.Value;
                }
            }
            else
            {
                List <UTXO> UtxoList = new List <UTXO>();
                if (bCommited)
                {
                    this.dicComitkeysUtxoList.TryGetValue(key, out UtxoList);
                }
                else
                {
                    this.dicUnComitkeysUtxoList.TryGetValue(key, out UtxoList);
                }

                foreach (var item in UtxoList)
                {
                    Output output = utxopool.getTxOutput(item);
                    dVal += output.value;
                }
            }

            LogHelper.WriteInfoLog(string.Format("IsCommited{0} {1}:{2} ", bCommited, key, dVal));
            return(dVal);
        }
예제 #4
0
        /// <summary>
        /// 仅用来提示哪个key在这个block里收到多少钱
        /// </summary>
        /// <param name="utxoSinglePool"></param>
        /// <returns></returns>
        //public List<PubKeyValue> RefKVFromSigUTxpool(UTXOPool utxoSinglePool)
        //{
        //    LogHelper.WriteMethodLog(true);
        //    DirectoryInfo KeyFolder = new DirectoryInfo(AppSettings.XXPKeysFolder);
        //    FileInfo[] files = KeyFolder.GetFiles("pubkey?.pem");
        //    List<PubKeyValue> lstPubKeyValue = new List<PubKeyValue>();
        //    foreach (UTXO utxo in utxoSinglePool.getAllUTXO())
        //    {
        //        Output output = utxoSinglePool.getTxOutput(utxo);

        //        foreach (FileInfo fi in files)
        //        {
        //            string pukhash = string.Empty;
        //            this.dicKey2Hash.TryGetValue(fi.Name, out pukhash);
        //            if (output.scriptPubKey.IndexOf(pukhash) >= 0)
        //            {
        //                PubKeyValue KV = new PubKeyValue(fi.Name, output.value);
        //                lstPubKeyValue.Add(KV);
        //                if (this.dickeyValue.ContainsKey(fi.Name))
        //                {
        //                    double dvalue = 0;
        //                    this.dickeyValue.TryGetValue(fi.Name, out dvalue);
        //                    this.dickeyValue[fi.Name] = dvalue + output.value;
        //                }
        //                else
        //                {
        //                    this.dickeyValue[fi.Name] = output.value;
        //                }
        //                // deleted by fdp 这样会导致key对应的utxo list 只增没减,导致金额显示不平
        //                //List<UTXO> lstTemp = new List<UTXO>();
        //                //this.dickeysUtxoList.TryGetValue(fi.Name, out lstTemp);
        //                //lstTemp.Add(utxo);
        //                //this.dickeysUtxoList[fi.Name] = lstTemp;

        //                break;
        //            }
        //        }
        //    }
        //    LogHelper.WriteMethodLog(false);
        //    return lstPubKeyValue;
        //}

        public Dictionary <string, double> GetDicKeyValue(bool bCommited, UTXOPool utxopool)
        {
            LogHelper.WriteMethodLog(true);
            Dictionary <string, double>       dickeyValue  = new Dictionary <string, double>();
            Dictionary <string, List <UTXO> > keysUtxoList = new Dictionary <string, List <UTXO> >();

            if (bCommited)
            {
                keysUtxoList = this.dicComitkeysUtxoList;
            }
            else
            {
                keysUtxoList = this.dicUnComitkeysUtxoList;
            }
            foreach (var item in keysUtxoList)
            {
                double dValue = 0;
                foreach (var utxo in item.Value)
                {
                    Output output = utxopool.getTxOutput(utxo);
                    dValue += output.value;
                }
                dickeyValue.Add(item.Key, dValue);
            }

            string logKeyValue = string.Empty;

            foreach (var item in dickeyValue)
            {
                logKeyValue += string.Format("{0}:{1}", item.Key, item.Value.ToString("0.0000")) + Environment.NewLine;
            }
            LogHelper.WriteInfoLog(logKeyValue);

            return(dickeyValue);
        }
예제 #5
0
파일: TxHandler.cs 프로젝트: v-Paul/Budding
        /// <summary>
        /// 根据block信息更新UTXO pool,同时返回该block生成的utxo list,用来提示该block收到了多少钱
        /// </summary>
        /// <param name="block"></param>
        /// <returns>just for update key corresponding value</returns>
        public UTXOPool BlockData2UTXOPool(bool bCommitedPool, Block block)
        {
            LogHelper.WriteMethodLog(true);
            LogHelper.WriteInfoLog("bCommitedPool: " + bCommitedPool.ToString());
            UTXOPool utxopoll = new UTXOPool();

            if (bCommitedPool)
            {
                utxopoll = this.CommitedUtxoPool;
            }
            else
            {
                utxopoll = this.UnCommitedUtxoPool;
            }



            UTXOPool sigleBlockPool = new UTXOPool();

            foreach (Transaction eTransaction in block.listTransactions)
            {
                foreach (Input eInput in eTransaction.listInputs)
                {
                    UTXO utxo = this.input2UTXO(eInput);
                    if (utxopoll.contains(utxo))
                    {
                        utxopoll.removeUTXO(utxo);
                    }
                    else
                    {
                        // 初始化时由于leveldb 树形结构第一个可能创世块,所以先记下来在后面再remove
                        if (utxo.getTxHash() != ConstHelper.BC_BaseCoinInputTxHash)
                        {
                            tempUTXOList.Add(utxo);
                        }
                    }
                }

                for (int i = 0; i < eTransaction.listOutputs.Count; i++)
                {
                    // hash is transaction hash
                    UTXO utxo1 = new UTXO(eTransaction.getHash(), (uint)i);

                    utxopoll.addUTXO(utxo1, eTransaction.listOutputs[i]);
                    sigleBlockPool.addUTXO(utxo1, eTransaction.listOutputs[i]);
                }
            }
            LogHelper.WriteMethodLog(false);
            return(sigleBlockPool);
        }
예제 #6
0
        public string CheckBalance(string strKey, double dPaytoAmount, UTXOPool utxopool)
        {
            LogHelper.WriteMethodLog(true);
            string strRet = ConstHelper.BC_OK;

            if (dPaytoAmount == 0)
            {
                strRet = "You want to trap me, NOWAY!";
            }

            if (strKey != ConstHelper.BC_All)
            {
                double dMyval = this.GetValue(true, strKey, utxopool);
                if (dMyval < dPaytoAmount)
                {
                    // check all
                    dMyval = this.GetValue(true, ConstHelper.BC_All, utxopool);
                    if (dMyval < dPaytoAmount)
                    {
                        strRet = "Insufficient balance";
                    }
                    else
                    {
                        strRet = @"Current key's balance is insufficient,pls select key <All>";
                    }
                }
            }
            else
            {
                double dMyval = this.GetValue(true, strKey, utxopool);
                if (dMyval < dPaytoAmount)
                {
                    strRet = "Insufficient balance";
                }
            }
            LogHelper.WriteInfoLog("CheckBalance result: " + strRet);
            return(strRet);
        }
예제 #7
0
 // Creates a new UTXOPool that is a copy of {@code uPool}
 public UTXOPool(UTXOPool uPool)
 {
     dicUtxoPool = new  Dictionary <UTXO, Output>(uPool.dicUtxoPool);
 }
예제 #8
0
        public Dictionary <UTXO, keyPair> FindInputUtxo(string key, double dPaytoAmount, UTXOPool utxopool, ref double inputTotalAmount)
        {
            LogHelper.WriteMethodLog(true);
            Dictionary <UTXO, keyPair> dicKeyAllUtxo = this.GetUtxoDic(key);
            Dictionary <UTXO, keyPair> dicInputUtxo  = new Dictionary <UTXO, keyPair>();
            double sumValue = 0;

            foreach (var item in dicKeyAllUtxo)
            {
                dicInputUtxo.Add(item.Key, item.Value);

                Output op = utxopool.getTxOutput(item.Key);
                sumValue += op.value;
                if (sumValue >= dPaytoAmount)
                {
                    inputTotalAmount = sumValue;
                    break;
                }
            }
            LogHelper.WriteMethodLog(false);
            return(dicInputUtxo);
        }
예제 #9
0
        public void RefKUtxoList(bool bCommited, UTXOPool utxopool)
        {
            LogHelper.WriteMethodLog(true);
            Dictionary <string, List <UTXO> > dicKeysUtxoList = new Dictionary <string, List <UTXO> >();

            if (bCommited)
            {
                dicKeysUtxoList = this.dicComitkeysUtxoList;
            }
            else
            {
                dicKeysUtxoList = this.dicUnComitkeysUtxoList;
            }



            DirectoryInfo KeyFolder = new DirectoryInfo(AppSettings.XXPKeysFolder);

            FileInfo[] files = KeyFolder.GetFiles("pubkey?.pem");

            if (dicKeysUtxoList.Count == 0)
            {// 初始化
                foreach (FileInfo fi in files)
                {
                    //this.dickeyValue.Add(fi.Name, 0);
                    if (!this.dicKey2Hash.ContainsKey(fi.Name))
                    {
                        this.dicKey2Hash.Add(fi.Name, this.pubkey2Hash(fi.FullName));
                    }

                    List <UTXO> lstKeyUtxo = new List <UTXO>();
                    dicKeysUtxoList.Add(fi.Name, lstKeyUtxo);
                }
            }
            else
            {//先清空,赋零,
                dicKeysUtxoList.Clear();
                foreach (FileInfo fi in files)
                {
                    //this.dickeyValue[fi.Name] = 0;
                    List <UTXO> lstKeyUtxo = new List <UTXO>();
                    dicKeysUtxoList.Add(fi.Name, lstKeyUtxo);
                }
            }

            // 再根据utxopool重新计算每个key对应的value
            foreach (UTXO utxo in utxopool.getAllUTXO())
            {
                Output output = utxopool.getTxOutput(utxo);

                foreach (FileInfo fi in files)
                {
                    string pukhash = string.Empty;
                    this.dicKey2Hash.TryGetValue(fi.Name, out pukhash);
                    if (output.scriptPubKey.IndexOf(pukhash) >= 0)
                    {
                        //deleted by fdp 181224 key corresponding value calculate from utxo list
                        //if(this.dickeyValue.ContainsKey(fi.Name))
                        //{
                        //    double dvalue = 0;
                        //    this.dickeyValue.TryGetValue(fi.Name, out dvalue);
                        //    this.dickeyValue[fi.Name] = dvalue + output.value;
                        //}
                        //else
                        //{
                        //    this.dickeyValue[fi.Name] = output.value;
                        //}
                        List <UTXO> lstTemp = new List <UTXO>();
                        dicKeysUtxoList.TryGetValue(fi.Name, out lstTemp);
                        lstTemp.Add(utxo);
                        dicKeysUtxoList[fi.Name] = lstTemp;

                        break;
                    }
                }
            }



            LogHelper.WriteMethodLog(false);
        }
예제 #10
0
        public void RefKUtxoList(bool bCommited, UTXOPool utxopool)
        {
            LogHelper.WriteMethodLog(true);
            Dictionary <string, List <UTXO> > keysUtxoList  = new Dictionary <string, List <UTXO> >();
            Dictionary <UTXO, Output>         dicMsUTXOPool = new Dictionary <UTXO, Output>();

            if (bCommited)
            {
                keysUtxoList  = this.dicComitkeysUtxoList;
                dicMsUTXOPool = this.dicComitMsUTXOPool;
            }
            else
            {
                keysUtxoList  = this.dicUnComitkeysUtxoList;
                dicMsUTXOPool = this.dicUnComitMsUTXOPool;
            }



            DirectoryInfo KeyFolder = new DirectoryInfo(AppSettings.XXPKeysFolder);

            FileInfo[] files = KeyFolder.GetFiles("pubkey?.pem");

            if (keysUtxoList.Count == 0)
            {// 初始化
                foreach (FileInfo fi in files)
                {
                    //this.dickeyValue.Add(fi.Name, 0);
                    if (!this.dicKeyHash.ContainsKey(fi.Name))
                    {
                        this.dicKeyHash.Add(fi.Name, this.pubkey2Hash(fi.FullName));
                    }

                    List <UTXO> lstKeyUtxo = new List <UTXO>();
                    keysUtxoList.Add(fi.Name, lstKeyUtxo);
                }
            }
            else
            {//先清空,赋零,
                keysUtxoList.Clear();
                foreach (FileInfo fi in files)
                {
                    //this.dickeyValue[fi.Name] = 0;
                    List <UTXO> lstKeyUtxo = new List <UTXO>();
                    keysUtxoList.Add(fi.Name, lstKeyUtxo);
                }

                // Multisign
                dicMsUTXOPool.Clear();
            }

            // 再根据utxopool重新计算每个key对应的value
            foreach (UTXO utxo in utxopool.getAllUTXO())
            {
                Output output = utxopool.getTxOutput(utxo);

                foreach (FileInfo fi in files)
                {
                    string pukhash = string.Empty;
                    this.dicKeyHash.TryGetValue(fi.Name, out pukhash);

                    if (output.scriptPubKey.IndexOf(pukhash) >= 0)
                    {
                        // MultiSign
                        if (output.scriptPubKey.IndexOf("OP_CHECKMULTISIG") >= 0)
                        {
                            dicMsUTXOPool.Add(utxo, output);
                        }
                        else
                        {
                            List <UTXO> lstTemp = new List <UTXO>();
                            keysUtxoList.TryGetValue(fi.Name, out lstTemp);
                            lstTemp.Add(utxo);
                            keysUtxoList[fi.Name] = lstTemp;
                        }
                        break;
                    }
                }
            }



            LogHelper.WriteMethodLog(false);
        }