Example #1
0
        public async Task Parallel_TransactionWithoutContract()
        {
            var chain = await _blockchainService.GetChainAsync();

            var tokenAmount    = _transactionCount / _groupCount;
            var accountAddress = await _accountService.GetAccountAsync();

            var(prepareTransactions, keyPairs) = await _parallelTestHelper.PrepareTokenForParallel(_groupCount, tokenAmount);

            var transactionWithoutContract = _parallelTestHelper.GenerateTransaction(accountAddress,
                                                                                     SampleAddress.AddressList[0], "Transfer", new Empty());
            var transactionHash = transactionWithoutContract.GetHash();
            var signature       = await _accountService.SignAsync(transactionHash.ToByteArray());

            transactionWithoutContract.Signature = ByteString.CopyFrom(signature);
            prepareTransactions.Add(transactionWithoutContract);

            var cancellableTransaction = _parallelTestHelper.GenerateTransaction(accountAddress,
                                                                                 _parallelTestHelper.BasicFunctionWithParallelContractAddress, "QueryWinMoney",
                                                                                 new Empty());

            signature = await _accountService.SignAsync(cancellableTransaction.GetHash().ToByteArray());

            cancellableTransaction.Signature = ByteString.CopyFrom(signature);
            var cancellableTransactions = new List <Transaction> {
                cancellableTransaction
            };
            var allTransactions = prepareTransactions.Concat(cancellableTransactions).ToList();

            await _parallelTestHelper.BroadcastTransactions(allTransactions);

            var groupedTransactions = await _grouper.GroupAsync(
                new ChainContext { BlockHash = chain.BestChainHash, BlockHeight = chain.BestChainHeight },
                prepareTransactions);

            groupedTransactions.Parallelizables.Count.ShouldBe(1);
            groupedTransactions.NonParallelizables.Count.ShouldBe(0);
            groupedTransactions.TransactionsWithoutContract.Count.ShouldBe(1);
            var transaction = groupedTransactions.TransactionsWithoutContract[0];

            transaction.GetHash().ShouldBe(transactionHash);

            var block = _parallelTestHelper.GenerateBlock(chain.BestChainHash, chain.BestChainHeight, allTransactions);

            block = await _blockExecutingService.ExecuteBlockAsync(block.Header, prepareTransactions,
                                                                   cancellableTransactions, CancellationToken.None);

            await _blockchainService.AddTransactionsAsync(allTransactions);

            await _blockchainService.AddBlockAsync(block);

            await _blockAttachService.AttachBlockAsync(block);

            var transactionResult =
                await _transactionResultManager.GetTransactionResultAsync(transactionHash,
                                                                          block.Header.GetHash());

            transactionResult.Status.ShouldBe(TransactionResultStatus.Failed);
            transactionResult.Error.ShouldContain("Invalid contract address");

            var systemTransactions = await _parallelTestHelper.GenerateTransferTransactions(1);

            cancellableTransactions = await _parallelTestHelper.GenerateTransactionsWithoutConflict(keyPairs, tokenAmount);

            transactionWithoutContract = _parallelTestHelper.GenerateTransaction(accountAddress,
                                                                                 SampleAddress.AddressList[0], "Transfer", new Empty());
            transactionHash = transactionWithoutContract.GetHash();
            signature       = await _accountService.SignAsync(transactionHash.ToByteArray());

            transactionWithoutContract.Signature = ByteString.CopyFrom(signature);
            cancellableTransactions.Add(transactionWithoutContract);

            var allTransaction = systemTransactions.Concat(cancellableTransactions).ToList();
            await _parallelTestHelper.BroadcastTransactions(allTransaction);

            groupedTransactions = await _grouper.GroupAsync(new ChainContext { BlockHash = block.GetHash(), BlockHeight = block.Height },
                                                            cancellableTransactions);

            groupedTransactions.Parallelizables.Count.ShouldBe(_groupCount);
            groupedTransactions.NonParallelizables.Count.ShouldBe(0);
            groupedTransactions.TransactionsWithoutContract.Count.ShouldBe(1);
            transaction = groupedTransactions.TransactionsWithoutContract[0];
            transaction.GetHash().ShouldBe(transactionHash);

            block = _parallelTestHelper.GenerateBlock(block.GetHash(), block.Height, allTransaction);
            block = await _blockExecutingService.ExecuteBlockAsync(block.Header, systemTransactions,
                                                                   cancellableTransactions, CancellationToken.None);

            await _blockchainService.AddTransactionsAsync(allTransactions);

            await _blockchainService.AddBlockAsync(block);

            await _blockAttachService.AttachBlockAsync(block);

            var codeRemarks = await _codeRemarksManager.GetCodeRemarksAsync(Hash.FromRawBytes(_parallelTestHelper.TokenContractCode));

            codeRemarks.ShouldBeNull();

            groupedTransactions = await _grouper.GroupAsync(new ChainContext { BlockHash = block.GetHash(), BlockHeight = block.Height },
                                                            cancellableTransactions);

            groupedTransactions.Parallelizables.Count.ShouldBe(_groupCount);
            groupedTransactions.NonParallelizables.Count.ShouldBe(0);

            block.TransactionIds.Count().ShouldBe(allTransaction.Count);
            block.TransactionIds.ShouldContain(transactionHash);

            transactionResult =
                await _transactionResultManager.GetTransactionResultAsync(transactionHash,
                                                                          block.Header.GetHash());

            transactionResult.Status.ShouldBe(TransactionResultStatus.Failed);
            transactionResult.Error.ShouldContain("Invalid contract address");
        }
Example #2
0
        public async Task WrongParallelTest()
        {
            var chain = await _blockchainService.GetChainAsync();

            await _blockchainService.SetIrreversibleBlockAsync(chain, chain.BestChainHeight, chain.BestChainHash);

            //prepare token for tx verify
            var(tokenTransactions, groupUsers) =
                await _parallelTestHelper.PrepareTokenForParallel(_groupCount, 1000_00000000);

            await _parallelTestHelper.BroadcastTransactions(tokenTransactions);

            var prepareBlock =
                _parallelTestHelper.GenerateBlock(chain.BestChainHash, chain.BestChainHeight, tokenTransactions);

            prepareBlock =
                await _blockExecutingService.ExecuteBlockAsync(prepareBlock.Header,
                                                               tokenTransactions);

            await _blockchainService.AddBlockAsync(prepareBlock);

            await _blockAttachService.AttachBlockAsync(prepareBlock);

            chain = await _blockchainService.GetChainAsync();

            var transactions =
                _parallelTestHelper.GenerateBasicFunctionWithParallelTransactions(groupUsers, _transactionCount);
            await _parallelTestHelper.BroadcastTransactions(transactions);

            var otherTransactions = _parallelTestHelper.GenerateBasicFunctionWithParallelTransactions(groupUsers, _transactionCount);
            await _parallelTestHelper.BroadcastTransactions(otherTransactions);

            var transferTransactions = await _parallelTestHelper.GenerateTransferTransactions(16);

            await _parallelTestHelper.BroadcastTransactions(transferTransactions);

            var poolSize = await _txHub.GetAllTransactionCountAsync();

            poolSize.ShouldBe(transactions.Count * 2 + transferTransactions.Count);

            var groupedTransactions = await _grouper.GroupAsync(
                new ChainContext { BlockHash = chain.BestChainHash, BlockHeight = chain.BestChainHeight },
                transactions);

            groupedTransactions.Parallelizables.Count.ShouldBe(_transactionCount);
            groupedTransactions.NonParallelizables.Count.ShouldBe(0);
            for (var i = 0; i < transactions.Count; i++)
            {
                transactions[i].GetHash().ShouldBe(groupedTransactions.Parallelizables[i][0].GetHash());
            }

            var otherGroupedTransactions = await _grouper.GroupAsync(
                new ChainContext { BlockHash = chain.BestChainHash, BlockHeight = chain.BestChainHeight },
                otherTransactions);

            otherGroupedTransactions.Parallelizables.Count.ShouldBe(_transactionCount);
            otherGroupedTransactions.NonParallelizables.Count.ShouldBe(0);

            var groupedTransferTransactions = await _grouper.GroupAsync(
                new ChainContext { BlockHash = chain.BestChainHash, BlockHeight = chain.BestChainHeight },
                transferTransactions);

            groupedTransferTransactions.Parallelizables.Count.ShouldBe(1);
            groupedTransferTransactions.Parallelizables[0].Count.ShouldBe(transferTransactions.Count);
            groupedTransferTransactions.NonParallelizables.Count.ShouldBe(0);

            var block = _parallelTestHelper.GenerateBlock(chain.BestChainHash, chain.BestChainHeight, transactions);

            block = await _blockExecutingService.ExecuteBlockAsync(block.Header, transactions);

            block.TransactionIds.Count().ShouldBe(_transactionCount);
            var transactionResults = await GetTransactionResultsAsync(block.Body.TransactionIds.ToList(), block.Header);

            transactionResults.Count(t => t.Status == TransactionResultStatus.Mined).ShouldBe(_groupCount);
            transactionResults.Count(t => t.Status == TransactionResultStatus.Conflict).ShouldBe(_groupCount);
            await _blockchainService.AddBlockAsync(block);

            await _blockAttachService.AttachBlockAsync(block);

            var accountAddress = await _accountService.GetAccountAsync();

            foreach (var transaction in transactions)
            {
                var param = IncreaseWinMoneyInput.Parser.ParseFrom(transaction.Params);
                var input = new QueryTwoUserWinMoneyInput
                {
                    First  = param.First,
                    Second = param.Second
                };
                var queryTransaction = _parallelTestHelper.GenerateTransaction(accountAddress,
                                                                               ParallelTestHelper.BasicFunctionWithParallelContractAddress,
                                                                               nameof(BasicFunctionWithParallelContract.QueryTwoUserWinMoney), input);
                var byteString = await _parallelTestHelper.ExecuteReadOnlyAsync(queryTransaction, block.GetHash(), block.Height);

                var output = TwoUserMoneyOut.Parser.ParseFrom(byteString);
                output.FirstInt64Value.ShouldBe(1);
                var result = transactionResults.First(t => t.TransactionId == transaction.GetHash());
                if (result.Status == TransactionResultStatus.Mined)
                {
                    output.SecondInt64Value.ShouldBe(1);
                }
                else if (result.Status == TransactionResultStatus.Conflict)
                {
                    output.SecondInt64Value.ShouldBe(0);
                }
            }

            var codeRemark =
                await _contractRemarksService.GetCodeRemarkAsync(new ChainContext
            {
                BlockHash   = block.GetHash(),
                BlockHeight = block.Height
            }, ParallelTestHelper.BasicFunctionWithParallelContractAddress,
                                                                 Hash.FromRawBytes(_parallelTestHelper.BasicFunctionWithParallelContractCode));

            codeRemark.CodeHash.ShouldBe(Hash.FromRawBytes(_parallelTestHelper.BasicFunctionWithParallelContractCode));
            codeRemark.NonParallelizable.ShouldBeTrue();

            groupedTransactions = await _grouper.GroupAsync(
                new ChainContext { BlockHash = block.GetHash(), BlockHeight = block.Height },
                transactions);

            groupedTransactions.Parallelizables.Count.ShouldBe(0);
            groupedTransactions.NonParallelizables.Count.ShouldBe(_transactionCount);

            otherGroupedTransactions = await _grouper.GroupAsync(
                new ChainContext { BlockHash = block.GetHash(), BlockHeight = block.Height },
                otherTransactions);

            otherGroupedTransactions.Parallelizables.Count.ShouldBe(0);
            otherGroupedTransactions.NonParallelizables.Count.ShouldBe(_transactionCount);

            groupedTransferTransactions = await _grouper.GroupAsync(
                new ChainContext { BlockHash = chain.BestChainHash, BlockHeight = chain.BestChainHeight },
                transferTransactions);

            groupedTransferTransactions.Parallelizables.Count.ShouldBe(1);
            groupedTransferTransactions.Parallelizables[0].Count.ShouldBe(transferTransactions.Count);
            groupedTransferTransactions.NonParallelizables.Count.ShouldBe(0);

            poolSize = await _txHub.GetAllTransactionCountAsync();

            poolSize.ShouldBe(transactions.Count * 2 + transferTransactions.Count - block.TransactionIds.Count());
        }
        public async Task WrongParallelTest()
        {
            var chain = await _blockchainService.GetChainAsync();

            await _blockchainService.SetIrreversibleBlockAsync(chain, chain.BestChainHeight, chain.BestChainHash);

            //prepare token for tx verify
            var(tokenTransactions, groupUsers) =
                await _parallelTestHelper.PrepareTokenForParallel(_groupCount, 1000_00000000);

            await _parallelTestHelper.BroadcastTransactions(tokenTransactions);

            var prepareBlock =
                _parallelTestHelper.GenerateBlock(chain.BestChainHash, chain.BestChainHeight, tokenTransactions);

            prepareBlock =
                (await _blockExecutingService.ExecuteBlockAsync(prepareBlock.Header,
                                                                tokenTransactions)).Block;
            await _blockchainService.AddBlockAsync(prepareBlock);

            await _blockAttachService.AttachBlockAsync(prepareBlock);

            chain = await _blockchainService.GetChainAsync();

            var transactions =
                _parallelTestHelper.GenerateBasicFunctionWithParallelTransactions(groupUsers, _transactionCount);
            var transferTransaction = await _parallelTestHelper.GenerateTransferTransaction(Address.FromPublicKey(groupUsers[1].PublicKey)
                                                                                            , "ELF", 10);

            transactions.Add(transferTransaction);
            await _parallelTestHelper.BroadcastTransactions(transactions);

            var otherTransactions        = _parallelTestHelper.GenerateBasicFunctionWithParallelTransactions(groupUsers, _transactionCount);
            var otherTransferTransaction = await _parallelTestHelper.GenerateTransferTransaction(Address.FromPublicKey(groupUsers[2].PublicKey)
                                                                                                 , "ELF", 10);

            otherTransactions.Add(otherTransferTransaction);
            await _parallelTestHelper.BroadcastTransactions(otherTransactions);

            var transferTransactions = await _parallelTestHelper.GenerateTransferTransactions(16);

            await _parallelTestHelper.BroadcastTransactions(transferTransactions);

            var poolSize = (await _txHub.GetTransactionPoolStatusAsync()).AllTransactionCount;

            poolSize.ShouldBe(transactions.Count * 2 + transferTransactions.Count);

            var groupedTransactions = await _grouper.GroupAsync(
                new ChainContext { BlockHash = chain.BestChainHash, BlockHeight = chain.BestChainHeight },
                transactions);

            groupedTransactions.Parallelizables.Count.ShouldBe(_transactionCount + 1);
            groupedTransactions.NonParallelizables.Count.ShouldBe(0);
            for (var i = 0; i < transactions.Count; i++)
            {
                transactions[i].GetHash().ShouldBe(groupedTransactions.Parallelizables[i][0].GetHash());
            }

            var otherGroupedTransactions = await _grouper.GroupAsync(
                new ChainContext { BlockHash = chain.BestChainHash, BlockHeight = chain.BestChainHeight },
                otherTransactions);

            otherGroupedTransactions.Parallelizables.Count.ShouldBe(_transactionCount + 1);
            otherGroupedTransactions.NonParallelizables.Count.ShouldBe(0);

            var groupedTransferTransactions = await _grouper.GroupAsync(
                new ChainContext { BlockHash = chain.BestChainHash, BlockHeight = chain.BestChainHeight },
                transferTransactions);

            groupedTransferTransactions.Parallelizables.Count.ShouldBe(1);
            groupedTransferTransactions.Parallelizables[0].Count.ShouldBe(transferTransactions.Count);
            groupedTransferTransactions.NonParallelizables.Count.ShouldBe(0);

            _localEventBus.Subscribe <ConflictingTransactionsFoundInParallelGroupsEvent>(e =>
            {
                e.ConflictingSets.Count.ShouldBe(_groupCount + 1);
                e.ExistingSets.Count.ShouldBe(_groupCount);
                return(Task.CompletedTask);
            });

            var block = _parallelTestHelper.GenerateBlock(chain.BestChainHash, chain.BestChainHeight, transactions);

            block = (await _blockExecutingService.ExecuteBlockAsync(block.Header, transactions)).Block;
            block.TransactionIds.Count().ShouldBe(_transactionCount + 1);

            var transactionResults = await GetTransactionResultsAsync(block.Body.TransactionIds.ToList(), block.Header);

            transactionResults.Count(t => t.Status == TransactionResultStatus.Mined).ShouldBe(_groupCount);
            transactionResults.Count(t => t.Status == TransactionResultStatus.Conflict).ShouldBe(_groupCount + 1);
            await _blockchainService.AddBlockAsync(block);

            await _blockAttachService.AttachBlockAsync(block);

            var accountAddress = await _accountService.GetAccountAsync();

            var chainContext = new ChainContext
            {
                BlockHash   = block.GetHash(),
                BlockHeight = block.Height
            };
            var tokenContractAddress =
                await _smartContractAddressService.GetAddressByContractNameAsync(chainContext, TokenSmartContractAddressNameProvider.StringName);

            var nonparallelContractCode =
                await _nonparallelContractCodeProvider.GetNonparallelContractCodeAsync(chainContext,
                                                                                       tokenContractAddress);

            nonparallelContractCode.ShouldBeNull();

            foreach (var transaction in transactions)
            {
                if (transaction.To == tokenContractAddress)
                {
                    continue;
                }
                var param = IncreaseWinMoneyInput.Parser.ParseFrom(transaction.Params);
                var input = new QueryTwoUserWinMoneyInput
                {
                    First  = param.First,
                    Second = param.Second
                };
                var queryTransaction = _parallelTestHelper.GenerateTransaction(accountAddress,
                                                                               ParallelTestHelper.BasicFunctionWithParallelContractAddress,
                                                                               nameof(BasicFunctionWithParallelContract.QueryTwoUserWinMoney), input);
                var byteString = await _parallelTestHelper.ExecuteReadOnlyAsync(queryTransaction, block.GetHash(), block.Height);

                var output = TwoUserMoneyOut.Parser.ParseFrom(byteString);
                output.FirstInt64Value.ShouldBe(1);
                var result = transactionResults.First(t => t.TransactionId == transaction.GetHash());
                if (result.Status == TransactionResultStatus.Mined)
                {
                    output.SecondInt64Value.ShouldBe(1);
                }
                else if (result.Status == TransactionResultStatus.Conflict)
                {
                    output.SecondInt64Value.ShouldBe(0);
                }
            }

            nonparallelContractCode =
                await _nonparallelContractCodeProvider.GetNonparallelContractCodeAsync(new ChainContext
            {
                BlockHash   = block.GetHash(),
                BlockHeight = block.Height
            }, ParallelTestHelper.BasicFunctionWithParallelContractAddress);

            nonparallelContractCode.CodeHash.ShouldBe(HashHelper.ComputeFrom(_parallelTestHelper.BasicFunctionWithParallelContractCode));

            var blockStateSet = await _blockStateSetManger.GetBlockStateSetAsync(block.GetHash());

            blockStateSet.Changes.Count.ShouldBeGreaterThan(0);
            var blockExecutedData = blockStateSet.BlockExecutedData.First();
            var versionedState    = await _versionedStates.GetAsync(blockExecutedData.Key);

            versionedState.ShouldBeNull();

            await _blockchainStateService.MergeBlockStateAsync(block.Height, block.GetHash());

            blockStateSet = await _blockStateSetManger.GetBlockStateSetAsync(block.GetHash());

            blockStateSet.ShouldBeNull();

            nonparallelContractCode =
                await _nonparallelContractCodeProvider.GetNonparallelContractCodeAsync(new ChainContext
            {
                BlockHash   = block.GetHash(),
                BlockHeight = block.Height
            }, ParallelTestHelper.BasicFunctionWithParallelContractAddress);

            nonparallelContractCode.CodeHash.ShouldBe(HashHelper.ComputeFrom(_parallelTestHelper.BasicFunctionWithParallelContractCode));

            versionedState = await _versionedStates.GetAsync(blockExecutedData.Key);

            versionedState.Key.ShouldBe(blockExecutedData.Key);
            versionedState.Value.ShouldBe(blockExecutedData.Value);

            groupedTransactions = await _grouper.GroupAsync(
                new ChainContext { BlockHash = block.GetHash(), BlockHeight = block.Height },
                transactions);

            groupedTransactions.Parallelizables.Count.ShouldBe(1);
            groupedTransactions.Parallelizables[0][0].To.ShouldBe(tokenContractAddress);
            groupedTransactions.NonParallelizables.Count.ShouldBe(_transactionCount);

            otherGroupedTransactions = await _grouper.GroupAsync(
                new ChainContext { BlockHash = block.GetHash(), BlockHeight = block.Height },
                otherTransactions);

            otherGroupedTransactions.Parallelizables.Count.ShouldBe(1);
            otherGroupedTransactions.Parallelizables[0][0].To.ShouldBe(tokenContractAddress);
            otherGroupedTransactions.NonParallelizables.Count.ShouldBe(_transactionCount);

            groupedTransferTransactions = await _grouper.GroupAsync(
                new ChainContext { BlockHash = chain.BestChainHash, BlockHeight = chain.BestChainHeight },
                transferTransactions);

            groupedTransferTransactions.Parallelizables.Count.ShouldBe(1);
            groupedTransferTransactions.Parallelizables[0].Count.ShouldBe(transferTransactions.Count);
            groupedTransferTransactions.NonParallelizables.Count.ShouldBe(0);

            poolSize = (await _txHub.GetTransactionPoolStatusAsync()).AllTransactionCount;

            poolSize.ShouldBe(transactions.Count * 2 + transferTransactions.Count - block.TransactionIds.Count());
        }