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());
        }
        private ChainedHeaderBlock CreateBlockWithVotingData(List <VotingData> data, int height)
        {
            var votingData = new List <byte>(VotingDataEncoder.VotingOutputPrefixBytes);

            votingData.AddRange(this.encoder.Encode(data));

            var votingOutputScript = new Script(OpcodeType.OP_RETURN, Op.GetPushOp(votingData.ToArray()));

            Transaction tx = this.network.CreateTransaction();

            tx.AddOutput(Money.COIN, votingOutputScript);

            Block block = PoaTestHelper.CreateBlock(this.network, tx, height);

            return(new ChainedHeaderBlock(block, new ChainedHeader(block.Header, block.GetHash(), height)));
        }
        private ChainedHeaderBlock CreateBlockWithVotingRequest(JoinFederationRequest votingRequest, int height)
        {
            var encoder = new JoinFederationRequestEncoder();

            var votingRequestData = new List <byte>();

            votingRequestData.AddRange(encoder.Encode(votingRequest));

            var votingRequestOutputScript = new Script(OpcodeType.OP_RETURN, Op.GetPushOp(votingRequestData.ToArray()));

            Transaction tx = this.network.CreateTransaction();

            tx.AddOutput(Money.COIN, votingRequestOutputScript);

            Block block = PoaTestHelper.CreateBlock(this.network, tx, height);

            return(new ChainedHeaderBlock(block, new ChainedHeader(block.Header, block.GetHash(), height)));
        }
 private ChainedHeaderBlock[] GetBlocksWithVotingRequest(int count, JoinFederationRequest votingRequest, ChainedHeader previous)
 {
     return(PoaTestHelper.GetBlocks(count, this.ChainIndexer, i => this.CreateBlockWithVotingRequest(votingRequest, i + 1), previous));
 }