-
Notifications
You must be signed in to change notification settings - Fork 0
/
Indexer..cs
266 lines (225 loc) · 12.2 KB
/
Indexer..cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Akka;
using Akka.Actor;
using Akka.Streams;
using Akka.Streams.Dsl;
using CirclesLand.BlockchainIndexer.ABIs;
using CirclesLand.BlockchainIndexer.DetailExtractors;
using CirclesLand.BlockchainIndexer.Persistence;
using CirclesLand.BlockchainIndexer.TransactionDetailModels;
using CirclesLand.BlockchainIndexer.Util;
using Nethereum.BlockchainProcessing.BlockStorage.Entities.Mapping;
using Nethereum.Hex.HexTypes;
namespace CirclesLand.BlockchainIndexer
{
public class IndexedBlockEventArgs : EventArgs
{
public HexBigInteger Block { get; }
public IndexedBlockEventArgs(HexBigInteger block)
{
Block = block;
}
}
public enum IndexerMode
{
NotRunning,
CatchUp,
Polling,
Live
}
public class Indexer
{
public event EventHandler<IndexedBlockEventArgs> NewBlock;
public IndexerMode Mode { get; private set; } = IndexerMode.NotRunning;
public async Task Run()
{
var system = ActorSystem.Create("system");
var materializer = system.Materializer();
var instanceContext = new InstanceContext();
while (true)
{
var roundContext = instanceContext.CreateRoundContext();
try
{
var roundStartsIn = roundContext.StartAt - DateTime.Now;
if (roundStartsIn.TotalMilliseconds > 0)
{
Logger.Log($"Round {roundContext.RoundNo} starting at {roundContext.StartAt} ..");
await Task.Delay(roundStartsIn);
}
roundContext.Log($"Round {roundContext.RoundNo} started at {DateTime.Now}.");
roundContext.Log($"Finding the last persisted block ..");
var lastPersistedBlock = roundContext.GetLastValidBlock();
roundContext.Log($"Last persisted block: {lastPersistedBlock}");
roundContext.Log($"Finding the latest blockchain block ..");
var currentBlock = await roundContext
.Web3
.Eth
.Blocks
.GetBlockNumber
.SendRequestAsync();
roundContext.Log($"Latest blockchain block: {currentBlock.Value}");
var delta = currentBlock.Value - lastPersistedBlock;
Source<HexBigInteger, NotUsed> source;
int flushEveryNthRound;
if (delta > Settings.UseBulkSourceThreshold)
{
roundContext.Log($"Found {delta} blocks to catch up. Using the 'BulkSource'.");
Mode = IndexerMode.CatchUp;
source = roundContext.SourceFactory.CreateBulkSource(
new HexBigInteger(lastPersistedBlock)
, currentBlock);
flushEveryNthRound = Settings.BulkFlushInterval;
}
else
{
roundContext.Log($"Found {delta} blocks to catch up. Using the 'PollingSource'.");
Mode = IndexerMode.Polling;
source = roundContext.SourceFactory.CreatePollingSource();
flushEveryNthRound = Settings.SerialFlushInterval;
}
await source
.Select(o =>
{
BlockTracker.AddRequested(roundContext.Connection, o.ToLong());
return o;
})
// Get the full block with all transactions
.SelectAsync(Settings.MaxParallelBlockDownloads, currentBlockNo =>
roundContext.Web3.Eth.Blocks
.GetBlockWithTransactionsByNumber
.SendRequestAsync(currentBlockNo))
.Buffer(Settings.MaxDownloadedBlockBufferSize, OverflowStrategy.Backpressure)
// Bundle the every transaction in a block with the block timestamp and send it downstream
.SelectMany(block =>
{
Interlocked.Increment(ref Statistics.TotalDownloadedBlocks);
var t = block.Transactions.ToArray();
Interlocked.Add(ref Statistics.TotalDownloadedTransactions, t.Length);
if (t.Length == 0)
{
BlockTracker.InsertEmptyBlock(roundContext.Connection, block);
}
var transactions = t
.Select(o => (
TotalTransactionsInBlock: t.Length,
Timestamp: block.Timestamp,
Transaction: o))
.ToArray();
return transactions;
})
.Buffer(Settings.MaxDownloadedTransactionsBufferSize, OverflowStrategy.Backpressure)
// Add the receipts for every transaction
.SelectAsync(Settings.MaxParallelReceiptDownloads, async timestampAndTransaction =>
{
var receipt = await roundContext.Web3.Eth.Transactions.GetTransactionReceipt
.SendRequestAsync(
timestampAndTransaction.Transaction.TransactionHash);
Interlocked.Increment(ref Statistics.TotalDownloadedReceipts);
return (
TotalTransactionsInBlock: timestampAndTransaction.TotalTransactionsInBlock,
Timestamp: timestampAndTransaction.Timestamp,
Transaction: timestampAndTransaction.Transaction,
Receipt: receipt
);
})
.Buffer(Settings.MaxDownloadedReceiptsBufferSize, OverflowStrategy.Backpressure)
// Classify all transactions
.Select(transactionAndReceipt =>
{
var classification = TransactionClassifier.Classify(
transactionAndReceipt.Transaction,
transactionAndReceipt.Receipt,
null);
return (
TotalTransactionsInBlock: transactionAndReceipt.TotalTransactionsInBlock,
Timestamp: transactionAndReceipt.Timestamp,
Transaction: transactionAndReceipt.Transaction,
Receipt: transactionAndReceipt.Receipt,
Classification: classification
);
})
// Add the details for each transaction
.SelectAsync(2, async classifiedTransactions =>
{
var extractedDetails = TransactionDetailExtractor.Extract(
classifiedTransactions.Classification,
classifiedTransactions.Transaction,
classifiedTransactions.Receipt)
.ToArray();
// For every CrcSignup-event check who the owner is
var signups = extractedDetails
.Where(o => o is CrcSignup)
.Cast<CrcSignup>();
foreach (var signup in signups)
{
var contract = roundContext.Web3.Eth.GetContract(
GnosisSafeABI.Json, signup.User);
var function = contract.GetFunction("getOwners");
var owners = await function.CallAsync<List<string>>();
signup.Owners = owners.Select(o => o.ToLower()).ToArray();
}
var organisationSignups = extractedDetails
.Where(o => o is CrcOrganisationSignup)
.Cast<CrcOrganisationSignup>();
foreach (var organisationSignup in organisationSignups)
{
var contract = roundContext.Web3.Eth.GetContract(
GnosisSafeABI.Json, organisationSignup.Organization);
var function = contract.GetFunction("getOwners");
var owners = await function.CallAsync<List<string>>();
organisationSignup.Owners = owners.Select(o => o.ToLower()).ToArray();
}
return (
TotalTransactionsInBlock: classifiedTransactions.TotalTransactionsInBlock,
TxHash: classifiedTransactions.Transaction.TransactionHash,
Timestamp: classifiedTransactions.Timestamp,
Transaction: classifiedTransactions.Transaction,
Receipt: classifiedTransactions.Receipt,
Classification: classifiedTransactions.Classification,
Details: extractedDetails
);
})
.GroupedWithin(Settings.WriteToStagingBatchSize,
TimeSpan.FromSeconds(Settings.WriteToStagingBatchMaxIntervalInSeconds))
.Buffer(Settings.MaxWriteToStagingBatchBufferSize, OverflowStrategy.Backpressure)
.RunForeach(transactionsWithExtractedDetails =>
{
roundContext.Log($" Writing batch to staging tables ..");
var txArr = transactionsWithExtractedDetails.ToArray();
TransactionsWriter.WriteTransactions(
roundContext.Connection,
txArr);
string[] writtenTransactions = { };
if (Statistics.TotalProcessedBatches % flushEveryNthRound == 0)
{
roundContext.Log($" Importing from staging tables ..");
ImportProcedure.ImportFromStaging(roundContext.Connection
, Mode == IndexerMode.CatchUp ? 120 : 10);
roundContext.Log($" Cleaning staging tables ..");
writtenTransactions = StagingTables.CleanImported(roundContext.Connection);
}
if ((Mode == IndexerMode.Polling || Mode == IndexerMode.Live)
&& writtenTransactions.Length > 0)
{
roundContext.OnBatchSuccessNotify(writtenTransactions);
}
else
{
roundContext.OnBatchSuccess();
}
}, materializer);
Logger.Log($"Completed the stream. Restarting ..");
}
catch (Exception ex)
{
roundContext.OnError(ex);
}
}
}
}
}