Example #1
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);
        }
Example #2
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);
        }
Example #3
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);
        }
Example #4
0
        /**
         * @return true if:
         * (1) all outputs claimed by {@code tx} are in the current UTXO pool,
         * (2) the signatures on each input of {@code tx} are valid,
         * (3) no UTXO is claimed multiple times by {@code tx},
         * (4) all of {@code tx}s output values are non-negative, and
         * (5) the sum of {@code tx}s input values is greater than or equal to the sum of its output
         *     values; and false otherwise.
         */
        public bool isValidTx(Transaction tx)
        {
            LogHelper.WriteMethodLog(true);
            if (tx.listInputs.Count == 0 || tx.listOutputs.Count == 0)
            {
                LogHelper.WriteInfoLog("empty input|output");
                return(false);
            }

            // IMPLEMENT THIS
            double sumOut = 0;
            double sumIn  = 0;

            Dictionary <string, UTXO> dicUsed = new Dictionary <string, UTXO>();

            for (int i = 0; i < tx.numInputs(); i++)
            {
                Input input = tx.getInput(i);

                UTXO utxo = new UTXO(input.PreTxHash, (uint)input.OutputIndex);
                if (!CommitedUtxoPool.contains(utxo))
                {
                    LogHelper.WriteInfoLog(" utxoPool not contain utxo:");
                    return(false); //check (1),utox 包含该交易返回false
                }

                Output PreOutput = CommitedUtxoPool.getTxOutput(utxo); // the consume coin correspond prev output coin;
                sumIn += PreOutput.value;                              //(5) 计算input 指向的pre output 的value,最后保证输入的value等于该笔交易输出的
                string strOriginalTxt = tx.getRawDataToSign(i);
                if (!Cryptor.VerifySignature(input.ScriptSig, PreOutput.scriptPubKey, strOriginalTxt))
                {
                    LogHelper.WriteInfoLog(" VerifySignature fail");
                    return(false);//check(2)
                }


                bool bIsContain = dicUsed.ContainsKey(utxo.utoxHashCode());
                if (!bIsContain) // UTXO不会被重复添加
                {
                    dicUsed.Add(utxo.utoxHashCode(), utxo);
                }
                else
                {
                    LogHelper.WriteInfoLog(" double spend :" + utxo.utoxHashCode());
                    return(false);
                }
            }
            foreach (Output output in tx.getOutputs())
            {
                if (output.value < 0)
                {
                    LogHelper.WriteInfoLog(" output.value < 0 ");
                    return(false);//check(5)
                }
                sumOut += output.value;
            }
            if (sumIn < sumOut)
            {
                LogHelper.WriteInfoLog(" sumIn < sumOut ");
                return(false);//check(5);
            }
            LogHelper.WriteInfoLog("Valid Tx");
            return(true);
        }
Example #5
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);
        }
Example #6
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);
        }