public bool CastVoteForPublisher(ParticipantHandler participantHandler, Guid publisherAddress, Guid senderAddress)
        {
            //If the publisher has already been confirmed, do nothing
            if (participantHandler.HasPublisher(publisherAddress))
            {
                return(true);
            }
            if (!(participantHandler.proposedPublishers.Where(p => p.Address == publisherAddress).Count() > 0))
            {
                return(false);
            }

            //If the publisher is still proposed
            //Add Vote to list of votes (if not already present from the current sender)
            var votesFromSender = from v in participantHandler.pendingVotes
                                  where v.VoteFor == publisherAddress && v.VoteFrom == senderAddress
                                  select v;

            if (votesFromSender.Count() > 0)
            {
                var hit = votesFromSender.FirstOrDefault();
                hit.Confirmed = true;
            }
            else
            {
                participantHandler.pendingVotes.Add(new Vote()
                {
                    VoteFor = publisherAddress
                    ,
                    VoteFrom = senderAddress
                    ,
                    Confirmed = true
                });
            }

            //Count list of valid votes for publisher => Valid votes must be more than half of currently confirmed publishers
            //If publisher is confirmed: Transfer to confirmed publishers, remove all votes from list
            var votesForPublisher = from v in participantHandler.pendingVotes
                                    where v.VoteFor == publisherAddress && v.Confirmed
                                    select v;

            if (votesForPublisher.Count() > Math.Floor(participantHandler.pendingVotes.Count() / 2.0))
            {
                var tranfer = participantHandler.proposedPublishers.Where(p => p.Address == publisherAddress);
                participantHandler.confirmedPublishers.AddRange(tranfer);
                participantHandler.proposedPublishers.RemoveAll(p => p.Address == publisherAddress);
                participantHandler.pendingVotes.RemoveAll(v => v.VoteFor == publisherAddress);
            }

            return(true);
        }
        public override bool ProcessContract(ParticipantHandler participantHandler, List <Chain> chains)
        {
            if (!ValidateContextual(participantHandler, chains))
            {
                return(false);
            }

            if (participantHandler.IsEmpty() && participantHandler.IsVotablePublisher(TransactionAddress)) //For the first publisher in the chain
            {
                if (Vote)
                {
                    return(participantHandler.CastVoteForPublisher(TransactionAddress, SenderAddress));
                }
                else
                {
                    participantHandler.CastVoteAgainstPublisher(TransactionAddress, SenderAddress);
                }
            }
            else if (participantHandler.IsVotablePublisher(TransactionAddress) && participantHandler.HasPublisher(SenderAddress))
            {
                if (Vote)
                {
                    return(participantHandler.CastVoteForPublisher(TransactionAddress, SenderAddress));
                }
                else
                {
                    participantHandler.CastVoteAgainstPublisher(TransactionAddress, SenderAddress);
                }
            }
            else if (participantHandler.IsVotablePhysician(TransactionAddress) && participantHandler.HasSender(SenderAddress))
            {
                if (Vote)
                {
                    participantHandler.CastVoteForPhysician(TransactionAddress, SenderAddress);
                }
                else
                {
                    participantHandler.CastVoteAgainstPhysician(TransactionAddress, SenderAddress);
                }
            }
            else
            {
                return(false);
            }

            return(true);
        }
        public bool CastVoteAgainstPublisher(ParticipantHandler participantHandler, Guid publisherAddress, Guid senderAddress)
        {
            //If the publisher is not confirmed, do nothing
            if (!participantHandler.HasPublisher(publisherAddress))
            {
                return(false);
            }

            //Add Vote to list of votes (if not already present from the current sender)
            var votesFromSender = from v in participantHandler.pendingVotes
                                  where v.VoteFor == publisherAddress && v.VoteFrom == senderAddress
                                  select v;

            if (votesFromSender.Count() > 0)
            {
                var hit = votesFromSender.FirstOrDefault();
                hit.Confirmed = false;
            }
            else
            {
                participantHandler.pendingVotes.Add(new Vote()
                {
                    VoteFor = publisherAddress
                    ,
                    VoteFrom = senderAddress
                    ,
                    Confirmed = false
                });
            }

            //Count list of valid votes against publisher => Valid votes must be more than half of currently confirmed publishers
            //If publisher is dismissed: Delete from List
            var votesAgainstPublisher = from v in participantHandler.pendingVotes
                                        where v.VoteFor == publisherAddress && !v.Confirmed
                                        select v;

            if (votesAgainstPublisher.Count() > Math.Floor(participantHandler.confirmedPublishers.Count() / 2.0))
            {
                participantHandler.confirmedPublishers.RemoveAll(p => p.Address == publisherAddress);
                participantHandler.pendingVotes.RemoveAll(v => v.VoteFor == publisherAddress);
            }

            return(true);
        }
 public override bool ValidateContextual(ParticipantHandler participantHandler, List <Chain> chains)
 {
     return(SenderAddress == TransactionId &&
            ValidateTransactionIntegrity(PublicKey) &&
            !participantHandler.HasPublisher(TransactionId));
 }
Exemplo n.º 5
0
        public bool ValidateContextual(ParticipantHandler participantHandler, List <Chain> context)
        {
            var blockIsValid = true;

            //Handle initializing block
            if (Index == 0)
            {
                blockIsValid &= TransactionList.Count() == 2;

                var registration = (from t in TransactionList
                                    where t.GetType() == typeof(PublisherRegistrationTransaction)
                                    select(PublisherRegistrationTransaction) t).FirstOrDefault();
                var vote = (from t in TransactionList
                            where t.GetType() == typeof(VotingTransaction)
                            select(VotingTransaction) t).FirstOrDefault();

                blockIsValid &= registration != null && vote != null;
                blockIsValid &= ValidateBlockIntegrity(registration.PublicKey);
                blockIsValid &= participantHandler.ProcessTransaction(registration, context);
                blockIsValid &= participantHandler.ProcessTransaction(vote, context);
            }
            //Handle new Publisher registration
            else if (TransactionList.Count() == 1 && TransactionList[0].GetType() == typeof(PublisherRegistrationTransaction))
            {
                var registration = (PublisherRegistrationTransaction)TransactionList[0];

                blockIsValid &= ValidateBlockIntegrity(registration.PublicKey);
                blockIsValid &= participantHandler.ProcessTransaction(registration, context);
            }
            //Handle regular block
            else if (participantHandler.HasPublisher(Publisher))
            {
                blockIsValid &= ValidateBlockIntegrity(participantHandler.GetPublisherKey(Publisher));

                foreach (var t in TransactionList)
                {
                    if (t.GetType() == typeof(PhysicianRegistrationTransaction))
                    {
                        PhysicianRegistrationTransaction temp = (PhysicianRegistrationTransaction)t;
                        blockIsValid &= temp.ValidateTransactionIntegrity(temp.PublicKey);
                        blockIsValid &= participantHandler.ProcessTransaction(temp, context);
                    }
                    else if (participantHandler.HasSender(t.SenderAddress))
                    {
                        blockIsValid &= t.ValidateTransactionIntegrity(participantHandler.GetSenderKey(t.SenderAddress));
                        blockIsValid &= participantHandler.ProcessTransaction(t, context);
                    }
                    else
                    {
                        blockIsValid = false;
                    }
                }
            }
            //Fallback - Block definitely invalid
            else
            {
                blockIsValid = false;
            }

            return(blockIsValid);
        }