CardanoSolutions / ogmios

❇️ A WebSocket JSON/RPC bridge for Cardano
https://ogmios.dev
Mozilla Public License 2.0
304 stars 90 forks source link

HasTransaction returns false even when there's a matching tx in the mempool #376

Closed szg251 closed 7 months ago

szg251 commented 7 months ago

What Git revision / release tag are you using?

6.0.1

Do you use any client SDK? If yes, which one?

None

Describe what the problem is?

hasTransaction returns a false even though the transaction can be confirmed via nextTransaction calls. A simple example:

            let _ = self.acquire_mempool().await?;
            dbg!(tx_hash);
            let has_tx = self.has_transaction(tx_hash).await?;
            dbg!(has_tx);
            let tx = self.next_transaction().await?;
            dbg!(tx);

The logs produced by dbg:

[src/utils/ogmios.rs:208] tx_hash = TransactionHash(
    82892b2eabd3e257a22733d8f6fd93c44edfd7171199292e6584f87c1c220362,
)
[src/utils/ogmios.rs:210] has_tx = false
[src/utils/ogmios.rs:212] tx = Object {
    "transaction": Object {
        "id": String("82892b2eabd3e257a22733d8f6fd93c44edfd7171199292e6584f87c1c220362"),
    },
}

What should be the expected behavior?

HasTransaction returns true when tx is in mempool

If applicable, what are the logs from the server around the occurence of the problem?

No response

KtorZ commented 7 months ago

I've been looking into this without success so far. I was originally suspecting an issue with the TypeScript client and possible coercion there. But no. The server is also working as expecting and the error comes from the node as far as I can tell. I've been tracing this back to the very messages sent to the node and, the node replies False even for transactions that are part of the mempool snapshot for some reason.

I've inspected the current implementation and interestingly, the server-side implements the hasTransaction handler by using a side set of transaction id which is said to be "maintained in sync with the transaction in the mempool". I suppose that this statement might be invalid.

I invite you to open a ticket on so let them know about the issue. Here's a minimal reproduction script using Ogmios on the Preview network (which assumes a my.sk and my.addr files with a secret key and an address respectively, with enough funds).

const WebSocket = require('ws');
const { execSync } = require('node:child_process');
const { readFileSync } = require('node:fs');

const ADDR = readFileSync("./my.addr");
const SECRET_KEY = "./my.sk";
const SOCKET_PATH = "/tmp/node.socket";
const NETWORK_MAGIC = 2 // 0: mainnet, 1: preprod, 2: preview

const ws = new WebSocket('ws://127.0.0.1:1337');

function rpc(method, params = {}, id) {
  return JSON.stringify({
    jsonrpc: '2.0',
    method,
    params,
    id
  });
}

function cli(cmd, ...opts) {
  const is = str => cmd.includes(str);

  if (is("build") || is("query") || is("submit")) {
    opts.unshift(`--socket-path ${SOCKET_PATH}`);
  }

  if (is("build") || is("query") || is("sign") || is("submit")) {
    opts.unshift(`--testnet-magic ${NETWORK_MAGIC}`);
  }

  if (is("build") || is("sign")) {
    opts.unshift(`--out-file /tmp/tx.cbor`);
  }

  if (is("txid") || is("sign") || is("submit")) {
    opts.unshift(`--tx-file /tmp/tx.cbor`);
  }

  return execSync(`cardano-cli ${cmd} ${opts.join(" ")}`).toString();
}

let txId;

ws.on('open', () => {
  const utxo = cli("query utxo", `--address  ${ADDR}`);

  console.log(utxo);

  const utxoRef = utxo
    .split("\n")[2]
    .split(" ")
    .map(x => x.trim())
    .filter(x => x !== "");

  cli("transaction build",
    `--tx-in ${utxoRef[0]}#${utxoRef[1]}`,
    `--change-address ${ADDR}`
  );

  txId = cli("transaction txid").trim();

  cli("transaction sign", `--signing-key-file ${SECRET_KEY}`);

  console.log(`submitting ${txId}`);

  cli("transaction submit");

  ws.send(rpc('acquireMempool'));
});

ws.on('message', (response) => {
  const { result, error, method } = JSON.parse(response);

  if (error) {
    console.log(error);
    ws.close();
    return;
  }

  console.log(`${method} -> ${JSON.stringify(result)}`);

  if (method === 'acquireMempool') {
    ws.send(rpc('hasTransaction', { id: txId }))
  } else if (method === 'hasTransaction' || result.transaction) {
    ws.send(rpc('nextTransaction'));
  } else {
    ws.close();
  }
});

ws.on('error', e => {
  console.error('Oopsie', e);
  process.exit(1)
});

This outputs the following:

                           TxHash                                 TxIx        Amount
--------------------------------------------------------------------------------------
cfaaf224394adde30fdf2962e78ec63bff14ae65cfece5ddee116ec4789821c6     0        8687960 lovelace + TxOutDatumNone

submitting 868d331ab1348f5c87c09c12bb719593ba0434c7607af503fe78dacec55ffca7
acquireMempool -> {"acquired":"mempool","slot":44029808}
hasTransaction -> false
nextTransaction -> {"transaction":{"id":"868d331ab1348f5c87c09c12bb719593ba0434c7607af503fe78dacec55ffca7"}}
nextTransaction -> {"transaction":null}
KtorZ commented 7 months ago

I found the issue in the consensus --> https://github.com/IntersectMBO/ouroboros-consensus/issues/1009

There's possibly a fix I can do in Ogmios while this gets fixed.