public void RegistryConfidenceBlockSerializerTest() { ulong syncBlockHeight = 1; uint nonce = 4; byte[] powHash = BinaryBuilder.GetPowHash(1234); ushort version = 1; ulong blockHeight = 9; byte[] prevHash = null; Random randNum = new Random(); ushort bitMaskLength = 375; byte[] bitMask = Enumerable.Repeat(0, bitMaskLength).Select(i => (byte)randNum.Next(0, 255)).ToArray(); byte[] expectedProof = Enumerable.Repeat(0, 16).Select(i => (byte)randNum.Next(0, 255)).ToArray(); byte[] expectedReferencedBodyHash = BinaryBuilder.GetDefaultHash(473826643); byte[] body; using (MemoryStream ms = new MemoryStream()) { using (BinaryWriter bw = new BinaryWriter(ms)) { bw.Write((ushort)bitMask.Length); bw.Write(bitMask); bw.Write(expectedProof); bw.Write(expectedReferencedBodyHash); } body = ms.ToArray(); } byte[] expectedPacket = BinaryBuilder.GetSignedPacket( PacketType.Registry, syncBlockHeight, nonce, powHash, version, BlockTypes.Registry_ConfidenceBlock, blockHeight, prevHash, body, _privateKey, out byte[] expectedSignature); RegistryConfidenceBlock block = new RegistryConfidenceBlock { SyncBlockHeight = syncBlockHeight, Nonce = nonce, PowHash = powHash, BlockHeight = blockHeight, BitMask = bitMask, ConfidenceProof = expectedProof, ReferencedBlockHash = expectedReferencedBodyHash }; RegistryConfidenceBlockSerializer serializer = new RegistryConfidenceBlockSerializer(_cryptoService, _identityKeyProvidersRegistry, _hashCalculationRepository); serializer.Initialize(block); byte[] actualPacket = serializer.GetBytes(); Trace.WriteLine(expectedPacket.ToHexString()); Trace.WriteLine(actualPacket.ToHexString()); Assert.Equal(expectedPacket, actualPacket); }
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); }