示例#1
0
 static void printRegister(Consensus consensus, List<string> addresses)
 {
     ulong total = 0;
     foreach (var s in addresses)
     {
         ulong money = consensus.QueryMoney(s);
         Console.WriteLine("{0} owns \t{1}\tcurrency", s, money);
         total += money;
     }
     Console.WriteLine("Total: {0}", total);
     Console.WriteLine("");
     Console.WriteLine("");
 }
示例#2
0
        static void Main(string[] args)
        {
            Console.WriteLine(DateTime.Now);
            const int difficulty = 12;

            Wallet w = new Wallet(Environment.ExpandEnvironmentVariables(@"C:\Users\ico\thyme\wallet"));
            w.Accounts.Clear();
            for (int i = 0; i < 5; ++i)
            {
                w.Accounts.Add(new Account());
            }
            Random rnd = new Random();

            //setup consensus
            Consensus consensus = new Consensus();
            Block block = new Block();
            //initialize genesis block
            var finderIndex = rnd.Next(5);
            block.data.finderAddress = w.Accounts[finderIndex].Address;
            block.data.transactionsList = new Tx[] {};
            block.data.register = w.Accounts.Select(i => new RegisterRecord() {address = i.Address, amount = 0}).ToArray();
            block.data.register[finderIndex].amount = 25;
            block.data.prevHash = new byte[] { };
            consensus.Work(block, difficulty);

            Console.WriteLine(DateTime.Now);
            printRegister(consensus, w.Accounts.Select(i => i.Address).ToList());
            while (true)
            {
                Console.WriteLine(" ------ ");
                block = new Block(consensus.Blocks[consensus.Blocks.Count - 1].data.getHash);
                block.data.finderAddress = w.Accounts[rnd.Next(5)].Address;
                List<Tx> txList = new List<Tx>();
                foreach (var acc in w.Accounts)
                {
                    foreach (var reciever in w.Accounts)
                    {
                        ulong money = (ulong)rnd.Next((int)consensus.QueryMoney(acc.Address) / 5);
                        if (money > 0 && acc != reciever)
                        {
                            Transaction t = new Transaction(acc.Address,
                                                            reciever.Address,
                                                            money,
                                                            DateTime.Now);
                            Tx tx = new Tx();
                            tx.t = t;
                            tx.senderPublicKey = acc.PublicKey;
                            tx.Sign(acc.keyPair.Private);
                            txList.Add(tx);
                        }
                    }
                }
                block.data.transactionsList = txList.ToArray();

                consensus.Work(block, difficulty);
                Console.WriteLine(DateTime.Now);
                for (int i = 0; i < block.data.transactionsList.Length; ++i)
                {
                    Console.WriteLine("{0} transferred {1} to {2}", block.data.transactionsList[i].t.sender, block.data.transactionsList[i].t.money, block.data.transactionsList[i].t.reciever);
                }
                Console.WriteLine("{0} recieved 25 for finding a block\n", block.data.finderAddress);

                printRegister(consensus, w.Accounts.Select(i => i.Address).ToList());
            }
            Console.WriteLine("Something went wrong");
        }