Esempio 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();
        }
Esempio n. 2
0
        public async Task GetExecutive_With_SmartContractRegistrationProvider_Test()
        {
            var chain = await _smartContractHelper.CreateChainWithGenesisContractAsync();

            var chainContext = new ChainContext
            {
                BlockHash   = chain.BestChainHash,
                BlockHeight = chain.BestChainHeight
            };
            //Get executive by smartContractRegistration in SmartContractRegistrationProvider
            await _smartContractRegistrationProvider.SetSmartContractRegistrationAsync(chainContext,
                                                                                       _defaultContractZeroCodeProvider.ContractZeroAddress,
                                                                                       _defaultContractZeroCodeProvider.DefaultContractZeroRegistration);

            var executive = await _smartContractExecutiveService
                            .GetExecutiveAsync(chainContext, _defaultContractZeroCodeProvider.ContractZeroAddress);

            executive.ContractHash.ShouldBe(_defaultContractZeroCodeProvider.DefaultContractZeroRegistration.CodeHash);

            await _smartContractExecutiveService.PutExecutiveAsync(chainContext,
                                                                   _defaultContractZeroCodeProvider.ContractZeroAddress, executive);

            //Get executive from executive pool
            executive = await _smartContractExecutiveService
                        .GetExecutiveAsync(chainContext, _defaultContractZeroCodeProvider.ContractZeroAddress);

            executive.ContractHash.ShouldBe(_defaultContractZeroCodeProvider.DefaultContractZeroRegistration.CodeHash);

            var otherExecutive = await _smartContractExecutiveService
                                 .GetExecutiveAsync(chainContext, _defaultContractZeroCodeProvider.ContractZeroAddress);

            await _smartContractExecutiveService.PutExecutiveAsync(chainContext,
                                                                   _defaultContractZeroCodeProvider.ContractZeroAddress, executive);

            await _smartContractExecutiveService.PutExecutiveAsync(chainContext,
                                                                   _defaultContractZeroCodeProvider.ContractZeroAddress, otherExecutive);

            _smartContractExecutiveProvider.GetExecutivePools()[_defaultContractZeroCodeProvider.ContractZeroAddress]
            .Count.ShouldBe(2);

            //Make codeHash different between smartContractRegistration and executive
            var code     = _smartContractHelper.Codes["AElf.Contracts.Configuration"];
            var codeHash = HashHelper.ComputeFrom(code);
            await _smartContractRegistrationProvider.SetSmartContractRegistrationAsync(chainContext,
                                                                                       _defaultContractZeroCodeProvider.ContractZeroAddress, new SmartContractRegistration
            {
                Category = KernelConstants.DefaultRunnerCategory,
                Code     = ByteString.CopyFrom(code),
                CodeHash = HashHelper.ComputeFrom(code)
            });

            executive = await _smartContractExecutiveService
                        .GetExecutiveAsync(chainContext, _defaultContractZeroCodeProvider.ContractZeroAddress);

            executive.ContractHash.ShouldBe(codeHash);
            _smartContractExecutiveProvider.GetExecutivePools()
            .TryGetValue(_defaultContractZeroCodeProvider.ContractZeroAddress, out _).ShouldBeFalse();
        }
 public void CleanIdleExecutive()
 {
     foreach (var executivePool in _smartContractExecutiveProvider.GetExecutivePools())
     {
         var executiveBag = executivePool.Value;
         if (executiveBag.Count > ExecutiveClearLimit && executiveBag.Min(o => o.LastUsedTime) <
             TimestampHelper.GetUtcNow() - TimestampHelper.DurationFromSeconds(ExecutiveExpirationTime))
         {
             if (executiveBag.TryTake(out _))
             {
                 Logger.LogDebug($"Cleaned an idle executive for address {executivePool.Key}.");
             }
         }
     }
 }