Exemple #1
0
        public static void Discover(ReplicaBase parentReplica, IEnumerable <ReplicaBase> secondaryReplicas)
        {
            if (parentReplica == null || secondaryReplicas.Any() == false)
            {
                return;
            }

            var leftReplicas  = secondaryReplicas.Where(r => r.Id != parentReplica.Id);
            var rightReplicas = leftReplicas.Skip(1);
            var restReplicas  = rightReplicas.Skip(1);

            if (leftReplicas.Any())
            {
                var leftReplica = leftReplicas.FirstOrDefault();

                leftReplica.ParentReplica = parentReplica;

                parentReplica.ChildReplicas.Add(leftReplica);

                Discover(leftReplica, restReplicas.Take(restReplicas.Count() / 2));
            }

            if (rightReplicas.Any())
            {
                var rightReplica = rightReplicas.FirstOrDefault();

                rightReplica.ParentReplica = parentReplica;

                parentReplica.ChildReplicas.Add(rightReplica);

                Discover(rightReplica, restReplicas.Skip(restReplicas.Count() / 2));
            }
        }
Exemple #2
0
 private static void GetReplicaTree(ReplicaBase replica, ICollection <int> replicaIds)
 {
     foreach (var childReplica in replica.ChildReplicas)
     {
         replicaIds.Add(childReplica.Id);
         GetReplicaTree(childReplica, replicaIds);
     }
 }
Exemple #3
0
        public static IEnumerable <int> GetReplicaTree(ReplicaBase replica)
        {
            var replicaIds = new List <int>();

            foreach (var childReplica in replica.ChildReplicas)
            {
                replicaIds.Add(childReplica.Id);
                GetReplicaTree(childReplica, replicaIds);
            }

            return(replicaIds);
        }
Exemple #4
0
        private void ShareSecretAmongReplicas(
            ReplicaBase replica,
            IReadOnlyDictionary <int, string> secretShares,
            uint counter,
            uint secretHash,
            IDictionary <int, byte[]> encryptedReplicaSecrets)
        {
            var childrenSecretHashes = replica.ChildReplicas
                                       .OrderBy(chr => chr.Id)
                                       .ToDictionary(
                chr => chr.Id,
                chr =>
            {
                var childSecretShares = new[] { secretShares[chr.Id] }
                .Concat(chr.ChildReplicas.Select(chr2 => secretShares[chr2.Id]))
                .ToArray();

                return(Crypto.GetHash(string.Join(string.Empty, childSecretShares)));
            });

            byte[] buffer;
            var    secretShare = secretShares[replica.Id];

            using (var memory = new MemoryStream())
                using (var writer = new BinaryWriter(memory))
                {
                    writer.Write(secretShare);
                    writer.Write(counter);
                    writer.Write(ViewNumber);

                    writer.Write(childrenSecretHashes.Count);
                    foreach (var childrenSecretHash in childrenSecretHashes)
                    {
                        writer.Write(childrenSecretHash.Key);
                        writer.Write(childrenSecretHash.Value);
                    }

                    writer.Write(secretHash);

                    buffer = memory.ToArray();
                }

            encryptedReplicaSecrets.Add(replica.Id, Crypto.EncryptAuth(replicaViewKeys[replica.Id], buffer));
        }
Exemple #5
0
 public static void Log(ReplicaBase replica, string message, params object[] args)
 {
     Console.WriteLine($"Replica #{replica.Id}: {string.Format(message, args)}");
 }