示例#1
0
        public override Task <TransactionalBlockEssense> GetLastTransactionalBlock(TransactionalBlockRequest request, ServerCallContext context)
        {
            if (request.PublicKey == null)
            {
                throw new ArgumentNullException(nameof(request.PublicKey));
            }

            byte[] keyBytes = request.PublicKey.ToByteArray();

            if (keyBytes.Length != Globals.NODE_PUBLIC_KEY_SIZE)
            {
                throw new ArgumentException($"Public key size must be of {Globals.NODE_PUBLIC_KEY_SIZE} bytes");
            }

            IKey key = _identityKeyProvider.GetKey(keyBytes);
            TransactionalBlockBase transactionalBlockBase = (TransactionalBlockBase)_transactionalDataService.GetLastBlock(key);

            TransactionalBlockEssense transactionalBlockEssense = new TransactionalBlockEssense
            {
                Height = transactionalBlockBase?.BlockHeight ?? 0,
                //TODO: need to reconsider hash calculation here since it is potential point of DoS attack
                Hash          = ByteString.CopyFrom(transactionalBlockBase != null ? _hashCalculation.CalculateHash(transactionalBlockBase.RawData) : new byte[Globals.DEFAULT_HASH_SIZE]),
                UpToDateFunds = transactionalBlockBase?.UptodateFunds ?? 0
            };

            return(Task.FromResult(transactionalBlockEssense));
        }
示例#2
0
        private void FillHeightInfo(TransactionalBlockBase transactionalBlockBase)
        {
            TransactionalBlockEssense transactionalBlockEssense = _networkAdapter.GetLastBlock(_clientState.GetPublicKey());

            transactionalBlockBase.BlockHeight = transactionalBlockEssense.Height + 1;
            transactionalBlockBase.HashPrev    = transactionalBlockEssense.Hash.ToByteArray();
        }
示例#3
0
        private BlockBase CreateIssueAssetsBlock(string issuanceInfo, byte[][] assetIds, string[] assetInfos, ulong tagId)
        {
            TransactionalBlockEssense transactionalBlockEssense = _networkAdapter.GetLastBlock(_clientState.GetPublicKey());

            IssueAssetsBlock issueAssetsBlock = new IssueAssetsBlock
            {
                BlockHeight     = transactionalBlockEssense.Height + 1,
                TagId           = tagId,
                UptodateFunds   = transactionalBlockEssense.UpToDateFunds,
                AssetIds        = new byte[0][],
                AssetAmounts    = new ulong[0],
                IssuedAssetIds  = assetIds,
                IssuedAssetInfo = assetInfos,
                IssuanceInfo    = issuanceInfo
            };

            FillHeightInfo(issueAssetsBlock);
            FillSyncData(issueAssetsBlock);
            FillRawData(issueAssetsBlock);

            return(issueAssetsBlock);
        }
示例#4
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");
        }
示例#5
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");
        }