Exemplo n.º 1
0
        //Compares this UTXO to the one specified by {@code other}, considering them equal if they have
        // {@code txHash} arrays with equal contents and equal {@code index} values
        public override bool Equals(Object other)
        {
            if (other == null)
            {
                return(false);
            }
            UTXO otherUtxo = (UTXO)other;

            // 直接对比hash是否一致
            if (this.utoxHashCode() != otherUtxo.utoxHashCode())
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }
Exemplo n.º 2
0
 public string  handleTxs(Transaction tx)
 {
     try
     {
         string sRet = ConstHelper.BC_OK;
         if (isValidTx(tx))
         {
             //add unspent coin
             for (uint i = 0; i < tx.numOutputs(); ++i)
             {
                 UTXO utxo = new UTXO(tx.getHash(), i);
                 UnCommitedUtxoPool.addUTXO(utxo, tx.getOutput((int)i));
                 LogHelper.WriteInfoLog(string.Format("Add utxo to uncommited pool, utxoHash:{0}", utxo.utoxHashCode()));
             }
             //delete spent coin
             for (int i = 0; i < tx.numInputs(); ++i)
             {
                 Input input = tx.getInput(i);
                 UTXO  utxo  = new UTXO(input.PreTxHash, (uint)input.OutputIndex);
                 CommitedUtxoPool.removeUTXO(utxo);
                 LogHelper.WriteInfoLog(string.Format("Remove utxo from commited pool, utxoHash:{0}", utxo.utoxHashCode()));
             }
         }
         else
         {
             sRet = "invalid transaction";
         }
         LogHelper.WriteInfoLog("handleTxs result:" + sRet);
         return(sRet);
     }
     catch (Exception ex)
     {
         LogHelper.WriteErrorLog(ex.Message);
         return("HandleTxs catch an exception ");
     }
 }
Exemplo n.º 3
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);
        }