Esempio n. 1
0
        private async Task UpdateTransactionsByFullRegistryBlock(SynchronizationRegistryCombinedBlock combinedBlock, byte[] fullRegistryBlockHash)
        {
            TransactionInfo registryFullBlockInfo = _syncLayerSyncManagerClient.GetFullRegistryBlock(new HeightHashRequest {
                Height = combinedBlock.SyncBlockHeight, Hash = ByteString.CopyFrom(fullRegistryBlockHash)
            });

            if (registryFullBlockInfo.IsEmpty)
            {
                return;
            }

            IBlockParsersRepository registryFullBlockParserRepo = _blockParsersRepositoriesRepository.GetBlockParsersRepository((PacketType)registryFullBlockInfo.PacketType);
            IBlockParser            registryFullBlockParser     = registryFullBlockParserRepo.GetInstance((ushort)registryFullBlockInfo.BlockType);

            RegistryFullBlock registryFullBlock = (RegistryFullBlock)registryFullBlockParser.Parse(registryFullBlockInfo.Content.ToByteArray());

            AsyncServerStreamingCall <TransactionInfo> asyncTransactionInfosStream = _storageLayerSyncManagerClient.GetTransactionInfos(new FullBlockRequest {
                SyncBlockHeight = registryFullBlock.SyncBlockHeight, Round = registryFullBlock.BlockHeight
            });

            while (await asyncTransactionInfosStream.ResponseStream.MoveNext(_cancellationToken))
            {
                TransactionInfo         transactionInfo            = asyncTransactionInfosStream.ResponseStream.Current;
                IBlockParsersRepository transactionBlockParserRepo = _blockParsersRepositoriesRepository.GetBlockParsersRepository((PacketType)transactionInfo.PacketType);
                IBlockParser            transactionBlockParser     = transactionBlockParserRepo.GetInstance((ushort)transactionInfo.BlockType);
                BlockBase transactionBlockBase = transactionBlockParser.Parse(transactionInfo.Content.ToByteArray());

                UpdateTransaction(transactionBlockBase, combinedBlock.BlockHeight);
            }
        }
Esempio n. 2
0
        public TestBase()
        {
            _transactionHashKeyProvider         = Substitute.For <IIdentityKeyProvider>();
            _identityKeyProvider                = Substitute.For <IIdentityKeyProvider>();
            _identityKeyProvidersRegistry       = Substitute.For <IIdentityKeyProvidersRegistry>();
            _hashCalculationRepository          = Substitute.For <IHashCalculationsRepository>();
            _blockParsersRepositoriesRepository = Substitute.For <IBlockParsersRepositoriesRepository>();
            _blockParsersRepository             = Substitute.For <IBlockParsersRepository>();
            _cryptoService = Substitute.For <ICryptoService>();

            _identityKeyProvidersRegistry.GetInstance().Returns(_identityKeyProvider);
            _identityKeyProvidersRegistry.GetTransactionsIdenityKeyProvider().Returns(_transactionHashKeyProvider);
            _identityKeyProvider.GetKey(null).ReturnsForAnyArgs(c => new Key32()
            {
                Value = c.Arg <Memory <byte> >()
            });
            _transactionHashKeyProvider.GetKey(null).ReturnsForAnyArgs(c => new Key16()
            {
                Value = c.Arg <Memory <byte> >()
            });
            _hashCalculationRepository.Create(HashType.MurMur).Returns(new MurMurHashCalculation());
            _blockParsersRepositoriesRepository.GetBlockParsersRepository(PacketType.Registry).ReturnsForAnyArgs(_blockParsersRepository);

            _privateKey = CryptoHelper.GetRandomSeed();
            Ed25519.KeyPairFromSeed(out _publicKey, out _expandedPrivateKey, _privateKey);

            _cryptoService.Sign(null).ReturnsForAnyArgs(c => Ed25519.Sign(c.Arg <byte[]>(), _expandedPrivateKey));
            _cryptoService.PublicKey.Returns(new Key32()
            {
                Value = _publicKey
            });


            ServiceLocator.Current.GetInstance <IUnityContainer>().RegisterInstance(_blockParsersRepositoriesRepository);
        }
Esempio n. 3
0
        private SynchronizationRegistryCombinedBlock GetRegistryCombinedBlock(TransactionInfo blockInfo)
        {
            try
            {
                SynchronizationRegistryCombinedBlock combinedBlock;
                IBlockParsersRepository blockParsersRepository = _blockParsersRepositoriesRepository.GetBlockParsersRepository((PacketType)blockInfo.PacketType);
                IBlockParser            blockParser            = blockParsersRepository.GetInstance((ushort)blockInfo.BlockType);
                BlockBase blockBase = blockParser.Parse(blockInfo.Content.ToByteArray());
                combinedBlock = (SynchronizationRegistryCombinedBlock)blockBase;
                return(combinedBlock);
            }
            catch (Exception ex)
            {
                _logger.Error($"Failure during deserealization of block of PacketType = {(PacketType)blockInfo.PacketType} and  BlockType = {blockInfo.BlockType} at Sync Block Height {blockInfo.SyncBlockHeight}", ex);
            }

            return(null);
        }
Esempio n. 4
0
        public TestBase()
        {
            _transactionHashKeyProvider         = Substitute.For <IIdentityKeyProvider>();
            _identityKeyProvider                = Substitute.For <IIdentityKeyProvider>();
            _identityKeyProvidersRegistry       = Substitute.For <IIdentityKeyProvidersRegistry>();
            _hashCalculationRepository          = Substitute.For <IHashCalculationsRepository>();
            _blockParsersRepositoriesRepository = Substitute.For <IBlockParsersRepositoriesRepository>();
            _blockParsersRepository             = Substitute.For <IBlockParsersRepository>();
            _signingService = Substitute.For <ISigningService>();

            _identityKeyProvidersRegistry.GetInstance().Returns(_identityKeyProvider);
            _identityKeyProvidersRegistry.GetTransactionsIdenityKeyProvider().Returns(_transactionHashKeyProvider);
            _identityKeyProvider.GetKey(null).ReturnsForAnyArgs(c => new Key32()
            {
                Value = c.Arg <Memory <byte> >()
            });
            _transactionHashKeyProvider.GetKey(null).ReturnsForAnyArgs(c => new Key16()
            {
                Value = c.Arg <Memory <byte> >()
            });
            _hashCalculationRepository.Create(HashType.MurMur).Returns(new MurMurHashCalculation());
            _blockParsersRepositoriesRepository.GetBlockParsersRepository(PacketType.Registry).ReturnsForAnyArgs(_blockParsersRepository);

            _privateKey     = ConfidentialAssetsHelper.GetRandomSeed();
            _privateViewKey = ConfidentialAssetsHelper.GetRandomSeed();
            Ed25519.KeyPairFromSeed(out _publicKey, out _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(((SignedPacketBase)c.ArgAt <IPacket>(0)).BodyBytes.ToArray(), _expandedPrivateKey);
            });
            _signingService.PublicKeys.Returns(new IKey[] { new Key32()
                                                            {
                                                                Value = _publicKey
                                                            } });

            _utxoSigningService = new UtxoSigningService(_identityKeyProvidersRegistry);
            _utxoSigningService.Initialize(_privateKey, _privateViewKey);


            ServiceLocator.Current.GetInstance <IUnityContainer>().RegisterInstance(_blockParsersRepositoriesRepository);
        }
Esempio n. 5
0
        public async Task <List <BlockBase> > GetTransactions(ulong syncBlockHeight, ulong round)
        {
            List <BlockBase> transactionInfos = new List <BlockBase>();

            AsyncServerStreamingCall <TransactionInfo> asyncServerStreaming = _transactionalChainManager.GetTransactionInfos(new FullBlockRequest {
                SyncBlockHeight = syncBlockHeight, Round = round
            });

            while (await asyncServerStreaming.ResponseStream.MoveNext().ConfigureAwait(false))
            {
                TransactionInfo         item = asyncServerStreaming.ResponseStream.Current;
                IBlockParsersRepository blockParsersRepository = _blockParsersRepositoriesRepository.GetBlockParsersRepository((PacketType)item.PacketType);
                IBlockParser            blockParser            = blockParsersRepository.GetInstance((ushort)item.BlockType);
                BlockBase blockBase = blockParser.Parse(item.Content.ToByteArray());
                transactionInfos.Add(blockBase);
            }

            return(transactionInfos);
        }
Esempio n. 6
0
        protected override Memory <byte> ParseSynced(ushort version, Memory <byte> spanBody, out SyncedBlockBase syncedBlockBase)
        {
            if (version == 1)
            {
                RegistryFullBlock transactionsFullBlock = new RegistryFullBlock();
                ushort            itemsCount            = BinaryPrimitives.ReadUInt16LittleEndian(spanBody.Span);

                transactionsFullBlock.TransactionHeaders = new SortedList <ushort, ITransactionRegistryBlock>(itemsCount);
                int readBytes = 2;

                if (itemsCount > 0)
                {
                    for (int i = 0; i < itemsCount; i++)
                    {
                        ushort order = BinaryPrimitives.ReadUInt16LittleEndian(spanBody.Span.Slice(readBytes));
                        readBytes += 2;

                        BlockParserBase.GetPacketAndBlockTypes(spanBody.Slice(readBytes), out PacketType packetType, out ushort blockType);
                        IBlockParsersRepository blockParsersRepository = _blockParsersRepositoriesRepository.Value.GetBlockParsersRepository(packetType);
                        IBlockParser            blockParser            = blockParsersRepository.GetInstance(blockType);
                        BlockBase block = blockParser.Parse(spanBody.Slice(readBytes));

                        readBytes += block?.RawData.Length ?? 0;

                        if (block is ITransactionRegistryBlock transactionRegistryBlock)
                        {
                            transactionsFullBlock.TransactionHeaders.Add(order, transactionRegistryBlock);
                        }
                    }
                }

                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);
        }
Esempio n. 7
0
        private bool CheckSpIdentityValidations(byte[] commitment, AssociatedProofs[] associatedProofsList, IEnumerable <SpIdenitityValidation> spIdenitityValidations, string sessionKey)
        {
            if (spIdenitityValidations != null && spIdenitityValidations.Count() > 0)
            {
                IBlockParsersRepository transactionalBlockParsersRepo  = _blockParsersRepositoriesRepository.GetBlockParsersRepository(PacketType.Transactional);
                IBlockParser            issueAssociatedAttrBlockParser = transactionalBlockParsersRepo.GetInstance(BlockTypes.Transaction_IssueAssociatedBlindedAsset);

                if (associatedProofsList == null)
                {
                    _idenitiesHubContext.Clients.Group(sessionKey).SendAsync("PushSpAuthorizationFailed", new { Code = 3, Message = "Validation proofs were not provided" }).Wait();
                    return(false);
                }

                foreach (var spIdentityValidation in spIdenitityValidations)
                {
                    if (!CheckSpIdentityValidation(commitment, associatedProofsList, spIdentityValidation, sessionKey))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Esempio n. 8
0
 public SynchronizationBlockToBlockBaseMapper(IBlockParsersRepositoriesRepository blockParsersFactoriesRepository)
 {
     _blockParsersRepository = blockParsersFactoriesRepository.GetBlockParsersRepository(PacketType.Synchronization);
 }
Esempio n. 9
0
 public TransactionsRegistryBlockToBlockBaseMapper(IBlockParsersRepositoriesRepository blockParsersFactoriesRepository)
 {
     _blockParsersRepository = blockParsersFactoriesRepository.GetBlockParsersRepository(PacketType.Registry);
 }
Esempio n. 10
0
 public UtxoConfidentialMapperBase(IBlockParsersRepositoriesRepository blockParsersFactoriesRepository)
 {
     _blockParsersRepository = blockParsersFactoriesRepository.GetBlockParsersRepository(PacketType.UtxoConfidential);
 }
Esempio n. 11
0
        private BlockBase ParseMessagePacket(byte[] messagePacket)
        {
            _log.Debug($"Packet being parsed {messagePacket.ToHexString()}");

            BlockBase  blockBase    = null;
            PacketType packetType   = (PacketType)BitConverter.ToUInt16(messagePacket, 0);
            int        blockTypePos = Globals.PACKET_TYPE_LENGTH + Globals.SYNC_BLOCK_HEIGHT_LENGTH + Globals.NONCE_LENGTH + Globals.POW_HASH_SIZE + Globals.VERSION_LENGTH;

            if (messagePacket.Length < blockTypePos + 2)
            {
                _log.Error($"Length of packet is insufficient for obtaining BlockType value: {messagePacket.ToHexString()}");
                return(blockBase);
            }

            ushort                  blockType           = BitConverter.ToUInt16(messagePacket, Globals.PACKET_TYPE_LENGTH + Globals.SYNC_BLOCK_HEIGHT_LENGTH + Globals.NONCE_LENGTH + Globals.POW_HASH_SIZE + Globals.VERSION_LENGTH);
            IBlockParser            blockParser         = null;
            IBlockParsersRepository blockParsersFactory = null;

            try
            {
                //TODO: weigh assumption that all messages are sync based (have reference to latest Sync Block)

                _log.Info($"Parsing packet of type {packetType} and block type {blockType}");

                blockParsersFactory = _blockParsersFactoriesRepository.GetBlockParsersRepository(packetType);

                if (blockParsersFactory != null)
                {
                    blockParser = blockParsersFactory.GetInstance(blockType);

                    if (blockParser != null)
                    {
                        blockBase = blockParser.Parse(messagePacket);
                    }
                    else
                    {
                        _log.Error($"Block parser of packet type {packetType} and block type {blockType} not found! Message: {messagePacket.ToHexString()}");
                    }
                }
                else
                {
                    _log.Error($"Block parser factory of packet type {packetType} not found! Message: {messagePacket.ToHexString()}");
                }
            }
            catch (Exception ex)
            {
                _log.Error($"Failed to parse message of packet type {packetType} and block type {blockType}: {messagePacket.ToHexString()}", ex);
            }

            _endToEndCountersService.ParsingThroughput.Increment();

            if (blockBase != null)
            {
                _log.Debug($"Parsed block {blockBase.RawData.ToHexString()}");
            }
            else
            {
                _log.Error($"Failed to parse block from message {messagePacket.ToHexString()}");
            }

            return(blockBase);
        }
Esempio n. 12
0
 public TransactionalMapperBase(IBlockParsersRepositoriesRepository blockParsersFactoriesRepository)
 {
     _blockParsersRepository = blockParsersFactoriesRepository.GetBlockParsersRepository(PacketType.Transactional);
 }