Repository navigation
Expand file tree
/
Copy pathinscriptionmempool.py
More file actions
351 lines (314 loc) Β· 15.1 KB
/
Copy pathinscriptionmempool.py
File metadata and controls
351 lines (314 loc) Β· 15.1 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#! /usr/bin/env python3
from PIL import Image, ImageFile
from io import BytesIO
from mimetypes import guess_extension, guess_type
from vicariouspanel import NodeyezPanel
import glob
import json
import os
import re
import sys
import time
import vicariousbitcoin
import vicariousnetwork
import vicarioustext
class InscriptionMempoolPanel(NodeyezPanel):
def __init__(self):
"""Instantiates a new Inscription Mempool panel"""
# Define which additional attributes we have
self.configAttributes = {
# legacy key name mappings
"colorBackground": "backgroundColor",
"colorTextFG": "textColor",
"sleepInterval": "interval",
# panel specific key names
}
# Directives
ImageFile.LOAD_TRUNCATED_IMAGES=True
# Define our defaults (all panel specific key names should be listed)
self._defaultattr("extensionsToExtract", ["bmp","gif","jpg","png","svg"])
self._defaultattr("headerEnabled", True)
self._defaultattr("headerText", "Recent Unmined Image Inscriptions In Mempool")
self._defaultattr("interval", 60)
# Initialize
super().__init__(name="inscriptionmempool")
# Create additional folders expected for this panel
inscriptionMempoolDir = f"{self.dataDirectory}inscription-mempool/"
if not os.path.exists(inscriptionMempoolDir):
os.makedirs(inscriptionMempoolDir)
# Load any previous known data between runs
self.inscriptionMempoolDir = inscriptionMempoolDir
self.notInscriptions = vicariousbitcoin.loadJSONData(f"{inscriptionMempoolDir}/data-notinscriptions.json", [])
self.inscriptions = vicariousbitcoin.loadJSONData(f"{inscriptionMempoolDir}/data-inscriptions.json", [])
# Build content types for extensions to extract
self.contentTypesToExtract = self._getContentTypesFromExtensions()
def _getContentTypesFromExtensions(self):
contentTypes = []
for e in self.extensionsToExtract:
if e == "svg":
contentTypes.append("image/svg")
contentTypes.append("image/svg+xml")
elif e == "webp":
contentTypes.append("image/webp")
else:
atype, _ = guess_type(f"somefile.{e}")
if atype is not None:
contentTypes.append(atype)
return contentTypes
def fetchData(self):
"""Fetches all the data needed for this panel"""
# get mempool
self.log(f"checking mempool at {vicarioustext.getdateandtime()}")
self.mempool = vicariousbitcoin.getmempool()
def _getFileExtensionFromContentType(self, ct):
r = "bin"
mt = ct.split(";")[0]
femap = {
"application/x-javascript": "js",
"application/x-msaccess": "mdb",
"audio/midi": "midi",
"image/svg": "svg",
"image/webp": "webp",
"image/x-icon": "ico",
"text/htm": "htm",
"video/quicktime": "qt",
"x-word/x-vrml": "vrml",
}
if mt in femap:
r = femap[mt]
else:
r = guess_extension(mt)
r = "bin" if r is None else r.rpartition(".")[2]
return r
def _getBaseFilenameForTXID(self, txid):
return f"{self.inscriptionMempoolDir}inscription-{txid}"
def _getInscriptionsForTX(self, txid):
inscriptions = []
tx = vicariousbitcoin.gettransaction(txid)
thepattern = re.compile("(.*)0063036f72640101(.*)68$") # OP_FALSE OP_IF push-3-bytes 'ord' push-1-byte 1
txidx = -1
if "vin" not in tx: return inscriptions
for vin in tx["vin"]:
if "txinwitness" not in vin: continue
for txinwitness in vin["txinwitness"]:
regexmatch = re.match(thepattern, txinwitness)
if regexmatch is None: continue
# This is an ordinal inscription.
txidx += 1
g2 = regexmatch.group(2)
pos = 0
contenttypelength = int.from_bytes(bytes.fromhex(g2[pos:pos+2]),"little")
pos += 2
contenttype = bytes.fromhex(g2[pos:pos+(contenttypelength*2)]).decode()
# only proceed for those content types matching types we want to extract
if contenttype not in self.contentTypesToExtract: continue
pos += (contenttypelength*2)
opcode = g2[pos:pos+2]
pos += 2
if opcode != '00':
self.log(f"warning. expected 0x00 divider between content type and data, but got 0x{opcode}")
datalengthtype = g2[pos:pos+2]
pos +=2
datalen = 0
totaldatalen = 0
rawbytes = bytearray()
while datalengthtype in ['4c','4d','4e']:
#self.log(f"- hex code for data length: {datalengthtype}")
if datalengthtype == "4c": # next 1 byte = size of data
datalen = int.from_bytes(bytes.fromhex(g2[pos:pos+2]),"little")
pos += 2
if datalengthtype == "4d": # next 2 bytes = size of data
datalen = int.from_bytes(bytes.fromhex(g2[pos:pos+4]),"little")
pos += 4
if datalengthtype == "43": # next 4 bytes = size of data
datalen = int.from_bytes(bytes.fromhex(g2[pos:pos+8]),"little")
pos += 8
totaldatalen += datalen
morebytes = bytes.fromhex(g2[pos:pos+(datalen*2)])
rawbytes.extend(morebytes)
pos += (datalen*2)
# see if more op codes to continue data
datalengthtype = g2[pos:pos+2]
pos += 2
# Check for extra bytes trailing into end. For now, we'll append these to existing, but this may be incorrect
remaininghex = g2[pos:]
remaininghexlength = len(remaininghex)
if remaininghexlength > 0:
morebytes = bytes.fromhex(g2[pos:])
rawbytes.extend(morebytes)
totaldatalen += (remaininghexlength/2)
# append an object
inscription = {"txid":txid,"txidx":txidx,"contenttype":contenttype,"data":rawbytes}
inscriptions.append(inscription)
return inscriptions
def _extractTX(self, txid="00"):
inscriptions = self._getInscriptionsForTX(txid)
inscriptionCount = len(inscriptions)
if inscriptionCount == 0:
self.notInscriptions.append(txid)
return inscriptionCount
for inscription in inscriptions:
txidx = int(inscription["txidx"]) if "txidx" in inscription else -1
contentType = inscription["contenttype"] if "contenttype" in inscription else "undefined"
fileExtension = self._getFileExtensionFromContentType(contentType)
if fileExtension not in self.extensionsToExtract: continue
baseFileName = self._getBaseFilenameForTXID(txid)
exportFileName = f"{baseFileName}-{txidx}.{fileExtension}"
# self.log(f"exporting file to {exportFileName}")
try:
bytesioblob = BytesIO(inscription["data"])
with open(exportFileName, "wb") as f:
f.write(bytesioblob.getbuffer())
except Exception as e:
self.log(f"error processing inscription: {e}")
self.log(f"txid: {txid}, txidx: {txidx}, content-type: {contentType}")
self.inscriptions.append(txid)
return inscriptionCount
def _getMostRecentImagesOfType(self, extension):
globpath = f"{self.inscriptionMempoolDir}inscription-*.{extension}"
return list(filter(os.path.isfile, glob.glob(globpath)))
def _getMostRecentImages(self, max):
filelist = []
for e in self.extensionsToExtract:
filelist.extend(self._getMostRecentImagesOfType(e))
filelist.sort(key=os.path.getctime)
limitedlist = filelist[max*-1:]
reversedlist = limitedlist[::-1]
return reversedlist
def run(self):
# reset metric counters
inscriptioncount = 0
newinscriptioncount = 0
removefilecount = 0
mempoolItem = 0
# report mempool opening state
mempoolLength = len(self.mempool)
self.log(f"current mempool transaction count: {mempoolLength}")
# get existing filenames
self.log(f"checking files vs mempool")
insnotinpool = list(set(self.inscriptions) - set(self.mempool))
if len(insnotinpool) > 0:
self.log(f"looking to remove {len(insnotinpool)} transactions")
# assuming listdir is faster then globbing each file because we dont know the file extension from txid
dirlist = os.listdir(self.inscriptionMempoolDir)
for fn in dirlist:
if not fn.startswith("inscription-"): continue
fntx = fn.split("-")[1]
if fntx.find(".") > -1: fntx = fntx.split(".")[0]
if fntx not in self.mempool:
os.remove(f"{self.inscriptionMempoolDir}{fn}")
if fntx in self.inscriptions:
self.inscriptions.remove(fntx)
removefilecount += 1
self.log(f"removed {removefilecount} files for inscriptions no longer in mempool")
# not inscriptions setup
nlb = len(self.notInscriptions)
self.log(f"checking {nlb} prior tx known not to be inscriptions vs mempool")
self.notInscriptions = list(set(self.mempool) & set(self.notInscriptions))
with open(f"{self.inscriptionMempoolDir}/data-notinscriptions.json", "w") as f:
json.dump(self.notInscriptions, f)
nl = len(self.notInscriptions)
self.log(f"after purge, the number of tx that are not inscriptions is {nl} (was {nlb})")
self.mempool = list(set(self.mempool) - set(self.notInscriptions))
# check tx in mempool
checkTimeAllowed = self.interval // 2
checkTimeStart = int(time.time())
nml = len(self.mempool)
self.log(f"checking {nml} remaining tx in mempool for inscriptions")
for txid in self.mempool:
processtx = True
mempoolItem += 1
# check if not an inscription
if txid in self.notInscriptions:
processtx = False
# check if file already extracted - there can be multiple extracted per tx
# baseFileName = self._getBaseFilenameForTXID(txid)
# for fm in glob.glob(f"{baseFileName}*"):
# processtx = False
# inscriptioncount += 1
if txid in self.inscriptions:
processtx = False
inscriptioncount += 1
if processtx:
inscriptionsExtractedFromTX = self._extractTX(txid)
inscriptioncount += inscriptionsExtractedFromTX
newinscriptioncount += inscriptionsExtractedFromTX
# eye candy every 500 tx, and intermediate save every 5000
if (mempoolItem % 500) == 0:
if (mempoolItem % 5000) != 0:
self.log(f"processed {mempoolItem} transactions so far ({inscriptioncount} inscriptions found)")
else:
self.log(f"processed {mempoolItem} of {mempoolLength} transactions so far")
with open(f"{self.inscriptionMempoolDir}/data-notinscriptions.json", "w") as f:
json.dump(self.notInscriptions, f)
with open(f"{self.inscriptionMempoolDir}/data-inscriptions.json", "w") as f:
json.dump(self.inscriptions, f)
checkTimeCurrent = int(time.time())
# bail if this is taking too long due to size of mempool
if checkTimeCurrent - checkTimeStart >= checkTimeAllowed:
self.log(f"not checking any more transactions as time expired")
break
# summary and save our known state
self.log(f"done this pass through mempool.")
self.log(f"inscriptions found: {inscriptioncount} ({newinscriptioncount} new) from {mempoolLength} mempool tx")
with open(f"{self.inscriptionMempoolDir}/data-notinscriptions.json", "w") as f:
json.dump(self.notInscriptions, f)
with open(f"{self.inscriptionMempoolDir}/data-inscriptions.json", "w") as f:
json.dump(self.inscriptions, f)
# Generate image of recent files extracted
# Set relative area sizes so that we create a 3x5 layout of images
self.headerHeight = int(self.height * 20 / 320)
super().startImage()
availHeight = self.getInsetHeight()
col, row = 0, 0
filelist = self._getMostRecentImages(25)
tileSideAdj = 72
tileSideAdj = 96 if len(filelist) < 18 else tileSideAdj
tileSideAdj = 144 if len(filelist) < 10 else tileSideAdj
tileSide = int(self.height * tileSideAdj / 320)
colMax = int(self.width/tileSide)
rowMax = int(availHeight/tileSide)
padleft = (self.width - (colMax*tileSide)) // 2
for filename in filelist:
if row >= rowMax:
break
x = int(padleft + (col*tileSide))
y = int(self.getInsetTop() + (row*tileSide))
fileext = filename.rpartition('.')[2]
try:
img = vicariousnetwork.getimagefromfile("file://" + filename)
img.thumbnail((tileSide,tileSide))
img = img.resize((tileSide,tileSide),Image.Resampling.NEAREST) # scales up small images to fit
x += (tileSide - img.width)//2
y += (tileSide - img.height)//2
self.canvas.paste(img, box=(x,y))
img.close()
# next tile position
col = col + 1 if col < colMax-1 else 0
row = row + 1 if col == 0 else row
except Exception as e:
self.log(f"error processing image: {e}")
super().finishImage()
# --------------------------------------------------------------------------------------
# Entry point if running this script directly
# --------------------------------------------------------------------------------------
if __name__ == '__main__':
p = InscriptionMempoolPanel()
# If arguments were passed in, treat as a single run
if len(sys.argv) > 1:
if sys.argv[1] in ['-h','--help']:
arg0 = sys.argv[0]
print(f"Produces image with Unmined Inscriptions")
print(f"Usage:")
print(f"1) Call without arguments to run continuously using the configured defaults")
print(f"2) Pass the desired width and height as an argument as follows")
print(f" {arg0} 1920 1080")
else:
if len(sys.argv) > 2:
p.width = int(sys.argv[1])
p.height = int(sys.argv[2])
p.fetchData()
p.run()
exit(0)
# Continuous run
p.runContinuous()