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"); }
public void Work(Block data, int difficulty) { List<Tx> validTransactions = new List<Tx>(); List<RegisterRecord> register; if (Blocks.Count > 0) { register = new List<RegisterRecord>(Blocks[Blocks.Count - 1].data.register); data.data.prevHash = Blocks[Blocks.Count - 1].data.getHash; } else { register = new List<RegisterRecord>(data.data.register); foreach (var r in register) { r.amount = 0; } } for (int i = 0; i < data.data.transactionsList.Length; ++i) { if (register.First(it => it.address.Equals(data.data.transactionsList[i].t.sender)).amount >= data.data.transactionsList[i].t.money) { validTransactions.Add(data.data.transactionsList[i]); register.First(it => it.address.Equals(data.data.transactionsList[i].t.sender)).amount -= data.data.transactionsList[i].t.money; register.First(it => it.address.Equals(data.data.transactionsList[i].t.reciever)).amount += data.data.transactionsList[i].t.money; } } register.First(it => it.address.Equals(data.data.finderAddress)).amount += 25; data.data.transactionsList = validTransactions.ToArray(); data.data.register = register.ToArray(); data.findNonce(difficulty); Blocks.Add(data); }