Пример #1
0
        private void updateUTXOList()
        {
            dgvInputs.Rows.Clear();

            foreach (KeyValuePair <TxOutId, TxOut> tx in UTXO)
            {
                dgvInputs.Rows.Add(false,
                                   Address.FromScript(tx.Value.scriptPubKey).ToString(),
                                   ((decimal)tx.Value.value / (decimal)100000000),
                                   tx.Key.index,
                                   HexString.FromByteArrayReversed(tx.Key.txid));
            }
        }
Пример #2
0
        private void txtTx_TextChanged(object sender, EventArgs e)
        {
            if (disableUpdate)
            {
                return;
            }

            try
            {
                Transaction tx = new Transaction(HexString.ToByteArray(txtTx.Text));

                // Disable *_Changed events
                disableUpdate = true;

                // Clear everything
                UTXO.Clear();
                updateUTXOList();
                selectedUTXO.Clear();
                dgvOutputs.Rows.Clear();
                outputs.Clear();

                foreach (TxIn txin in tx.inputs)
                {
                    TxOutId txi;
                    TxOut   txo;

                    // Try local bitcoind first
                    if (rpc != null)
                    {
                        try
                        {
                            string      sPrevTx = rpc.doRequest <string>("getrawtransaction", new object[] { HexString.FromByteArrayReversed(txin.prevOut) });
                            Transaction prevTx  = new Transaction(HexString.ToByteArray(sPrevTx));
                            txi = new TxOutId(txin.prevOut, txin.prevOutIndex);
                            txo = prevTx.outputs[txin.prevOutIndex];
                            UTXO.Add(txi, txo);
                            selectedUTXO.Add(txi, txo);
                            continue;
                        }
                        catch (Exception)
                        {
                        }
                    }

                    // Get from blockchain if bitcoind fails
                    try
                    {
                        string json;
                        using (WebClient wc = new WebClient())
                        {
                            json = wc.DownloadString("https://blockchain.info/rawtx/" + HexString.FromByteArrayReversed(txin.prevOut));
                        }
                        JavaScriptSerializer          jss  = new JavaScriptSerializer();
                        Dictionary <string, object>   jso  = jss.Deserialize <Dictionary <string, object> >(json);
                        Dictionary <string, object>[] outs = jss.ConvertToType <Dictionary <string, object>[]>(jso["out"]);
                        Address addr         = new Address((string)outs[txin.prevOutIndex]["addr"]);
                        byte[]  scriptPubKey = scriptPubKey = ScriptTemplate.PayToAddress(addr).ToBytes();
                        txi = new TxOutId(txin.prevOut, txin.prevOutIndex);
                        txo = new TxOut(jss.ConvertToType <UInt64>(outs[txin.prevOutIndex]["value"]), scriptPubKey);
                        UTXO.Add(txi, txo);
                        selectedUTXO.Add(txi, txo);
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Could not get input data.");
                        return;
                    }
                }
                updateUTXOList();
                foreach (DataGridViewRow row in dgvInputs.Rows)
                {
                    row.Cells["inSelected"].Value = true;
                }
                foreach (TxOut txout in tx.outputs)
                {
                    string addr;
                    try
                    {
                        addr = Address.FromScript(txout.scriptPubKey).ToString();
                    }
                    catch (Exception)
                    {
                        addr = "Could not decode address";
                    }
                    dgvOutputs.Rows.Add(addr, (decimal)txout.value / 100000000m);
                    outputs.Add(txout);
                }
                updateTx(false);
                txtTx.ForeColor = SystemColors.WindowText;
            }
            // Transaction invalid
            catch (Exception)
            {
                txtTx.ForeColor = Color.Red;
                return;
            }
            finally
            {
                disableUpdate = false;
            }
        }
Пример #3
0
        public static void Main(string[] args)
        {
            BlockQueue q = new BlockQueue();

            FileStream fso = new FileStream(@"D:\bootstrap.dat", FileMode.Create);

            Dictionary <Hash, int>       diskBlockIndex = new Dictionary <Hash, int>();
            Dictionary <int, Block_Disk> diskBlocks     = new Dictionary <int, Block_Disk>();

            List <Queue <Hash> > chains = new List <Queue <Hash> >();

            chains.Add(new Queue <Hash>());

            Queue <Hash> bestChain = new Queue <Hash>();

            bool first = true;

            int index = 0;

            while (q.Count > 0)
            {
                Block_Disk b = q.Dequeue();
//				diskBlocks.Add(index, b);
                diskBlockIndex.Add(b.Hash, index);

                if (first)
                {
                    chains.Single().Enqueue(b.Hash);
                    first = false;
                }
                else
                {
                    int ci = chains.FindIndex(x => x.ToList().Contains(b.prev_block));
                    if (ci < 0)
                    {
                        Console.WriteLine(HexString.FromByteArrayReversed(b.Hash));
                        Console.WriteLine(HexString.FromByteArrayReversed(b.prev_block));
                        Console.WriteLine(HexString.FromByteArrayReversed(chains[ci].Last()));
                        throw new Exception("Invalid PrevBlock!");
                    }
                    if (chains[ci].Last().Equals(b.prev_block))
                    {
                        // Add to chain
                        chains[ci].Enqueue(b.Hash);
                    }
                    else
                    {
                        // not last block in chain, duplicate and add
                        List <Hash> l  = chains[ci].ToList();
                        int         bi = l.FindIndex(x => x.Equals(b.prev_block));
                        l.RemoveRange(bi + 1, l.Count - (bi + 1));
                        l.Add(b.Hash);
                        chains.Add(makeQueue(l));
                    }

                    int longestChain = chains.Max(x => x.Count);
                    chains.RemoveAll(x => x.Count < (longestChain - MAX_ORPHAN_DEPTH));

                    while (chains.Max(x => x.Count) > MAX_ORPHAN_DEPTH * 2)
                    {
                        bestChain.Enqueue(chains.First(x => x.Count == chains.Max(y => y.Count)).Peek());
                        chains.ForEach(c => c.Dequeue());
                        chains.RemoveAll(c => c.Count == 0);
                    }
                }
                index++;
            }
            while (chains.Count > 0 && chains.Max(x => x.Count) > 0)
            {
                bestChain.Enqueue(chains.First(x => x.Count == chains.Max(y => y.Count)).Peek());
                chains.ForEach(c => c.Dequeue());
                chains.RemoveAll(c => c.Count == 0);
            }

            Console.ReadLine();
        }