Esempio n. 1
0
        public async Task <ByteString> PrepareExtraDataForNextMiningAsync(Hash blockHash, long blockHeight)
        {
            var utcNow = TimestampHelper.GetUtcNow();
            var indexingProposalStatusList = await GetIndexingProposalStatusAsync(blockHash, blockHeight, utcNow);

            var toBeReleasedChainIdList = FindToBeReleasedChainIdList(indexingProposalStatusList, utcNow);

            if (toBeReleasedChainIdList.Count > 0)
            {
                // release pending proposal and unable to propose anything if it is ready
                _transactionInputForBlockMiningDataProvider.AddTransactionInputForBlockMining(blockHash,
                                                                                              new CrossChainTransactionInput
                {
                    PreviousBlockHeight = blockHeight,
                    MethodName          =
                        nameof(CrossChainContractImplContainer.CrossChainContractImplStub
                               .ReleaseCrossChainIndexingProposal),
                    Value = new ReleaseCrossChainIndexingProposalInput {
                        ChainIdList = { toBeReleasedChainIdList }
                    }
                    .ToByteString()
                });
                var toBeReleasedCrossChainBlockData = AggregateToBeReleasedCrossChainData(
                    toBeReleasedChainIdList.Select(chainId =>
                                                   indexingProposalStatusList.ChainIndexingProposalStatus[chainId].ProposedCrossChainBlockData));
                return(toBeReleasedCrossChainBlockData.ExtractCrossChainExtraDataFromCrossChainBlockData());
            }

            var pendingChainIdList = FindPendingStatusChainIdList(indexingProposalStatusList, utcNow);

            var crossChainBlockData =
                await GetCrossChainBlockDataForNextMining(blockHash, blockHeight, new HashSet <int>(pendingChainIdList));

            if (!crossChainBlockData.IsNullOrEmpty())
            {
                _transactionInputForBlockMiningDataProvider.AddTransactionInputForBlockMining(blockHash,
                                                                                              new CrossChainTransactionInput
                {
                    PreviousBlockHeight = blockHeight,
                    MethodName          =
                        nameof(CrossChainContractImplContainer.CrossChainContractImplStub.ProposeCrossChainIndexing),
                    Value = crossChainBlockData.ToByteString()
                });
            }
            return(ByteString.Empty);
        }
        public async Task <ByteString> PrepareExtraDataForNextMiningAsync(Hash blockHash, long blockHeight)
        {
            if (!_transactionPackingService.IsTransactionPackingEnabled())
            {
                return(ByteString.Empty);
            }

            var pendingProposal = await _readerFactory.Create(blockHash, blockHeight)
                                  .GetPendingCrossChainIndexingProposal.CallAsync(new Empty());

            var utcNow = TimestampHelper.GetUtcNow();

            if (pendingProposal == null || pendingProposal.ExpiredTime <= utcNow)
            {
                // propose new cross chain indexing data if pending proposal is null or expired
                var crossChainBlockData = await GetCrossChainBlockDataForNextMining(blockHash, blockHeight);

                if (!crossChainBlockData.IsNullOrEmpty())
                {
                    _transactionInputForBlockMiningDataProvider.AddTransactionInputForBlockMining(blockHash,
                                                                                                  new CrossChainTransactionInput
                    {
                        PreviousBlockHeight = blockHeight,
                        MethodName          =
                            nameof(CrossChainContractContainer.CrossChainContractStub.ProposeCrossChainIndexing),
                        Value = crossChainBlockData.ToByteString()
                    });
                }
                return(ByteString.Empty);
            }

            if (!pendingProposal.ToBeReleased)
            {
                return(ByteString.Empty); // do nothing if pending proposal is not ready to be released
            }
            // release pending proposal and unable to propose anything if it is ready
            _transactionInputForBlockMiningDataProvider.AddTransactionInputForBlockMining(blockHash,
                                                                                          new CrossChainTransactionInput
            {
                PreviousBlockHeight = blockHeight,
                MethodName          =
                    nameof(CrossChainContractContainer.CrossChainContractStub.ReleaseCrossChainIndexing),
                Value = pendingProposal.ProposalId.ToByteString()
            });
            return(ExtractCrossChainExtraDataFromCrossChainBlockData(pendingProposal.ProposedCrossChainBlockData));
        }
Esempio n. 3
0
        public void TransactionInputForBlockMiningDataProvider_AddAndGet_Tests()
        {
            var blockHash = HashHelper.ComputeFrom("Random");
            var crossChainTransactionInput = new CrossChainTransactionInput
            {
                PreviousBlockHeight = 10
            };

            _transactionInputForBlockMiningDataProvider.AddTransactionInputForBlockMining(blockHash,
                                                                                          crossChainTransactionInput);

            {
                var actual = _transactionInputForBlockMiningDataProvider.GetTransactionInputForBlockMining(blockHash);
                actual.ShouldBe(crossChainTransactionInput);
            }

            {
                _transactionInputForBlockMiningDataProvider.ClearExpiredTransactionInput(11);
                var actual = _transactionInputForBlockMiningDataProvider.GetTransactionInputForBlockMining(blockHash);
                actual.ShouldBeNull();
            }
        }