Esempio n. 1
0
 public InitMessage(Key32 id, JoinSecret secret, byte maxSize, LevelChangeMessage level, PlayerJoinMessage[] players)
 {
     ID      = id;
     Secret  = secret;
     MaxSize = maxSize;
     Level   = level;
     Players = players;
 }
Esempio n. 2
0
        private void ConnectLocal(IPEndPoint endPoint, JoinSecret secret, Key32 hostKey)
        {
            Connect(endPoint, hostKey, secret, info =>
            {
                _clientLog.LogError("Disconnected from local server. Something probably caused the frame to hang for more than 5s (debugging breakpoint?). Restarting host...");

                StartCoroutine(_Host());
            });
        }
        public static Key32 GetKey32(this NetDataReader @this)
        {
            var data = new byte[Key32.SIZE];

            @this.GetBytes(data, Key32.SIZE);

            if (!Key32.TryFromBytes(data, out var value))
            {
                throw new FormatException(nameof(Key32.TryFromBytes) + " returned false (should never happen; data buffer is fixed size).");
            }

            return(value);
        }
Esempio n. 4
0
        internal H3Server(ManualLogSource log, HostConfig config, RandomNumberGenerator rng, PeerMessageList <H3Server> messages, byte channelsCount, Version version, double tickDeltaTime, IPEndPoint publicEndPoint)
            : base(log, messages, channelsCount, new Events(messages.Definitions[typeof(Timestamped <PingMessage>)]), version, config.Binding.IPv4.Value, config.Binding.IPv6.Value, config.Binding.Port.Value)
        {
            _log     = log;
            _config  = config;
            _partyID = Key32.FromRandom(rng);

            _tickTimer = new LoopTimer(tickDeltaTime);
            _peerIDs   = new Dictionary <Peer, byte>();
            _husks     = new Dictionary <byte, Husk>();

            _selfID = -1;

            Secret  = new JoinSecret(version, publicEndPoint, Key32.FromRandom(rng), tickDeltaTime);
            HostKey = Key32.FromRandom(rng);
        }
 public static void Put(this NetDataWriter @this, Key32 value)
 {
     @this.Put(value.Data);
 }
 public ConnectionRequestMessage(Key32 accessKey, Key32?hostKey = null)
 {
     AccessKey  = accessKey;
     HostKey    = hostKey;
     ClientTime = LocalTime.Now;
 }
Esempio n. 7
0
        public void GetMostConfidentFullBlockTest()
        {
            List <RegistryFullBlock>  registryFullBlocks    = new List <RegistryFullBlock>();
            List <RegistryShortBlock> registryShortBlocks   = new List <RegistryShortBlock>();
            Dictionary <IKey, int>    votesPerShortBlockKey = new Dictionary <IKey, int>();

            int   fullBlockCount  = 10;
            int   votersCount     = 100;
            ulong syncBlockHeight = 1;
            ulong blockHeight     = 12;
            uint  nonce           = 0;

            byte[]           powHash = BinaryBuilder.GetPowHash(1234);
            IHashCalculation hashCalculationTransactionKey = new MurMurHashCalculation();

            IHashCalculation              hashCalculationDefault             = new Keccak256HashCalculation();
            IHashCalculation              hashCalculationMurMur              = new MurMurHashCalculation();
            ISerializersFactory           signatureSupportSerializersFactory = Substitute.For <ISerializersFactory>();
            IHashCalculationsRepository   hashCalculationsRepository         = Substitute.For <IHashCalculationsRepository>();
            IIdentityKeyProvider          identityKeyProviderTransactionKey  = Substitute.For <IIdentityKeyProvider>();
            IIdentityKeyProvidersRegistry identityKeyProvidersRegistry       = Substitute.For <IIdentityKeyProvidersRegistry>();
            ICryptoService          cryptoService          = GetRandomCryptoService();
            ILoggerService          loggerService          = Substitute.For <ILoggerService>();
            IStatesRepository       statesRepository       = Substitute.For <IStatesRepository>();
            ISynchronizationContext synchronizationContext = new Wist.Core.Synchronization.SynchronizationContext(loggerService);

            statesRepository.GetInstance <ISynchronizationContext>().ReturnsForAnyArgs(synchronizationContext);

            identityKeyProviderTransactionKey.GetKey(null).ReturnsForAnyArgs(c => new Key16(c.ArgAt <Memory <byte> >(0)));

            identityKeyProvidersRegistry.GetInstance("DefaultHash").Returns(new DefaultHashKeyProvider());
            identityKeyProvidersRegistry.GetTransactionsIdenityKeyProvider().Returns(identityKeyProviderTransactionKey);

            hashCalculationsRepository.Create(HashType.Keccak256).Returns(hashCalculationDefault);
            hashCalculationsRepository.Create(HashType.MurMur).Returns(hashCalculationMurMur);

            signatureSupportSerializersFactory.Create(null).ReturnsForAnyArgs(c =>
            {
                RegistryShortBlockSerializer registryShortBlockSerializer = new RegistryShortBlockSerializer(cryptoService, identityKeyProvidersRegistry, hashCalculationsRepository);
                registryShortBlockSerializer.Initialize(c.Arg <SignedBlockBase>());
                return(registryShortBlockSerializer);
            });

            SyncRegistryMemPool syncRegistryMemPool = new SyncRegistryMemPool(signatureSupportSerializersFactory, hashCalculationsRepository, identityKeyProvidersRegistry, cryptoService, statesRepository, loggerService);

            for (int i = 0; i < fullBlockCount; i++)
            {
                ICryptoService cryptoService1 = GetRandomCryptoService();
                ushort         expectedCount  = 1000;

                SortedList <ushort, ITransactionRegistryBlock> transactionHeaders = GetTransactionHeaders(syncBlockHeight, blockHeight, nonce, expectedCount);
                SortedList <ushort, IKey> transactionHeaderKeys = GetTransactionHeaderKeys(hashCalculationTransactionKey, transactionHeaders);

                RegistryShortBlock registryShortBlock = new RegistryShortBlock
                {
                    SyncBlockHeight         = syncBlockHeight,
                    BlockHeight             = blockHeight,
                    Nonce                   = nonce,
                    PowHash                 = powHash,
                    TransactionHeaderHashes = transactionHeaderKeys
                };

                RegistryShortBlockSerializer registryShortBlockSerializer = new RegistryShortBlockSerializer(cryptoService1, identityKeyProvidersRegistry, hashCalculationsRepository);
                registryShortBlockSerializer.Initialize(registryShortBlock);
                registryShortBlockSerializer.FillBodyAndRowBytes();

                RegistryFullBlock registryFullBlock = new RegistryFullBlock
                {
                    SyncBlockHeight    = syncBlockHeight,
                    BlockHeight        = blockHeight,
                    Nonce              = nonce,
                    PowHash            = powHash,
                    TransactionHeaders = transactionHeaders,
                    ShortBlockHash     = hashCalculationDefault.CalculateHash(registryShortBlock.RawData)
                };

                RegistryFullBlockSerializer serializer = new RegistryFullBlockSerializer(cryptoService1, identityKeyProvidersRegistry, hashCalculationsRepository);
                serializer.Initialize(registryFullBlock);
                serializer.FillBodyAndRowBytes();

                registryFullBlocks.Add(registryFullBlock);
                registryShortBlocks.Add(registryShortBlock);
            }

            foreach (RegistryFullBlock fullBlock in registryFullBlocks)
            {
                syncRegistryMemPool.AddCandidateBlock(fullBlock);
            }

            Random random = new Random();

            for (int i = 0; i < votersCount; i++)
            {
                ICryptoService cryptoService2 = GetRandomCryptoService();

                foreach (var registryShortBlock in registryShortBlocks)
                {
                    byte[] hashBytes     = hashCalculationDefault.CalculateHash(registryShortBlock.RawData);
                    Random randNum       = new Random();
                    byte[] bitMask       = Enumerable.Repeat(0, registryShortBlock.TransactionHeaderHashes.Count).Select(j => (byte)randNum.Next(0, 255)).ToArray();
                    byte[] expectedProof = Enumerable.Repeat(0, 16).Select(j => (byte)randNum.Next(0, 255)).ToArray();
                    IKey   shortBlockKey = new Key32(hashBytes);
                    long   vote          = GetConfidence(bitMask);
                    if (!votesPerShortBlockKey.ContainsKey(shortBlockKey))
                    {
                        votesPerShortBlockKey.Add(shortBlockKey, (ushort)vote);
                    }
                    else
                    {
                        votesPerShortBlockKey[shortBlockKey] += (ushort)vote;
                    }

                    RegistryConfidenceBlock registryConfidenceBlock = new RegistryConfidenceBlock
                    {
                        SyncBlockHeight     = syncBlockHeight,
                        BlockHeight         = blockHeight,
                        Nonce               = nonce,
                        PowHash             = powHash,
                        ReferencedBlockHash = hashBytes,
                        BitMask             = bitMask,
                        ConfidenceProof     = expectedProof
                    };

                    RegistryConfidenceBlockSerializer registryConfidenceBlockSerializer = new RegistryConfidenceBlockSerializer(cryptoService2, identityKeyProvidersRegistry, hashCalculationsRepository);
                    registryConfidenceBlockSerializer.Initialize(registryConfidenceBlock);
                    registryConfidenceBlockSerializer.FillBodyAndRowBytes();

                    syncRegistryMemPool.AddVotingBlock(registryConfidenceBlock);
                }
            }

            IKey expectedMostConfidentKey = votesPerShortBlockKey.OrderByDescending(kv => kv.Value).Select(kv => kv.Key).First();

            RegistryFullBlock actualFullBlock = syncRegistryMemPool.GetMostConfidentFullBlock(blockHeight);
            IKey actualMostConfidentKey       = new Key32(actualFullBlock.ShortBlockHash);

            Assert.Equal(expectedMostConfidentKey, actualMostConfidentKey);
        }