-
Notifications
You must be signed in to change notification settings - Fork 35
/
AccountsCache.cs
281 lines (241 loc) · 9.54 KB
/
AccountsCache.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Dapper;
using Tzkt.Api.Models;
namespace Tzkt.Api.Services.Cache
{
public class AccountsCache : DbConnection
{
#region static
const string SelectQuery = @"SELECT * FROM ""Accounts""";
#endregion
readonly object Crit = new();
readonly Dictionary<int, RawAccount> AccountsById;
readonly Dictionary<string, RawAccount> AccountsByAddress;
int LastUpdate;
readonly StateCache State;
readonly CacheConfig Config;
readonly ILogger Logger;
public AccountsCache(StateCache state, IConfiguration config, ILogger<AccountsCache> logger) : base(config)
{
logger.LogDebug("Initializing accounts cache...");
State = state;
Config = config.GetCacheConfig();
Logger = logger;
var limit = Config.MaxAccounts > 0
? (int)(Math.Min(State.Current.AccountsCount, Config.MaxAccounts) * Config.LoadRate)
: (int)(State.Current.AccountsCount * Config.LoadRate);
var capacity = Config.MaxAccounts > 0
? Math.Min((int)(limit * 1.1), Config.MaxAccounts + 1)
: (int)(limit * 1.1);
using var db = GetConnection();
using var reader = db.ExecuteReader($@"{SelectQuery} ORDER BY ""LastLevel"" DESC LIMIT @limit", new { limit });
AccountsById = new Dictionary<int, RawAccount>(capacity);
AccountsByAddress = new Dictionary<string, RawAccount>(capacity);
var parsers = new Func<IDataReader, RawAccount>[5]
{
reader.GetRowParser<RawUser>(),
reader.GetRowParser<RawDelegate>(),
reader.GetRowParser<RawContract>(),
reader.GetRowParser<RawAccount>(),
reader.GetRowParser<RawRollup>()
};
while (reader.Read())
{
var account = parsers[reader.GetInt32(2)](reader);
AccountsById.Add(account.Id, account);
AccountsByAddress.Add(account.Address, account);
}
LastUpdate = State.Current.Level;
logger.LogInformation("Loaded {1} of {2} accounts", AccountsByAddress.Count, State.Current.AccountsCount);
}
public async Task UpdateAsync()
{
Logger.LogDebug("Updating accounts cache...");
var from = Math.Min(LastUpdate, State.ValidLevel);
#region check reorg
if (State.Reorganized)
{
List<RawAccount> corrupted;
lock (Crit)
{
corrupted = AccountsByAddress.Values
.Where(x => x.LastLevel > from)
.ToList();
foreach (var account in corrupted)
{
AccountsById.Remove(account.Id);
AccountsByAddress.Remove(account.Address);
}
}
Logger.LogDebug("Removed {1} corrupted accounts", corrupted.Count);
}
#endregion
using var db = GetConnection();
using var reader = await db.ExecuteReaderAsync($@"{SelectQuery} WHERE ""LastLevel"" > @from", new { from });
var parsers = new Func<IDataReader, RawAccount>[5]
{
reader.GetRowParser<RawUser>(),
reader.GetRowParser<RawDelegate>(),
reader.GetRowParser<RawContract>(),
reader.GetRowParser<RawAccount>(),
reader.GetRowParser<RawRollup>()
};
var cnt = 0;
while (reader.Read())
{
var accType = reader.GetInt32(2);
Add(parsers[accType](reader)); // TODO: don't cache new accounts until they are requested
cnt++;
}
LastUpdate = State.Current.Level;
Logger.LogDebug("Updated {1} accounts since block {2}", cnt, from);
}
#region metadata
public Alias GetAlias(int id)
{
// WARN: possible NullReferenceException if chain reorgs during request execution (very unlikely)
return Get(id).Info;
}
public async Task<Alias> GetAliasAsync(int id)
{
// WARN: possible NullReferenceException if chain reorgs during request execution (very unlikely)
return (await GetAsync(id)).Info;
}
public void UpdateMetadata(string address, string json)
{
if (TryGetSafe(address, out var account))
account.Metadata = AccountMetadata.Parse(json)?.Profile;
}
#endregion
public RawAccount Get(int id)
{
if (!TryGetSafe(id, out var account))
{
account = LoadRawAccount(id);
if (account != null) Add(account);
}
return account;
}
public async Task<RawAccount> GetAsync(int id)
{
if (!TryGetSafe(id, out var account))
{
account = await LoadRawAccountAsync(id);
if (account != null) Add(account);
}
return account;
}
public RawAccount Get(string address)
{
if (!TryGetSafe(address, out var account))
{
account = LoadRawAccount(address);
if (account != null) Add(account);
}
return account;
}
public async Task<RawAccount> GetAsync(string address)
{
if (!TryGetSafe(address, out var account))
{
account = await LoadRawAccountAsync(address);
if (account != null) Add(account);
}
return account;
}
RawAccount LoadRawAccount(int id)
{
var sql = $@"{SelectQuery} WHERE ""Id"" = @id LIMIT 1";
return LoadRawAccount(sql, new { id });
}
Task<RawAccount> LoadRawAccountAsync(int id)
{
var sql = $@"{SelectQuery} WHERE ""Id"" = @id LIMIT 1";
return LoadRawAccountAsync(sql, new { id });
}
RawAccount LoadRawAccount(string address)
{
var sql = $@"{SelectQuery} WHERE ""Address"" = @address::varchar(37) LIMIT 1";
return LoadRawAccount(sql, new { address });
}
Task<RawAccount> LoadRawAccountAsync(string address)
{
var sql = $@"{SelectQuery} WHERE ""Address"" = @address::varchar(37) LIMIT 1";
return LoadRawAccountAsync(sql, new { address });
}
RawAccount LoadRawAccount(string sql, object param)
{
using var db = GetConnection();
using var reader = db.ExecuteReader(sql, param);
if (!reader.Read()) return null;
return reader.GetInt32(2) switch
{
0 => reader.GetRowParser<RawUser>()(reader),
1 => reader.GetRowParser<RawDelegate>()(reader),
2 => reader.GetRowParser<RawContract>()(reader),
3 => reader.GetRowParser<RawAccount>()(reader),
4 => reader.GetRowParser<RawRollup>()(reader),
_ => throw new Exception($"Invalid account type")
};
}
async Task<RawAccount> LoadRawAccountAsync(string sql, object param)
{
using var db = GetConnection();
using var reader = await db.ExecuteReaderAsync(sql, param);
if (!reader.Read()) return null;
return reader.GetInt32(2) switch
{
0 => reader.GetRowParser<RawUser>()(reader),
1 => reader.GetRowParser<RawDelegate>()(reader),
2 => reader.GetRowParser<RawContract>()(reader),
3 => reader.GetRowParser<RawAccount>()(reader),
4 => reader.GetRowParser<RawRollup>()(reader),
_ => throw new Exception($"Invalid account type")
};
}
bool TryGetSafe(int id, out RawAccount account)
{
lock (Crit)
{
return AccountsById.TryGetValue(id, out account);
}
}
bool TryGetSafe(string address, out RawAccount account)
{
lock (Crit)
{
return AccountsByAddress.TryGetValue(address, out account);
}
}
void Add(RawAccount account)
{
lock (Crit)
{
#region check limits
if (Config.MaxAccounts > 0 && AccountsByAddress.Count >= Config.MaxAccounts)
{
Logger.LogDebug("Cache is full. Clearing...");
var oldest = AccountsByAddress.Values
.Take((int)(AccountsByAddress.Count * 0.25))
.ToList();
foreach (var acc in oldest)
{
AccountsById.Remove(acc.Id);
AccountsByAddress.Remove(acc.Address);
}
Logger.LogDebug("Removed {1} oldest accounts", oldest.Count);
}
#endregion
AccountsById[account.Id] = account;
AccountsByAddress[account.Address] = account;
}
Logger.LogDebug("Account {1} cached", account.Address);
}
}
}