public async Task HandleNewIrreversibleBlockFoundAsync(NewIrreversibleBlockFoundEvent eventData) { var contractInfoCache = _smartContractExecutiveService.GetContractInfoCache(); var addresses = contractInfoCache.Where(c => c.Value <= eventData.BlockHeight).Select(c => c.Key).ToArray(); var transactionIds = _resourceCache.Where(c => c.Value.Address.IsIn(addresses) && c.Value.ResourceInfo.ParallelType != ParallelType.NonParallelizable).Select(c => c.Key); ClearResourceCache(transactionIds.Concat(_resourceCache .Where(c => c.Value.RefBlockNumber <= eventData.BlockHeight) .Select(c => c.Key)).Distinct()); _smartContractExecutiveService.ClearContractInfoCache(eventData.BlockHeight); await Task.CompletedTask; }
public async Task HandleNewIrreversibleBlockFoundAsync(NewIrreversibleBlockFoundEvent eventData) { var contractInfoCache = _smartContractExecutiveService.GetContractInfoCache(); var addresses = contractInfoCache.Where(c => c.Value <= eventData.BlockHeight).Select(c => c.Key).ToArray(); try { var transactionIds = _resourceCache.Where(c => c.Value.Address.IsIn(addresses) && c.Value.ResourceInfo.ParallelType != ParallelType.NonParallelizable).Select(c => c.Key); ClearResourceCache(transactionIds.Concat(_resourceCache .Where(c => c.Value.ResourceUsedBlockHeight <= eventData.BlockHeight) .Select(c => c.Key)).Distinct().ToList()); _smartContractExecutiveService.ClearContractInfoCache(eventData.BlockHeight); } catch (InvalidOperationException e) { Logger.LogError(e, "Unexpected case occured when clear resource info."); } await Task.CompletedTask; }
public async Task UpdateContract_With_Two_Different_Contract_Code() { var blockHeader = await _blockchainService.GetBestChainLastBlockHeaderAsync(); var startBlockHeight = blockHeader.Height; var startBlockHash = blockHeader.GetHash(); var basic11ContractStub = GetTestBasicUpdateContractStub(DefaultSenderKeyPair); //First branch { var transactionResult = (await BasicContractZeroStub.UpdateSmartContract.SendAsync( new Acs0.ContractUpdateInput { Address = BasicFunctionContractAddress, Code = ByteString.CopyFrom(Codes.Single(kv => kv.Key.EndsWith("BasicUpdate")).Value) } )).TransactionResult; transactionResult.Status.ShouldBe(TransactionResultStatus.Mined); transactionResult = (await basic11ContractStub.UpdateStopBet.SendAsync( new Empty())).TransactionResult; transactionResult.Status.ShouldBe(TransactionResultStatus.Mined); } //Second branch { var input = new Acs0.ContractUpdateInput { Address = BasicFunctionContractAddress, Code = ByteString.CopyFrom(Codes.Single(kv => kv.Key.EndsWith("BasicFunctionWithParallel")).Value) }.ToByteString(); var transaction = CreateTransaction(DefaultSender, ContractZeroAddress, nameof(BasicContractZeroStub.UpdateSmartContract), input, startBlockHeight, startBlockHash); var branchTwoBlock = await ExecuteAsync(transaction, startBlockHeight, startBlockHash); await _blockAttachService.AttachBlockAsync(branchTwoBlock); var basicFunctionContractStub = GetTestBasicFunctionContractStub(DefaultSenderKeyPair); await basicFunctionContractStub.QueryWinMoney.CallAsync(new Empty()); _smartContractExecutiveService.ClearContractInfoCache(100); var queryTwoUserWinMoneyInput = new QueryTwoUserWinMoneyInput { First = SampleAddress.AddressList[0], Second = SampleAddress.AddressList[1] }.ToByteString(); var queryTwoUserWinMoneyTransaction = CreateTransaction(DefaultSender, BasicFunctionContractAddress, "QueryTwoUserWinMoney", queryTwoUserWinMoneyInput, branchTwoBlock.Height, branchTwoBlock.GetHash()); branchTwoBlock = await ExecuteAsync(queryTwoUserWinMoneyTransaction, branchTwoBlock.Height, branchTwoBlock.GetHash()); await _blockAttachService.AttachBlockAsync(branchTwoBlock); var queryTwoUserWinMoneyTransactionResult = await _transactionResultManager.GetTransactionResultAsync(queryTwoUserWinMoneyTransaction.GetHash(), branchTwoBlock.Header.GetPreMiningHash()); queryTwoUserWinMoneyTransactionResult.Status.ShouldBe(TransactionResultStatus.Failed); queryTwoUserWinMoneyTransactionResult.Error.ShouldContain("Failed to find handler for QueryTwoUserWinMoney"); await _smartContractExecutiveService.InitContractInfoCacheAsync(); queryTwoUserWinMoneyInput = new QueryTwoUserWinMoneyInput { First = SampleAddress.AddressList[0], Second = SampleAddress.AddressList[1] }.ToByteString(); queryTwoUserWinMoneyTransaction = CreateTransaction(DefaultSender, BasicFunctionContractAddress, "QueryTwoUserWinMoney", queryTwoUserWinMoneyInput, branchTwoBlock.Height, branchTwoBlock.GetHash()); branchTwoBlock = await ExecuteAsync(queryTwoUserWinMoneyTransaction, branchTwoBlock.Height, branchTwoBlock.GetHash()); await _blockAttachService.AttachBlockAsync(branchTwoBlock); queryTwoUserWinMoneyTransactionResult = await _transactionResultManager.GetTransactionResultAsync(queryTwoUserWinMoneyTransaction .GetHash(), branchTwoBlock.Header.GetPreMiningHash()); queryTwoUserWinMoneyTransactionResult.Status.ShouldBe(TransactionResultStatus.Mined); } //Third branch { var updateStopBetTransaction = CreateTransaction(DefaultSender, BasicFunctionContractAddress, nameof(basic11ContractStub.UpdateStopBet), new Empty().ToByteString(), startBlockHeight, startBlockHash); var branchThreeBlock = await ExecuteAsync(updateStopBetTransaction, startBlockHeight, startBlockHash); await _blockAttachService.AttachBlockAsync(branchThreeBlock); var updateStopBetTransactionResult = await _transactionResultManager.GetTransactionResultAsync(updateStopBetTransaction.GetHash(), branchThreeBlock.Header.GetPreMiningHash()); updateStopBetTransactionResult.Status.ShouldBe(TransactionResultStatus.Failed); updateStopBetTransactionResult.Error.ShouldContain("Failed to find handler for UpdateStopBet."); var queryTwoUserWinMoneyInput = new QueryTwoUserWinMoneyInput { First = SampleAddress.AddressList[0], Second = SampleAddress.AddressList[1] }.ToByteString(); var queryTwoUserWinMoneyTransaction = CreateTransaction(DefaultSender, BasicFunctionContractAddress, "QueryTwoUserWinMoney", queryTwoUserWinMoneyInput, branchThreeBlock.Height, branchThreeBlock.GetHash()); branchThreeBlock = await ExecuteAsync(queryTwoUserWinMoneyTransaction, branchThreeBlock.Height, branchThreeBlock.GetHash()); await _blockAttachService.AttachBlockAsync(branchThreeBlock); var queryTwoUserWinMoneyTransactionResult = await _transactionResultManager.GetTransactionResultAsync(queryTwoUserWinMoneyTransaction.GetHash(), branchThreeBlock.Header.GetPreMiningHash()); queryTwoUserWinMoneyTransactionResult.Status.ShouldBe(TransactionResultStatus.Failed); queryTwoUserWinMoneyTransactionResult.Error.ShouldContain("Failed to find handler for QueryTwoUserWinMoney."); } }