Example #1
0
        public static void Main(string[] args)
        {
            Dictionary <Address, UInt64> balances = new Dictionary <Address, UInt64>();

            UnspentTxOutList utxo = new UnspentTxOutList();
            Hash             lastBlockHash;

            using (FileStream fs = new FileStream(@"D:\utxo.dat", FileMode.Open))
            {
                BinaryReader br = new BinaryReader(fs);
                lastBlockHash = br.ReadBytes(32);
                utxo          = UnspentTxOutList.FromStream(fs);
            }

            foreach (KeyValuePair <OutPoint, TxOut> txo in utxo)
            {
                Address a = Address.FromScript(txo.Value.scriptPubKey);
                if (a == null)
                {
                    continue;
                }
                if (!balances.ContainsKey(a))
                {
                    balances.Add(a, 0);
                }
                balances[a] += txo.Value.value;
            }

            foreach (KeyValuePair <Address, UInt64> bal in balances.AsParallel().OrderByDescending(x => x.Value))
            {
                Console.WriteLine(bal.Key + "\t" + bal.Value);
            }
        }
        public static void Main(string[] args)
        {
            string outFile = @"D:\utxo.dat";

            UnspentTxOutList utxo = new UnspentTxOutList();

            Hash lastBlockHash = null;

            if (File.Exists(outFile))
            {
                using (FileStream fs = new FileStream(outFile, FileMode.Open))
                {
                    BinaryReader br = new BinaryReader(fs);
                    lastBlockHash = br.ReadBytes(32);
                    utxo          = UnspentTxOutList.FromStream(fs);
                }
            }

            BlockFileReader blockDb = new BlockFileReader();

            Block_Disk b;
            Block      lastBlock = null;

            while ((b = blockDb.getNext()) != null)
            {
                lastBlock = b;
                if (lastBlockHash != null && !b.prev_block.Equals(lastBlockHash))
                {
                    continue;
                }
                lastBlockHash = null;

                foreach (Transaction tx in b.txns)
                {
                    foreach (TxIn txin in tx.inputs)
                    {
                        if (txin.prevOutIndex == 0xFFFFFFFF)                         //coinbase
                        {
                            continue;
                        }
                        if (!utxo.TryRemove(new TxOutId(txin.prevOut, txin.prevOutIndex)))
                        {
                            Console.WriteLine("Error: Invalid TxIn! Blockchain likely contains orphans.");
                            return;
                        }
                    }
                    for (uint i = 0; i < tx.outputs.Length; i++)
                    {
                        utxo.Add(new TxOutId(tx.Hash, i), tx.outputs[i]);
                    }
                }
            }

            using (FileStream fs = new FileStream(outFile, FileMode.Create))
            {
                BinaryWriter bw = new BinaryWriter(fs);
                bw.Write(lastBlock.Hash, 0, 32);
                utxo.Write(fs);
            }
        }
        public static void Main(string[] args)
        {
            Dictionary<Address, UInt64> balances = new Dictionary<Address, UInt64>();

            UnspentTxOutList utxo = new UnspentTxOutList();
            Hash lastBlockHash;
            using (FileStream fs = new FileStream(@"D:\utxo.dat", FileMode.Open))
            {
                BinaryReader br = new BinaryReader(fs);
                lastBlockHash = br.ReadBytes(32);
                utxo = UnspentTxOutList.FromStream(fs);
            }

            foreach (KeyValuePair<TxOutId, TxOut> txo in utxo)
            {
                Address a = Address.FromScript(txo.Value.scriptPubKey);
                if (a == null)
                    continue;
                if (!balances.ContainsKey(a))
                    balances.Add(a, 0);
                balances[a] += txo.Value.value;
            }

            foreach (KeyValuePair<Address, UInt64> bal in balances.AsParallel().OrderByDescending(x=>x.Value))
            {
                Console.WriteLine(bal.Key + "\t" + bal.Value);
            }
        }
        public static void Main(string[] args)
        {
            string outFile = @"D:\utxo.dat";

            UnspentTxOutList utxo = new UnspentTxOutList();

            Hash lastBlockHash = null;

            if (File.Exists(outFile))
            {
                using (FileStream fs = new FileStream(outFile, FileMode.Open))
                {
                    BinaryReader br = new BinaryReader(fs);
                    lastBlockHash = br.ReadBytes(32);
                    utxo = UnspentTxOutList.FromStream(fs);
                }
            }

            BlockFileReader blockDb = new BlockFileReader();

            Block_Disk b;
            Block lastBlock = null;
            while ((b = blockDb.getNext()) != null)
            {
                lastBlock = b;
                if (lastBlockHash != null && !b.prev_block.Equals(lastBlockHash))
                    continue;
                lastBlockHash = null;

                foreach (Transaction tx in b.txns)
                {
                    foreach (TxIn txin in tx.inputs)
                    {
                        if (txin.prevOutIndex == 0xFFFFFFFF) //coinbase
                            continue;
                        if (!utxo.TryRemove(new UnspentTxOutHeader(txin.prevOut, txin.prevOutIndex)))
                        {
                            Console.WriteLine("Error: Invalid TxIn! Blockchain likely contains orphans.");
                            return;
                        }
                    }
                    for (uint i = 0; i < tx.outputs.Length; i++)
                        utxo.Add(new UnspentTxOutHeader(tx.Hash, i), tx.outputs[i]);
                }
            }

            using (FileStream fs = new FileStream(outFile, FileMode.Create))
            {
                BinaryWriter bw = new BinaryWriter(fs);
                bw.Write(lastBlock.Hash, 0, 32);
                utxo.Write(fs);
            }
        }