Example #1
0
        public void Write(Block b)
        {
            using (var file = new FileStream(Path.Combine(folder, string.Format("{0:x}.blk", b.Id)), FileMode.CreateNew, FileAccess.Write))
            using (var writer = new StreamWriter(file))
            {
                var formatter = new BlockPrettyWriter(writer);
                formatter.Write(b);
            }

            using (var file = new FileStream(Path.Combine(folder, headBlockFileName), FileMode.Create, FileAccess.Write))
            using (var writer = new StreamWriter(file))
            {
                var formatter = new BlockPrettyWriter(writer);
                formatter.Write(b);
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            var cryptoFunction = MD5.Create();

            var chain = new FolderBlockChain();

            var block = new Block();
            block.Transactions = new []
            {
                CreateDummy(),
                CreateDummy(),
                CreateDummy()
            };

            try
            {
                var headBlock = chain.Head();

                block.PreviousBlockId = headBlock.Id;
            }
            catch
            {
                block.PreviousBlockId = 0;
            }

            byte[] hashBytes = new byte[0];
            uint nonce = 0;

            var startTime = DateTime.Now;

            Console.WriteLine("Start Time: {0}", startTime);

            do
            {
                block.Nonce = nonce;
                var blockBytes = block.GetBytes();
                hashBytes = cryptoFunction.ComputeHash(blockBytes);

                var hash = BitConverter.ToUInt64(hashBytes, 0);

                Console.WriteLine("{0:X} - {1:X}", nonce, hash);

                // increment nonce for next attempt
                nonce++;

            // difficulty of 8 zeros
            }while((hashBytes[0] & 0xFF) != 0);

            var endTime = DateTime.Now;

            Console.WriteLine("End Time: {0}\nElapsed Time: {1} ms\nVerified Block:\n\n", endTime, (endTime - startTime).Milliseconds);

            var formatter = new BlockPrettyWriter(Console.Out);
            formatter.Write(block);

            chain.Write(block);

            Console.ReadKey();
        }