feat: Start working on name indexing

This commit is contained in:
Nathan Woodburn 2025-02-09 21:33:20 +11:00
parent e30ced94c3
commit f807caee52
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1
2 changed files with 52 additions and 9 deletions

View File

@ -279,8 +279,21 @@ class Bid:
self.tx: Transaction = tx
self.bidHash = covenant.hash
self.bid = covenant
self.reveal = None
self.redeem = None
self.value = 0
self.blind = 0
self.txs = [tx.hash]
# TODO add blind calculation
def update(self, covenant: Covenant, tx: Transaction):
if covenant.type == 4: # REVEAL
self.reveal = covenant
self.txs.append(tx.hash)
# TODO add true bid calculation
# TODO add redeem/register covenants
@ -335,6 +348,7 @@ class Name:
def update(self, covenant: Covenant, tx: Transaction):
self.txs.append(tx.hash)
if covenant.type == 0: # NONE
return
if covenant.type == 1: # CLAIM
@ -342,12 +356,40 @@ class Name:
self.claimed += 1
if covenant.type == 2: # OPEN
self.state = "OPEN"
if self.height == 0:
self.height = covenant.height
self.height = covenant.height
if covenant.type == 3: # BID
bid: Bid = Bid(covenant, tx)
print(covenant.toJSON())
self.bids.append(bid)
if covenant.type == 4: # REVEAL
# Get the index of the REVEAL in the outputs
index = 0
for output in tx.outputs:
if output.covenant.hash == covenant.hash:
break
index += 1
# Get input from index
tx_input = tx.inputs[index]
# TODO get matching bid
print(tx_input)
print(covenant)
print(tx)
print(self.bids)
raise NotImplementedError
if covenant.type == 7: # UPDATE
# TODO
raise NotImplementedError
if covenant.type in [6,8]: # REGISTER, RENEW
self.lastRenewal = covenant.height
self.registered = True
if covenant.type == 6: # REGISTER
# TODO
raise NotImplementedError
if covenant.type == 9: # TRANSFER
# TODO
raise NotImplementedError
if covenant.type == 10: # FINALIZE
# TODO
raise NotImplementedError
def toJSON(self) -> dict:

11
main.py
View File

@ -108,7 +108,7 @@ def saveTransactions(txList, blockHeight):
# Bulk insert transactions
query = """
INSERT INTO transactions (hash, witnessHash, fee, rate, mtime, block, `index`, version,
INSERT INTO transactions (hash, witnessHash, fee, rate, mtime, block, tx_index, version,
inputs, outputs, locktime, hex)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
ON DUPLICATE KEY UPDATE hash=hash
@ -150,7 +150,7 @@ def setupDB():
"""Creates the database tables"""
with dbSave.cursor() as cursor:
cursor.execute("CREATE TABLE IF NOT EXISTS blocks ( hash String, height UInt64, depth Int32, version Int32, prevBlock String, merkleRoot String, witnessRoot String, treeRoot String, reservedRoot String, time UInt32, bits Int32, nonce UInt64, extraNonce String, mask String, txs String ) ENGINE = MergeTree() ORDER BY (hash, height)")
cursor.execute("CREATE TABLE IF NOT EXISTS transactions ( hash String, witnessHash String, fee Int64, rate Int64, mtime Int64, block UInt64, index Int32, version Int32, inputs String, outputs String, locktime Int64, hex String ) ENGINE = MergeTree() ORDER BY (hash, block)")
cursor.execute("CREATE TABLE IF NOT EXISTS transactions ( hash String, witnessHash String, fee Int64, rate Int64, mtime Int64, block UInt64, tx_index Int32, version Int32, inputs String, outputs String, locktime Int64, hex String ) ENGINE = MergeTree() ORDER BY (hash, block)")
cursor.execute("CREATE TABLE IF NOT EXISTS names ( name String, nameHash String, state String, height UInt64, lastRenewal Int64, owner String, value Int64, highest Int64, data String, transfer Int64, revoked Int64, claimed Int64, renewals Int64, registered UInt8, expired UInt8, weak UInt8, stats String, start String, txs String, bids String ) ENGINE = MergeTree() ORDER BY (name, height)")
# Get the newest block height in the database
@ -238,9 +238,11 @@ def getNamesFromBlock(height):
if cov.nameHash in names:
for name in namesToSave:
if name.nameHash == cov.nameHash:
name.update(cov,tx)
name.txs.append(tx.hash)
# Remove name from list
namesToSave.remove(name)
# Update name
name.update(cov,tx)
else:
name = getNameFromHash(cov.nameHash)
if name == -1:
@ -250,7 +252,6 @@ def getNamesFromBlock(height):
name.height = height
else:
name.update(cov,tx)
name.txs.append(tx.hash)
namesToSave.append(name)
queryData = []