Beispiel #1
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 = BinaryHelper.GetPowHash(1234);
            IHashCalculation hashCalculationTransactionKey = new MurMurHashCalculation();

            IHashCalculation              hashCalculationDefault            = new Keccak256HashCalculation();
            IHashCalculation              hashCalculationMurMur             = new MurMurHashCalculation();
            ISerializersFactory           serializersFactory                = Substitute.For <ISerializersFactory>();
            IHashCalculationsRepository   hashCalculationsRepository        = Substitute.For <IHashCalculationsRepository>();
            IIdentityKeyProvider          identityKeyProviderTransactionKey = Substitute.For <IIdentityKeyProvider>();
            IIdentityKeyProvidersRegistry identityKeyProvidersRegistry      = Substitute.For <IIdentityKeyProvidersRegistry>();
            ISigningService         signingService         = 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);

            serializersFactory.Create(null).ReturnsForAnyArgs(c =>
            {
                RegistryShortBlockSerializer registryShortBlockSerializer = new RegistryShortBlockSerializer();
                registryShortBlockSerializer.Initialize(c.Arg <SignedPacketBase>());
                return(registryShortBlockSerializer);
            });

            SyncRegistryMemPool syncRegistryMemPool = new SyncRegistryMemPool(loggerService, hashCalculationsRepository);

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

                SortedList <ushort, RegistryRegisterBlock> transactionHeaders = GetTransactionHeaders(syncBlockHeight, blockHeight, nonce, expectedCount);
                WitnessStateKey[] transactionHeaderKeys = GetTransactionHeaderKeys(transactionHeaders);

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

                RegistryShortBlockSerializer registryShortBlockSerializer = new RegistryShortBlockSerializer();
                registryShortBlockSerializer.Initialize(registryShortBlock);
                registryShortBlockSerializer.SerializeBody();
                signingService1.Sign(registryShortBlock);
                registryShortBlockSerializer.SerializeFully();

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

                RegistryFullBlockSerializer serializer = new RegistryFullBlockSerializer();
                serializer.Initialize(registryFullBlock);
                serializer.SerializeBody();
                signingService.Sign(registryFullBlock);
                serializer.SerializeFully();

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

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

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

            IEnumerable <RegistryFullBlock> actualFullBlocks = syncRegistryMemPool.GetRegistryBlocks();
        }
Beispiel #2
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);
        }