Example #1
0
        private void CreateAndDistributeCombinedBlock(RegistryFullBlock transactionsFullBlockMostConfident)
        {
            lock (_synchronizationContext)
            {
                SynchronizationRegistryCombinedBlock lastCombinedBlock = (SynchronizationRegistryCombinedBlock)_synchronizationChainDataService.GetAllLastBlocksByType(BlockTypes.Synchronization_RegistryCombinationBlock).Single();
                byte[] prevHash = lastCombinedBlock != null?_defaultTransactionHashCalculation.CalculateHash(lastCombinedBlock.RawData) : new byte[Globals.DEFAULT_HASH_SIZE];

                byte[] fullBlockHash = _defaultTransactionHashCalculation.CalculateHash(transactionsFullBlockMostConfident?.RawData ?? new byte[Globals.DEFAULT_HASH_SIZE]);
                //TODO: For initial POC there will be only one participant at Synchronization Layer, thus combination of FullBlocks won't be implemented fully
                SynchronizationRegistryCombinedBlock synchronizationRegistryCombinedBlock = new SynchronizationRegistryCombinedBlock
                {
                    SyncBlockHeight = _synchronizationContext.LastBlockDescriptor?.BlockHeight ?? 0,
                    PowHash         = _powCalculation.CalculateHash(_synchronizationContext.LastBlockDescriptor?.Hash ?? new byte[Globals.DEFAULT_HASH_SIZE]),
                    BlockHeight     = ++_synchronizationContext.LastRegistrationCombinedBlockHeight,
                    HashPrev        = prevHash,
                    ReportedTime    = DateTime.Now,
                    BlockHashes     = new byte[][] { fullBlockHash }
                };

                ISerializer combinedBlockSerializer = _signatureSupportSerializersFactory.Create(synchronizationRegistryCombinedBlock);
                combinedBlockSerializer.FillBodyAndRowBytes();

                IEnumerable <IKey> storageLayerKeys = _nodesResolutionService.GetStorageNodeKeys(combinedBlockSerializer);
                _communicationService.PostMessage(storageLayerKeys, combinedBlockSerializer);

                _synchronizationChainDataService.Add(synchronizationRegistryCombinedBlock);
            }
        }