public bool EnqueueTransactionWitness(RegistryRegisterBlock transactionWitness) { lock (_sync) { bool witnessExist = false; if (_transactionStateWitnessesBySender.ContainsKey(transactionWitness.Signer)) { witnessExist = _transactionStateWitnessesBySender[transactionWitness.Signer].Any(t => t.BlockHeight == transactionWitness.BlockHeight); } if (!witnessExist) { if (!_transactionStateWitnessesBySender.ContainsKey(transactionWitness.Signer)) { _transactionStateWitnessesBySender.Add(transactionWitness.Signer, new List <RegistryRegisterBlock>()); } _transactionStateWitnessesBySender[transactionWitness.Signer].Add(transactionWitness); _transactionStateWitnesses.Add(transactionWitness); } return(witnessExist); } }
public void ClearWitnessed(RegistryShortBlock transactionsShortBlock) { lock (_sync) { foreach (var witnessStateKey in transactionsShortBlock.WitnessStateKeys) { if (_transactionStateWitnessesBySender.ContainsKey(witnessStateKey.PublicKey)) { RegistryRegisterBlock transactionWitness = _transactionStateWitnessesBySender[witnessStateKey.PublicKey].FirstOrDefault(t => t.BlockHeight == witnessStateKey.Height); if (transactionWitness != null) { _transactionStateWitnessesBySender[witnessStateKey.PublicKey].Remove(transactionWitness); if (_transactionStateWitnessesBySender[witnessStateKey.PublicKey].Count == 0) { _transactionStateWitnessesBySender.Remove(witnessStateKey.PublicKey); } _transactionStateWitnesses.Remove(transactionWitness); } } } foreach (var witnessUtxoKey in transactionsShortBlock.WitnessUtxoKeys) { if (_transactionUtxoWitnessesByKeyImage.ContainsKey(witnessUtxoKey.KeyImage)) { RegistryRegisterUtxoConfidential transactionWitness = _transactionUtxoWitnessesByKeyImage[witnessUtxoKey.KeyImage]; _transactionStateWitnessesBySender.Remove(witnessUtxoKey.KeyImage); _transactionUtxoWitnesses.Remove(transactionWitness); } } } }
public void RegistryRegisterBlockParserTransitionalTest() { ulong syncBlockHeight = 1; uint nonce = 4; byte[] powHash = BinaryHelper.GetPowHash(1234); ushort version = 1; ulong blockHeight = 9; byte[] prevHash = null; PacketType expectedReferencedPacketType = PacketType.Transactional; ushort expectedReferencedBlockType = BlockTypes.Transaction_TransferAssetToUtxo; byte[] expectedReferencedBodyHash = BinaryHelper.GetDefaultHash(473826643); byte[] expectedTarget = BinaryHelper.GetDefaultHash(BinaryHelper.GetRandomPublicKey()); byte[] transactionKey = ConfidentialAssetsHelper.GetRandomSeed(); byte[] body; using (MemoryStream ms = new MemoryStream()) { using (BinaryWriter bw = new BinaryWriter(ms)) { bw.Write((ushort)expectedReferencedPacketType); bw.Write(expectedReferencedBlockType); bw.Write(expectedReferencedBodyHash); bw.Write(expectedTarget); bw.Write(transactionKey); } body = ms.ToArray(); } byte[] packet = BinaryHelper.GetSignedPacket( PacketType.Registry, syncBlockHeight, nonce, powHash, version, BlockTypes.Registry_Register, blockHeight, prevHash, body, _privateKey, out byte[] expectedSignature); RegistryRegisterBlockParser registryRegisterBlockParser = new RegistryRegisterBlockParser(_identityKeyProvidersRegistry); RegistryRegisterBlock block = (RegistryRegisterBlock)registryRegisterBlockParser.Parse(packet); Assert.Equal(syncBlockHeight, block.SyncBlockHeight); Assert.Equal(nonce, block.Nonce); Assert.Equal(powHash, block.PowHash); Assert.Equal(version, block.Version); Assert.Equal(blockHeight, block.BlockHeight); Assert.Equal(expectedReferencedPacketType, block.ReferencedPacketType); Assert.Equal(expectedReferencedBlockType, block.ReferencedBlockType); Assert.Equal(expectedReferencedBodyHash, block.ReferencedBodyHash); Assert.Equal(expectedTarget, block.ReferencedTarget); Assert.Equal(transactionKey, block.ReferencedTransactionKey); Assert.Equal(_publicKey, block.Signer.Value.ToArray()); Assert.Equal(expectedSignature, block.Signature.ToArray()); }
public void MemPool_AddedXUniqueTransactions_AllContained() { SynchronizationDescriptor synchronizationDescriptor = new SynchronizationDescriptor(1, new byte[Globals.DEFAULT_HASH_SIZE], DateTime.Now, DateTime.Now, 1); IHash transactionKeyHash = HashFactory.Hash128.CreateMurmur3_128(); ILogger logger = Substitute.For <ILogger>(); ILoggerService loggerService = Substitute.For <ILoggerService>(); IIdentityKeyProvider identityKeyProvider = new TransactionRegistryKeyProvider(); IIdentityKeyProvidersRegistry identityKeyProvidersRegistry = Substitute.For <IIdentityKeyProvidersRegistry>(); IHashCalculationsRepository hashCalculationsRepository = Substitute.For <IHashCalculationsRepository>(); ICryptoService cryptoService = Substitute.For <ICryptoService>(); SynchronizationContext synchronizationContext = new SynchronizationContext(loggerService); synchronizationContext.UpdateLastSyncBlockDescriptor(synchronizationDescriptor); IStatesRepository statesRepository = Substitute.For <IStatesRepository>(); hashCalculationsRepository.Create(HashType.MurMur).Returns(new MurMurHashCalculation()); logger.WhenForAnyArgs(l => l.Error(null)).DoNotCallBase(); logger.WhenForAnyArgs(l => l.Warning(null)).DoNotCallBase(); logger.WhenForAnyArgs(l => l.Info(null)).DoNotCallBase(); loggerService.GetLogger(null).Returns(logger); identityKeyProvidersRegistry.GetInstance(null).ReturnsForAnyArgs(identityKeyProvider); statesRepository.GetInstance <ISynchronizationContext>().Returns(synchronizationContext); byte[] privateKey = CryptoHelper.GetRandomSeed(); Ed25519.KeyPairFromSeed(out byte[] publicKey, out byte[] expandedPrivateKey, privateKey); cryptoService.ComputeTransactionKey(new Memory <byte>()).ReturnsForAnyArgs(c => transactionKeyHash.ComputeBytes(c.Arg <Memory <byte> >().ToArray()).GetBytes()); cryptoService.ComputeTransactionKey(new byte[0]).ReturnsForAnyArgs(c => transactionKeyHash.ComputeBytes(c.Arg <byte[]>()).GetBytes()); cryptoService.Sign(null).ReturnsForAnyArgs(c => Ed25519.Sign(c.Arg <byte[]>(), expandedPrivateKey)); cryptoService.PublicKey.ReturnsForAnyArgs(new Key32(publicKey)); TransactionRegistryMemPool transactionRegistryMemPool = new TransactionRegistryMemPool(loggerService, identityKeyProvidersRegistry, cryptoService, statesRepository, new TransactionsRegistryHelper(cryptoService, identityKeyProvidersRegistry), hashCalculationsRepository); ulong expectedCount = 10; SortedList <ushort, RegistryRegisterBlock> expectedBlocks = new SortedList <ushort, RegistryRegisterBlock>(); for (ulong i = 0; i < expectedCount; i++) { RegistryRegisterBlock transactionRegisterBlock = PacketsBuilder.GetTransactionRegisterBlock(synchronizationContext.LastBlockDescriptor.BlockHeight, 1, null, i, PacketType.Transactional, BlockTypes.Transaction_TransferFunds, new byte[Globals.POW_HASH_SIZE], new byte[Globals.DEFAULT_HASH_SIZE], privateKey); RegistryRegisterBlockSerializer serializer = new RegistryRegisterBlockSerializer(cryptoService, identityKeyProvidersRegistry, hashCalculationsRepository); serializer.Initialize(transactionRegisterBlock); serializer.FillBodyAndRowBytes(); expectedBlocks.Add((ushort)i, transactionRegisterBlock); transactionRegistryMemPool.EnqueueTransactionRegisterBlock(transactionRegisterBlock); } SortedList <ushort, ITransactionRegistryBlock> actualBlocks = transactionRegistryMemPool.DequeueBulk(-1); Assert.Equal(expectedCount, (ushort)actualBlocks.Count); for (ushort i = 0; i < (ushort)expectedCount; i++) { Assert.Equal(expectedBlocks[i].BlockHeight, ((RegistryRegisterBlock)actualBlocks[i]).BlockHeight); } }
private static WitnessStateKey[] GetTransactionHeaderKeys(SortedList <ushort, RegistryRegisterBlock> transactionHeaders) { WitnessStateKey[] transactionHeaderKeys = new WitnessStateKey[transactionHeaders.Count]; foreach (ushort order in transactionHeaders.Keys) { RegistryRegisterBlock registryRegisterBlock = transactionHeaders[order]; WitnessStateKey key = new WitnessStateKey { PublicKey = registryRegisterBlock.Signer, Height = registryRegisterBlock.BlockHeight }; transactionHeaderKeys[order] = key; } return(transactionHeaderKeys); }
public BlockBase CreateRegisterBlock(TransactionalBlockBase transactionalBlock, byte[] target) { RegistryRegisterBlock registerBlock = new RegistryRegisterBlock { SyncBlockHeight = transactionalBlock.SyncBlockHeight, BlockHeight = transactionalBlock.BlockHeight, Nonce = transactionalBlock.Nonce, PowHash = transactionalBlock.PowHash, ReferencedPacketType = transactionalBlock.PacketType, ReferencedBlockType = transactionalBlock.BlockType, ReferencedTarget = target ?? new byte[32], ReferencedBodyHash = _hashCalculation.CalculateHash(transactionalBlock.RawData) }; return(registerBlock); }
protected override Memory <byte> ParseSigned(ushort version, Memory <byte> spanBody, out SignedPacketBase syncedBlockBase) { if (version == 1) { int readBytes = 0; RegistryFullBlock transactionsFullBlock = new RegistryFullBlock(); ushort stateWitnessesCount = BinaryPrimitives.ReadUInt16LittleEndian(spanBody.Span.Slice(readBytes)); readBytes += sizeof(ushort); ushort utxoWitnessesCount = BinaryPrimitives.ReadUInt16LittleEndian(spanBody.Span.Slice(readBytes)); readBytes += sizeof(ushort); transactionsFullBlock.StateWitnesses = new RegistryRegisterBlock[stateWitnessesCount]; transactionsFullBlock.UtxoWitnesses = new RegistryRegisterUtxoConfidential[utxoWitnessesCount]; for (int i = 0; i < stateWitnessesCount; i++) { RegistryRegisterBlock block = (RegistryRegisterBlock)_registryRegisterBlockParser.Parse(spanBody.Slice(readBytes)); readBytes += block?.RawData.Length ?? 0; transactionsFullBlock.StateWitnesses[i] = block; } for (int i = 0; i < utxoWitnessesCount; i++) { RegistryRegisterUtxoConfidential block = (RegistryRegisterUtxoConfidential)_registryRegisterUtxoConfidentialBlockParser.Parse(spanBody.Slice(readBytes)); readBytes += block?.RawData.Length ?? 0; transactionsFullBlock.UtxoWitnesses[i] = block; } transactionsFullBlock.ShortBlockHash = spanBody.Slice(readBytes, Globals.DEFAULT_HASH_SIZE).ToArray(); readBytes += Globals.DEFAULT_HASH_SIZE; syncedBlockBase = transactionsFullBlock; return(spanBody.Slice(readBytes)); } throw new BlockVersionNotSupportedException(version, BlockType); }
public static RegistryRegisterBlock GetTransactionRegisterBlock(ulong syncBlockHeight, uint nonce, byte[] powHash, ulong blockHeight, PacketType referencedPacketType, ushort referencedBlockType, byte[] referencedBlockHash, byte[] referencedTarget, byte[] privateKey) { byte[] publicKey = Ed25519.PublicKeyFromSeed(privateKey); RegistryRegisterBlock transactionRegisterBlock = new RegistryRegisterBlock { SyncBlockHeight = syncBlockHeight, Nonce = nonce, PowHash = powHash ?? new byte[Globals.POW_HASH_SIZE], BlockHeight = blockHeight, ReferencedPacketType = referencedPacketType, ReferencedBlockType = referencedBlockType, ReferencedBodyHash = referencedBlockHash, ReferencedTarget = referencedTarget, Signer = new Key32(publicKey) }; return(transactionRegisterBlock); }
protected override Memory <byte> ParseSigned(ushort version, Memory <byte> spanBody, out SignedPacketBase syncedBlockBase) { if (version == 1) { int readBytes = 0; PacketType referencedPacketType = (PacketType)BinaryPrimitives.ReadUInt16LittleEndian(spanBody.Span); readBytes += sizeof(ushort); ushort referencedBlockType = BinaryPrimitives.ReadUInt16LittleEndian(spanBody.Span.Slice(readBytes)); readBytes += sizeof(ushort); byte[] referencedBlockHash = spanBody.Slice(readBytes, Globals.DEFAULT_HASH_SIZE).ToArray(); readBytes += Globals.DEFAULT_HASH_SIZE; byte[] referencedTarget = spanBody.Slice(readBytes, Globals.DEFAULT_HASH_SIZE).ToArray(); readBytes += Globals.DEFAULT_HASH_SIZE; byte[] transactionKey = null; if ((referencedBlockType & BlockTypes.TransitionalFlag) == BlockTypes.TransitionalFlag) { transactionKey = spanBody.Slice(readBytes, Globals.DEFAULT_HASH_SIZE).ToArray(); readBytes += Globals.DEFAULT_HASH_SIZE; } RegistryRegisterBlock transactionRegisterBlock = new RegistryRegisterBlock { ReferencedPacketType = referencedPacketType, ReferencedBlockType = referencedBlockType, ReferencedBodyHash = referencedBlockHash, ReferencedTarget = referencedTarget, ReferencedTransactionKey = transactionKey }; syncedBlockBase = transactionRegisterBlock; return(spanBody.Slice(readBytes)); } throw new BlockVersionNotSupportedException(version, BlockType); }
private static SortedList <ushort, RegistryRegisterBlock> GetTransactionHeaders(ulong syncBlockHeight, ulong blockHeight, uint nonce, ushort expectedCount) { PacketType expectedReferencedPacketType = PacketType.Transactional; ushort expectedReferencedBlockType = BlockTypes.Transaction_TransferFunds; SortedList <ushort, RegistryRegisterBlock> transactionHeaders = new SortedList <ushort, RegistryRegisterBlock>(expectedCount); for (ushort j = 0; j < expectedCount; j++) { RegistryRegisterBlock registryRegisterBlock = new RegistryRegisterBlock { SyncBlockHeight = syncBlockHeight, Nonce = nonce + j, PowHash = BinaryHelper.GetPowHash(1234 + j), BlockHeight = blockHeight, ReferencedPacketType = expectedReferencedPacketType, ReferencedBlockType = expectedReferencedBlockType, ReferencedBodyHash = BinaryHelper.GetDefaultHash(473826643 + j), ReferencedTarget = BinaryHelper.GetDefaultHash(BinaryHelper.GetRandomPublicKey()) }; ISigningService signingService = GetRandomCryptoService(); IIdentityKeyProvider transactionIdentityKeyProvider = Substitute.For <IIdentityKeyProvider>(); transactionIdentityKeyProvider.GetKey(null).ReturnsForAnyArgs(c => new Key16(c.ArgAt <Memory <byte> >(0))); IIdentityKeyProvidersRegistry transactionIdentityKeyProvidersRegistry = Substitute.For <IIdentityKeyProvidersRegistry>(); transactionIdentityKeyProvidersRegistry.GetTransactionsIdenityKeyProvider().Returns(transactionIdentityKeyProvider); IHashCalculationsRepository transactionHashCalculationsRepository = Substitute.For <IHashCalculationsRepository>(); transactionHashCalculationsRepository.Create(HashType.MurMur).Returns(new MurMurHashCalculation()); RegistryRegisterBlockSerializer serializer1 = new RegistryRegisterBlockSerializer(); serializer1.Initialize(registryRegisterBlock); serializer1.SerializeBody(); signingService.Sign(registryRegisterBlock); serializer1.SerializeFully(); transactionHeaders.Add(j, registryRegisterBlock); } return(transactionHeaders); }
public override void Start() { ulong index = _synchronizationContext.LastBlockDescriptor?.BlockHeight ?? 0; byte[] syncHash = _synchronizationContext.LastBlockDescriptor?.Hash ?? new byte[Globals.DEFAULT_HASH_SIZE]; string cmd = null; byte[] powHash = GetPowHash(syncHash, 1234); byte[] targetAddress = GetRandomTargetAddress(); do { RegistryRegisterBlock transactionRegisterBlock = new RegistryRegisterBlock { SyncBlockHeight = index, BlockHeight = 1, Signer = _key, Nonce = 1234, PowHash = powHash, ReferencedPacketType = PacketType.Transactional, ReferencedBlockType = BlockTypes.Transaction_TransferFunds, ReferencedBodyHash = new byte[Globals.DEFAULT_HASH_SIZE], ReferencedTarget = targetAddress }; ISerializer signatureSupportSerializer = _signatureSupportSerializersFactory.Create(transactionRegisterBlock); _log.Info($"Sending message: {signatureSupportSerializer.GetBytes().ToHexString()}"); _communicationService.PostMessage(_keyTarget, signatureSupportSerializer); if (_cancellationToken.IsCancellationRequested) { break; } Console.WriteLine("Block sent. Press <Enter> for next or type 'exit' and press <Enter> for exit..."); cmd = Console.ReadLine(); } while (!_cancellationToken.IsCancellationRequested && cmd != "exit"); }
protected override Memory <byte> ParseSynced(ushort version, Memory <byte> spanBody, out SyncedBlockBase syncedBlockBase) { if (version == 1) { PacketType referencedPacketType = (PacketType)BinaryPrimitives.ReadUInt16LittleEndian(spanBody.Span); ushort referencedBlockType = BinaryPrimitives.ReadUInt16LittleEndian(spanBody.Span.Slice(2)); byte[] referencedBlockHash = spanBody.Slice(4, Globals.DEFAULT_HASH_SIZE).ToArray(); byte[] referencedTargetHash = spanBody.Slice(4 + Globals.DEFAULT_HASH_SIZE, Globals.DEFAULT_HASH_SIZE).ToArray(); RegistryRegisterBlock transactionRegisterBlock = new RegistryRegisterBlock { ReferencedPacketType = referencedPacketType, ReferencedBlockType = referencedBlockType, ReferencedBodyHash = referencedBlockHash, ReferencedTarget = referencedTargetHash }; syncedBlockBase = transactionRegisterBlock; return(spanBody.Slice(4 + Globals.DEFAULT_HASH_SIZE + Globals.DEFAULT_HASH_SIZE)); } throw new BlockVersionNotSupportedException(version, BlockType); }
public void RegistryRegisterBlockSerializerTest() { ulong syncBlockHeight = 1; uint nonce = 4; byte[] powHash = BinaryBuilder.GetPowHash(1234); ushort version = 1; ulong blockHeight = 9; byte[] prevHash = null; PacketType expectedReferencedPacketType = PacketType.Transactional; ushort expectedReferencedBlockType = BlockTypes.Transaction_TransferFunds; byte[] expectedReferencedBodyHash = BinaryBuilder.GetDefaultHash(473826643); byte[] expectedTarget = BinaryBuilder.GetDefaultHash(BinaryBuilder.GetRandomPublicKey()); byte[] body; using (MemoryStream ms = new MemoryStream()) { using (BinaryWriter bw = new BinaryWriter(ms)) { bw.Write((ushort)expectedReferencedPacketType); bw.Write(expectedReferencedBlockType); bw.Write(expectedReferencedBodyHash); bw.Write(expectedTarget); } body = ms.ToArray(); } byte[] expectedPacket = BinaryBuilder.GetSignedPacket( PacketType.Registry, syncBlockHeight, nonce, powHash, version, BlockTypes.Registry_Register, blockHeight, prevHash, body, _privateKey, out byte[] expectedSignature); RegistryRegisterBlock block = new RegistryRegisterBlock { SyncBlockHeight = syncBlockHeight, Nonce = nonce, PowHash = powHash, BlockHeight = blockHeight, ReferencedPacketType = expectedReferencedPacketType, ReferencedBlockType = expectedReferencedBlockType, ReferencedBodyHash = expectedReferencedBodyHash, ReferencedTarget = expectedTarget }; RegistryRegisterBlockSerializer serializer = new RegistryRegisterBlockSerializer(_cryptoService, _identityKeyProvidersRegistry, _hashCalculationRepository); serializer.Initialize(block); byte[] actualPacket = serializer.GetBytes(); Trace.WriteLine(expectedPacket.ToHexString()); Trace.WriteLine(actualPacket.ToHexString()); Assert.Equal(expectedPacket, actualPacket); }
public void RegistryFullBlockParserTest() { IBlockParser blockParser = new RegistryRegisterBlockParser(_identityKeyProvidersRegistry); _blockParsersRepository.GetInstance(0).ReturnsForAnyArgs(blockParser); ulong syncBlockHeight = 1; uint nonce = 4; byte[] powHash = BinaryHelper.GetPowHash(1234); ushort version = 1; ulong blockHeight = 9; byte[] prevHash = null; PacketType expectedReferencedPacketType = PacketType.Transactional; ushort expectedReferencedBlockType = BlockTypes.Transaction_TransferFunds; byte[] body; ushort expectedCount = 1; byte[] expectedShortBlockHash; RegistryRegisterBlock[] stateWitnesses = new RegistryRegisterBlock[expectedCount]; RegistryRegisterUtxoConfidential[] utxoWitnesses = new RegistryRegisterUtxoConfidential[expectedCount]; for (ushort i = 0; i < expectedCount; i++) { RegistryRegisterBlock registryRegisterBlock = new RegistryRegisterBlock { SyncBlockHeight = syncBlockHeight, Nonce = nonce + i, PowHash = BinaryHelper.GetPowHash(1234 + i), BlockHeight = blockHeight, ReferencedPacketType = expectedReferencedPacketType, ReferencedBlockType = expectedReferencedBlockType, ReferencedBodyHash = BinaryHelper.GetDefaultHash(473826643 + i), ReferencedTarget = BinaryHelper.GetDefaultHash(BinaryHelper.GetRandomPublicKey()) }; RegistryRegisterBlockSerializer serializer1 = new RegistryRegisterBlockSerializer(); serializer1.Initialize(registryRegisterBlock); serializer1.SerializeBody(); _signingService.Sign(registryRegisterBlock); serializer1.SerializeFully(); stateWitnesses[i] = registryRegisterBlock; RegistryRegisterUtxoConfidential registryRegisterUtxoConfidentialBlock = new RegistryRegisterUtxoConfidential { SyncBlockHeight = syncBlockHeight, Nonce = nonce + i, PowHash = BinaryHelper.GetPowHash(1234 + i), KeyImage = new Key32(ConfidentialAssetsHelper.GetRandomSeed()), ReferencedPacketType = expectedReferencedPacketType, ReferencedBlockType = expectedReferencedBlockType, ReferencedBodyHash = BinaryHelper.GetDefaultHash(473826643 + i), DestinationKey = ConfidentialAssetsHelper.GetRandomSeed(), DestinationKey2 = ConfidentialAssetsHelper.GetRandomSeed(), TransactionPublicKey = ConfidentialAssetsHelper.GetRandomSeed(), PublicKeys = new Key32[] { new Key32(ConfidentialAssetsHelper.GetRandomSeed()), new Key32(ConfidentialAssetsHelper.GetRandomSeed()), new Key32(ConfidentialAssetsHelper.GetRandomSeed()) }, Signatures = new RingSignature[] { new RingSignature { R = ConfidentialAssetsHelper.GetRandomSeed(), C = ConfidentialAssetsHelper.GetRandomSeed() }, new RingSignature { R = ConfidentialAssetsHelper.GetRandomSeed(), C = ConfidentialAssetsHelper.GetRandomSeed() }, new RingSignature { R = ConfidentialAssetsHelper.GetRandomSeed(), C = ConfidentialAssetsHelper.GetRandomSeed() } } }; RegistryRegisterUtxoConfidentialBlockSerializer serializer2 = new RegistryRegisterUtxoConfidentialBlockSerializer(); serializer2.Initialize(registryRegisterUtxoConfidentialBlock); serializer2.SerializeFully(); utxoWitnesses[i] = registryRegisterUtxoConfidentialBlock; } using (MemoryStream ms = new MemoryStream()) { using (BinaryWriter bw = new BinaryWriter(ms)) { bw.Write((ushort)stateWitnesses.Length); bw.Write((ushort)utxoWitnesses.Length); foreach (RegistryRegisterBlock witness in stateWitnesses) { bw.Write(witness.RawData.ToArray()); } foreach (RegistryRegisterUtxoConfidential witness in utxoWitnesses) { bw.Write(witness.RawData.ToArray()); } expectedShortBlockHash = BinaryHelper.GetDefaultHash(1111); bw.Write(expectedShortBlockHash); } body = ms.ToArray(); } byte[] packet = BinaryHelper.GetSignedPacket( PacketType.Registry, syncBlockHeight, nonce, powHash, version, BlockTypes.Registry_FullBlock, blockHeight, prevHash, body, _privateKey, out byte[] expectedSignature); RegistryFullBlockParser registryFullBlockParser = new RegistryFullBlockParser(_identityKeyProvidersRegistry); RegistryFullBlock block = (RegistryFullBlock)registryFullBlockParser.Parse(packet); Assert.Equal(syncBlockHeight, block.SyncBlockHeight); Assert.Equal(nonce, block.Nonce); Assert.Equal(powHash, block.PowHash); Assert.Equal(version, block.Version); Assert.Equal(blockHeight, block.BlockHeight); for (int i = 0; i < expectedCount; i++) { RegistryRegisterBlock registryRegisterBlock = block.StateWitnesses[i]; Assert.Equal(stateWitnesses[i].PacketType, registryRegisterBlock.PacketType); Assert.Equal(stateWitnesses[i].SyncBlockHeight, registryRegisterBlock.SyncBlockHeight); Assert.Equal(stateWitnesses[i].Nonce, registryRegisterBlock.Nonce); Assert.Equal(stateWitnesses[i].PowHash, registryRegisterBlock.PowHash); Assert.Equal(stateWitnesses[i].BlockHeight, registryRegisterBlock.BlockHeight); Assert.Equal(stateWitnesses[i].BlockType, registryRegisterBlock.BlockType); Assert.Equal(stateWitnesses[i].ReferencedPacketType, registryRegisterBlock.ReferencedPacketType); Assert.Equal(stateWitnesses[i].ReferencedBlockType, registryRegisterBlock.ReferencedBlockType); Assert.Equal(stateWitnesses[i].ReferencedBodyHash, registryRegisterBlock.ReferencedBodyHash); Assert.Equal(stateWitnesses[i].ReferencedTarget, registryRegisterBlock.ReferencedTarget); Assert.Equal(stateWitnesses[i].Signature.ToArray(), registryRegisterBlock.Signature.ToArray()); Assert.Equal(stateWitnesses[i].Signer, registryRegisterBlock.Signer); RegistryRegisterUtxoConfidential registryRegisterUtxoConfidentialBlock = block.UtxoWitnesses[i]; Assert.Equal(utxoWitnesses[i].PacketType, registryRegisterUtxoConfidentialBlock.PacketType); Assert.Equal(utxoWitnesses[i].SyncBlockHeight, registryRegisterUtxoConfidentialBlock.SyncBlockHeight); Assert.Equal(utxoWitnesses[i].Nonce, registryRegisterUtxoConfidentialBlock.Nonce); Assert.Equal(utxoWitnesses[i].PowHash, registryRegisterUtxoConfidentialBlock.PowHash); Assert.Equal(utxoWitnesses[i].KeyImage, registryRegisterUtxoConfidentialBlock.KeyImage); Assert.Equal(utxoWitnesses[i].BlockType, registryRegisterUtxoConfidentialBlock.BlockType); Assert.Equal(utxoWitnesses[i].ReferencedPacketType, registryRegisterUtxoConfidentialBlock.ReferencedPacketType); Assert.Equal(utxoWitnesses[i].ReferencedBlockType, registryRegisterUtxoConfidentialBlock.ReferencedBlockType); Assert.Equal(utxoWitnesses[i].DestinationKey2, registryRegisterUtxoConfidentialBlock.DestinationKey2); Assert.Equal(utxoWitnesses[i].ReferencedBodyHash, registryRegisterUtxoConfidentialBlock.ReferencedBodyHash); Assert.Equal(utxoWitnesses[i].DestinationKey, registryRegisterUtxoConfidentialBlock.DestinationKey); } Assert.Equal(expectedShortBlockHash, block.ShortBlockHash); Assert.Equal(_publicKey, block.Signer.Value.ToArray()); Assert.Equal(expectedSignature, block.Signature.ToArray()); }
public void RegistryFullBlockSerializerTest() { ulong syncBlockHeight = 1; uint nonce = 4; byte[] powHash = BinaryHelper.GetPowHash(1234); ushort version = 1; ulong blockHeight = 9; byte[] prevHash = null; PacketType expectedReferencedPacketType = PacketType.Transactional; ushort expectedReferencedBlockType = BlockTypes.Transaction_TransferFunds; byte[] expectedReferencedBodyHash = BinaryHelper.GetDefaultHash(473826643); byte[] expectedTarget = BinaryHelper.GetDefaultHash(BinaryHelper.GetRandomPublicKey()); byte[] body; ushort expectedCount = 1000; RegistryRegisterBlock[] stateWitnesses = new RegistryRegisterBlock[expectedCount]; RegistryRegisterUtxoConfidential[] utxoWitnesses = new RegistryRegisterUtxoConfidential[expectedCount]; for (ushort i = 0; i < expectedCount; i++) { RegistryRegisterBlock registryRegisterBlock = new RegistryRegisterBlock { SyncBlockHeight = syncBlockHeight, Nonce = nonce + i, PowHash = BinaryHelper.GetPowHash(1234 + i), BlockHeight = blockHeight, ReferencedPacketType = expectedReferencedPacketType, ReferencedBlockType = expectedReferencedBlockType, ReferencedBodyHash = BinaryHelper.GetDefaultHash(473826643 + i), ReferencedTarget = BinaryHelper.GetDefaultHash(BinaryHelper.GetRandomPublicKey()) }; RegistryRegisterBlockSerializer serializer1 = new RegistryRegisterBlockSerializer(); serializer1.Initialize(registryRegisterBlock); serializer1.SerializeBody(); _signingService.Sign(registryRegisterBlock); serializer1.SerializeFully(); stateWitnesses[i] = registryRegisterBlock; RegistryRegisterUtxoConfidential registryRegisterUtxoConfidentialBlock = new RegistryRegisterUtxoConfidential { SyncBlockHeight = syncBlockHeight, Nonce = nonce + i, PowHash = BinaryHelper.GetPowHash(1234 + i), DestinationKey = ConfidentialAssetsHelper.GetRandomSeed(), ReferencedPacketType = expectedReferencedPacketType, ReferencedBlockType = expectedReferencedBlockType, DestinationKey2 = ConfidentialAssetsHelper.GetRandomSeed(), ReferencedBodyHash = BinaryHelper.GetDefaultHash(473826643 + i), KeyImage = new Key32(ConfidentialAssetsHelper.GetRandomSeed()), TransactionPublicKey = ConfidentialAssetsHelper.GetRandomSeed(), PublicKeys = new IKey[] { new Key32(ConfidentialAssetsHelper.GetRandomSeed()), new Key32(ConfidentialAssetsHelper.GetRandomSeed()), new Key32(ConfidentialAssetsHelper.GetRandomSeed()) }, Signatures = new RingSignature[] { new RingSignature { C = ConfidentialAssetsHelper.GetRandomSeed(), R = ConfidentialAssetsHelper.GetRandomSeed() }, new RingSignature { C = ConfidentialAssetsHelper.GetRandomSeed(), R = ConfidentialAssetsHelper.GetRandomSeed() }, new RingSignature { C = ConfidentialAssetsHelper.GetRandomSeed(), R = ConfidentialAssetsHelper.GetRandomSeed() } } }; RegistryRegisterUtxoConfidentialBlockSerializer serializer2 = new RegistryRegisterUtxoConfidentialBlockSerializer(); serializer2.Initialize(registryRegisterUtxoConfidentialBlock); serializer2.SerializeFully(); utxoWitnesses[i] = registryRegisterUtxoConfidentialBlock; } using (MemoryStream ms = new MemoryStream()) { using (BinaryWriter bw = new BinaryWriter(ms)) { bw.Write((ushort)stateWitnesses.Length); bw.Write((ushort)utxoWitnesses.Length); foreach (RegistryRegisterBlock witness in stateWitnesses) { bw.Write(witness.RawData.ToArray()); } foreach (RegistryRegisterUtxoConfidential witness in utxoWitnesses) { bw.Write(witness.RawData.ToArray()); } bw.Write(BinaryHelper.GetDefaultHash(1111)); } body = ms.ToArray(); } byte[] expectedPacket = BinaryHelper.GetSignedPacket( PacketType.Registry, syncBlockHeight, nonce, powHash, version, BlockTypes.Registry_FullBlock, blockHeight, prevHash, body, _privateKey, out byte[] expectedSignature); RegistryFullBlock block = new RegistryFullBlock { SyncBlockHeight = syncBlockHeight, Nonce = nonce, PowHash = powHash, BlockHeight = blockHeight, StateWitnesses = stateWitnesses, UtxoWitnesses = utxoWitnesses, ShortBlockHash = BinaryHelper.GetDefaultHash(1111) }; RegistryFullBlockSerializer serializer = new RegistryFullBlockSerializer(); serializer.Initialize(block); serializer.SerializeBody(); _signingService.Sign(block); byte[] actualPacket = serializer.GetBytes(); Trace.WriteLine(expectedPacket.ToHexString()); Trace.WriteLine(actualPacket.ToHexString()); Assert.Equal(expectedPacket, actualPacket); }
public void RegistryFullBlockParserTest() { IBlockParser blockParser = new RegistryRegisterBlockParser(_identityKeyProvidersRegistry, _hashCalculationRepository); _blockParsersRepository.GetInstance(0).ReturnsForAnyArgs(blockParser); ulong syncBlockHeight = 1; uint nonce = 4; byte[] powHash = BinaryBuilder.GetPowHash(1234); ushort version = 1; ulong blockHeight = 9; byte[] prevHash = null; PacketType expectedReferencedPacketType = PacketType.Transactional; ushort expectedReferencedBlockType = BlockTypes.Transaction_TransferFunds; byte[] body; ushort expectedCount = 1000; byte[] expectedShortBlockHash; SortedList <ushort, RegistryRegisterBlock> transactionHeaders = new SortedList <ushort, RegistryRegisterBlock>(); for (ushort i = 0; i < expectedCount; i++) { RegistryRegisterBlock registryRegisterBlock = new RegistryRegisterBlock { SyncBlockHeight = syncBlockHeight, Nonce = nonce + i, PowHash = BinaryBuilder.GetPowHash(1234 + i), BlockHeight = blockHeight, ReferencedPacketType = expectedReferencedPacketType, ReferencedBlockType = expectedReferencedBlockType, ReferencedBodyHash = BinaryBuilder.GetDefaultHash(473826643 + i), ReferencedTarget = BinaryBuilder.GetDefaultHash(BinaryBuilder.GetRandomPublicKey()) }; RegistryRegisterBlockSerializer serializer1 = new RegistryRegisterBlockSerializer(_cryptoService, _identityKeyProvidersRegistry, _hashCalculationRepository); serializer1.Initialize(registryRegisterBlock); serializer1.FillBodyAndRowBytes(); transactionHeaders.Add(i, registryRegisterBlock); } using (MemoryStream ms = new MemoryStream()) { using (BinaryWriter bw = new BinaryWriter(ms)) { bw.Write((ushort)transactionHeaders.Count); foreach (ushort order in transactionHeaders.Keys) { bw.Write(order); bw.Write(transactionHeaders[order].RawData.ToArray()); } expectedShortBlockHash = BinaryBuilder.GetDefaultHash(1111); bw.Write(expectedShortBlockHash); } body = ms.ToArray(); } byte[] packet = BinaryBuilder.GetSignedPacket( PacketType.Registry, syncBlockHeight, nonce, powHash, version, BlockTypes.Registry_FullBlock, blockHeight, prevHash, body, _privateKey, out byte[] expectedSignature); RegistryFullBlockParser registryFullBlockParser = new RegistryFullBlockParser(_identityKeyProvidersRegistry, _hashCalculationRepository); RegistryFullBlock block = (RegistryFullBlock)registryFullBlockParser.Parse(packet); Assert.Equal(syncBlockHeight, block.SyncBlockHeight); Assert.Equal(nonce, block.Nonce); Assert.Equal(powHash, block.PowHash); Assert.Equal(version, block.Version); Assert.Equal(blockHeight, block.BlockHeight); foreach (var item in transactionHeaders) { RegistryRegisterBlock registryRegisterBlock = (RegistryRegisterBlock)block.TransactionHeaders[item.Key]; Assert.True(block.TransactionHeaders.ContainsKey(item.Key)); Assert.Equal(item.Value.PacketType, registryRegisterBlock.PacketType); Assert.Equal(item.Value.SyncBlockHeight, registryRegisterBlock.SyncBlockHeight); Assert.Equal(item.Value.Nonce, registryRegisterBlock.Nonce); Assert.Equal(item.Value.PowHash, registryRegisterBlock.PowHash); Assert.Equal(item.Value.BlockHeight, registryRegisterBlock.BlockHeight); Assert.Equal(item.Value.BlockType, registryRegisterBlock.BlockType); Assert.Equal(item.Value.ReferencedPacketType, registryRegisterBlock.ReferencedPacketType); Assert.Equal(item.Value.ReferencedBlockType, registryRegisterBlock.ReferencedBlockType); Assert.Equal(item.Value.ReferencedBodyHash, registryRegisterBlock.ReferencedBodyHash); Assert.Equal(item.Value.ReferencedTarget, registryRegisterBlock.ReferencedTarget); Assert.Equal(item.Value.Signature.ToArray(), registryRegisterBlock.Signature.ToArray()); Assert.Equal(item.Value.Signer, registryRegisterBlock.Signer); } Assert.Equal(expectedShortBlockHash, block.ShortBlockHash); Assert.Equal(_publicKey, block.Signer.Value.ToArray()); Assert.Equal(expectedSignature, block.Signature.ToArray()); }
public override void Start() { string cmd = null; Channel channel = new Channel("127.0.0.1", 5050, ChannelCredentials.Insecure); SyncManager.SyncManagerClient syncManagerClient = new SyncManager.SyncManagerClient(channel); TransactionalChainManager.TransactionalChainManagerClient transactionalChainManagerClient = new TransactionalChainManager.TransactionalChainManagerClient(channel); TransactionalBlockEssense transactionalBlockEssense = transactionalChainManagerClient.GetLastTransactionalBlock( new TransactionalBlockRequest { PublicKey = ByteString.CopyFrom(_key.Value.ToArray()) }); byte[] hashPrev = transactionalBlockEssense.Hash.ToByteArray(); ulong blockHeight = transactionalBlockEssense.Height + 1; ulong uptodateFunds = transactionalBlockEssense.UpToDateFunds > 0 ? transactionalBlockEssense.UpToDateFunds : 100000; do { SyncBlockDescriptor lastSyncBlock = syncManagerClient.GetLastSyncBlock(new Empty()); uptodateFunds -= 10; byte[] syncHash = lastSyncBlock.Hash.ToByteArray(); uint nonce = 1111; byte[] powHash = GetPowHash(syncHash, nonce); byte[] targetAddress = GetRandomTargetAddress(); TransferFundsBlock transferFundsBlock = new TransferFundsBlock { SyncBlockHeight = lastSyncBlock.Height, BlockHeight = blockHeight, Nonce = nonce, PowHash = powHash, HashPrev = hashPrev, TargetOriginalHash = targetAddress, UptodateFunds = uptodateFunds }; ISerializer transferFundsSerializer = _signatureSupportSerializersFactory.Create(transferFundsBlock); transferFundsSerializer.FillBodyAndRowBytes(); RegistryRegisterBlock transactionRegisterBlock = new RegistryRegisterBlock { SyncBlockHeight = lastSyncBlock.Height, BlockHeight = blockHeight, Nonce = nonce, PowHash = powHash, ReferencedPacketType = PacketType.Transactional, ReferencedBlockType = BlockTypes.Transaction_TransferFunds, ReferencedBodyHash = _hashCalculation.CalculateHash(transferFundsBlock.RawData), ReferencedTarget = targetAddress }; ISerializer transactionRegisterBlockSerializer = _signatureSupportSerializersFactory.Create(transactionRegisterBlock); _log.Info($"Sending message: {transactionRegisterBlockSerializer.GetBytes().ToHexString()}"); _communicationService.PostMessage(_keyTarget, transactionRegisterBlockSerializer); _communicationService.PostMessage(_keyTarget, transferFundsSerializer); Console.WriteLine("Block sent. Press <Enter> for next or type 'exit' and press <Enter> for exit..."); cmd = Console.ReadLine(); blockHeight++; hashPrev = transactionRegisterBlock.ReferencedBodyHash; } while (!_cancellationToken.IsCancellationRequested && cmd != "exit"); }
public void MemPool_ContainsXItems_ConfidenceLevelOnAll() { SynchronizationDescriptor synchronizationDescriptor = new SynchronizationDescriptor(1, new byte[Globals.DEFAULT_HASH_SIZE], DateTime.Now, DateTime.Now, 1); IHash transactionKeyHash = HashFactory.Hash128.CreateMurmur3_128(); ILogger logger = Substitute.For <ILogger>(); ILoggerService loggerService = Substitute.For <ILoggerService>(); IIdentityKeyProvider identityKeyProvider = new TransactionRegistryKeyProvider(); IIdentityKeyProvidersRegistry identityKeyProvidersRegistry = Substitute.For <IIdentityKeyProvidersRegistry>(); IHashCalculationsRepository hashCalculationsRepository = Substitute.For <IHashCalculationsRepository>(); ICryptoService cryptoService = Substitute.For <ICryptoService>(); SynchronizationContext synchronizationContext = new SynchronizationContext(loggerService); synchronizationContext.UpdateLastSyncBlockDescriptor(synchronizationDescriptor); IStatesRepository statesRepository = Substitute.For <IStatesRepository>(); hashCalculationsRepository.Create(HashType.MurMur).Returns(new MurMurHashCalculation()); logger.WhenForAnyArgs(l => l.Error(null)).DoNotCallBase(); logger.WhenForAnyArgs(l => l.Warning(null)).DoNotCallBase(); logger.WhenForAnyArgs(l => l.Info(null)).DoNotCallBase(); loggerService.GetLogger(null).Returns(logger); identityKeyProvidersRegistry.GetTransactionsIdenityKeyProvider().ReturnsForAnyArgs(identityKeyProvider); statesRepository.GetInstance <ISynchronizationContext>().Returns(synchronizationContext); byte[] privateKey = CryptoHelper.GetRandomSeed(); Ed25519.KeyPairFromSeed(out byte[] publicKey, out byte[] expandedPrivateKey, privateKey); cryptoService.ComputeTransactionKey(new Memory <byte>()).ReturnsForAnyArgs(c => transactionKeyHash.ComputeBytes(c.Arg <Memory <byte> >().ToArray()).GetBytes()); cryptoService.ComputeTransactionKey(new byte[0]).ReturnsForAnyArgs(c => transactionKeyHash.ComputeBytes(c.Arg <byte[]>()).GetBytes()); cryptoService.Sign(null).ReturnsForAnyArgs(c => Ed25519.Sign(c.Arg <byte[]>(), expandedPrivateKey)); cryptoService.PublicKey.ReturnsForAnyArgs(new Key32(publicKey)); TransactionRegistryMemPool transactionRegistryMemPool = new TransactionRegistryMemPool(loggerService, identityKeyProvidersRegistry, cryptoService, statesRepository, new TransactionsRegistryHelper(cryptoService, identityKeyProvidersRegistry), hashCalculationsRepository); SortedList <ushort, RegistryRegisterBlock> expectedBlocks = new SortedList <ushort, RegistryRegisterBlock>(); ulong[] heights = new ulong[] { 1, 2, 2, 5, 4, 3, 4, 3, 4, 5, 3 }; HashSet <ulong> addedHeights = new HashSet <ulong>(); ushort order = 0; for (ulong i = 0; i < (ulong)heights.Length; i++) { RegistryRegisterBlock transactionRegisterBlock = PacketsBuilder.GetTransactionRegisterBlock(synchronizationContext.LastBlockDescriptor.BlockHeight, 1, null, heights[i], PacketType.Transactional, BlockTypes.Transaction_TransferFunds, new byte[Globals.POW_HASH_SIZE], new byte[Globals.DEFAULT_HASH_SIZE], privateKey); RegistryRegisterBlockSerializer serializer = new RegistryRegisterBlockSerializer(cryptoService, identityKeyProvidersRegistry, hashCalculationsRepository); serializer.Initialize(transactionRegisterBlock); serializer.FillBodyAndRowBytes(); if (!addedHeights.Contains(heights[i])) { expectedBlocks.Add(order++, transactionRegisterBlock); addedHeights.Add(heights[i]); } transactionRegistryMemPool.EnqueueTransactionRegisterBlock(transactionRegisterBlock); } RegistryShortBlock transactionsShortBlockAll = PacketsBuilder.GetTransactionsShortBlock(1, 1, null, 1, 1, expectedBlocks.Values, privateKey, new TransactionsRegistryHelper(cryptoService, identityKeyProvidersRegistry)); RegistryShortBlock transactionsShortBlockOneLess = PacketsBuilder.GetTransactionsShortBlock(1, 1, null, 1, 1, expectedBlocks.Values.Skip(1), privateKey, new TransactionsRegistryHelper(cryptoService, identityKeyProvidersRegistry)); byte[] bitMaskAll, bitMaskOneLess; byte[] confidenceProofAll = transactionRegistryMemPool.GetConfidenceMask(transactionsShortBlockAll, out bitMaskAll); byte[] confidenceProofOneLess = transactionRegistryMemPool.GetConfidenceMask(transactionsShortBlockOneLess, out bitMaskOneLess); long expectedConfidenceAll = expectedBlocks.Count; long expectedConfidenceOneLess = expectedBlocks.Count - 1; long actualConfidenceAll = GetConfidence(bitMaskAll); long actualConfidenceOneLess = GetConfidence(bitMaskOneLess); Assert.Equal(expectedConfidenceAll, actualConfidenceAll); Assert.Equal(expectedConfidenceOneLess, actualConfidenceOneLess); }
public override void Start() { string cmd = null; Channel channel = new Channel("127.0.0.1", 5050, ChannelCredentials.Insecure); SyncManager.SyncManagerClient syncManagerClient = new SyncManager.SyncManagerClient(channel); TransactionalChainManager.TransactionalChainManagerClient transactionalChainManagerClient = new TransactionalChainManager.TransactionalChainManagerClient(channel); TransactionalBlockEssense transactionalBlockEssense = transactionalChainManagerClient.GetLastTransactionalBlock( new TransactionalBlockRequest { PublicKey = ByteString.CopyFrom(_key.Value.ToArray()) }); byte[] hashPrev = transactionalBlockEssense.Hash.ToByteArray(); ulong blockHeight = transactionalBlockEssense.Height + 1; ulong uptodateFunds = transactionalBlockEssense.UpToDateFunds > 0 ? transactionalBlockEssense.UpToDateFunds : 100000; ulong tagId = 5005; byte[][] idCards = new byte[][] { GetComplementedAssetId(327483038), GetComplementedAssetId(327152054), GetComplementedAssetId(328051065), }; string[] assetInfos = new string[] { "Kirill Gandyl", "Elena Zaychikov", "Etel Gandyl" }; do { SyncBlockDescriptor lastSyncBlock = syncManagerClient.GetLastSyncBlock(new Empty()); byte[] syncHash = lastSyncBlock.Hash.ToByteArray(); uint nonce = 1111; byte[] powHash = GetPowHash(syncHash, nonce); byte[] targetAddress = new byte[32]; IssueAssetsBlock issueAssetsBlock = new IssueAssetsBlock { SyncBlockHeight = lastSyncBlock.Height, BlockHeight = blockHeight, Nonce = nonce, PowHash = powHash, HashPrev = hashPrev, TagId = tagId, IssuanceInfo = "Issue ID cards", IssuedAssetIds = idCards, IssuedAssetInfo = assetInfos }; ISerializer issueAssetsBlockSerializer = _signatureSupportSerializersFactory.Create(issueAssetsBlock); issueAssetsBlockSerializer.FillBodyAndRowBytes(); RegistryRegisterBlock transactionRegisterBlock = new RegistryRegisterBlock { SyncBlockHeight = lastSyncBlock.Height, BlockHeight = blockHeight, Nonce = nonce, PowHash = powHash, ReferencedPacketType = PacketType.Transactional, ReferencedBlockType = BlockTypes.Transaction_TransferFunds, ReferencedBodyHash = _hashCalculation.CalculateHash(issueAssetsBlock.RawData), ReferencedTarget = targetAddress }; ISerializer transactionRegisterBlockSerializer = _signatureSupportSerializersFactory.Create(transactionRegisterBlock); _log.Info($"Sending message: {transactionRegisterBlockSerializer.GetBytes().ToHexString()}"); _communicationService.PostMessage(_keyTarget, transactionRegisterBlockSerializer); _communicationService.PostMessage(_keyTarget, issueAssetsBlockSerializer); Console.WriteLine("Block sent. Press <Enter> for next or type 'exit' and press <Enter> for exit..."); cmd = Console.ReadLine(); blockHeight++; hashPrev = transactionRegisterBlock.ReferencedBodyHash; } while (!_cancellationToken.IsCancellationRequested && cmd != "exit"); }
public void RegistryRegisterBlockSerializerTransitionalTest() { ulong syncBlockHeight = 1; uint nonce = 4; byte[] powHash = BinaryHelper.GetPowHash(1234); ushort version = 1; ulong blockHeight = 9; byte[] prevHash = null; PacketType expectedReferencedPacketType = PacketType.Transactional; ushort expectedReferencedBlockType = BlockTypes.Transaction_TransferAssetToUtxo; byte[] expectedReferencedBodyHash = BinaryHelper.GetDefaultHash(473826643); byte[] expectedTarget = BinaryHelper.GetDefaultHash(BinaryHelper.GetRandomPublicKey()); byte[] transactionKey = ConfidentialAssetsHelper.GetRandomSeed(); byte[] body; using (MemoryStream ms = new MemoryStream()) { using (BinaryWriter bw = new BinaryWriter(ms)) { bw.Write((ushort)expectedReferencedPacketType); bw.Write(expectedReferencedBlockType); bw.Write(expectedReferencedBodyHash); bw.Write(expectedTarget); bw.Write(transactionKey); } body = ms.ToArray(); } byte[] expectedPacket = BinaryHelper.GetSignedPacket( PacketType.Registry, syncBlockHeight, nonce, powHash, version, BlockTypes.Registry_Register, blockHeight, prevHash, body, _privateKey, out byte[] expectedSignature); RegistryRegisterBlock block = new RegistryRegisterBlock { SyncBlockHeight = syncBlockHeight, Nonce = nonce, PowHash = powHash, BlockHeight = blockHeight, ReferencedPacketType = expectedReferencedPacketType, ReferencedBlockType = expectedReferencedBlockType, ReferencedBodyHash = expectedReferencedBodyHash, ReferencedTarget = expectedTarget, ReferencedTransactionKey = transactionKey }; RegistryRegisterBlockSerializer serializer = new RegistryRegisterBlockSerializer(); serializer.Initialize(block); serializer.SerializeBody(); _signingService.Sign(block); byte[] actualPacket = serializer.GetBytes(); Trace.WriteLine(expectedPacket.ToHexString()); Trace.WriteLine(actualPacket.ToHexString()); Assert.Equal(expectedPacket, actualPacket); }
public void MemPool_AddedNonUniqueTransactions_NotAllContained() { SynchronizationDescriptor synchronizationDescriptor = new SynchronizationDescriptor(1, new byte[Globals.DEFAULT_HASH_SIZE], DateTime.Now, DateTime.Now, 1); IHash transactionKeyHash = HashFactory.Hash128.CreateMurmur3_128(); ILogger logger = Substitute.For <ILogger>(); ILoggerService loggerService = Substitute.For <ILoggerService>(); IIdentityKeyProvider identityKeyProvider = new TransactionRegistryKeyProvider(); IIdentityKeyProvidersRegistry identityKeyProvidersRegistry = Substitute.For <IIdentityKeyProvidersRegistry>(); IHashCalculationsRepository hashCalculationsRepository = Substitute.For <IHashCalculationsRepository>(); ISigningService signingService = Substitute.For <ISigningService>(); SynchronizationContext synchronizationContext = new SynchronizationContext(loggerService); synchronizationContext.UpdateLastSyncBlockDescriptor(synchronizationDescriptor); IStatesRepository statesRepository = Substitute.For <IStatesRepository>(); hashCalculationsRepository.Create(HashType.MurMur).Returns(new MurMurHashCalculation()); logger.WhenForAnyArgs(l => l.Error(null)).DoNotCallBase(); logger.WhenForAnyArgs(l => l.Warning(null)).DoNotCallBase(); logger.WhenForAnyArgs(l => l.Info(null)).DoNotCallBase(); loggerService.GetLogger(null).Returns(logger); identityKeyProvidersRegistry.GetTransactionsIdenityKeyProvider().ReturnsForAnyArgs(identityKeyProvider); //identityKeyProvider.GetKey(null).ReturnsForAnyArgs(c => new Key16() { Value = c.Arg<Memory<byte>>() }); statesRepository.GetInstance <ISynchronizationContext>().Returns(synchronizationContext); byte[] privateKey = ConfidentialAssetsHelper.GetRandomSeed(); Ed25519.KeyPairFromSeed(out byte[] publicKey, out byte[] expandedPrivateKey, privateKey); signingService.WhenForAnyArgs(s => s.Sign(null, null)).Do(c => { ((SignedPacketBase)c.ArgAt <IPacket>(0)).Signer = new Key32(publicKey); ((SignedPacketBase)c.ArgAt <IPacket>(0)).Signature = Ed25519.Sign(c.Arg <byte[]>(), expandedPrivateKey); }); signingService.PublicKeys.ReturnsForAnyArgs(new IKey[] { new Key32(publicKey) }); TransactionRegistryMemPool transactionRegistryMemPool = new TransactionRegistryMemPool(loggerService, identityKeyProvidersRegistry, statesRepository, hashCalculationsRepository); SortedList <ushort, RegistryRegisterBlock> expectedBlocks = new SortedList <ushort, RegistryRegisterBlock>(); ulong[] heights = new ulong[] { 1, 2, 2, 5, 4, 3, 4, 3, 4, 5, 3 }; HashSet <ulong> addedHeights = new HashSet <ulong>(); ushort order = 0; for (ulong i = 0; i < (ulong)heights.Length; i++) { RegistryRegisterBlock transactionRegisterBlock = PacketsBuilder.GetTransactionRegisterBlock(synchronizationContext.LastBlockDescriptor.BlockHeight, 1, null, heights[i], PacketType.Transactional, BlockTypes.Transaction_TransferFunds, new byte[Globals.POW_HASH_SIZE], new byte[Globals.DEFAULT_HASH_SIZE], privateKey); RegistryRegisterBlockSerializer serializer = new RegistryRegisterBlockSerializer(); serializer.Initialize(transactionRegisterBlock); serializer.SerializeBody(); signingService.Sign(transactionRegisterBlock); serializer.SerializeFully(); if (!addedHeights.Contains(heights[i])) { expectedBlocks.Add(order++, transactionRegisterBlock); addedHeights.Add(heights[i]); } transactionRegistryMemPool.EnqueueTransactionWitness(transactionRegisterBlock); } SortedList <ushort, RegistryRegisterBlock> actualBlocks = transactionRegistryMemPool.DequeueStateWitnessBulk(); Assert.Equal(expectedBlocks.Count, actualBlocks.Count); for (ushort i = 0; i < (ushort)expectedBlocks.Count; i++) { Assert.Equal(expectedBlocks[i].BlockHeight, actualBlocks[i].BlockHeight); } }
public override void Start() { byte[] seedTarget = GetRandomSeed(); byte[] targetKeyBytes = Ed25519.PublicKeyFromSeed(seedTarget); byte[] targetHash = CryptoHelper.ComputeHash(targetKeyBytes); //IHash hash = HashFactory.Crypto.CreateTiger_4_192(); //List<byte[]> seeds = new List<byte[]>(); //List<byte[]> hashes = new List<byte[]>(); //byte[] seed1 = GetRandomSeed(); //BigInteger bigInteger1 = new BigInteger(seed1); //bool found = false; //Stopwatch stopwatch1 = Stopwatch.StartNew(); //for (int i = 0; i < 10000000; i++) //{ // bigInteger1 += 1; // byte[] calc = hash.ComputeBytes(bigInteger1.ToByteArray()).GetBytes(); // if(calc[0] == 0 && calc[1] == 0 && calc[2] == 0 ) // { // found = true; // break; // } //} //long elapsed = stopwatch1.ElapsedMilliseconds; ulong index = _synchronizationContext.LastBlockDescriptor?.BlockHeight ?? 0; byte[] syncHash = _synchronizationContext.LastBlockDescriptor?.Hash ?? new byte[Globals.DEFAULT_HASH_SIZE]; BigInteger bigInteger = new BigInteger(syncHash); ulong nonce = 1234; bigInteger += nonce; byte[] hashNonce = bigInteger.ToByteArray(); byte[] powHash = _proofOfWorkCalculation.CalculateHash(hashNonce); ulong count = 0; Stopwatch stopwatch = Stopwatch.StartNew(); do { RegistryRegisterBlock transactionRegisterBlock = new RegistryRegisterBlock { SyncBlockHeight = index, BlockHeight = count, Signer = _key, Nonce = 1234, PowHash = powHash, ReferencedPacketType = PacketType.Transactional, ReferencedBlockType = BlockTypes.Transaction_TransferFunds, ReferencedBodyHash = new byte[Globals.DEFAULT_HASH_SIZE], ReferencedTarget = targetHash }; ISerializer signatureSupportSerializer = _serializersFactory.Create(transactionRegisterBlock); _communicationService.PostMessage(_keyTarget, signatureSupportSerializer); _loadCountersService.SentMessages.Increment(); count++; } while (!_cancellationToken.IsCancellationRequested); }
public void RegistryFullBlockSerializerTest() { ulong syncBlockHeight = 1; uint nonce = 4; byte[] powHash = BinaryBuilder.GetPowHash(1234); ushort version = 1; ulong blockHeight = 9; byte[] prevHash = null; PacketType expectedReferencedPacketType = PacketType.Transactional; ushort expectedReferencedBlockType = BlockTypes.Transaction_TransferFunds; byte[] expectedReferencedBodyHash = BinaryBuilder.GetDefaultHash(473826643); byte[] expectedTarget = BinaryBuilder.GetDefaultHash(BinaryBuilder.GetRandomPublicKey()); byte[] body; ushort expectedCount = 1000; SortedList <ushort, ITransactionRegistryBlock> transactionHeaders = new SortedList <ushort, ITransactionRegistryBlock>(); for (ushort i = 0; i < expectedCount; i++) { RegistryRegisterBlock registryRegisterBlock = new RegistryRegisterBlock { SyncBlockHeight = syncBlockHeight, Nonce = nonce + i, PowHash = BinaryBuilder.GetPowHash(1234 + i), BlockHeight = blockHeight, ReferencedPacketType = expectedReferencedPacketType, ReferencedBlockType = expectedReferencedBlockType, ReferencedBodyHash = BinaryBuilder.GetDefaultHash(473826643 + i), ReferencedTarget = BinaryBuilder.GetDefaultHash(BinaryBuilder.GetRandomPublicKey()) }; RegistryRegisterBlockSerializer serializer1 = new RegistryRegisterBlockSerializer(_cryptoService, _identityKeyProvidersRegistry, _hashCalculationRepository); serializer1.Initialize(registryRegisterBlock); serializer1.FillBodyAndRowBytes(); transactionHeaders.Add(i, registryRegisterBlock); } using (MemoryStream ms = new MemoryStream()) { using (BinaryWriter bw = new BinaryWriter(ms)) { bw.Write((ushort)transactionHeaders.Count); foreach (ushort order in transactionHeaders.Keys) { bw.Write(order); bw.Write(transactionHeaders[order].RawData.ToArray()); } bw.Write(BinaryBuilder.GetDefaultHash(1111)); } body = ms.ToArray(); } byte[] expectedPacket = BinaryBuilder.GetSignedPacket( PacketType.Registry, syncBlockHeight, nonce, powHash, version, BlockTypes.Registry_FullBlock, blockHeight, prevHash, body, _privateKey, out byte[] expectedSignature); RegistryFullBlock block = new RegistryFullBlock { SyncBlockHeight = syncBlockHeight, Nonce = nonce, PowHash = powHash, BlockHeight = blockHeight, TransactionHeaders = transactionHeaders, ShortBlockHash = BinaryBuilder.GetDefaultHash(1111) }; RegistryFullBlockSerializer serializer = new RegistryFullBlockSerializer(_cryptoService, _identityKeyProvidersRegistry, _hashCalculationRepository); serializer.Initialize(block); byte[] actualPacket = serializer.GetBytes(); Trace.WriteLine(expectedPacket.ToHexString()); Trace.WriteLine(actualPacket.ToHexString()); Assert.Equal(expectedPacket, actualPacket); }