Esempio n. 1
0
        public static ChainValidationResults BallotChainIsValid(List <Ballot> ballots)
        {
            var results          = new ChainValidationResults();
            var eoc              = false;
            var processedBallots = 0;

            byte[] nextBallotHash = Ballot.EmptyHash;
            while (eoc == false)
            {
                if (processedBallots == ballots.Count)
                {
                    eoc = true;
                    break;
                }
                else
                {
                    processedBallots++;
                }
                Ballot ballot = null;
                try
                {
                    ballot = ballots.Single(x => x.PreviousBallotHash.SequenceEqual(nextBallotHash));
                }
                catch (Exception ex)
                {
                    results.Messages.Add(ex.Message + " for ballot " + ballot.Id.ToString());
                }
                if (!ballot.BallotIsValid())
                {
                    results.Messages.Add("Ballot data has been tampered with " + ballot.Id.ToString());
                }
                if (!ballot.ballotHasValidHash())
                {
                    results.Messages.Add("Ballot chain is broken " + ballot.Id.ToString());
                }
                if (!ballot.PreviousBallotHash.SequenceEqual(Ballot.EmptyHash) && ballots.Count(x => x.BallotHash == ballot.PreviousBallotHash) == 0)
                {
                    results.Messages.Add("Ballot chain is broken " + ballot.Id.ToString());
                }
                nextBallotHash = ballot.BallotHash;
            }
            return(results);
        }
Esempio n. 2
0
 public static ChainValidationResults BallotChainIsValid(List<Ballot> ballots)
 {
     var results = new ChainValidationResults();
     var eoc = false;
     var processedBallots = 0;
     byte[] nextBallotHash = Ballot.EmptyHash;
     while(eoc == false) {
         if(processedBallots == ballots.Count)
         {
             eoc = true;
             break;
         }
         else
         {
             processedBallots++;
         }
         Ballot ballot = null;
         try
         {
             ballot = ballots.Single(x => x.PreviousBallotHash.SequenceEqual(nextBallotHash));
         }
         catch(Exception ex)
         {
             results.Messages.Add(ex.Message + " for ballot " + ballot.Id.ToString());
         }
         if (!ballot.BallotIsValid())
         {
             results.Messages.Add("Ballot data has been tampered with " + ballot.Id.ToString());
         }
         if (!ballot.ballotHasValidHash())
         {
             results.Messages.Add("Ballot chain is broken " + ballot.Id.ToString());
         }
         if(!ballot.PreviousBallotHash.SequenceEqual(Ballot.EmptyHash) && ballots.Count(x=> x.BallotHash == ballot.PreviousBallotHash) == 0)
         {
             results.Messages.Add("Ballot chain is broken " + ballot.Id.ToString());
         }
         nextBallotHash = ballot.BallotHash;
     }
     return results;
 }