internal void SetupNewPublisher(string country, string region, string entityName) { var keys = EncryptionHandler.GenerateNewKeys(); transactionGenerator = new TransactionGenerator(keys.PrivateKey); ITransaction registration = transactionGenerator.InitializeAsNewPublisher(keys.PublicKey, country, region, entityName); node.User = new UserProperties() { Username = currentUser , Keys = keys , PublisherAddress = registration.TransactionId }; if (!node.IsChainInitialized()) { ITransaction vote = transactionGenerator.GenerateVotingTransaction(registration.TransactionId, true); node.RequestChainInitialization(registration, vote); } else { node.OnReveiceTransaction(registration); } }
public void TransactionGeneration() { var keys = EncryptionHandler.GenerateNewKeys(); TransactionGenerator transactionGenerator = new TransactionGenerator(keys.PrivateKey); var registration = transactionGenerator.InitializeAsNewPublisher(keys.PublicKey, "Austria", "Vienna", "Master"); Assert.IsTrue(registration.ValidateTransactionIntegrity(keys.PublicKey)); }
public void RegistrateAndVotePhysician() { #region Set up Publisher var keysPublisher = EncryptionHandler.GenerateNewKeys(); TransactionGenerator transactionGeneratorPublisher = new TransactionGenerator(keysPublisher.PrivateKey); var registrationPublisher = transactionGeneratorPublisher.InitializeAsNewPublisher(keysPublisher.PublicKey, "Austria", "Vienna", "Publisher"); var votePublisher = transactionGeneratorPublisher.GenerateVotingTransaction(registrationPublisher.TransactionId, true); PublisherNode publisher = new PublisherNode(); publisher.User = new NodeManagement.Entities.UserProperties() { Keys = keysPublisher, UserAddress = registrationPublisher.TransactionId, Username = "******" }; publisher.InitializeEmptyChain(registrationPublisher, votePublisher); Assert.IsTrue(publisher.participantHandler.HasPublisher(registrationPublisher.TransactionId)); #endregion #region Generate registration for physician var keysPhysician = EncryptionHandler.GenerateNewKeys(); TransactionGenerator transactionGeneratorPhyisician = new TransactionGenerator(keysPhysician.PrivateKey); var registrationPhysician = transactionGeneratorPhyisician.InitializeAsNewPhysician(keysPhysician.PublicKey, "Austria", "Vienna", "Physician"); var votePhysician = transactionGeneratorPublisher.GenerateVotingTransaction(registrationPhysician.TransactionId, true); #endregion publisher.OnReceiveTransaction(registrationPhysician); Assert.IsFalse(publisher.participantHandler.HasPhysician(registrationPhysician.TransactionId)); publisher.OnReceiveTransaction(votePhysician); publisher.BundleOpenTransactionsManually(); publisher.PublishOpenBlocks(publisher); Assert.IsTrue(publisher.participantHandler.HasPhysician(registrationPhysician.TransactionId)); }
public void AddTransactionToBlock() { var keys = EncryptionHandler.GenerateNewKeys(); TransactionGenerator transactionGenerator = new TransactionGenerator(keys.PrivateKey); var registration = transactionGenerator.InitializeAsNewPublisher(keys.PublicKey, "Austria", "Vienna", "Master"); Block block = new Block(); block.Index = 0; block.Publisher = registration.TransactionId; block.AddTransaction(registration); Assert.IsTrue(block.TransactionList.Count == 1); block.AddTransaction(registration); Assert.IsTrue(block.TransactionList.Count == 2); block.Sign(keys.PrivateKey); Assert.IsTrue(block.ValidateBlockIntegrity(keys.PublicKey)); }
public void AddBlockToChain() { var keys = EncryptionHandler.GenerateNewKeys(); TransactionGenerator transactionGenerator = new TransactionGenerator(keys.PrivateKey); var registration = transactionGenerator.InitializeAsNewPublisher(keys.PublicKey, "Austria", "Vienna", "Master"); var vote = transactionGenerator.GenerateVotingTransaction(registration.TransactionId, true); Block block = new Block(); block.Index = 0; block.Publisher = registration.TransactionId; block.AddTransaction(registration); block.AddTransaction(vote); block.Sign(keys.PrivateKey); Chain chain = new Chain(); chain.Add(block); Assert.IsTrue(chain.ValidateContextual(new ParticipantHandler(), null)); }
internal void StartAsNewPublisher(string country, string region, string entityName, ServerAddress selfAddress, ServerAddress initializerAddress) { var keys = EncryptionHandler.GenerateNewKeys(); transactionGenerator = new TransactionGenerator(keys.PrivateKey); ITransaction registration = transactionGenerator.InitializeAsNewPublisher(keys.PublicKey, country, region, entityName); node.User = new UserProperties() { Username = currentUser , Keys = keys , UserAddress = registration.TransactionId }; serverSettings = selfAddress; node.SetSelfAddress(serverSettings); Start(); if (initializerAddress != null) { RegisterAt(initializerAddress); UpdateKnownNodes(); } if (!node.IsChainInitialized()) { ITransaction vote = transactionGenerator.GenerateVotingTransaction(registration.TransactionId, true); node.InitializeEmptyChain(registration, vote); } else { node.OnReceiveTransaction(registration); } }
public void MeasureTransactionProcessing() { #region Set up Publisher var keysPublisher = EncryptionHandler.GenerateNewKeys(); TransactionGenerator transactionGeneratorPublisher = new TransactionGenerator(keysPublisher.PrivateKey); var registrationPublisher = transactionGeneratorPublisher.InitializeAsNewPublisher(keysPublisher.PublicKey, "Austria", "Vienna", "Publisher"); var votePublisher = transactionGeneratorPublisher.GenerateVotingTransaction(registrationPublisher.TransactionId, true); PublisherNode publisher = new PublisherNode(); publisher.User = new NodeManagement.Entities.UserProperties() { Keys = keysPublisher, UserAddress = registrationPublisher.TransactionId, Username = "******" }; publisher.InitializeEmptyChain(registrationPublisher, votePublisher); Assert.IsTrue(publisher.participantHandler.HasPublisher(registrationPublisher.TransactionId)); #endregion var referencePhysicianKeys = EncryptionHandler.GenerateNewKeys(); var referencePhysicianGenerator = new TransactionGenerator(referencePhysicianKeys.PrivateKey); var referencePhysician = referencePhysicianGenerator.InitializeAsNewPhysician(referencePhysicianKeys.PrivateKey, "Austria", "Vienna", "Physician"); var referencePhysicianVote = transactionGeneratorPublisher.GenerateVotingTransaction(referencePhysician.TransactionId, true); var referencePatient = referencePhysicianGenerator.GeneratePatientRegistrationTransaction("Austria", "Vienna", "1990"); var referenceTreatment = referencePhysicianGenerator.GenerateTreatmentTransaction(referencePhysician.TransactionId, referencePatient.TransactionId); publisher.OnReceiveTransaction(referencePhysician); publisher.OnReceiveTransaction(referencePhysicianVote); publisher.OnReceiveTransaction(referencePatient); publisher.OnReceiveTransaction(referenceTreatment); publisher.BundleOpenTransactionsManually(); publisher.PublishOpenBlocks(publisher); Assert.AreEqual(1, publisher.GetChain().Blockhead.Index); Random random = new Random(); for (int i = 0; i < 50000; i++) { int choice = random.Next(1, 6); switch (choice) { case 1: var keysNewPhysician = EncryptionHandler.GenerateNewKeys(); TransactionGenerator transactionGeneratorNewPhysician = new TransactionGenerator(keysNewPhysician.PrivateKey); var transaction = transactionGeneratorNewPhysician.InitializeAsNewPhysician(keysNewPhysician.PublicKey, "Austria", "Vienna", "Physician"); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); publisher.OnReceiveTransaction(transaction); stopwatch.Stop(); string log = "PHYSICIAN\t" + (i % maxBlockSize) + "\t" + maxBlockSize + "\t" + publisher.GetChain().Blockhead.Index + "\t" + publisher.GetChain().GetTransactions().Count + "\t" + stopwatch.ElapsedMilliseconds + "\t" + GetRAMUsage() + "\n"; FileHandler.Append("D:\\DiagnosticChain\\DiagnosticChain\\TransactionLog.txt", log); break; case 2: transaction = referencePhysicianGenerator.GeneratePatientRegistrationTransaction("Austria", "Vienna", "Patient"); stopwatch = new Stopwatch(); stopwatch.Start(); publisher.OnReceiveTransaction(transaction); stopwatch.Stop(); log = "PATIENT\t" + (i % maxBlockSize) + "\t" + maxBlockSize + "\t" + publisher.GetChain().Blockhead.Index + "\t" + publisher.GetChain().GetTransactions().Count + "\t" + stopwatch.ElapsedMilliseconds + "\t" + GetRAMUsage() + "\n"; FileHandler.Append("D:\\DiagnosticChain\\DiagnosticChain\\TransactionLog.txt", log); break; case 3: var patients = (from x in publisher.GetChain().GetTransactions() where x.GetType() == typeof(PatientRegistrationTransaction) select x.TransactionId).ToList(); var p = patients.Count() > 1 ? patients[random.Next(1, patients.Count()) - 1] : referencePatient.TransactionId; transaction = referencePhysicianGenerator.GenerateTreatmentTransaction(referencePhysician.TransactionId, p); stopwatch = new Stopwatch(); stopwatch.Start(); publisher.OnReceiveTransaction(transaction); stopwatch.Stop(); log = "TREATMENT\t" + (i % maxBlockSize) + "\t" + maxBlockSize + "\t" + publisher.GetChain().Blockhead.Index + "\t" + publisher.GetChain().GetTransactions().Count + "\t" + stopwatch.ElapsedMilliseconds + "\t" + GetRAMUsage() + "\n"; FileHandler.Append("D:\\DiagnosticChain\\DiagnosticChain\\TransactionLog.txt", log); break; case 4: var treatments = (from x in publisher.GetChain().GetTransactions() where x.GetType() == typeof(TreatmentTransaction) select x.TransactionId).ToList(); var t = treatments.Count() > 1 ? treatments[random.Next(1, treatments.Count()) - 1] : referenceTreatment.TransactionId; transaction = referencePhysicianGenerator.GenerateSymptomTransaction(t, new List <string>() { "Symptom" }); stopwatch = new Stopwatch(); stopwatch.Start(); publisher.OnReceiveTransaction(transaction); stopwatch.Stop(); log = "SYMPTOM\t" + (i % maxBlockSize) + "\t" + maxBlockSize + "\t" + publisher.GetChain().Blockhead.Index + "\t" + publisher.GetChain().GetTransactions().Count + "\t" + stopwatch.ElapsedMilliseconds + "\t" + GetRAMUsage() + "\n"; FileHandler.Append("D:\\DiagnosticChain\\DiagnosticChain\\TransactionLog.txt", log); break; case 5: transaction = transactionGeneratorPublisher.GenerateVotingTransaction(referencePhysician.TransactionId, true); stopwatch = new Stopwatch(); stopwatch.Start(); publisher.OnReceiveTransaction(transaction); stopwatch.Stop(); log = "VOTING\t" + (i % maxBlockSize) + "\t" + maxBlockSize + "\t" + publisher.GetChain().Blockhead.Index + "\t" + publisher.GetChain().GetTransactions().Count + "\t" + stopwatch.ElapsedMilliseconds + "\t" + GetRAMUsage() + "\n"; FileHandler.Append("D:\\DiagnosticChain\\DiagnosticChain\\TransactionLog.txt", log); break; default: break; } publisher.participantHandler.EvaluateParkedTransactions(); if (i % maxBlockSize == 0) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); publisher.BundleOpenTransactionsManually(); publisher.PublishOpenBlocks(publisher); stopwatch.Stop(); string log = "BLOCK\t" + (i % maxBlockSize) + "\t" + maxBlockSize + "\t" + publisher.GetChain().Blockhead.Index + "\t" + publisher.GetChain().GetTransactions().Count + "\t" + stopwatch.ElapsedMilliseconds + "\n"; FileHandler.Append("D:\\DiagnosticChain\\DiagnosticChain\\BlockLog.txt", log); stopwatch = new Stopwatch(); stopwatch.Start(); publisher.ValidateChain(); stopwatch.Stop(); log = "VALIDATION\t" + (i % maxBlockSize) + "\t" + maxBlockSize + "\t" + publisher.GetChain().Blockhead.Index + "\t" + publisher.GetChain().GetTransactions().Count + "\t" + stopwatch.ElapsedMilliseconds + "\n"; FileHandler.Append("D:\\DiagnosticChain\\DiagnosticChain\\ValidationLog.txt", log); QueryNode query = new QueryNode(); query.UpdateChain(publisher.GetChain()); stopwatch = new Stopwatch(); stopwatch.Start(); query.ExtractData((PatientRegistrationTransaction p) => true, (TreatmentTransaction t) => true, (SymptomsTransaction s) => true, (DiagnosesTransaction d) => true); stopwatch.Stop(); log = "QUERY\t" + (i % maxBlockSize) + "\t" + maxBlockSize + "\t" + publisher.GetChain().Blockhead.Index + "\t" + publisher.GetChain().GetTransactions().Count + "\t" + stopwatch.ElapsedMilliseconds + "\n"; FileHandler.Append("D:\\DiagnosticChain\\DiagnosticChain\\QueryLog.txt", log); } } }
public void ProcessPatientData() { #region Set up Publisher var keysPublisher = EncryptionHandler.GenerateNewKeys(); TransactionGenerator transactionGeneratorPublisher = new TransactionGenerator(keysPublisher.PrivateKey); var registrationPublisher = transactionGeneratorPublisher.InitializeAsNewPublisher(keysPublisher.PublicKey, "Austria", "Vienna", "Publisher"); var votePublisher = transactionGeneratorPublisher.GenerateVotingTransaction(registrationPublisher.TransactionId, true); PublisherNode publisher = new PublisherNode(); publisher.User = new NodeManagement.Entities.UserProperties() { Keys = keysPublisher, UserAddress = registrationPublisher.TransactionId, Username = "******" }; publisher.InitializeEmptyChain(registrationPublisher, votePublisher); Assert.IsTrue(publisher.participantHandler.HasPublisher(registrationPublisher.TransactionId)); #endregion #region Register physician var keysPhysician = EncryptionHandler.GenerateNewKeys(); TransactionGenerator transactionGeneratorPhyisician = new TransactionGenerator(keysPhysician.PrivateKey); var registrationPhysician = transactionGeneratorPhyisician.InitializeAsNewPhysician(keysPhysician.PublicKey, "Austria", "Vienna", "Physician"); var votePhysician = transactionGeneratorPublisher.GenerateVotingTransaction(registrationPhysician.TransactionId, true); publisher.OnReceiveTransaction(registrationPhysician); publisher.OnReceiveTransaction(votePhysician); publisher.BundleOpenTransactionsManually(); publisher.PublishOpenBlocks(publisher); Assert.IsTrue(publisher.participantHandler.HasPhysician(registrationPhysician.TransactionId)); #endregion var patientA = transactionGeneratorPhyisician.GeneratePatientRegistrationTransaction("Austria", "Vienna", "1990"); publisher.OnReceiveTransaction(patientA); Assert.IsTrue(publisher.participantHandler.parkedPatients.Contains(patientA)); Assert.IsFalse(publisher.participantHandler.HasPatient(patientA.TransactionId)); publisher.BundleOpenTransactionsManually(); publisher.PublishOpenBlocks(publisher); Assert.IsTrue(publisher.participantHandler.parkedPatients.Contains(patientA)); Assert.IsFalse(publisher.participantHandler.HasPatient(patientA.TransactionId)); for (int i = 0; i < 2; i++) { publisher.OnReceiveTransaction( transactionGeneratorPhyisician.GeneratePatientRegistrationTransaction("Austria", "Vienna", "1990") ); } Assert.IsTrue(publisher.participantHandler.parkedPatients.Contains(patientA)); Assert.IsFalse(publisher.participantHandler.HasPatient(patientA.TransactionId)); publisher.EvaluateParkedTransactions(); publisher.BundleOpenTransactionsManually(); publisher.PublishOpenBlocks(publisher); Assert.IsFalse(publisher.participantHandler.parkedPatients.Contains(patientA)); Assert.IsTrue(publisher.participantHandler.HasPatient(patientA.TransactionId)); }
public void ConsolidateChains() { #region Set up Publisher A var keysA = EncryptionHandler.GenerateNewKeys(); TransactionGenerator transactionGeneratorA = new TransactionGenerator(keysA.PrivateKey); var registrationA = transactionGeneratorA.InitializeAsNewPublisher(keysA.PublicKey, "Austria", "Vienna", "PublisherA"); var voteA = transactionGeneratorA.GenerateVotingTransaction(registrationA.TransactionId, true); PublisherNode publisherA = new PublisherNode(); publisherA.User = new NodeManagement.Entities.UserProperties() { Keys = keysA, UserAddress = registrationA.TransactionId, Username = "******" }; publisherA.InitializeEmptyChain(registrationA, voteA); #endregion #region Set up Publisher B var keysB = EncryptionHandler.GenerateNewKeys(); TransactionGenerator transactionGeneratorB = new TransactionGenerator(keysB.PrivateKey); var registrationB = transactionGeneratorB.InitializeAsNewPublisher(keysB.PublicKey, "Austria", "Vienna", "PublisherB"); PublisherNode publisherB = new PublisherNode(); publisherB.User = new NodeManagement.Entities.UserProperties() { Keys = keysB, UserAddress = registrationB.TransactionId, Username = "******" }; publisherB.OnReceiveChain(publisherA.GetChain()); publisherB.OnReceiveTransaction(registrationB); publisherB.BundleOpenTransactionsManually(); publisherB.PublishOpenBlocks(publisherB); #endregion #region Authorize Publisher B publisherA.OnReceiveChain(publisherB.GetChainDelta(0)); Assert.AreEqual(publisherA.GetChain().AsXML(), publisherB.GetChain().AsXML()); var voteB = transactionGeneratorA.GenerateVotingTransaction(registrationB.TransactionId, true); publisherA.OnReceiveTransaction(voteB); publisherA.BundleOpenTransactionsManually(); publisherA.PublishOpenBlocks(publisherA); publisherB.OnReceiveChain(publisherA.GetChainDelta(1)); Assert.AreEqual(publisherA.GetChain().AsXML(), publisherB.GetChain().AsXML()); #endregion var transactionA = transactionGeneratorA.GenerateVotingTransaction(registrationB.TransactionId, true); var transactionB = transactionGeneratorB.GenerateVotingTransaction(registrationA.TransactionId, true); var transactionC = transactionGeneratorB.GenerateVotingTransaction(registrationA.TransactionId, true); #region Send transactions to publishers publisherA.OnReceiveTransaction(transactionA); publisherA.BundleOpenTransactionsManually(); publisherA.PublishOpenBlocks(publisherA); publisherB.OnReceiveTransaction(transactionB); publisherB.BundleOpenTransactionsManually(); publisherB.PublishOpenBlocks(publisherB); publisherB.OnReceiveTransaction(transactionC); publisherB.BundleOpenTransactionsManually(); publisherB.PublishOpenBlocks(publisherB); #endregion publisherA.OnReceiveChain(publisherB.GetChainDelta(2)); var hit = from t in publisherA.GetChain().GetTransactions() where t.TransactionId == transactionA.TransactionId select t; Assert.AreEqual(hit.Count(), 0); publisherA.BundleOpenTransactionsManually(); publisherA.PublishOpenBlocks(publisherA); hit = from t in publisherA.GetChain().GetTransactions() where t.TransactionId == transactionA.TransactionId select t; Assert.AreEqual(1, hit.Count()); }
public void RegistrateAndVotePublisher() { #region Set up Publisher A var keysA = EncryptionHandler.GenerateNewKeys(); TransactionGenerator transactionGeneratorA = new TransactionGenerator(keysA.PrivateKey); var registrationA = transactionGeneratorA.InitializeAsNewPublisher(keysA.PublicKey, "Austria", "Vienna", "PublisherA"); var voteA = transactionGeneratorA.GenerateVotingTransaction(registrationA.TransactionId, true); PublisherNode publisherA = new PublisherNode(); publisherA.User = new NodeManagement.Entities.UserProperties() { Keys = keysA, UserAddress = registrationA.TransactionId, Username = "******" }; publisherA.InitializeEmptyChain(registrationA, voteA); Assert.IsTrue(publisherA.participantHandler.HasPublisher(registrationA.TransactionId)); #endregion #region Set up Publisher B var keysB = EncryptionHandler.GenerateNewKeys(); TransactionGenerator transactionGeneratorB = new TransactionGenerator(keysB.PrivateKey); var registrationB = transactionGeneratorB.InitializeAsNewPublisher(keysB.PublicKey, "Austria", "Vienna", "PublisherB"); PublisherNode publisherB = new PublisherNode(); publisherB.User = new NodeManagement.Entities.UserProperties() { Keys = keysB, UserAddress = registrationB.TransactionId, Username = "******" }; publisherB.OnReceiveChain(publisherA.GetChain()); publisherB.OnReceiveTransaction(registrationB); publisherB.BundleOpenTransactionsManually(); publisherB.PublishOpenBlocks(publisherB); #endregion Assert.IsFalse(publisherA.participantHandler.HasPublisher(registrationB.TransactionId)); Assert.IsFalse(publisherB.participantHandler.HasPublisher(registrationB.TransactionId)); #region Authorize Publisher B publisherA.OnReceiveChain(publisherB.GetChainDelta(0)); Assert.AreEqual(publisherA.GetChain().AsXML(), publisherB.GetChain().AsXML()); var voteB = transactionGeneratorA.GenerateVotingTransaction(registrationB.TransactionId, true); publisherA.OnReceiveTransaction(voteB); publisherA.BundleOpenTransactionsManually(); publisherA.PublishOpenBlocks(publisherA); publisherB.OnReceiveChain(publisherA.GetChainDelta(1)); Assert.AreEqual(publisherA.GetChain().AsXML(), publisherB.GetChain().AsXML()); #endregion Assert.IsTrue(publisherA.participantHandler.HasPublisher(registrationB.TransactionId)); Assert.IsTrue(publisherB.participantHandler.HasPublisher(registrationB.TransactionId)); }