aboutsummaryrefslogtreecommitdiffstats
path: root/tools/thread-prefetch-test.js
Commit message (Collapse)AuthorAgeFilesLines
* Threads open at once from the replies already in hand, and fill in as the ↵Adam Soltys2026-10-161-5/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | relays answer. Adam, 2026-10-16: "Still seeing a spinner sometimes when I click into a thread." Three things stood between a tap and the conversation. (1) The feed already fetches every reply to each post on screen — the count on the reply button comes from them (watchZaps, kinds 1 and 1111 by #e) — and then kept only their ids. Now the replies themselves are kept (replyEvents: the 400 targets most recently heard from, 60 each) and a thread is seeded from them when it is opened (repliesInHand: those naming the root, and those naming only the reply they answer), so a post that has been on screen opens as the whole conversation, no relay asked; a thread fetched ahead before the count answered takes them when opened, and the open thread takes them when the count fetch lands (adoptRepliesInHand). (2) The loader waited for every relay's EOSE — the slowest relay, 4.5 s at worst — before the thread replaced its spinner; now replies go in as they arrive (queryStreamOn), the page repainted in place, and the root the moment it is found. (3) The spinner stood under whatever was already there while the load ran; now it shows only while there is nothing to read, and "no replies" only once the relays have spoken. And the fetch-ahead fires after 600 ms of reading instead of 1.2 s (a quick reader beat it to the tap), a dozen a minute. tools/thread-instant-test.js (the relays answered in the page): a post read for a moment is fetched ahead, the count answer released only after that entry exists, and the thread opens with its reply 12 ms after the tap, not after its own 6 s ask; a post on screen opens whole in 16 ms with the three replies the count brought, before the relays answer the thread's ask, and their answer adds nothing twice; a post with nothing in hand shows a spinner, then its first reply at ~470 ms with the slow one still on its way, the spinner gone, the slow one landing in place. thread-prefetch-test follows the new dwell and rate; thread-cache, thread-pager, thread-back-scroll, feed-conversations, moderation, feed-live, feed-switch-seed, profile-pager, notif-report-fold and reply-count pass (thread-reply-test's 30 s wait fails on clean HEAD the same way). Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01J1Kon8bxJoHRpJdSzshCkp
* Prefetch visible post threads after a bounded reading dwellAdam Soltys2026-10-081-0/+31
/a> 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
const BitcoinCore = require("@asoltys/bitcoin-core");
const { Op } = require("sequelize");
const { join } = require("path");
const fs = require("fs");
const read = require("../lib/read");

ah(async () => {
  seen = [];
  addresses = {};
  change = [];
  issuances = {};

  const exceptions = [];
  try {
    read(fs.createReadStream("exceptions"), (data) => exceptions.push(data));
  } catch (e) {
    l.warn("couldn't read exceptions file", e.message);
  }

  try {
  (
    await db.Invoice.findAll({
      include: {
        model: db.User,
        as: "user",
      },
    })
  ).map(({ address, user, unconfidential }) => {
    if (address && user) addresses[address] = user.username;
    if (unconfidential && user) addresses[unconfidential] = user.username;
  });
  } catch(e) {
    console.log(e);
  } 

  try {
    const accounts = await db.Account.findAll({
      where: { pubkey: { [Op.ne]: null } },
      include: {
        model: db.User,
        as: "user",
      },
    });

    accounts.map(({ address, user: { username } }) => {
      addresses[address] = username;
    });
  } catch (e) {
    console.log(e);
  }

  payments = (
    await db.Payment.findAll({
      attributes: ["hash"],
    })
  ).map((p) => p.hash);


  setInterval(async () => {
    const unconfirmed = (
      await db.Payment.findAll({
        where: {
          confirmed: 0,
        },
      })
    ).map((p) => p.hash);

    const transactions = await bc.listTransactions("*", 1000);

    transactions
      .filter(
        (tx) =>
          tx.category === "receive" &&
          tx.confirmations > 0 &&
          unconfirmed.includes(tx.txid)
      )
      .map((tx) => {
        l.warn("tx unconfirmed in db", tx.txid, tx.address);
      });

    const unaccounted = [];

    transactions.map((tx) => {
      if (!payments.includes(tx.txid) && !exceptions.includes(tx.txid)) {
        unaccounted.push(tx.txid);
      }
    });

    if (unaccounted.length)
      l.warn("wallet transactions missing from database", unaccounted);
  }, 720000);

  app.post("/send", auth, require("./send"));

  if (config.lna) {
    if (config.lna.clightning) {
      lna = require("clightning-client")(config.lna.dir);
    } else {
      throw new Error("lnd not supported");
    }

    app.post("/lightning/channel", require("./lightning/channel"));
    app.post(
      "/lightning/channelRequest",
      require("./lightning/channelRequest")
    );
    app.post("/lightning/invoice", require("./lightning/invoice"));
    app.post("/lightning/query", auth, require("./lightning/query"));
    app.post("/lightning/send", auth, require("./lightning/send"));
    require("./lightning/receive");
  }

  if (config.bitcoin) {
    bc = new BitcoinCore(config.bitcoin);
    app.post(
      "/bitcoin/broadcast",
      optionalAuth,
      require("./bitcoin/broadcast")
    );
    app.get("/bitcoin/generate", auth, require("./bitcoin/generate"));
    app.post("/bitcoin/sweep", auth, require("./bitcoin/sweep"));
    app.post("/bitcoin/fee", auth, require("./bitcoin/fee"));
    app.post("/bitcoin/send", auth, require("./bitcoin/send"));
    require("./bitcoin/receive");

    setTimeout(async () => {
      try {
        const address = await bc.getNewAddress();
        const { hdkeypath } = await bc.getAddressInfo(address);
        const parts = hdkeypath.split("/");
        app.set("bcAddressIndex", parts[parts.length - 1].slice(0, -1));
      } catch (e) {
        console.error(e);
      }
    }, 50);
  }

  if (config.liquid) {
    lq = new BitcoinCore(config.liquid);
    rare = new BitcoinCore(config.rare);
    app.post("/liquid/broadcast", optionalAuth, require("./liquid/broadcast"));
    app.get("/liquid/generate", auth, require("./liquid/generate"));
    app.post("/liquid/fee", auth, require("./liquid/fee"));
    app.post("/liquid/send", auth, require("./liquid/send"));
    app.post("/taxi", auth, require("./liquid/taxi"));
    require("./liquid/receive");

    setTimeout(async () => {
      try {
        const address = await lq.getNewAddress();
        const { hdkeypath } = await lq.getAddressInfo(address);
        const parts = hdkeypath.split("/");
        app.set("lqAddressIndex", parts[parts.length - 1].slice(0, -1));
      } catch (e) {
        console.error("Problem getting liquid address", e);
      }
    }, 50);
  }

  app.get(
    "/payments",
    auth,
    ah(async (req, res) => {
      let payments = await req.user.getPayments({
        where: {
          account_id: req.user.account_id
        },
        order: [["id", "DESC"]],
        include: {
          model: db.Account,
          as: "account"
        }
      });

      res.send(payments);
    })
  );

  app.get(
    "/payment/:redeemcode",
    ah(async (req, res) => {
      try {
        const { redeemcode } = req.params;
        let payment = await db.Payment.findOne({
          where: {
            redeemcode
          },
          include: {
            model: db.Account,
            as: "account"
          }
        });

        if (!payment) fail("invalid code");

        res.send(payment);
      } catch (e) {
        res.status(500).send(e.message);
      }
    })
  );
})();