static void Main(string[] args) { var chain = new Blockchain <Vote>(); var numberGenerator = new Random(); var personGenerator = new PersonNameGenerator(numberGenerator); var candidates = personGenerator.GenerateMultipleFirstAndLastNames(5).Select(e => new Candidate(e)).ToList(); for (var x = 0; x < 100000; x++) { var vote = new Vote(new Voter(personGenerator.GenerateRandomFirstAndLastName()), candidates[numberGenerator.Next(candidates.Count)]); chain.Add(vote); } //Console.WriteLine("ID\tPrevious Hash\t\t\t\t\tHash\t\t\t\t\t\t\tTimestamp"); var totals = new int[candidates.Count]; foreach (var block in chain.Chain) { if (block.Data != null) { totals[candidates.IndexOf(block.Data.Candidate)]++; } //Console.WriteLine($"{block.Id}\t{block?.PreviousHash ?? "",-44}\t{block.Hash,-44}\t{block.Timestamp}\t{block.Data?.Name ?? ""}:{block.Data?.Candidate ?? ""}"); } for (var x = 0; x < candidates.Count; x++) { Console.WriteLine($"{candidates[x].FullName,-30}\t{totals[x]:#,#}"); } Console.WriteLine(); Console.WriteLine($"Has blockchain been tampered with? {!chain.IsValid()}"); Console.ReadKey(); }
public void AddBlock() { Block block = new Block(0); Block block1 = new Block(1); Blockchain blockchain = new Blockchain(block); blockchain.Add(block1); Assert.AreEqual(1, blockchain.BestBlockNumber); }
static void Main(string[] args) { Test(); int difficulty = 3; var chain = new Blockchain <int>(difficulty); chain.AddTag(new Tag("even")); chain.AddTag(new Tag("odd")); var data = new List <ISignedData <int> >(); for (int i = 0; i < 10; i++) { var tag = i % 2 == 0 ? "even" : "odd"; data.Add(chain.Add(i, tag)); } Console.WriteLine($"Chain is:{chain.IsValid()}"); var d = chain[7]; Console.WriteLine($"Fake block is valid: { chain.ValidateData(d) }"); foreach (var item in data) { Console.WriteLine($"Data at index '{item.Index}' is valided: {chain.ValidateData(item)}"); } var odd = chain.GetByTag("odd"); var json = chain.ToJson(); var newChain = Blockchain <int> .CreateFromJson(json); Console.WriteLine($"New chain is valid: {newChain.IsValid()}"); Console.WriteLine($"Data is valid: {newChain.ValidateData(d)}"); Console.WriteLine($"Chains compared and equal: {chain.Compare(newChain)}"); }
public void RejectBlockWithTooMuchGreaterNumber() { Block block = new Block(0); Block block1 = new Block(2); Blockchain blockchain = new Blockchain(block); try { blockchain.Add(block1); Assert.Fail(); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(InvalidOperationException)); Assert.AreEqual("Rejected block", ex.Message); } }
// добавить данные в блокчейн public static void AddData(this Blockchain chain, byte[] Data) { IBlock block = new Block(Data); chain.Add(block); }