Exemplo n.º 1
0
        public void SmartContractExecutiveProvider_Test()
        {
            var address = SampleAddress.AddressList[0];

            _smartContractExecutiveProvider.TryGetValue(address, out var executives).ShouldBeFalse();
            executives.ShouldBeNull();
            _smartContractExecutiveProvider.TryRemove(address, out executives).ShouldBeFalse();
            executives.ShouldBeNull();

            var executivePools = _smartContractExecutiveProvider.GetExecutivePools();

            executivePools.Count.ShouldBe(0);
            var pool = _smartContractExecutiveProvider.GetPool(address);

            pool.Count.ShouldBe(0);
            _smartContractExecutiveProvider.TryGetValue(address, out executives).ShouldBeTrue();
            executives.ShouldBe(pool);
            executivePools = _smartContractExecutiveProvider.GetExecutivePools();
            executivePools.Count.ShouldBe(1);

            _smartContractExecutiveProvider.TryRemove(address, out executives).ShouldBeTrue();
            executives.ShouldBe(pool);
            _smartContractExecutiveProvider.TryGetValue(address, out executives).ShouldBeFalse();
            executives.ShouldBeNull();
        }
        public async Task <IExecutive> GetExecutiveAsync(IChainContext chainContext, Address address)
        {
            if (address == null)
            {
                throw new ArgumentNullException(nameof(address));
            }

            var reg = await GetSmartContractRegistrationAsync(chainContext, address);

            var pool = _smartContractExecutiveProvider.GetPool(address, reg.CodeHash);

            if (!pool.TryTake(out var executive))
            {
                executive = await GetExecutiveAsync(reg);
            }

            return(executive);
        }
        public async Task <IExecutive> GetExecutiveAsync(IChainContext chainContext, Address address)
        {
            if (address == null)
            {
                throw new ArgumentNullException(nameof(address));
            }

            var pool = _smartContractExecutiveProvider.GetPool(address);
            var smartContractRegistration = await GetSmartContractRegistrationAsync(chainContext, address);

            if (!pool.TryTake(out var executive))
            {
                executive = await GetExecutiveAsync(smartContractRegistration);
            }
            else if (smartContractRegistration.CodeHash != executive.ContractHash)
            {
                _smartContractExecutiveProvider.TryRemove(address, out _);
                executive = await GetExecutiveAsync(smartContractRegistration);
            }

            return(executive);
        }
        public async Task ExecuteAsync_Test()
        {
            var chain = await _smartContractHelper.CreateChainWithGenesisContractAsync();

            var chainContext = new ChainContext
            {
                BlockHash   = chain.BestChainHash,
                BlockHeight = chain.BestChainHeight
            };
            var transaction = new Transaction
            {
                From       = SampleAddress.AddressList[0],
                To         = SampleAddress.AddressList[0],
                MethodName = "NotExistMethod",
                Params     = ByteString.Empty
            };

            _transactionReadOnlyExecutionService.ExecuteAsync(chainContext, transaction, TimestampHelper.GetUtcNow())
            .ShouldThrow <SmartContractFindRegistrationException>();
            _transactionReadOnlyExecutionService.ExecuteAsync <Address>(chainContext, transaction, TimestampHelper.GetUtcNow(), false)
            .ShouldThrow <SmartContractFindRegistrationException>();

            transaction = new Transaction
            {
                From       = SampleAddress.AddressList[0],
                To         = _defaultContractZeroCodeProvider.ContractZeroAddress,
                MethodName = "NotExistMethod",
                Params     = ByteString.Empty
            };
            var trace = await _transactionReadOnlyExecutionService.ExecuteAsync(chainContext, transaction,
                                                                                TimestampHelper.GetUtcNow());

            trace.Error.ShouldContain("Failed to find handler for NotExistMethod");
            trace.ExecutionStatus.ShouldBe(ExecutionStatus.SystemError);
            var hash = await _transactionReadOnlyExecutionService.ExecuteAsync <Hash>(chainContext, transaction,
                                                                                      TimestampHelper.GetUtcNow(), false);

            hash.ShouldBeNull();
            _transactionReadOnlyExecutionService.ExecuteAsync <Hash>(chainContext, transaction,
                                                                     TimestampHelper.GetUtcNow(), true).ShouldThrow <SmartContractExecutingException>();

            _smartContractExecutiveProvider.GetPool(_defaultContractZeroCodeProvider.ContractZeroAddress).Single()
            .ContractHash.ShouldBe(_defaultContractZeroCodeProvider.DefaultContractZeroRegistration.CodeHash);
            transaction = new Transaction
            {
                From       = SampleAddress.AddressList[0],
                To         = _defaultContractZeroCodeProvider.ContractZeroAddress,
                MethodName = nameof(ACS0Container.ACS0Stub.GetSmartContractRegistrationByAddress),
                Params     = _defaultContractZeroCodeProvider.ContractZeroAddress.ToByteString()
            };
            trace = await _transactionReadOnlyExecutionService.ExecuteAsync(chainContext, transaction,
                                                                            TimestampHelper.GetUtcNow());

            trace.ExecutionStatus.ShouldBe(ExecutionStatus.Executed);
            var smartContractRegistration = SmartContractRegistration.Parser.ParseFrom(trace.ReturnValue);

            CheckSmartContractRegistration(smartContractRegistration);

            _smartContractExecutiveProvider.GetPool(_defaultContractZeroCodeProvider.ContractZeroAddress).Single()
            .ContractHash.ShouldBe(_defaultContractZeroCodeProvider.DefaultContractZeroRegistration.CodeHash);

            smartContractRegistration = await _transactionReadOnlyExecutionService.ExecuteAsync <SmartContractRegistration>(chainContext, transaction,
                                                                                                                            TimestampHelper.GetUtcNow(), true);

            CheckSmartContractRegistration(smartContractRegistration);

            _smartContractExecutiveProvider.GetPool(_defaultContractZeroCodeProvider.ContractZeroAddress).Single()
            .ContractHash.ShouldBe(_defaultContractZeroCodeProvider.DefaultContractZeroRegistration.CodeHash);
        }