Esempio n. 1
0
        public override async Task ProcessAsync(Block block, Dictionary <TransactionResult, List <LogEvent> > logEventsMap)
        {
            var blockHash               = block.GetHash();
            var blockHeight             = block.Height;
            var totalResourceTokensMaps = new TotalResourceTokensMaps
            {
                BlockHash   = blockHash,
                BlockHeight = blockHeight
            };

            foreach (var logEvent in logEventsMap.Values.SelectMany(logEvents => logEvents))
            {
                var eventData = new ResourceTokenCharged();
                eventData.MergeFrom(logEvent);
                if (eventData.Symbol == null || eventData.Amount == 0)
                {
                    continue;
                }

                if (totalResourceTokensMaps.Value.Any(b => b.ContractAddress == eventData.ContractAddress))
                {
                    var oldBill =
                        totalResourceTokensMaps.Value.First(b => b.ContractAddress == eventData.ContractAddress);
                    if (oldBill.TokensMap.Value.ContainsKey(eventData.Symbol))
                    {
                        oldBill.TokensMap.Value[eventData.Symbol] += eventData.Amount;
                    }
                    else
                    {
                        oldBill.TokensMap.Value.Add(eventData.Symbol, eventData.Amount);
                    }
                }
                else
                {
                    var contractTotalResourceTokens = new ContractTotalResourceTokens
                    {
                        ContractAddress = eventData.ContractAddress,
                        TokensMap       = new TotalResourceTokensMap
                        {
                            Value =
                            {
                                { eventData.Symbol, eventData.Amount }
                            }
                        }
                    };
                    totalResourceTokensMaps.Value.Add(contractTotalResourceTokens);
                }
            }

            await _totalTotalResourceTokensMapsProvider.SetTotalResourceTokensMapsAsync(new BlockIndex
            {
                BlockHash   = blockHash,
                BlockHeight = blockHeight
            }, totalResourceTokensMaps);
        }
Esempio n. 2
0
        public async Task ProcessAsync(Block block, TransactionResult transactionResult, LogEvent logEvent)
        {
            var eventData = new ResourceTokenCharged();

            eventData.MergeFrom(logEvent);
            if (eventData.Symbol == null || eventData.Amount == 0)
            {
                return;
            }

            var blockHash   = block.GetHash();
            var blockHeight = block.Height;
            // TODO: Get -> Modify -> Set is slow, consider collect all logEvents then generate the totalResourceTokensMap at once.
            var totalResourceTokensMaps =
                await _totalTotalResourceTokensMapsProvider.GetTotalResourceTokensMapsAsync(
                    new ChainContext
            {
                BlockHash   = block.GetHash(),
                BlockHeight = block.Height,
            });

            // Initial totalTxFeesMap if necessary (either never initialized or not initialized for current block link)
            if (totalResourceTokensMaps == null)
            {
                totalResourceTokensMaps = new TotalResourceTokensMaps
                {
                    BlockHash   = blockHash,
                    BlockHeight = blockHeight
                };
            }
            else if (totalResourceTokensMaps.BlockHash != blockHash || totalResourceTokensMaps.BlockHeight != blockHeight)
            {
                totalResourceTokensMaps = new TotalResourceTokensMaps
                {
                    BlockHash   = blockHash,
                    BlockHeight = blockHeight
                };
            }

            if (totalResourceTokensMaps.Value.Any() &&
                totalResourceTokensMaps.Value.Any(b => b.ContractAddress == eventData.ContractAddress))
            {
                var oldBill = totalResourceTokensMaps.Value.First(b => b.ContractAddress == eventData.ContractAddress);
                if (oldBill.TokensMap.Value.ContainsKey(eventData.Symbol))
                {
                    oldBill.TokensMap.Value[eventData.Symbol] += eventData.Amount;
                }
                else
                {
                    oldBill.TokensMap.Value.Add(eventData.Symbol, eventData.Amount);
                }
            }
            else
            {
                var contractTotalResourceTokens = new ContractTotalResourceTokens
                {
                    ContractAddress = eventData.ContractAddress,
                    TokensMap       = new TotalResourceTokensMap
                    {
                        Value =
                        {
                            { eventData.Symbol, eventData.Amount }
                        }
                    }
                };
                totalResourceTokensMaps.Value.Add(contractTotalResourceTokens);
            }

            await _totalTotalResourceTokensMapsProvider.SetTotalResourceTokensMapsAsync(new BlockIndex
            {
                BlockHash   = blockHash,
                BlockHeight = blockHeight
            }, totalResourceTokensMaps);
        }
        public async Task <List <Transaction> > GenerateTransactionsAsync(Address @from, long preBlockHeight,
                                                                          Hash preBlockHash)
        {
            var generatedTransactions = new List <Transaction>();

            if (preBlockHeight == 1)
            {
                return(generatedTransactions);
            }

            if (preBlockHeight < AElfConstants.GenesisBlockHeight)
            {
                return(generatedTransactions);
            }

            var tokenContractAddress = _smartContractAddressService.GetAddressByContractName(
                TokenSmartContractAddressNameProvider.Name);

            if (tokenContractAddress == null)
            {
                return(generatedTransactions);
            }

            var chainContext = new ChainContext
            {
                BlockHash   = preBlockHash,
                BlockHeight = preBlockHeight
            };
            var totalResourceTokensMaps = await _totalResourceTokensMapsProvider.GetTotalResourceTokensMapsAsync(
                chainContext);

            var input = ByteString.Empty;

            if (totalResourceTokensMaps != null && totalResourceTokensMaps.BlockHeight == preBlockHeight &&
                totalResourceTokensMaps.BlockHash == preBlockHash)
            {
                input = totalResourceTokensMaps.ToByteString();
            }
            else
            {
                await _totalResourceTokensMapsProvider.SetTotalResourceTokensMapsAsync(new BlockIndex
                {
                    BlockHash   = preBlockHash,
                    BlockHeight = preBlockHeight
                }, TotalResourceTokensMaps.Parser.ParseFrom(ByteString.Empty));
            }

            generatedTransactions.AddRange(new List <Transaction>
            {
                new Transaction
                {
                    From           = from,
                    MethodName     = nameof(TokenContractImplContainer.TokenContractImplStub.DonateResourceToken),
                    To             = tokenContractAddress,
                    RefBlockNumber = preBlockHeight,
                    RefBlockPrefix = ByteString.CopyFrom(preBlockHash.Value.Take(4).ToArray()),
                    Params         = input
                }
            });

            Logger.LogInformation("Tx DonateResourceToken generated.");
            return(generatedTransactions);
        }