private async Task InitSystemContractAddressesAsync()
        {
            var blockchainService = Application.ServiceProvider.GetService <IBlockchainService>();
            var chain             = await blockchainService.GetChainAsync();

            var block = await blockchainService.GetBlockByHashAsync(chain.GenesisBlockHash);

            var transactionResultManager = Application.ServiceProvider.GetService <ITransactionResultManager>();
            var transactionResults       =
                await transactionResultManager.GetTransactionResultsAsync(block.Body.TransactionIds, block.GetHash());

            foreach (var transactionResult in transactionResults)
            {
                Assert.True(transactionResult.Status == TransactionResultStatus.Mined, transactionResult.Error);
                var relatedLogs = transactionResult.Logs.Where(l => l.Name == nameof(ContractDeployed)).ToList();
                if (!relatedLogs.Any())
                {
                    break;
                }
                foreach (var relatedLog in relatedLogs)
                {
                    var eventData = new ContractDeployed();
                    eventData.MergeFrom(relatedLog);
                    SystemContractAddresses[eventData.Name] = eventData.Address;
                }
            }
        }
Exemple #2
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}");
        }
        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}");
        }
        public Task HandleAsync(Block block, TransactionResult transactionResult, LogEvent logEvent)
        {
            var eventData = new ContractDeployed();

            eventData.MergeFrom(logEvent);

            _deployedContractAddressProvider.AddDeployedContractAddress(eventData.Address);
            Logger.LogTrace($"Added deployed contract address of {eventData}");

            return(Task.CompletedTask);
        }
        private async Task ProcessTransactionResultsAsync(TransactionResult[] transactionResultList, Block block)
        {
            var chainContext = new ChainContext
            {
                BlockHash   = block.GetHash(),
                BlockHeight = block.Height
            };
            var interestedEvent = await _contractDeployedLogEventProcessor.GetInterestedEventAsync(chainContext);

            foreach (var transactionResult in transactionResultList)
            {
                var logEvent = transactionResult.Logs.First(l =>
                                                            l.Address == interestedEvent.LogEvent.Address && l.Name == interestedEvent.LogEvent.Name);
                var contractDeployed = new ContractDeployed();
                contractDeployed.MergeFrom(logEvent);
                if (contractDeployed.Name != null)
                {
                    var smartContractAddress = await _smartContractAddressProvider.GetSmartContractAddressAsync(chainContext,
                                                                                                                contractDeployed.Name.ToStorageKey());

                    smartContractAddress.ShouldBeNull();
                }
                var smartContractRegistration = await _smartContractRegistrationProvider.GetSmartContractRegistrationAsync(chainContext,
                                                                                                                           contractDeployed.Address);

                smartContractRegistration.ShouldBeNull();

                await _contractDeployedLogEventProcessor.ProcessAsync(block,
                                                                      new Dictionary <TransactionResult, List <LogEvent> >
                {
                    { transactionResult, new List <LogEvent> {
                          logEvent
                      } }
                });

                smartContractRegistration = await _smartContractRegistrationProvider.GetSmartContractRegistrationAsync(chainContext,
                                                                                                                       contractDeployed.Address);

                smartContractRegistration.ShouldNotBeNull();
                if (contractDeployed.Name == null)
                {
                    continue;
                }
                {
                    var smartContractAddress = await _smartContractAddressProvider.GetSmartContractAddressAsync(chainContext,
                                                                                                                contractDeployed.Name.ToStorageKey());

                    smartContractAddress.ShouldNotBeNull();
                }
            }
        }
Exemple #6
0
        private void PrepareBloom()
        {
            if (_bloom != null)
            {
                // already prepared
                return;
            }

            _contractAddress =
                _smartContractAddressService.GetZeroSmartContractAddress();
            _interestedEvent = new ContractDeployed();
            _logEvent        = _interestedEvent.ToLogEvent(_contractAddress);
            _bloom           = _logEvent.GetBloom();
        }
Exemple #7
0
        public async Task GetExecutive_With_GenesisContract_Test()
        {
            var chain = await _smartContractHelper.CreateChainWithGenesisContractAsync();

            var chainContext = new ChainContext
            {
                BlockHash   = chain.BestChainHash,
                BlockHeight = chain.BestChainHeight
            };
            //Get executive of genesis contract by smartContractRegistration in genesis contract and blockHeight=1
            var executive = await _smartContractExecutiveService
                            .GetExecutiveAsync(chainContext, _defaultContractZeroCodeProvider.ContractZeroAddress);

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

            var transaction = _smartContractHelper.BuildDeploySystemSmartContractTransaction(
                ConfigurationSmartContractAddressNameProvider.Name,
                _smartContractHelper.Codes["AElf.Contracts.Configuration"]);
            var block = await _smartContractHelper.GenerateBlockAsync(chain.BestChainHeight, chain.BestChainHash, new List <Transaction>
            {
                transaction
            });

            await _smartContractHelper.MineBlockAsync(block);

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

            var transactionResult = await _transactionResultQueryService.GetTransactionResultAsync(transaction.GetHash(), block.GetHash());

            var contractDeployed = new ContractDeployed();

            contractDeployed.MergeFrom(transactionResult.Logs.First(l => l.Name == nameof(ContractDeployed)));

            //Get executive of configuration contract in genesis contract
            executive = await _smartContractExecutiveService.GetExecutiveAsync(chainContext, contractDeployed.Address);

            executive.ContractHash.ShouldBe(
                HashHelper.ComputeFrom(_smartContractHelper.Codes["AElf.Contracts.Configuration"]));

            //Get executive of genesis contract by smartContractRegistration in genesis contract and blockHeight=2
            executive = await _smartContractExecutiveService
                        .GetExecutiveAsync(chainContext, _defaultContractZeroCodeProvider.ContractZeroAddress);

            executive.ContractHash.ShouldBe(_defaultContractZeroCodeProvider.DefaultContractZeroRegistration.CodeHash);
        }
        public async Task HandleAsync(Block block, TransactionResult transactionResult, LogEvent logEvent)
        {
            var eventData = new ContractDeployed();

            eventData.MergeFrom(logEvent);

            _deployedContractAddressProvider.AddDeployedContractAddress(eventData.Address,
                                                                        new BlockIndex {
                BlockHash = block.GetHash(), BlockHeight = block.Height
            });
            Logger.LogDebug($"Added deployed contract address of {eventData}");
            await _smartContractRegistrationService.AddSmartContractRegistrationAsync(eventData.Address, eventData.CodeHash,
                                                                                      new BlockIndex
            {
                BlockHash   = block.GetHash(),
                BlockHeight = block.Height
            });
        }
Exemple #9
0
        public async Task <List <Address> > GetDeployedContractAddresses(Chain chain, IEnumerable <Hash> blockIdsInOrder)
        {
            PrepareBloom();

            var addresses = new List <Address>();

            foreach (var blockId in blockIdsInOrder)
            {
                var block = await _blockchainService.GetBlockByHashAsync(blockId);

                Logger.LogTrace($"Check event for block {blockId} - {block.Height}");

                if (!_bloom.IsIn(new Bloom(block.Header.Bloom.ToByteArray())))
                {
                    // No interested event in the block
                    continue;
                }

                Logger.LogTrace("ContractDeployed event received.");

                foreach (var transactionId in block.Body.TransactionIds)
                {
                    var result = await _transactionResultQueryService.GetTransactionResultAsync(transactionId);

                    if (result == null)
                    {
                        Logger.LogTrace($"Transaction result is null, transactionId: {transactionId}");
                        continue;
                    }

                    if (result.Status == TransactionResultStatus.Failed)
                    {
                        Logger.LogTrace(
                            $"Transaction failed, transactionId: {transactionId}, error: {result.Error}");
                        continue;
                    }

                    if (result.Bloom.Length == 0 || !_bloom.IsIn(new Bloom(result.Bloom.ToByteArray())))
                    {
                        continue;
                    }

                    foreach (var log in result.Logs)
                    {
                        if (log.Address != _contractAddress || log.Name != _logEvent.Name)
                        {
                            continue;
                        }

                        var message = new ContractDeployed();
                        message.MergeFrom(log);

                        Logger.LogTrace($"Add {message.Address} to deployed contract address list.");

                        addresses.Add(message.Address);
                    }
                }
            }

            return(addresses);
        }