Example #1
0
        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");
        }
Example #2
0
        public async void Update()
        {
            if (_isUpdating)
            {
                return;
            }

            lock (_sync)
            {
                if (_isUpdating)
                {
                    return;
                }

                _isUpdating = true;
            }

            BulkUpdate bulkUpdate = new BulkUpdate();

            try
            {
                ulong lastUpdatedSyncHeight = 0;

                AsyncServerStreamingCall <SyncBlockDescriptor> serverStreamingCall = _syncManagerClient.GetDeltaSyncBlocks(new ByHeightRequest {
                    Height = _lastUpdatedSyncHeight
                });
                while (await serverStreamingCall.ResponseStream.MoveNext())
                {
                    SyncBlockDescriptor syncBlockDescriptor = serverStreamingCall.ResponseStream.Current;
                    if (syncBlockDescriptor.Height > _lastUpdatedSyncHeight)
                    {
                        bulkUpdate.SyncBlockInfos.Add(new Models.SyncBlockInfo {
                            SyncBlockHeight = syncBlockDescriptor.Height
                        });
                        if (lastUpdatedSyncHeight < syncBlockDescriptor.Height)
                        {
                            lastUpdatedSyncHeight = syncBlockDescriptor.Height;
                        }
                    }
                }

                if (_lastUpdatedSyncHeight < lastUpdatedSyncHeight)
                {
                    _lastUpdatedSyncHeight = lastUpdatedSyncHeight;
                }

                ulong lastUpdatedCombinedBlockHeight = 0;
                AsyncServerStreamingCall <CombinedRegistryBlockInfo> serverStreamingCall2 = _syncManagerClient.GetCombinedRegistryBlocksInfoSinceHeight(new ByHeightRequest {
                    Height = _lastUpdatedCombinedBlockHeight
                });
                while (await serverStreamingCall2.ResponseStream.MoveNext())
                {
                    CombinedRegistryBlockInfo combinedRegistryBlockInfo = serverStreamingCall2.ResponseStream.Current;

                    if (combinedRegistryBlockInfo.Height > _lastUpdatedCombinedBlockHeight)
                    {
                        bulkUpdate.CombinedBlockInfos.Add(
                            new CombinedBlockInfo
                        {
                            SyncBlockHeight             = combinedRegistryBlockInfo.SyncBlockHeight,
                            BlockHeight                 = combinedRegistryBlockInfo.Height,
                            CombinedRegistryBlocksCount = combinedRegistryBlockInfo.CombinedRegistryBlocksCount,
                            RegistryFullBlockInfos      = combinedRegistryBlockInfo.BlockDescriptors.Select(
                                b => new RegistryFullBlockInfo
                            {
                                SyncBlockHeight   = b.SyncBlockHeight,
                                Round             = b.Round,
                                TransactionsCount = b.TransactionsCount
                            }).ToList()
                        });

                        if (lastUpdatedCombinedBlockHeight < combinedRegistryBlockInfo.Height)
                        {
                            lastUpdatedCombinedBlockHeight = combinedRegistryBlockInfo.Height;
                        }
                    }
                }

                if (_lastUpdatedCombinedBlockHeight < lastUpdatedCombinedBlockHeight)
                {
                    _lastUpdatedCombinedBlockHeight = lastUpdatedCombinedBlockHeight;
                }
            }
            finally
            {
                _isUpdating = false;
            }

            foreach (IObserver <BulkUpdate> observer in _observers)
            {
                observer.OnNext(bulkUpdate);
            }
        }
Example #3
0
        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");
        }