// Checks that node can sync from scratch if federation voted in favor of adding a new fed member. public async Task CanSyncIfFedMemberAddedAsync() { List <IFederationMember> originalFedMembers = this.node1.FullNode.NodeService <IFederationManager>().GetFederationMembers(); TestHelper.Connect(this.node1, this.node2); IFederationMember federationMember = new FederationMember(new PubKey("03025fcadedd28b12665de0542c8096f4cd5af8e01791a4d057f67e2866ca66ba7")); byte[] fedMemberBytes = (this.poaNetwork.Consensus.ConsensusFactory as PoAConsensusFactory).SerializeFederationMember(federationMember); var votingData = new VotingData() { Key = VoteKey.AddFederationMember, Data = fedMemberBytes }; this.node1.FullNode.NodeService <VotingManager>().ScheduleVote(votingData); this.node2.FullNode.NodeService <VotingManager>().ScheduleVote(votingData); await this.node1.MineBlocksAsync(1); CoreNodePoAExtensions.WaitTillSynced(this.node1, this.node2); await this.node2.MineBlocksAsync((int)this.poaNetwork.Consensus.MaxReorgLength * 3); CoreNodePoAExtensions.WaitTillSynced(this.node1, this.node2); List <IFederationMember> newFedMembers = this.node1.FullNode.NodeService <IFederationManager>().GetFederationMembers(); Assert.Equal(originalFedMembers.Count + 1, newFedMembers.Count); TestHelper.Connect(this.node2, this.node3); CoreNodePoAExtensions.WaitTillSynced(this.node1, this.node2, this.node3); }
// Checks that node can sync from scratch if federation voted in favor of kicking a fed member. public async Task CanSyncIfFedMemberKickedAsync() { int originalFedMembersCount = this.node1.FullNode.NodeService <IFederationManager>().GetFederationMembers().Count; TestHelper.Connect(this.node1, this.node2); byte[] fedMemberBytes = (this.poaNetwork.Consensus.ConsensusFactory as PoAConsensusFactory).SerializeFederationMember(new FederationMember(this.poaNetwork.FederationKey2.PubKey)); var votingData = new VotingData() { Key = VoteKey.KickFederationMember, Data = fedMemberBytes }; this.node1.FullNode.NodeService <VotingManager>().ScheduleVote(votingData); this.node2.FullNode.NodeService <VotingManager>().ScheduleVote(votingData); await this.node2.MineBlocksAsync(1); CoreNodePoAExtensions.WaitTillSynced(this.node1, this.node2); await this.node1.MineBlocksAsync((int)this.poaNetwork.Consensus.MaxReorgLength * 3); CoreNodePoAExtensions.WaitTillSynced(this.node1, this.node2); Assert.Equal(originalFedMembersCount - 1, this.node1.FullNode.NodeService <IFederationManager>().GetFederationMembers().Count); TestHelper.Connect(this.node2, this.node3); CoreNodePoAExtensions.WaitTillSynced(this.node1, this.node2, this.node3); }
private ChainedHeaderBlock[] GetBlocksWithVotingData(int count, VotingData votingData, ChainedHeader previous) { return(PoaTestHelper.GetBlocks(count, this.ChainIndexer, i => this.CreateBlockWithVotingData(new List <VotingData>() { votingData }, i + 1), previous)); }
public void CanExpireAndUnExpirePollViaNodeRewind() { // Create add federation member vote. var votingData = new VotingData() { Key = VoteKey.AddFederationMember, Data = new Key().PubKey.ToBytes() }; // Create a single pending poll. ChainedHeaderBlock[] blocks = GetBlocksWithVotingData(1, votingData, new ChainedHeader(this.network.GetGenesis().Header, this.network.GetGenesis().GetHash(), 0)); this.TriggerOnBlockConnected(blocks[0]); Assert.Single(this.votingManager.GetPendingPolls()); // Advance the chain so that the poll expires. blocks = PoaTestHelper.GetEmptyBlocks(this.ChainIndexer, this.network, 10); for (int i = 0; i < blocks.Length; i++) { this.TriggerOnBlockConnected(blocks[i]); } // Assert that the poll expired. Assert.Single(this.votingManager.GetExpiredPolls()); // Fake a rewind via setting the node's tip back (this will generally happen via the api/node/rewind call) this.ChainIndexer.Remove(this.ChainIndexer.Tip); // Re-initialize the voting manager this.votingManager.Initialize(this.federationHistory); // Assert that the poll was "un-expired". Assert.Single(this.votingManager.GetPendingPolls()); }
public void CanExpireAndUnExpirePollViaBlockDisconnected() { // Create add federation member vote. var votingData = new VotingData() { Key = VoteKey.AddFederationMember, Data = new Key().PubKey.ToBytes() }; // Create a single pending poll. ChainedHeaderBlock[] blocks = GetBlocksWithVotingData(1, votingData, new ChainedHeader(this.network.GetGenesis().Header, this.network.GetGenesis().GetHash(), 0)); this.TriggerOnBlockConnected(blocks[0]); Assert.Single(this.votingManager.GetPendingPolls()); // Advance the chain so that the poll expires. blocks = PoaTestHelper.GetEmptyBlocks(this.ChainIndexer, this.network, 10); for (int i = 0; i < blocks.Length; i++) { this.TriggerOnBlockConnected(blocks[i]); } // Assert that the poll expired. Assert.Single(this.votingManager.GetExpiredPolls()); // Fake a rewind via block disconnected (this will generally happen via a re-org) this.TriggerOnBlockDisconnected(blocks[9]); // Assert that the poll was "un-expired". Assert.Single(this.votingManager.GetPendingPolls()); }
public void CanEncodeAndDecodeLargeAmountsOfData() { var dataList = new List <VotingData>(); for (int i = 0; i < 2000; i++) { var tempData = new VotingData() { Key = VoteKey.AddFederationMember, Data = RandomUtils.GetBytes(25) }; string data = Encoding.ASCII.GetString(tempData.Data); if (data.Contains("\r\n")) { data = data.Replace("\r\n", "\n"); tempData.Data = Encoding.ASCII.GetBytes(data); } dataList.Add(tempData); } byte[] encodedBytes = this.encoder.Encode(dataList); List <VotingData> decoded = this.encoder.Decode(encodedBytes); byte[] encodedBytes2 = this.encoder.Encode(decoded); Assert.True(encodedBytes.SequenceEqual(encodedBytes2)); }
private ChainedHeaderBlock[] GetBlocksWithVotingData(int count, VotingData votingData) { return(GetBlocks(count, i => this.CreateBlockWithVotingData(new List <VotingData>() { votingData }, i + 1))); }
// Checks that fed members cant vote twice. // Checks that miner adds voting data if it exists. public async Task CantVoteTwiceAsync() { int originalFedMembersCount = this.node1.FullNode.NodeService <IFederationManager>().GetFederationMembers().Count; TestHelper.Connect(this.node1, this.node2); await this.node1.MineBlocksAsync(3); IFederationMember federationMember = new FederationMember(new PubKey("03025fcadedd28b12665de0542c8096f4cd5af8e01791a4d057f67e2866ca66ba7")); byte[] fedMemberBytes = (this.poaNetwork.Consensus.ConsensusFactory as PoAConsensusFactory).SerializeFederationMember(federationMember); var votingData = new VotingData() { Key = VoteKey.AddFederationMember, Data = fedMemberBytes }; this.node1.FullNode.NodeService <VotingManager>().ScheduleVote(votingData); Assert.Single(this.node1.FullNode.NodeService <VotingManager>().GetScheduledVotes()); Assert.Empty(this.node1.FullNode.NodeService <VotingManager>().GetPendingPolls()); await this.node1.MineBlocksAsync(1); CoreNodePoAExtensions.WaitTillSynced(this.node1, this.node2); Assert.Empty(this.node1.FullNode.NodeService <VotingManager>().GetScheduledVotes()); Assert.Single(this.node1.FullNode.NodeService <VotingManager>().GetPendingPolls()); // Vote 2nd time and make sure nothing changed. this.node1.FullNode.NodeService <VotingManager>().ScheduleVote(votingData); await this.node1.MineBlocksAsync(1); Assert.Empty(this.node1.FullNode.NodeService <VotingManager>().GetScheduledVotes()); Assert.Single(this.node1.FullNode.NodeService <VotingManager>().GetPendingPolls()); CoreNodePoAExtensions.WaitTillSynced(this.node1, this.node2); // Node 2 votes. After that it will be enough to change the federation. this.node2.FullNode.NodeService <VotingManager>().ScheduleVote(votingData); await this.node2.MineBlocksAsync((int)this.poaNetwork.Consensus.MaxReorgLength + 1); CoreNodePoAExtensions.WaitTillSynced(this.node1, this.node2); Assert.Equal(originalFedMembersCount + 1, this.node1.FullNode.NodeService <IFederationManager>().GetFederationMembers().Count); Assert.Equal(originalFedMembersCount + 1, this.node2.FullNode.NodeService <IFederationManager>().GetFederationMembers().Count); TestHelper.Connect(this.node2, this.node3); CoreNodePoAExtensions.WaitTillSynced(this.node1, this.node2, this.node3); }
private CollateralFederationMember GetMember(VotingData votingData) { if (!(this.network.Consensus.ConsensusFactory is CollateralPoAConsensusFactory collateralPoAConsensusFactory)) { return(null); } if (!(collateralPoAConsensusFactory.DeserializeFederationMember(votingData.Data) is CollateralFederationMember collateralFederationMember)) { return(null); } return(collateralFederationMember); }
private async Task VoteAndMineBlockAsync(PubKey key, bool add, CoreNode node) { byte[] fedMemberBytes = (this.poaNetwork.Consensus.ConsensusFactory as PoAConsensusFactory).SerializeFederationMember(new FederationMember(key)); var votingData = new VotingData() { Key = add ? VoteKey.AddFederationMember : VoteKey.KickFederationMember, Data = fedMemberBytes }; node.FullNode.NodeService <VotingManager>().ScheduleVote(votingData); await node.MineBlocksAsync(1); CoreNodePoAExtensions.WaitTillSynced(this.node1, this.node2, this.node3); }
public void CanVote() { var votingData = new VotingData() { Key = VoteKey.AddFederationMember, Data = (new Key()).PubKey.ToBytes() }; int votesRequired = (this.federationManager.GetFederationMembers().Count / 2) + 1; ChainedHeaderBlock[] blocks = GetBlocksWithVotingData(votesRequired, votingData, new ChainedHeader(this.network.GetGenesis().Header, this.network.GetGenesis().GetHash(), 0)); for (int i = 0; i < votesRequired; i++) { this.TriggerOnBlockConnected(blocks[i]); } Assert.Single(this.votingManager.GetApprovedPolls()); }
public void AddVoteAfterPollComplete() { //TODO: When/if we remove duplicate polls, this test will need to be changed to account for the new expected functionality. var votingData = new VotingData() { Key = VoteKey.AddFederationMember, Data = (new Key()).PubKey.ToBytes() }; int votesRequired = (this.federationManager.GetFederationMembers().Count / 2) + 1; for (int i = 0; i < votesRequired; i++) { this.TriggerOnBlockConnected(this.CreateBlockWithVotingData(new List <VotingData>() { votingData }, i + 1)); } Assert.Single(this.votingManager.GetApprovedPolls()); Assert.Empty(this.votingManager.GetPendingPolls()); // Now that poll is complete, add another vote for it. ChainedHeaderBlock blockToDisconnect = this.CreateBlockWithVotingData(new List <VotingData>() { votingData }, votesRequired + 1); this.TriggerOnBlockConnected(blockToDisconnect); // Now we have 1 finished and 1 pending for the same data. Assert.Single(this.votingManager.GetApprovedPolls()); Assert.Single(this.votingManager.GetPendingPolls()); // This previously caused an error because of Single() being used. this.TriggerOnBlockDisconnected(blockToDisconnect); // VotingManager cleverly removed the pending poll but kept the finished poll. Assert.Single(this.votingManager.GetApprovedPolls()); Assert.Empty(this.votingManager.GetPendingPolls()); }
public void CanVote() { var votingData = new VotingData() { Key = VoteKey.AddFederationMember, Data = (new Key()).PubKey.ToBytes() }; int votesRequired = (this.federationManager.GetFederationMembers().Count / 2) + 1; for (int i = 0; i < votesRequired; i++) { this.TriggerOnBlockConnected(this.CreateBlockWithVotingData(new List <VotingData>() { votingData }, i + 1)); } Assert.Single(this.votingManager.GetApprovedPolls()); }
public async Task ReorgRevertsAppliedChangesAsync() { TestHelper.Connect(this.node1, this.node2); byte[] fedMemberBytes = (this.poaNetwork.Consensus.ConsensusFactory as PoAConsensusFactory).SerializeFederationMember(new FederationMember(this.testPubKey)); var votingData = new VotingData() { Key = VoteKey.AddFederationMember, Data = fedMemberBytes }; this.node1.FullNode.NodeService <VotingManager>().ScheduleVote(votingData); votingData = new VotingData() { Key = VoteKey.KickFederationMember, Data = fedMemberBytes }; this.node1.FullNode.NodeService <VotingManager>().ScheduleVote(votingData); await this.node1.MineBlocksAsync(1); CoreNodePoAExtensions.WaitTillSynced(this.node1, this.node2); votingData = new VotingData() { Key = VoteKey.AddFederationMember, Data = fedMemberBytes }; this.node2.FullNode.NodeService <VotingManager>().ScheduleVote(votingData); await this.node2.MineBlocksAsync(1); CoreNodePoAExtensions.WaitTillSynced(this.node1, this.node2); Assert.Single(this.node2.FullNode.NodeService <VotingManager>().GetPendingPolls()); Assert.Single(this.node2.FullNode.NodeService <VotingManager>().GetApprovedPolls()); await this.node3.MineBlocksAsync(4); TestHelper.Connect(this.node2, this.node3); CoreNodePoAExtensions.WaitTillSynced(this.node1, this.node2, this.node3); Assert.Empty(this.node2.FullNode.NodeService <VotingManager>().GetPendingPolls()); Assert.Empty(this.node2.FullNode.NodeService <VotingManager>().GetApprovedPolls()); }
public void CanVote() { var votingData = new VotingData() { Key = VoteKey.AddFederationMember, Data = RandomUtils.GetBytes(20) }; int votesRequired = (this.federationManager.GetFederationMembers().Count / 2) + 1; for (int i = 0; i < votesRequired; i++) { Assert.Empty(this.changesApplied); this.TriggerOnBlockConnected(this.CreateBlockWithVotingData(new List <VotingData>() { votingData }, i + 1)); } Assert.Single(this.changesApplied); }
public ActionResult VotingData(VotingData VotingData, string CompanyName) { if (CompanyName == "ignore") { ViewBag.CompanyNames = PopulateCompanyName(); return(View()); } string userid = User.Identity.GetUserId(); VotingData.UserProfileId = userid; if (ModelState.IsValid) { var check = (from list in db.VotingDatas where list.CompanyName == CompanyName && list.UserProfileId == userid select list).FirstOrDefault(); if (check == null) { db.VotingDatas.Add(VotingData); } else { check.Clarity = VotingData.Clarity; check.Content = VotingData.Content; check.Delivery = VotingData.Delivery; check.Comment = VotingData.Comment; } db.SaveChanges(); //return PartialView("_RefreshedCompany"); ViewBag.CompanyNames = PopulateCompanyName(); return(View("Confirmation")); } ViewBag.CompanyNames = PopulateCompanyName(); return(View()); //return View(); }