Instructions to use staccs/qwencoin with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use staccs/qwencoin with Transformers:
# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("staccs/qwencoin", device_map="auto") - Notebooks
- Google Colab
- Kaggle
qwencoin: graph/
Browse files- graph/index.py +93 -31
graph/index.py
CHANGED
|
@@ -31,8 +31,16 @@ in) is done once; the result for every inscription buried KEEP_DEPTH blocks or m
|
|
| 31 |
the node (qwg-index.json) and taken up again by the next process, if the chain still has the block
|
| 32 |
it was kept under and the code that read it is the code here. Without that, every start of an
|
| 33 |
agent, a model's loop or the pages verified the whole history again: a quarter of an hour with a
|
| 34 |
-
thousand inscriptions, and longer with every block.
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
"""
|
| 37 |
import json, os, sys, time, zlib
|
| 38 |
from dataclasses import dataclass, field
|
|
@@ -60,6 +68,8 @@ class Stake:
|
|
| 60 |
outpoint: str = ""
|
| 61 |
stage: int | None = None
|
| 62 |
status: str = "unknown" # active | unbonding | slashed | exited | invalid | replay
|
|
|
|
|
|
|
| 63 |
|
| 64 |
@property
|
| 65 |
def live(self) -> bool:
|
|
@@ -80,12 +90,27 @@ class Proof:
|
|
| 80 |
KEEP_DEPTH = 24 # blocks under which what was read of an inscription is kept between runs
|
| 81 |
|
| 82 |
|
|
|
|
|
|
|
|
|
|
| 83 |
def reading() -> str:
|
| 84 |
-
"""A fingerprint of the code that reads an inscription
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
|
| 91 |
@dataclass
|
|
@@ -95,6 +120,8 @@ class Index:
|
|
| 95 |
records: dict = field(default_factory=dict) # inscription id -> what reading it gave (_read), as it is kept
|
| 96 |
taken_up: int = 0 # how many of them came from the file kept beside the node
|
| 97 |
_kept: list = field(default_factory=lambda: [False, 0, 0.0]) # looked for the file; records in it; when it was last written
|
|
|
|
|
|
|
| 98 |
stakes: dict = field(default_factory=dict) # inscription id -> Stake
|
| 99 |
heads: dict = field(default_factory=dict) # (pid, epoch) -> {root: sig}
|
| 100 |
attests: list = field(default_factory=list) # (pid, x, y, sig, checked)
|
|
@@ -110,37 +137,45 @@ class Index:
|
|
| 110 |
if not self._kept[0]:
|
| 111 |
self._kept[0] = True
|
| 112 |
self._take_up()
|
| 113 |
-
page
|
|
|
|
|
|
|
|
|
|
| 114 |
while True:
|
| 115 |
r = net.ord_get(self.node, f"/inscriptions/{page}")
|
| 116 |
-
for iid in r["ids"]
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
| 120 |
break
|
| 121 |
page += 1
|
|
|
|
| 122 |
first = {} # signature -> the earliest inscription born on a bond with it
|
| 123 |
for st in self.stakes.values():
|
| 124 |
if st.born and (st.sig not in first or st.number < first[st.sig].number):
|
| 125 |
first[st.sig] = st
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
# A tip counts if it was paid: its note was born, alone, on an output of exactly its sats that only the
|
| 134 |
# receiver's own key can spend, and it is the first note with its signature born that way. (The receiver's
|
| 135 |
# own key is the `own` of its bond, so this is decided once the receiver's bond is known.)
|
| 136 |
owns, seen = self.owns(), set()
|
| 137 |
for t in sorted(self.tips, key=lambda t: t["number"]):
|
| 138 |
if t["ok"] is None and t["to"] in owns:
|
| 139 |
-
|
| 140 |
-
out0 = reveal["transaction"]["output"][0]
|
| 141 |
-
t["paid"] = out0["value"] if t["id"][64:] == "i0" and out0["script_pubkey"] == C.tip_spk(owns[t["to"]]).hex() else 0
|
| 142 |
-
t["ok"] = bool(t["paid"] == t["sats"] and reveal["inscription_count"] == 1)
|
| 143 |
-
self.records[t["id"]].update(paid=t["paid"], tip_ok=t["ok"]) # as read from the chain (a copy is told apart below)
|
| 144 |
sig = self.seen[t["id"]]["sig"]
|
| 145 |
if t["ok"] and sig in seen:
|
| 146 |
t["ok"] = False # the same signed note inscribed a second time
|
|
@@ -160,7 +195,7 @@ class Index:
|
|
| 160 |
return
|
| 161 |
try:
|
| 162 |
k = json.loads(self._file().read_text())
|
| 163 |
-
if k["reading"]
|
| 164 |
return
|
| 165 |
for rec in k["records"]:
|
| 166 |
if rec["id"] not in self.seen:
|
|
@@ -226,6 +261,16 @@ class Index:
|
|
| 226 |
rec["ok"] = bool(sha256(bytes.fromhex(m["x"])) == bytes.fromhex(m["y"])) # recomputed in this node's weights
|
| 227 |
return rec
|
| 228 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
def _apply(self, rec: dict) -> None:
|
| 230 |
"""Take what was read of an inscription into the index (nothing is asked of the node here)."""
|
| 231 |
iid, m, number = rec["id"], rec["m"], rec["number"]
|
|
@@ -249,11 +294,23 @@ class Index:
|
|
| 249 |
# ord's inscription number is the chain's order of inscriptions: the order of the feed
|
| 250 |
self.posts.append((pid, int(m["seq"]), m["text"], number, iid, m.get("re"), tuple(m.get("att", []))))
|
| 251 |
|
| 252 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
"""Where a stake's inscription sits now (its confirmed location): on the bond, on the
|
| 254 |
-
unbonding output, burned out of one of them by a slash, or gone with its owner.
|
| 255 |
-
|
| 256 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
info = net.ord_get(self.node, f"/inscription/{st.inscription}")
|
| 258 |
outpoint = info["satpoint"].rsplit(":", 1)[0]
|
| 259 |
if "burned" in info.get("charms", []):
|
|
@@ -262,8 +319,8 @@ class Index:
|
|
| 262 |
ptxid, pvout = tx["input"][0]["previous_output"].split(":")
|
| 263 |
prev = net.ord_get(self.node, f"/tx/{ptxid}")["transaction"]["output"][int(pvout)]
|
| 264 |
slashed = len(tx["input"]) == 1 and st.bond.stage_of(prev["script_pubkey"]) is not None
|
| 265 |
-
st.stage, st.outpoint, st.status = None, outpoint, "slashed" if slashed else "exited"
|
| 266 |
-
return
|
| 267 |
out = net.ord_get(self.node, f"/output/{outpoint}")
|
| 268 |
stage = st.bond.stage_of(out["script_pubkey"])
|
| 269 |
if stage and out["value"] == st.bond.value(stage) and out["inscriptions"] == [st.inscription]:
|
|
@@ -271,6 +328,8 @@ class Index:
|
|
| 271 |
st.status = "active" if stage == 1 else "unbonding"
|
| 272 |
else:
|
| 273 |
st.stage, st.status = None, "exited"
|
|
|
|
|
|
|
| 274 |
|
| 275 |
def feed(self, count: int | None = None) -> list:
|
| 276 |
"""The signed posts, newest first: (identity, seq, text). Call refresh first."""
|
|
@@ -379,3 +438,6 @@ class Index:
|
|
| 379 |
cap[a, b] -= push
|
| 380 |
cap[b, a] = cap.get((b, a), 0) + push
|
| 381 |
flow += push
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
the node (qwg-index.json) and taken up again by the next process, if the chain still has the block
|
| 32 |
it was kept under and the code that read it is the code here. Without that, every start of an
|
| 33 |
agent, a model's loop or the pages verified the whole history again: a quarter of an hour with a
|
| 34 |
+
thousand inscriptions, and longer with every block. QWG_INDEX_KEEP=0: nothing is kept or taken up.
|
| 35 |
+
|
| 36 |
+
Where a stake's inscription sits now is never kept between runs: it is read off the chain. An
|
| 37 |
+
inscription moves only when the output it sits on is spent, so a process that has found a stake
|
| 38 |
+
on an output asks its node, at every block, whether that output is still unspent (gettxout, the
|
| 39 |
+
node's own coins), and asks ord where the inscription went only when it is not. (Asking ord about
|
| 40 |
+
every stake at every refresh was most of what reading the graph took: two lookups a stake, and on
|
| 41 |
+
the public node, where ord takes two seconds to look an inscription up, the pages were a minute
|
| 42 |
+
and more behind the chain with nine stakes.) Likewise ord's list of inscriptions, newest first, is
|
| 43 |
+
read only as far as it has something new, once a process has read all of it.
|
| 44 |
"""
|
| 45 |
import json, os, sys, time, zlib
|
| 46 |
from dataclasses import dataclass, field
|
|
|
|
| 68 |
outpoint: str = ""
|
| 69 |
stage: int | None = None
|
| 70 |
status: str = "unknown" # active | unbonding | slashed | exited | invalid | replay
|
| 71 |
+
at: str = "" # the output ord last found its inscription on: while the node has that output unspent, it has not moved
|
| 72 |
+
burned: bool = False # its inscription was burned (by a slash or by its owner): it moves no more
|
| 73 |
|
| 74 |
@property
|
| 75 |
def live(self) -> bool:
|
|
|
|
| 90 |
KEEP_DEPTH = 24 # blocks under which what was read of an inscription is kept between runs
|
| 91 |
|
| 92 |
|
| 93 |
+
_reading = [] # the fingerprint, taken once, when this module is loaded (at its end)
|
| 94 |
+
|
| 95 |
+
|
| 96 |
def reading() -> str:
|
| 97 |
+
"""A fingerprint of the code that reads an inscription (Index._content, _read and _paid as they are written
|
| 98 |
+
here, the protocol and the covenant): what another version read is not taken up. Of the code as this process
|
| 99 |
+
loaded it: a process that runs on while the files are replaced under it goes on naming what it keeps by the
|
| 100 |
+
code it runs, not by what is on the disk by then."""
|
| 101 |
+
if not _reading:
|
| 102 |
+
import inspect
|
| 103 |
+
|
| 104 |
+
c = zlib.crc32("".join(inspect.getsource(f) for f in (Index._content, Index._read, Index._paid)).encode())
|
| 105 |
+
for n in ("protocol.py", "covenant.py"):
|
| 106 |
+
c = zlib.crc32((Path(__file__).parent / n).read_bytes(), c)
|
| 107 |
+
_reading.append(f"{c:08x}")
|
| 108 |
+
return _reading[0]
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
# Fingerprints of earlier versions that read an inscription word for word as this one does (the fingerprint then was
|
| 112 |
+
# of this whole file, and changed with every change to anything in it): what they kept is taken up.
|
| 113 |
+
READ_ALIKE = ("b1fd6bb3",)
|
| 114 |
|
| 115 |
|
| 116 |
@dataclass
|
|
|
|
| 120 |
records: dict = field(default_factory=dict) # inscription id -> what reading it gave (_read), as it is kept
|
| 121 |
taken_up: int = 0 # how many of them came from the file kept beside the node
|
| 122 |
_kept: list = field(default_factory=lambda: [False, 0, 0.0]) # looked for the file; records in it; when it was last written
|
| 123 |
+
_swept: bool = False # ord's whole list of inscriptions has been read by this process, and no reading of it since was cut short
|
| 124 |
+
_settled: str = "" # the node's tip at which every stake was last found where the node's coins say it is
|
| 125 |
stakes: dict = field(default_factory=dict) # inscription id -> Stake
|
| 126 |
heads: dict = field(default_factory=dict) # (pid, epoch) -> {root: sig}
|
| 127 |
attests: list = field(default_factory=list) # (pid, x, y, sig, checked)
|
|
|
|
| 137 |
if not self._kept[0]:
|
| 138 |
self._kept[0] = True
|
| 139 |
self._take_up()
|
| 140 |
+
# ord lists the inscriptions newest first, a hundred a page. A process reads the whole list once; after
|
| 141 |
+
# that, everything older than a page with nothing new on it has been read, and the reading stops there.
|
| 142 |
+
# (A reading that was cut short may have left a gap further down: the next one reads the whole list again.)
|
| 143 |
+
page, whole, self._swept = 0, not self._swept, False
|
| 144 |
while True:
|
| 145 |
r = net.ord_get(self.node, f"/inscriptions/{page}")
|
| 146 |
+
new = [iid for iid in r["ids"] if iid not in self.seen]
|
| 147 |
+
for iid in new:
|
| 148 |
+
self._settled = "" # something new is read: the stakes are looked at again (below)
|
| 149 |
+
self._ingest(iid)
|
| 150 |
+
if not r.get("more") or not (new or whole):
|
| 151 |
break
|
| 152 |
page += 1
|
| 153 |
+
self._swept = True
|
| 154 |
first = {} # signature -> the earliest inscription born on a bond with it
|
| 155 |
for st in self.stakes.values():
|
| 156 |
if st.born and (st.sig not in first or st.number < first[st.sig].number):
|
| 157 |
first[st.sig] = st
|
| 158 |
+
# Where each stake's inscription sits: looked at when the node's chain has moved, when something new was read,
|
| 159 |
+
# and for as long as a stake is not where the node's coins say (ord's index a block behind the node) or a
|
| 160 |
+
# look was cut short.
|
| 161 |
+
tip = net.cli(self.node, "getbestblockhash", timeout=60)
|
| 162 |
+
if tip != self._settled:
|
| 163 |
+
self._settled, settled = "", True
|
| 164 |
+
for st in self.stakes.values():
|
| 165 |
+
if not st.born:
|
| 166 |
+
st.stage, st.status, st.at = None, "invalid", ""
|
| 167 |
+
elif first[st.sig] is not st:
|
| 168 |
+
st.stage, st.status, st.at = None, "replay", ""
|
| 169 |
+
else:
|
| 170 |
+
settled = self._locate(st) and settled
|
| 171 |
+
self._settled = tip if settled else ""
|
| 172 |
# A tip counts if it was paid: its note was born, alone, on an output of exactly its sats that only the
|
| 173 |
# receiver's own key can spend, and it is the first note with its signature born that way. (The receiver's
|
| 174 |
# own key is the `own` of its bond, so this is decided once the receiver's bond is known.)
|
| 175 |
owns, seen = self.owns(), set()
|
| 176 |
for t in sorted(self.tips, key=lambda t: t["number"]):
|
| 177 |
if t["ok"] is None and t["to"] in owns:
|
| 178 |
+
self._paid(t, owns[t["to"]])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
sig = self.seen[t["id"]]["sig"]
|
| 180 |
if t["ok"] and sig in seen:
|
| 181 |
t["ok"] = False # the same signed note inscribed a second time
|
|
|
|
| 195 |
return
|
| 196 |
try:
|
| 197 |
k = json.loads(self._file().read_text())
|
| 198 |
+
if k["reading"] not in (reading(), *READ_ALIKE) or net.cli(self.node, "getblockhash", k["anchor"][0]) != k["anchor"][1]:
|
| 199 |
return
|
| 200 |
for rec in k["records"]:
|
| 201 |
if rec["id"] not in self.seen:
|
|
|
|
| 261 |
rec["ok"] = bool(sha256(bytes.fromhex(m["x"])) == bytes.fromhex(m["y"])) # recomputed in this node's weights
|
| 262 |
return rec
|
| 263 |
|
| 264 |
+
def _paid(self, t: dict, own: bytes) -> None:
|
| 265 |
+
"""What a tip's note was born on, read off its reveal: "paid", the sats of its output if that is the
|
| 266 |
+
receiver's own key and the note the reveal's first inscription, and "ok", paid exactly what it says with
|
| 267 |
+
no other inscription in the reveal. Into the tip and into what is kept of its inscription."""
|
| 268 |
+
reveal = net.ord_get(self.node, f"/tx/{t['id'][:64]}")
|
| 269 |
+
out0 = reveal["transaction"]["output"][0]
|
| 270 |
+
t["paid"] = out0["value"] if t["id"][64:] == "i0" and out0["script_pubkey"] == C.tip_spk(own).hex() else 0
|
| 271 |
+
t["ok"] = bool(t["paid"] == t["sats"] and reveal["inscription_count"] == 1)
|
| 272 |
+
self.records[t["id"]].update(paid=t["paid"], tip_ok=t["ok"]) # as read from the chain (a copy is told apart in refresh)
|
| 273 |
+
|
| 274 |
def _apply(self, rec: dict) -> None:
|
| 275 |
"""Take what was read of an inscription into the index (nothing is asked of the node here)."""
|
| 276 |
iid, m, number = rec["id"], rec["m"], rec["number"]
|
|
|
|
| 294 |
# ord's inscription number is the chain's order of inscriptions: the order of the feed
|
| 295 |
self.posts.append((pid, int(m["seq"]), m["text"], number, iid, m.get("re"), tuple(m.get("att", []))))
|
| 296 |
|
| 297 |
+
def _unspent(self, outpoint: str) -> bool:
|
| 298 |
+
"""Does the node's chain have that output unspent? (Its blocks only, as ord's index: not its mempool.)"""
|
| 299 |
+
txid, vout = outpoint.split(":")
|
| 300 |
+
try:
|
| 301 |
+
return bool(net.cli(self.node, "gettxout", txid, vout, "false", timeout=60))
|
| 302 |
+
except RuntimeError:
|
| 303 |
+
return False # the node does not say: ord is asked
|
| 304 |
+
|
| 305 |
+
def _locate(self, st: Stake) -> bool:
|
| 306 |
"""Where a stake's inscription sits now (its confirmed location): on the bond, on the
|
| 307 |
+
unbonding output, burned out of one of them by a slash, or gone with its owner. While the node has the
|
| 308 |
+
output unspent on which ord last found it, it is where it was, and ord is not asked. True: where it was
|
| 309 |
+
found is where the node's coins say it is (False: ord's index has yet to read the block that moved it)."""
|
| 310 |
+
if st.burned:
|
| 311 |
+
return True # a burn is final
|
| 312 |
+
if st.at and self._unspent(st.at):
|
| 313 |
+
return True
|
| 314 |
info = net.ord_get(self.node, f"/inscription/{st.inscription}")
|
| 315 |
outpoint = info["satpoint"].rsplit(":", 1)[0]
|
| 316 |
if "burned" in info.get("charms", []):
|
|
|
|
| 319 |
ptxid, pvout = tx["input"][0]["previous_output"].split(":")
|
| 320 |
prev = net.ord_get(self.node, f"/tx/{ptxid}")["transaction"]["output"][int(pvout)]
|
| 321 |
slashed = len(tx["input"]) == 1 and st.bond.stage_of(prev["script_pubkey"]) is not None
|
| 322 |
+
st.stage, st.outpoint, st.status, st.at, st.burned = None, outpoint, "slashed" if slashed else "exited", "", True
|
| 323 |
+
return True
|
| 324 |
out = net.ord_get(self.node, f"/output/{outpoint}")
|
| 325 |
stage = st.bond.stage_of(out["script_pubkey"])
|
| 326 |
if stage and out["value"] == st.bond.value(stage) and out["inscriptions"] == [st.inscription]:
|
|
|
|
| 328 |
st.status = "active" if stage == 1 else "unbonding"
|
| 329 |
else:
|
| 330 |
st.stage, st.status = None, "exited"
|
| 331 |
+
st.at = outpoint
|
| 332 |
+
return self._unspent(outpoint)
|
| 333 |
|
| 334 |
def feed(self, count: int | None = None) -> list:
|
| 335 |
"""The signed posts, newest first: (identity, seq, text). Call refresh first."""
|
|
|
|
| 438 |
cap[a, b] -= push
|
| 439 |
cap[b, a] = cap.get((b, a), 0) + push
|
| 440 |
flow += push
|
| 441 |
+
|
| 442 |
+
|
| 443 |
+
reading() # now, of the files this process has just loaded
|