Beispiel #1
0
        protected override Memory <byte> ParseSynchronization(ushort version, Memory <byte> spanBody, out SynchronizationBlockBase synchronizationBlockBase)
        {
            if (version == 1)
            {
                ushort   round           = BinaryPrimitives.ReadUInt16LittleEndian(spanBody.Span);
                byte     numberOfSigners = spanBody.Span[2];
                byte[][] signers         = new byte[numberOfSigners][];
                byte[][] signatures      = new byte[numberOfSigners][];

                for (int i = 0; i < numberOfSigners; i++)
                {
                    signers[i]    = spanBody.Slice(3 + (Globals.NODE_PUBLIC_KEY_SIZE + Globals.SIGNATURE_SIZE) * i, Globals.NODE_PUBLIC_KEY_SIZE).ToArray();
                    signatures[i] = spanBody.Slice(3 + (Globals.NODE_PUBLIC_KEY_SIZE + Globals.SIGNATURE_SIZE) * i + Globals.NODE_PUBLIC_KEY_SIZE, Globals.SIGNATURE_SIZE).ToArray();
                }

                SynchronizationBlockBase synchronizationConfirmedBlock = new SynchronizationConfirmedBlock()
                {
                    Round      = round,
                    PublicKeys = signers,
                    Signatures = signatures
                };

                synchronizationBlockBase = synchronizationConfirmedBlock;

                return(spanBody.Slice(3 + (Globals.NODE_PUBLIC_KEY_SIZE + Globals.SIGNATURE_SIZE) * numberOfSigners));
            }

            throw new BlockVersionNotSupportedException(version, BlockType);
        }
Beispiel #2
0
        public override void Start()
        {
            ulong index = _synchronizationContext.LastBlockDescriptor?.BlockHeight ?? 0;

            byte[] prevHash = _synchronizationContext.LastBlockDescriptor?.Hash;

            do
            {
                unchecked
                {
                    SynchronizationConfirmedBlock synchronizationConfirmedBlock = new SynchronizationConfirmedBlock()
                    {
                        SyncBlockHeight = index,
                        BlockHeight     = (ulong)++index,
                        Signer          = _key,
                        ReportedTime    = DateTime.Now,
                        Round           = 1,
                        HashPrev        = prevHash ?? new byte[Globals.DEFAULT_HASH_SIZE],
                        PowHash         = prevHash ?? new byte[Globals.DEFAULT_HASH_SIZE]
                    };

                    ISerializer signatureSupportSerializer = _serializersFactory.Create(synchronizationConfirmedBlock);
                    signatureSupportSerializer.SerializeFully();

                    prevHash = _hashCalculation.CalculateHash(synchronizationConfirmedBlock.RawData);

                    _communicationService.PostMessage(_keyTarget, signatureSupportSerializer);

                    _loadCountersService.SentMessages.Increment();
                }
            } while (!_cancellationToken.IsCancellationRequested);
        }
Beispiel #3
0
        public void ProcessBlock(PacketBase blockBase)
        {
            SynchronizationConfirmedBlock synchronizationBlock = blockBase as SynchronizationConfirmedBlock;

            if (synchronizationBlock != null)
            {
                _synchronizationBlocks.Add(synchronizationBlock);
            }
        }
Beispiel #4
0
        public override BlockBase Translate(SynchronizationBlock synchronizationBlock)
        {
            if (synchronizationBlock == null)
            {
                return(null);
            }

            SynchronizationConfirmedBlock synchronizationConfirmedBlock = null;

            IBlockParser blockParser = _blockParsersRepository.GetInstance(BlockTypes.Synchronization_ConfirmedBlock);

            synchronizationConfirmedBlock = blockParser.Parse(synchronizationBlock.BlockContent) as SynchronizationConfirmedBlock;

            return(synchronizationConfirmedBlock);
        }
Beispiel #5
0
        private void BroadcastConfirmation(ulong height, ushort round, byte[] prevHash, ulong syncBlockHeight, byte[] powValue)
        {
            //List<SynchronizationBlockRetransmissionV1> retransmittedSyncBlocks = _synchronizationBlocksByHeight[height].Where(r => _nodeContext.SyncGroupParticipants.Any(p => p.Key == r.Key)).Select(kv => kv.Value.First()).OrderBy(s => s.ConfirmationPublicKey.ToHexString()).ToList();

            SynchronizationConfirmedBlock synchronizationConfirmedBlock = new SynchronizationConfirmedBlock
            {
                BlockHeight     = height,
                HashPrev        = prevHash,        //TODO: does not seems too secure
                SyncBlockHeight = syncBlockHeight, //TODO: does not seems too secure
                PowHash         = powValue,        //TODO: does not seems too secure
                Round           = round,
                PublicKeys      = new byte[0][],   // retransmittedSyncBlocks.Select(b => b.ConfirmationPublicKey).ToArray(),
                Signatures      = new byte[0][]    //retransmittedSyncBlocks.Select(b => b.ConfirmationSignature).ToArray()
            };

            ISerializer confirmationBlockSerializer = _signatureSupportSerializersFactory.Create(synchronizationConfirmedBlock);

            _communicationService.PostMessage(_neighborhoodState.GetAllNeighbors(), confirmationBlockSerializer);
        }
Beispiel #6
0
        public override void Start()
        {
            ulong index = _synchronizationContext.LastBlockDescriptor?.BlockHeight ?? 0;

            string cmd = null;

            byte[] prevHash    = _synchronizationContext.LastBlockDescriptor?.Hash;
            byte[] prevPowHash = _proofOfWorkCalculation.CalculateHash(prevHash ?? new byte[Globals.DEFAULT_HASH_SIZE]);
            do
            {
                unchecked
                {
                    SynchronizationConfirmedBlock synchronizationConfirmedBlock = new SynchronizationConfirmedBlock()
                    {
                        SyncBlockHeight = index,
                        BlockHeight     = ++index,
                        Signer          = _key,
                        ReportedTime    = DateTime.Now,
                        Round           = 1,
                        HashPrev        = prevHash ?? new byte[Globals.DEFAULT_HASH_SIZE],
                        PowHash         = prevPowHash
                    };

                    ISerializer signatureSupportSerializer = _signatureSupportSerializersFactory.Create(synchronizationConfirmedBlock);
                    signatureSupportSerializer.FillBodyAndRowBytes();

                    prevHash    = _hashCalculation.CalculateHash(synchronizationConfirmedBlock.RawData);
                    prevPowHash = _proofOfWorkCalculation.CalculateHash(prevHash);

                    _communicationService.PostMessage(_keyTarget, signatureSupportSerializer);

                    _loadCountersService.SentMessages.Increment();
                }

                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");
        }
Beispiel #7
0
        protected override void InitializeInner()
        {
            _logger.Info("Starting Synchronization Initializer");

            try
            {
                SynchronizationConfirmedBlock synchronizationConfirmedBlock = (SynchronizationConfirmedBlock)_chainDataService.GetAllLastBlocksByType(BlockTypes.Synchronization_ConfirmedBlock).Single();

                if (synchronizationConfirmedBlock != null)
                {
                    _synchronizationContext.UpdateLastSyncBlockDescriptor(new SynchronizationDescriptor(synchronizationConfirmedBlock.BlockHeight, _hashCalculation.CalculateHash(synchronizationConfirmedBlock.RawData), synchronizationConfirmedBlock.ReportedTime, DateTime.Now, synchronizationConfirmedBlock.Round));
                }

                SynchronizationRegistryCombinedBlock combinedBlock = (SynchronizationRegistryCombinedBlock)_chainDataService.GetAllLastBlocksByType(BlockTypes.Synchronization_RegistryCombinationBlock).Single();
                if (combinedBlock != null)
                {
                    _synchronizationContext.LastRegistrationCombinedBlockHeight = combinedBlock.BlockHeight;
                }
            }
            finally
            {
                _logger.Info("Synchronization Initializer completed");
            }
        }
Beispiel #8
0
        public void SynchronizationConfirmedBlockParserTest()
        {
            byte signersCount = 10;

            byte[] body;
            ushort round           = 1;
            ulong  syncBlockHeight = 1;
            uint   nonce           = 4;
            ushort version         = 1;
            ulong  blockHeight     = 9;

            byte[][] expectedSignerPKs        = new byte[signersCount][];
            byte[][] expectedSignerSignatures = new byte[signersCount][];

            DateTime expectedDateTime = DateTime.Now;

            byte[] powHash  = BinaryHelper.GetPowHash(1234);
            byte[] prevHash = BinaryHelper.GetDefaultHash(1234);

            for (int i = 0; i < signersCount; i++)
            {
                byte[] privateSignerKey = ConfidentialAssetsHelper.GetRandomSeed();
                Ed25519.KeyPairFromSeed(out byte[] publicSignerKey, out byte[] expandedSignerKey, privateSignerKey);

                expectedSignerPKs[i] = publicSignerKey;

                byte[] roundBytes = BitConverter.GetBytes(round);

                byte[] signerSignature = Ed25519.Sign(roundBytes, expandedSignerKey);

                expectedSignerSignatures[i] = signerSignature;
            }

            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter bw = new BinaryWriter(ms))
                {
                    bw.Write(expectedDateTime.ToBinary());
                    bw.Write(round);
                    bw.Write(signersCount);

                    for (int i = 0; i < signersCount; i++)
                    {
                        bw.Write(expectedSignerPKs[i]);
                        bw.Write(expectedSignerSignatures[i]);
                    }
                }

                body = ms.ToArray();
            }

            byte[] packet = BinaryHelper.GetSignedPacket(
                PacketType.Synchronization,
                syncBlockHeight,
                nonce, powHash, version,
                BlockTypes.Synchronization_ConfirmedBlock, blockHeight, prevHash, body, _privateKey, out byte[] expectedSignature);
            string packetExpectedString = packet.ToHexString();

            SynchronizationConfirmedBlockParser synchronizationConfirmedBlockParser = new SynchronizationConfirmedBlockParser(_identityKeyProvidersRegistry);
            SynchronizationConfirmedBlock       block = (SynchronizationConfirmedBlock)synchronizationConfirmedBlockParser.Parse(packet);

            string packetActualString = block.RawData.ToHexString();

            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(prevHash, block.HashPrev);
            Assert.Equal(expectedDateTime, block.ReportedTime);
            Assert.Equal(round, block.Round);

            for (int i = 0; i < signersCount; i++)
            {
                Assert.Equal(expectedSignerPKs[i], block.PublicKeys[i]);
                Assert.Equal(expectedSignerSignatures[i], block.Signatures[i]);
            }

            Assert.Equal(_publicKey, block.Signer.Value.ToArray());
            Assert.Equal(expectedSignature, block.Signature.ToArray());
        }
Beispiel #9
0
        public void SynchronizationConfirmedBlockSerializerTest()
        {
            ulong syncBlockHeight = 1;
            uint  nonce           = 4;

            byte[] powHash     = BinaryBuilder.GetPowHash(1234);
            ushort version     = 1;
            ulong  blockHeight = 9;

            byte[] prevHash = BinaryBuilder.GetDefaultHash(1234);

            ushort round        = 1;
            byte   signersCount = 10;

            byte[] body = new byte[11 + Globals.NODE_PUBLIC_KEY_SIZE * signersCount + Globals.SIGNATURE_SIZE * signersCount];

            byte[][] expectedSignerPKs        = new byte[signersCount][];
            byte[][] expectedSignerSignatures = new byte[signersCount][];

            DateTime expectedDateTime = DateTime.Now;


            for (int i = 0; i < signersCount; i++)
            {
                byte[] privateSignerKey = CryptoHelper.GetRandomSeed();

                Ed25519.KeyPairFromSeed(out byte[] publicSignerKey, out byte[] expandedSignerKey, privateSignerKey);

                expectedSignerPKs[i] = publicSignerKey;

                byte[] roundBytes      = BitConverter.GetBytes(round);
                byte[] signerSignature = Ed25519.Sign(roundBytes, expandedSignerKey);

                expectedSignerSignatures[i] = signerSignature;
            }

            using (MemoryStream ms = new MemoryStream(body))
            {
                using (BinaryWriter bw = new BinaryWriter(ms))
                {
                    bw.Write(expectedDateTime.ToBinary());
                    bw.Write(round);
                    bw.Write(signersCount);

                    for (int i = 0; i < signersCount; i++)
                    {
                        bw.Write(expectedSignerPKs[i]);
                        bw.Write(expectedSignerSignatures[i]);
                    }
                }
            }

            byte[] expectedPacket = BinaryBuilder.GetSignedPacket(
                PacketType.Synchronization,
                syncBlockHeight,
                nonce, powHash, version,
                BlockTypes.Synchronization_ConfirmedBlock, blockHeight, prevHash, body, _privateKey, out byte[] expectedSignature);

            SynchronizationConfirmedBlock block = new SynchronizationConfirmedBlock()
            {
                SyncBlockHeight = syncBlockHeight,
                BlockHeight     = blockHeight,
                Nonce           = nonce,
                PowHash         = powHash,
                HashPrev        = prevHash,
                ReportedTime    = expectedDateTime,
                Round           = round,
                PublicKeys      = new byte[signersCount][],
                Signatures      = new byte[signersCount][]
            };

            for (int i = 0; i < signersCount; i++)
            {
                block.PublicKeys[i] = expectedSignerPKs[i];
                block.Signatures[i] = expectedSignerSignatures[i];
            }

            SynchronizationConfirmedBlockSerializer serializer = new SynchronizationConfirmedBlockSerializer(_cryptoService, _identityKeyProvidersRegistry, _hashCalculationRepository);

            serializer.Initialize(block);

            byte[] actualPacket = serializer.GetBytes();

            Assert.Equal(expectedPacket, actualPacket);
        }