|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Numerics; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Castle.Components.DictionaryAdapter; |
| 9 | +using Nethereum.ABI.FunctionEncoding.Attributes; |
| 10 | +using Nethereum.BlockchainProcessing.Handlers; |
| 11 | +using Nethereum.BlockchainProcessing.Processing; |
| 12 | +using Nethereum.BlockchainProcessing.Processing.Logs; |
| 13 | +using Nethereum.BlockchainProcessing.Web3Abstractions; |
| 14 | +using Nethereum.Contracts; |
| 15 | +using Nethereum.Contracts.Extensions; |
| 16 | +using Nethereum.RPC.Eth.DTOs; |
| 17 | +using Xunit; |
| 18 | + |
| 19 | +namespace Nethereum.BlockchainProcessing.Samples |
| 20 | +{ |
| 21 | + public class EventLogEnumeration |
| 22 | + { |
| 23 | + /* |
| 24 | +Solidity Contract Excerpt |
| 25 | +* event Transfer(address indexed _from, address indexed _to, uint256 indexed _value); |
| 26 | +Other contracts may have transfer events with different signatures, this won't work for those. |
| 27 | +*/ |
| 28 | + [Event("Transfer")] |
| 29 | + public class TransferEvent |
| 30 | + { |
| 31 | + [Parameter("address", "_from", 1, true)] |
| 32 | + public string From {get; set;} |
| 33 | + |
| 34 | + [Parameter("address", "_to", 2, true)] |
| 35 | + public string To {get; set;} |
| 36 | + |
| 37 | + [Parameter("uint256", "_value", 3, true)] |
| 38 | + public BigInteger Value {get; set;} |
| 39 | + } |
| 40 | + |
| 41 | + public class TransferEventProcessor : ILogProcessor |
| 42 | + { |
| 43 | + public List<(FilterLog, EventLog<TransferEvent>)> ProcessedEvents = new List<(FilterLog, EventLog<TransferEvent>)>(); |
| 44 | + public List<(FilterLog, Exception)> DecodingErrors = new List<(FilterLog, Exception)>(); |
| 45 | + |
| 46 | + public bool IsLogForEvent(FilterLog log) |
| 47 | + { |
| 48 | + return log.IsLogForEvent<TransferEvent>(); |
| 49 | + } |
| 50 | + |
| 51 | + public Task ProcessLogsAsync(params FilterLog[] eventLogs) |
| 52 | + { |
| 53 | + foreach (var eventLog in eventLogs) |
| 54 | + { |
| 55 | + try |
| 56 | + { |
| 57 | + var eventDto = eventLog.DecodeEvent<TransferEvent>(); |
| 58 | + ProcessedEvents.Add((eventLog, eventDto)); |
| 59 | + |
| 60 | + } |
| 61 | + catch (Exception ex) |
| 62 | + { |
| 63 | + DecodingErrors.Add((eventLog, ex)); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return Task.CompletedTask; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public class CatchAllEventProcessor : ILogProcessor |
| 72 | + { |
| 73 | + public List<FilterLog> ProcessedEvents = new List<FilterLog>(); |
| 74 | + |
| 75 | + public bool IsLogForEvent(FilterLog log) |
| 76 | + { |
| 77 | + return true; |
| 78 | + } |
| 79 | + |
| 80 | + public Task ProcessLogsAsync(params FilterLog[] eventLogs) |
| 81 | + { |
| 82 | + ProcessedEvents.AddRange(eventLogs); |
| 83 | + return Task.CompletedTask; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + [Fact] |
| 88 | + public async Task RunOnce() |
| 89 | + { |
| 90 | + var web3Wrapper = new Web3Wrapper("https://rinkeby.infura.io/v3/25e7b6dfc51040b3bfc0e47317d38f60"); |
| 91 | + |
| 92 | + var transferEventProcessor = new TransferEventProcessor(); |
| 93 | + var catchAllEventProcessor = new CatchAllEventProcessor(); |
| 94 | + var eventProcessors = new ILogProcessor[] {catchAllEventProcessor, transferEventProcessor}; |
| 95 | + |
| 96 | + var logProcessor = new BlockchainLogProcessor(web3Wrapper, eventProcessors); |
| 97 | + |
| 98 | + var progressFileNameAndPath = Path.Combine(Path.GetTempPath(), "BlockProcess.json"); |
| 99 | + if(File.Exists(progressFileNameAndPath)) File.Delete(progressFileNameAndPath); |
| 100 | + |
| 101 | + var progressRepository = new JsonBlockProcessProgressRepository(progressFileNameAndPath); |
| 102 | + var progressService = new PreDefinedRangeBlockchainProcessingProgressService( |
| 103 | + 3146684, 3146684, progressRepository); |
| 104 | + |
| 105 | + var batchProcessorService = new BlockchainBatchProcessorService( |
| 106 | + logProcessor, progressService, maxNumberOfBlocksPerBatch: 1); |
| 107 | + |
| 108 | + await batchProcessorService.ProcessLatestBlocks(); |
| 109 | + |
| 110 | + Assert.Single(transferEventProcessor.ProcessedEvents); |
| 111 | + Assert.Equal(7, catchAllEventProcessor.ProcessedEvents.Count); |
| 112 | + |
| 113 | + Assert.Equal((ulong?)3146684, await progressRepository.GetLatestAsync()); |
| 114 | + |
| 115 | + } |
| 116 | + |
| 117 | + [Fact] |
| 118 | + public async Task RunContinually() |
| 119 | + { |
| 120 | + const ulong StartingBlockNumber = 3146684; |
| 121 | + var web3Wrapper = new Web3Wrapper("https://rinkeby.infura.io/v3/25e7b6dfc51040b3bfc0e47317d38f60"); |
| 122 | + |
| 123 | + var transferEventProcessor = new TransferEventProcessor(); |
| 124 | + var catchAllEventProcessor = new CatchAllEventProcessor(); |
| 125 | + var eventProcessors = new ILogProcessor[] {catchAllEventProcessor, transferEventProcessor}; |
| 126 | + |
| 127 | + var logProcessor = new BlockchainLogProcessor(web3Wrapper, eventProcessors); |
| 128 | + |
| 129 | + var progressFileNameAndPath = Path.Combine(Path.GetTempPath(), "BlockProcess.json"); |
| 130 | + if(File.Exists(progressFileNameAndPath)) File.Delete(progressFileNameAndPath); |
| 131 | + |
| 132 | + var progressRepository = new JsonBlockProcessProgressRepository(progressFileNameAndPath); |
| 133 | + |
| 134 | + //this will get the last block on the chain each time a "to" block is requested |
| 135 | + var progressService = new LatestBlockBlockchainProcessingProgressService( |
| 136 | + web3Wrapper, StartingBlockNumber, progressRepository); |
| 137 | + |
| 138 | + var batchProcessorService = new BlockchainBatchProcessorService( |
| 139 | + logProcessor, progressService, maxNumberOfBlocksPerBatch: 10); |
| 140 | + |
| 141 | + var iterations = 0; |
| 142 | + |
| 143 | + //iterate until we reach an arbitrary ending block |
| 144 | + //to process continually - remove the condition from the while loop |
| 145 | + while (progressRepository.Latest < (StartingBlockNumber + 100)) |
| 146 | + { |
| 147 | + await batchProcessorService.ProcessLatestBlocks(); |
| 148 | + iterations++; |
| 149 | + } |
| 150 | + |
| 151 | + Assert.Equal(10, iterations); |
| 152 | + Assert.Equal(1533, catchAllEventProcessor.ProcessedEvents.Count); |
| 153 | + Assert.Equal(40, transferEventProcessor.ProcessedEvents.Count); |
| 154 | + |
| 155 | + //events on other contracts may have same name and input parameter types |
| 156 | + //however they may differ in the number of indexed fields |
| 157 | + //this leads to decoding errors |
| 158 | + //it's not a problem - just something to be aware of |
| 159 | + Assert.Equal(201, transferEventProcessor.DecodingErrors.Count); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments