Exemple #1
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();
        }
        protected override async Task ProcessLogEventAsync(Block block, LogEvent logEvent)
        {
            var eventData = new ContractDeployed();

            eventData.MergeFrom(logEvent);

            var chainContext = new ChainContext
            {
                BlockHash   = block.GetHash(),
                BlockHeight = block.Height
            };

            var smartContractRegistration =
                await _smartContractRegistrationInStateProvider.GetSmartContractRegistrationAsync(chainContext
                                                                                                  , eventData.Address);

            await _smartContractRegistrationProvider.SetSmartContractRegistrationAsync(chainContext, eventData.Address,
                                                                                       smartContractRegistration);

            if (block.Height > AElfConstants.GenesisBlockHeight)
            {
                _smartContractExecutiveService.CleanExecutive(eventData.Address);
            }

            if (eventData.Name != null)
            {
                await _smartContractAddressService.SetSmartContractAddressAsync(chainContext, eventData.Name.ToStorageKey(),
                                                                                eventData.Address);
            }

            Logger.LogDebug($"Deployed contract {eventData}");
        }
Exemple #3
0
        public async Task ProcessAsync(Block block, TransactionResult transactionResult, LogEvent logEvent)
        {
            var eventData = new ContractDeployed();

            eventData.MergeFrom(logEvent);

            var smartContractRegistration =
                await _smartContractRegistrationInStateProvider.GetSmartContractRegistrationAsync(new ChainContext
            {
                BlockHash   = block.GetHash(),
                BlockHeight = block.Height
            }, eventData.Address);

            await _smartContractRegistrationProvider.SetSmartContractRegistrationAsync(new BlockIndex
            {
                BlockHash   = block.GetHash(),
                BlockHeight = block.Height
            }, eventData.Address, smartContractRegistration);

            if (block.Height > AElfConstants.GenesisBlockHeight)
            {
                _smartContractExecutiveService.CleanExecutive(eventData.Address);
            }
            Logger.LogDebug($"Deployed contract {eventData}");
        }
Exemple #4
0
        public async Task SmartContractRegistrationSetAndGet_Test()
        {
            var genesisBlock = _kernelTestHelper.GenerateBlock(0, Hash.Empty, new List <Transaction>());
            var chain        = await _blockchainService.CreateChainAsync(genesisBlock, new List <Transaction>());

            var blockStateSet = new BlockStateSet
            {
                BlockHash   = chain.BestChainHash,
                BlockHeight = chain.BestChainHeight
            };
            await _blockStateSetManger.SetBlockStateSetAsync(blockStateSet);

            var blockExecutedDataKey = $"BlockExecutedData/SmartContractRegistration/{SampleAddress.AddressList[0]}";

            blockStateSet.BlockExecutedData.ShouldNotContainKey(blockExecutedDataKey);

            var chainContext = new ChainContext
            {
                BlockHash   = chain.BestChainHash,
                BlockHeight = chain.BestChainHeight
            };

            var smartContractRegistration = new SmartContractRegistration
            {
                CodeHash = Hash.FromString(blockExecutedDataKey),
                Category = KernelConstants.CodeCoverageRunnerCategory,
                Version  = 1
            };
            await _smartContractRegistrationProvider.SetSmartContractRegistrationAsync(chainContext,
                                                                                       SampleAddress.AddressList[0], smartContractRegistration);

            blockStateSet = await _blockStateSetManger.GetBlockStateSetAsync(chain.BestChainHash);

            blockStateSet.BlockExecutedData.ShouldContainKey(blockExecutedDataKey);

            var smartContractRegistrationFromState =
                await _smartContractRegistrationProvider.GetSmartContractRegistrationAsync(chainContext,
                                                                                           SampleAddress.AddressList[0]);

            smartContractRegistrationFromState.ShouldBe(smartContractRegistration);
        }
Exemple #5
0
        protected override async Task ProcessLogEventAsync(Block block, LogEvent logEvent)
        {
            var eventData = new CodeUpdated();

            eventData.MergeFrom(logEvent);

            var chainContext = new ChainContext
            {
                BlockHash   = block.GetHash(),
                BlockHeight = block.Height
            };
            var smartContractRegistration =
                await _smartContractRegistrationInStateProvider.GetSmartContractRegistrationAsync(chainContext,
                                                                                                  eventData.Address);

            await _smartContractRegistrationProvider.SetSmartContractRegistrationAsync(chainContext, eventData.Address,
                                                                                       smartContractRegistration);

            _smartContractExecutiveService.CleanExecutive(eventData.Address);

            Logger.LogDebug($"Updated contract {eventData}");
        }
        public async Task SmartContractRegistrationSetAndGet_Test()
        {
            var chain = await _smartContractHelper.CreateChainAsync();

            var blockExecutedDataKey = $"BlockExecutedData/SmartContractRegistration/{SampleAddress.AddressList[0]}";

            var chainContext = new ChainContext
            {
                BlockHash   = chain.BestChainHash,
                BlockHeight = chain.BestChainHeight
            };

            var smartContractRegistrationFromProvider =
                await _smartContractRegistrationProvider.GetSmartContractRegistrationAsync(chainContext,
                                                                                           SampleAddress.AddressList[0]);

            smartContractRegistrationFromProvider.ShouldBeNull();

            var smartContractRegistration = new SmartContractRegistration
            {
                CodeHash = HashHelper.ComputeFrom(blockExecutedDataKey),
                Category = KernelConstants.CodeCoverageRunnerCategory,
                Version  = 1
            };
            await _smartContractRegistrationProvider.SetSmartContractRegistrationAsync(chainContext,
                                                                                       SampleAddress.AddressList[0], smartContractRegistration);

            var blockStateSet = await _blockStateSetManger.GetBlockStateSetAsync(chain.BestChainHash);

            blockStateSet.BlockExecutedData.ShouldContainKey(blockExecutedDataKey);

            smartContractRegistrationFromProvider =
                await _smartContractRegistrationProvider.GetSmartContractRegistrationAsync(chainContext,
                                                                                           SampleAddress.AddressList[0]);

            smartContractRegistrationFromProvider.ShouldBe(smartContractRegistration);
        }