Exemplo n.º 1
0
        public bool EnqueueTransactionRegisterBlock(ITransactionRegistryBlock transactionRegisterBlock)
        {
            lock (_sync)
            {
                AscertainRoundInitialized(transactionRegisterBlock.SyncBlockHeight);

                IKey transactionTwiceHashedKey = _transactionsRegistryHelper.GetTransactionRegistryTwiceHashedKey(transactionRegisterBlock);


                if (!_transactionOrderByTransactionKey[transactionRegisterBlock.SyncBlockHeight].ContainsKey(transactionTwiceHashedKey))
                {
                    _transactionRegisterBlocksOrdered[transactionRegisterBlock.SyncBlockHeight].Add(_transactionsIndicies[transactionRegisterBlock.SyncBlockHeight], transactionRegisterBlock);
                    _transactionOrderByTransactionKey[transactionRegisterBlock.SyncBlockHeight].Add(transactionTwiceHashedKey, _transactionsIndicies[transactionRegisterBlock.SyncBlockHeight]);

                    if (!_transactionKeyBySourceKeys[transactionRegisterBlock.SyncBlockHeight].ContainsKey(transactionRegisterBlock.TransactionSourceKey))
                    {
                        _transactionKeyBySourceKeys[transactionRegisterBlock.SyncBlockHeight].Add(transactionRegisterBlock.TransactionSourceKey, new List <IKey>());
                    }

                    _transactionKeyBySourceKeys[transactionRegisterBlock.SyncBlockHeight][transactionRegisterBlock.TransactionSourceKey].Add(transactionTwiceHashedKey);
                    _transactionSourceKeyByTransactionKey[transactionRegisterBlock.SyncBlockHeight].Add(transactionTwiceHashedKey, transactionRegisterBlock.TransactionSourceKey);

                    _transactionsIndicies[transactionRegisterBlock.SyncBlockHeight]++;
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 2
0
        //TODO: need to understand whether it is needed to pass height of Sync Block or automatically take latest one?
        public SortedList <ushort, ITransactionRegistryBlock> DequeueBulk(int maxCount)
        {
            SortedList <ushort, ITransactionRegistryBlock> items = new SortedList <ushort, ITransactionRegistryBlock>();

            lock (_sync)
            {
                ulong syncBlockHeight = _synchronizationContext.LastBlockDescriptor?.BlockHeight ?? 0;

                if (_transactionRegisterBlocksOrdered.ContainsKey(syncBlockHeight))
                {
                    ushort order = 0;

                    foreach (int orderKey in _transactionRegisterBlocksOrdered[syncBlockHeight].Keys)
                    {
                        ITransactionRegistryBlock transactionRegisterBlock = _transactionRegisterBlocksOrdered[syncBlockHeight][orderKey];

                        items.Add(order++, transactionRegisterBlock);

                        if (order == ushort.MaxValue)
                        {
                            break;
                        }
                    }
                }
            }

            _logger.Debug($"MemPool returns {items.Count} items");
            return(items);
        }
Exemplo n.º 3
0
        public byte[] GetConfidenceMask(RegistryShortBlock transactionsShortBlock, out byte[] bitMask)
        {
            lock (_sync)
            {
                bool[]   bools    = transactionsShortBlock.TransactionHeaderHashes.Select(kvp => _transactionOrderByTransactionKey[transactionsShortBlock.SyncBlockHeight].ContainsKey(kvp.Value)).ToArray();
                BitArray bitArray = bools.ToBitArray();

                bitMask = new byte[bitArray.Length / 8 + ((bitArray.Length % 8 > 0) ? 1 : 0)];

                bitArray.CopyTo(bitMask, 0);

                BigInteger bigIntegerSum = new BigInteger();
                int        i             = 0;
                foreach (var key in transactionsShortBlock.TransactionHeaderHashes.Keys)
                {
                    if (bools[i++])
                    {
                        int transactionHeaderOrder = _transactionOrderByTransactionKey[transactionsShortBlock.SyncBlockHeight][transactionsShortBlock.TransactionHeaderHashes[key]];
                        ITransactionRegistryBlock registryRegisterBlock = _transactionRegisterBlocksOrdered[transactionsShortBlock.SyncBlockHeight][transactionHeaderOrder];
                        IKey       registryRegisterBlockKey             = _transactionsRegistryHelper.GetTransactionRegistryHashKey(registryRegisterBlock);
                        BigInteger bigInteger = new BigInteger(registryRegisterBlockKey.Value.ToArray());
                        bigIntegerSum += bigInteger;
                    }
                }

                byte[] sumBytes    = bigIntegerSum.ToByteArray().Take(Globals.TRANSACTION_KEY_HASH_SIZE).ToArray();
                byte[] returnValue = new byte[Globals.TRANSACTION_KEY_HASH_SIZE];
                Array.Copy(sumBytes, 0, returnValue, 0, sumBytes.Length);
                return(returnValue);
            }
        }
Exemplo n.º 4
0
        public IKey GetTransactionRegistryTwiceHashedKey(ITransactionRegistryBlock registryRegisterBlock)
        {
            byte[] hash = _cryptoService.ComputeTransactionKey(_cryptoService.ComputeTransactionKey(registryRegisterBlock.RawData));
            IKey   key  = _transactionHashKey.GetKey(hash);

            return(key);
        }
Exemplo n.º 5
0
        private static SortedList <ushort, IKey> GetTransactionHeaderKeys(IHashCalculation hashCalculationTransactionKey, SortedList <ushort, ITransactionRegistryBlock> transactionHeaders)
        {
            SortedList <ushort, IKey> transactionHeaderKeys = new SortedList <ushort, IKey>(transactionHeaders.Count);

            foreach (ushort order in transactionHeaders.Keys)
            {
                ITransactionRegistryBlock registryRegisterBlock = transactionHeaders[order];
                byte[] hashBytes = hashCalculationTransactionKey.CalculateHash(registryRegisterBlock.RawData);
                IKey   key       = new Key16(hashBytes);

                transactionHeaderKeys.Add(order, key);
            }

            return(transactionHeaderKeys);
        }
Exemplo n.º 6
0
        public IKey GetTransactionRegistryHashKey(ITransactionRegistryBlock registryRegisterBlock)
        {
            IKey key = _transactionHashKey.GetKey(_cryptoService.ComputeTransactionKey(registryRegisterBlock.RawData));

            return(key);
        }