-
Notifications
You must be signed in to change notification settings - Fork 33
/
graphmap.ts
282 lines (253 loc) · 11 KB
/
graphmap.ts
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
282
import { SlpTokenGraph } from "./slptokengraph";
import { GraphTxnDbo, GraphTxnDetailsDbo, GraphTxnOutputDbo, TokenDBObject,
GraphTxnInput, GraphTxnOutput, GraphTxn, SlpTransactionDetailsDbo,
TokenPruneStateDbo } from "./interfaces";
import { Decimal128 } from "mongodb";
import { Config } from "./config";
import { RpcClient } from "./rpc";
import { SlpTransactionType, SlpTransactionDetails, SlpVersionType } from "slpjs";
import BigNumber from "bignumber.js";
import { slpUtxos } from './utxos';
const globalUtxoSet = slpUtxos();
export class GraphMap extends Map<string, GraphTxn> {
private _pruned = new Map<string, GraphTxn>();
private _dirtyItems = new Set<string>();
private _txidsToDelete = new Set<string>(); // used for double spent transaction items and reorgs
private _lastPruneHeight = 0;
private _rootId: string;
private _rootGraphTxn!: GraphTxn;
private _container: SlpTokenGraph;
private _prunedSendCount = 0;
private _prunedMintCount = 0;
constructor(graph: SlpTokenGraph) {
super();
this._rootId = graph._tokenIdHex;
this._container = graph;
}
get DirtyCount() {
return this._dirtyItems.size;
}
get TotalTransactionCount() {
return this._prunedSendCount + this._prunedMintCount + this.size - 1;
}
private setFromDb(txid: string, graphTxn: GraphTxn) {
return super.set(txid, graphTxn);
}
// @ts-ignore
public set(txid: string, graphTxn: GraphTxn) {
throw Error("method is not implemented, use 'setDirty(txid, graphTxn)' instead");
}
public setDirty(txid: string, graphTxn?: GraphTxn) {
if (graphTxn && txid === this._rootId) {
this._rootGraphTxn = graphTxn;
}
this._dirtyItems.add(txid);
if (! graphTxn) {
graphTxn = this.get(txid)!;
}
return super.set(txid, graphTxn);
}
public delete(txid: string) {
this._txidsToDelete.add(txid);
if (this.has(txid)) {
return super.delete(txid);
}
return false;
}
public has(txid: string, includePrunedItems=false): boolean {
if (includePrunedItems) {
return super.has(txid) || this._pruned.has(txid);
}
return super.has(txid);
}
public get(txid: string, includePrunedItems=false): GraphTxn|undefined {
if (txid === this._rootId && this._rootGraphTxn) {
return this._rootGraphTxn;
}
if (includePrunedItems) {
return super.get(txid) || this._pruned.get(txid);
}
return super.get(txid);
}
private prune(txid: string, pruneHeight: number) {
if (txid === this._rootId) {
return false;
}
this._lastPruneHeight = pruneHeight;
if (this.has(txid)) {
let gt = this.get(txid)!;
if (! gt.prevPruneHeight || pruneHeight >= gt.prevPruneHeight) {
this._pruned.set(txid, gt);
this.delete(txid);
console.log(`[INFO] Pruned ${txid} with prune height of ${pruneHeight}`);
if (gt.details.transactionType === SlpTransactionType.SEND) {
this._prunedSendCount++;
} else if (gt.details.transactionType === SlpTransactionType.MINT) {
this._prunedMintCount++;
}
return true;
} else if (pruneHeight < gt.prevPruneHeight) {
console.log(`[INFO] Pruning deferred until ${gt.prevPruneHeight}`);
}
}
return false;
}
private _flush() {
const txids = Array.from(this._pruned.keys());
this._pruned.forEach((i, txid) => {
RpcClient.transactionCache.delete(txid);
delete this._container._slpValidator.cachedRawTransactions[txid];
delete this._container._slpValidator.cachedValidations[txid];
});
this._txidsToDelete.clear();
this._pruned.clear();
this._dirtyItems.clear();
return txids;
}
public static toDbos(graph: GraphMap): { itemsToUpdate: GraphTxnDbo[], tokenDbo: TokenDBObject, txidsToDelete: string[] } {
let tg = graph._container;
let itemsToUpdate: GraphTxnDbo[] = [];
for (const txid of graph._dirtyItems) {
if (Array.from(graph._txidsToDelete).includes(txid)) {
graph.delete(txid);
continue;
}
let g = graph.get(txid);
if (g) {
let dbo: GraphTxnDbo = {
tokenDetails: { tokenIdHex: graph._container._tokenIdHex },
graphTxn: {
txid,
details: GraphMap._mapTokenDetailsToDbo(g.details, tg._tokenDetails.decimals),
outputs: GraphMap._txnOutputsToDbo(tg, g.outputs),
inputs: g.inputs.map((i) => {
return {
address: i.address,
txid: i.txid,
vout: i.vout,
bchSatoshis: i.bchSatoshis,
slpAmount: Decimal128.fromString(i.slpAmount.dividedBy(10**tg._tokenDetails.decimals).toFixed())
}
}),
_blockHash: g.blockHash,
_pruneHeight: g.prevPruneHeight
}
};
if (g.details.versionType === SlpVersionType.TokenVersionType1_NFT_Child) {
dbo.tokenDetails.nftGroupIdHex = tg._nftParentId!
}
if ((g.details.transactionType === SlpTransactionType.SEND ||
g.details.transactionType === SlpTransactionType.MINT) &&
dbo.graphTxn.inputs.length === 0) {
console.log(`[WARN] Cannot store a SEND or MINT transaction without any inputs (${txid})`);
//throw Error("Cannot store a SEND or MINT transaction without any inputs");
}
itemsToUpdate.push(dbo);
}
}
let txidsToDelete = Array.from(graph._txidsToDelete);
// Do the pruning here
itemsToUpdate.forEach(dbo => {
if (dbo.graphTxn._pruneHeight) {
graph.prune(dbo.graphTxn.txid, dbo.graphTxn._pruneHeight);
}
});
graph._flush();
let tokenDbo = GraphMap._mapTokenToDbo(graph);
return { itemsToUpdate, tokenDbo, txidsToDelete };
}
public fromDbos(dag: GraphTxnDbo[], pruneState: TokenPruneStateDbo) {
dag.forEach((item, idx) => {
let gt = GraphMap.mapGraphTxnFromDbo(item, this._container._tokenDetails.decimals);
gt.outputs.forEach(o => {
globalUtxoSet.set(`${item.graphTxn.txid}:${o.vout}`, Buffer.from(this._rootId, "hex"));
});
this.setFromDb(item.graphTxn.txid, gt);
});
this._lastPruneHeight = pruneState.pruneHeight;
this._prunedSendCount = pruneState.sendCount;
this._prunedMintCount = pruneState.mintCount;
}
private static _mapTokenToDbo(graph: GraphMap): TokenDBObject {
let tg = graph._container;
let tokenDetails = GraphMap._mapTokenDetailsToDbo(tg._tokenDetails, tg._tokenDetails.decimals);
let result: TokenDBObject = {
schema_version: Config.db.token_schema_version,
lastUpdatedBlock: tg._lastUpdatedBlock,
tokenDetails: tokenDetails,
mintBatonUtxo: tg._mintBatonUtxo,
mintBatonStatus: tg._mintBatonStatus,
tokenStats: {
block_created: tg._blockCreated,
approx_txns_since_genesis: graph.TotalTransactionCount
},
_pruningState: {
pruneHeight: graph._lastPruneHeight,
sendCount: graph._prunedSendCount,
mintCount: graph._prunedMintCount,
}
}
if (tg._tokenDetails.versionType === SlpVersionType.TokenVersionType1_NFT_Child) {
if (!tg._nftParentId) {
throw Error("Missing NFT1 parent token Id.");
}
result.nftParentId = tg._nftParentId;
}
return result;
}
private static _txnOutputsToDbo(tokenGraph: SlpTokenGraph, outputs: GraphTxnOutput[]): GraphTxnOutputDbo[] {
let mapped: GraphTxnDetailsDbo["outputs"] = [];
outputs.forEach(o => {
let m = Object.create(o);
//console.log(m);
try {
m.slpAmount = Decimal128.fromString(m.slpAmount.dividedBy(10**tokenGraph._tokenDetails.decimals).toFixed());
} catch(_) {
m.slpAmount = Decimal128.fromString("0");
}
mapped.push(m);
})
return mapped;
}
public static mapGraphTxnFromDbo(dbo: GraphTxnDbo, decimals: number): GraphTxn {
dbo.graphTxn.outputs.map(o => {
o.slpAmount = <any>new BigNumber(o.slpAmount.toString()).multipliedBy(10**decimals)
});
dbo.graphTxn.inputs.map(o => o.slpAmount = <any>new BigNumber(o.slpAmount.toString()).multipliedBy(10**decimals))
let gt: GraphTxn = {
details: SlpTokenGraph.MapDbTokenDetailsFromDbo(dbo.graphTxn.details, decimals),
outputs: dbo.graphTxn.outputs as any as GraphTxnOutput[],
inputs: dbo.graphTxn.inputs as any as GraphTxnInput[],
blockHash: dbo.graphTxn._blockHash,
prevPruneHeight: dbo.graphTxn._pruneHeight
}
return gt;
}
private static _mapTokenDetailsToDbo(details: SlpTransactionDetails, decimals: number): SlpTransactionDetailsDbo {
let res: SlpTransactionDetailsDbo = {
decimals: details.decimals,
tokenIdHex: details.tokenIdHex,
timestamp: details.timestamp ? details.timestamp : null,
timestamp_unix: details.timestamp ? this.ConvertToUnixTime(details.timestamp) : null,
transactionType: details.transactionType,
versionType: details.versionType,
documentUri: details.documentUri,
documentSha256Hex: details.documentSha256 ? details.documentSha256.toString('hex') : null,
symbol: details.symbol,
name: details.name,
batonVout: details.batonVout,
containsBaton: details.containsBaton ? true : false,
genesisOrMintQuantity: details.genesisOrMintQuantity ? Decimal128.fromString(details.genesisOrMintQuantity!.dividedBy(10**decimals).toFixed()) : null,
sendOutputs: details.sendOutputs ? details.sendOutputs.map(o => Decimal128.fromString(o.dividedBy(10**decimals).toFixed())) : null
}
return res;
}
private static ConvertToUnixTime(Y_m_d_H_M_S: string): number|null {
// timestamp is formatted as "%Y-%m-%d %H:%M:%S"
if(Y_m_d_H_M_S) {
let d = Y_m_d_H_M_S.split(" ")[0] + "T" + Y_m_d_H_M_S.split(" ")[1] + "Z";
return Date.parse(d)/1000;
}
return null;
}
}