//! Reading the Ğ1 indexer (duniter-squid, GraphQL): the comments that carry a //! given remark, with their author, the author's identity and the transfers of //! the same transaction. //! //! A comment is found by `hash`, the blake2b-256 of the remark's bytes that the //! chain publishes in its `System.Remarked` event. The indexers of //! `LD_INDEXERS` are asked in turn: one that does not answer, or answers with //! errors, is skipped; one that answers « nothing » may be behind, so the next //! is asked too. Nobody answering is not a « no »: the ballot stays pending. //! //! **Finality.** The indexer serves the best chain, blocks not yet final //! included. `squidStatus` exposes `height` and `hash` only (no //! `finalizedHeight`); `height` is the last final block it processed — relevé //! on indexer.duniter.org on 2026-10-31: always 3 blocks behind the newest block //! it serves (`blocks(orderBy: HEIGHT_DESC, first: 1)`), the 2 to 3 blocks of the //! finality of Duniter v2. Only a comment at or below it settles a ballot: a //! comment above may still vanish with its block. //! //! **What is asked.** Only what may settle the ballot: for a ballot bound to an //! account, that account's comments; for one bound to none (a Ğecko code), the //! comments paid to the vote account — a bare copy of a remark costs nothing to //! send, and must never hide the real one. The engine judges again what comes //! back (`ld_engine::ballot::judge`): an indexer that ignores the filter changes //! nothing but the number of pages. //! //! **Pages.** In the order of the chain, 50 comments a page, page after page //! until a comment settles the ballot, a block not yet final is reached, or the //! last page — at most 20 pages a reading. use crate::AppState; use ld_engine::address; use ld_engine::ballot::{ChainIdentity, ChainPosition, Comment, Transfer}; use serde::Deserialize; use serde_json::json; use std::time::Duration; /// Validated on the indexer of the Ğ1 chain (indexer.duniter.org), 2026-10-31. pub const BALLOT_QUERY: &str = "query Ballot($hash: String!, $filter: TxCommentFilter, $after: Cursor) { \ squidStatus { height } txComments(condition: {hash: $hash}, filter: $filter, \ orderBy: [BLOCK_NUMBER_ASC, ID_ASC], first: 50, after: $after) { pageInfo { hasNextPage endCursor } \ nodes { id blockNumber remark author { id identity { index name isMember } } \ transfersByCommentId { nodes { fromId toId amount } } } } }"; /// Pages read at most for one ballot, one reading. pub const MAX_PAGES: usize = 20; /// The largest answer read: a page of 50 comments weighs about 30 KB. const MAX_BODY: usize = 1 << 20; /// A Ğ1 identity name is 42 characters at most. const MAX_NAME: usize = 64; const TIMEOUT: Duration = Duration::from_secs(6); #[derive(Deserialize)] struct Answer { data: Option, #[serde(default)] errors: Option, } #[derive(Deserialize)] #[serde(rename_all = "camelCase")] struct Data { squid_status: Status, tx_comments: Connection, } #[derive(Deserialize)] struct Status { height: u32, } #[derive(Deserialize)] #[serde(rename_all = "camelCase")] struct Connection { #[serde(default)] page_info: Option, nodes: Vec, } #[derive(Deserialize)] #[serde(rename_all = "camelCase")] struct PageInfo { has_next_page: bool, end_cursor: Option, } #[derive(Deserialize)] struct Nodes { nodes: Vec, } #[derive(Deserialize)] #[serde(rename_all = "camelCase")] struct Node { id: String, block_number: u32, remark: String, author: Author, transfers_by_comment_id: Nodes, } #[derive(Deserialize)] struct Author { id: String, identity: Option, } #[derive(Deserialize)] #[serde(rename_all = "camelCase")] struct IdentityNode { index: u32, name: String, is_member: bool, } #[derive(Deserialize)] #[serde(rename_all = "camelCase")] struct TransferNode { from_id: String, to_id: String, /// A big integer, served as a string. amount: serde_json::Value, } /// A comment of the chain, as the engine judges it, with what the server keeps. #[derive(Clone, Debug)] pub struct Found { pub comment: Comment, pub remark: String, pub event_id: String, /// The account that signed, as a Ğ1 address. pub signer: String, /// The name of the signer's identity, if any. pub name: Option, } /// One page of an answer. #[derive(Debug)] pub struct Page { /// The last final block (`squidStatus.height`). pub finalized: u32, pub comments: Vec, /// The cursor of the next page, if there is one. pub next: Option, } #[derive(Debug)] pub enum Reading { /// No indexer answered. Silent, /// What an indexer answered: the comments in final blocks, in the order of the /// chain (possibly none), and whether others wait in blocks not yet final. Found { comments: Vec, unfinal: bool }, } fn amount(v: &serde_json::Value) -> Option { match v { serde_json::Value::String(s) => s.parse().ok(), serde_json::Value::Number(n) => n.as_u64(), _ => None, } } fn found(node: Node) -> Option { let position = ChainPosition::from_event_id(&node.id)?; if position.block != node.block_number { return None; } if node.author.identity.as_ref().is_some_and(|i| i.name.len() > MAX_NAME) { return None; } let (_, author) = address::decode(&node.author.id)?; let transfers = node .transfers_by_comment_id .nodes .iter() .map(|t| { Some(Transfer { from: address::decode(&t.from_id)?.1, to: address::decode(&t.to_id)?.1, amount: amount(&t.amount)?, }) }) .collect::>>()?; Some(Found { comment: Comment { position, author, identity: node.author.identity.as_ref().map(|i| ChainIdentity { index: i.index, member: i.is_member }), transfers, }, remark: node.remark, event_id: node.id, signer: address::encode(address::G1_PREFIX, &author), name: node.author.identity.map(|i| i.name), }) } /// One page of an answer of the indexer; `None` if it is not one. pub fn parse(body: &serde_json::Value) -> Option { let answer: Answer = serde_json::from_value(body.clone()).ok()?; if answer.errors.as_ref().is_some_and(|e| !e.is_null()) { return None; } let data = answer.data?; let nodes = data.tx_comments.nodes; let count = nodes.len(); let comments: Vec = nodes.into_iter().filter_map(found).collect(); if comments.len() != count { tracing::warn!(read = comments.len(), served = count, "indexer: comments that do not read were left aside"); } let next = data.tx_comments.page_info.filter(|p| p.has_next_page).and_then(|p| p.end_cursor); Some(Page { finalized: data.squid_status.height, comments, next }) } /// What may settle a ballot, asked of the indexer: the comments of the bound /// account; unbound, those paid to the vote account — at least the amount. pub fn filter(account: &str, amount: u64, author: Option<&str>) -> serde_json::Value { match author { Some(a) => json!({ "authorId": { "equalTo": a } }), None => json!({ "transfersByCommentId": { "some": { "toId": { "equalTo": account }, "amount": { "greaterThanOrEqualTo": amount.to_string() }, } } }), } } /// Reads a body of at most `MAX_BODY` bytes as JSON. async fn read_json(mut res: reqwest::Response) -> Option { if res.content_length().is_some_and(|n| n > MAX_BODY as u64) { return None; } let mut buf = Vec::new(); while let Some(chunk) = res.chunk().await.ok()? { if buf.len() + chunk.len() > MAX_BODY { return None; } buf.extend_from_slice(&chunk); } serde_json::from_slice(&buf).ok() } async fn page(state: &AppState, url: &str, body: &serde_json::Value) -> Option { let res = state.http.post(url).timeout(TIMEOUT).json(body).send().await; let value = match res { Ok(r) if r.status().is_success() => read_json(r).await, Ok(r) => { tracing::debug!(%url, status = %r.status(), "indexer"); None } Err(e) => { tracing::debug!(%url, error = %e, "indexer"); None } }; value.as_ref().and_then(parse) } /// The comments in final blocks that carry the remark of hash `remark_hash` /// (`0x…`) and pass `filter`, page after page until `settled` says one settles /// the ballot. pub async fn comments( state: &AppState, remark_hash: &str, filter: &serde_json::Value, settled: impl Fn(&[Found]) -> bool, ) -> Reading { let mut answered = false; for url in &state.cfg.indexers { let mut kept: Vec = Vec::new(); let mut unfinal = false; let mut after: Option = None; let mut finalized: Option = None; for n in 0..MAX_PAGES { let body = json!({ "query": BALLOT_QUERY, "variables": { "hash": remark_hash, "filter": filter, "after": after } }); let Some(p) = page(state, url, &body).await else { break }; // The finality of the first page holds for the reading: later pages may only see more. let last_final = *finalized.get_or_insert(p.finalized); answered = true; for f in p.comments { if f.comment.position.block > last_final { unfinal = true; } else { kept.push(f); } } if unfinal || settled(&kept) { break; } match p.next { Some(cursor) => after = Some(cursor), None => break, } if n + 1 == MAX_PAGES { tracing::warn!(%url, hash = remark_hash, "indexer: more pages than read; the ballot stays pending"); } } if !kept.is_empty() || unfinal { return Reading::Found { comments: kept, unfinal }; } } if answered { Reading::Found { comments: Vec::new(), unfinal: false } } else { Reading::Silent } } #[cfg(test)] mod tests { use super::*; /// A real answer of the Ğ1 indexer to `BALLOT_QUERY` (2026-10-31), for the remark « Merci ». const REAL: &str = include_str!("../tests/fixtures/indexer-merci.json"); #[test] fn a_real_answer_reads() { let body: serde_json::Value = serde_json::from_str(REAL).expect("json"); let page = parse(&body).expect("an answer"); assert_eq!(page.finalized, 3_407_492); assert_eq!(page.next, None, "one page: 26 comments, fewer than 50"); let list = page.comments; assert_eq!(list.len(), 26, "the old query stopped at 20"); let first = &list[0]; assert_eq!(first.comment.position, ChainPosition { block: 196_944, event: 3 }); assert_eq!(first.remark, "Merci "); assert_eq!(first.signer, "g1NrqKgJW9FoFabaYW82zhS9tDWdQcL1DGpivaJdj7GW9M6iv"); assert_eq!(first.name.as_deref(), Some("SIMONEANTONELLI")); assert_eq!(first.comment.identity, Some(ChainIdentity { index: 1304, member: true })); assert_eq!(first.comment.transfers.len(), 1); assert_eq!(first.comment.transfers[0].amount, 30_000); assert_eq!(first.comment.transfers[0].from, first.comment.author); // An account without identity. assert_eq!(list[2].comment.identity, None); assert_eq!(list[2].name, None); // In the order of the chain. assert!(list.windows(2).all(|w| w[0].comment.position < w[1].comment.position)); } #[test] fn errors_or_a_strange_shape_are_no_answer() { assert!(parse(&json!({ "errors": [{ "message": "boom" }] })).is_none()); assert!(parse(&json!({ "data": null })).is_none()); assert!(parse(&json!({ "nope": 1 })).is_none()); // Without the finality, an answer cannot settle anything: no answer. assert!(parse(&json!({ "data": { "txComments": { "nodes": [] } } })).is_none()); let empty = json!({ "data": { "squidStatus": { "height": 7 }, "txComments": { "nodes": [] } } }); assert_eq!(parse(&empty).map(|p| (p.finalized, p.comments.len(), p.next)), Some((7, 0, None))); let more = json!({ "data": { "squidStatus": { "height": 7 }, "txComments": { "pageInfo": { "hasNextPage": true, "endCursor": "abc" }, "nodes": [] } } }); assert_eq!(parse(&more).and_then(|p| p.next).as_deref(), Some("abc")); } #[test] fn a_node_that_does_not_read_is_left_aside() { let author = "g1NrqKgJW9FoFabaYW82zhS9tDWdQcL1DGpivaJdj7GW9M6iv"; let body = json!({ "data": { "squidStatus": { "height": 100 }, "txComments": { "nodes": [ { "id": "0000000010-abcde-000002", "blockNumber": 11, "remark": "x", "author": { "id": author, "identity": null }, "transfersByCommentId": { "nodes": [] } }, { "id": "0000000010-abcde-000002", "blockNumber": 10, "remark": "x", "author": { "id": "not an address", "identity": null }, "transfersByCommentId": { "nodes": [] } }, { "id": "0000000010-abcde-000002", "blockNumber": 10, "remark": "x", "author": { "id": "z".repeat(100_000), "identity": null }, "transfersByCommentId": { "nodes": [] } }, { "id": "0000000010-abcde-000002", "blockNumber": 10, "remark": "x", "author": { "id": author, "identity": { "index": 1, "name": "n".repeat(65), "isMember": true } }, "transfersByCommentId": { "nodes": [] } }, ] } } }); assert_eq!( parse(&body).map(|p| p.comments.len()), Some(0), "block and id disagree; a bad address; a long one; a name no identity has" ); } #[test] fn what_is_asked_is_what_may_settle() { let vote = "g1L5EYvvSBaYuQCPW78bHxYcvHfmWAqHYe3Gsrp9SV4kWZaEM"; let alice = "g1Pn1geRZnHcVdWdKkoYwQrAARDTXK1D7H3zuXj7GNayT1thu"; assert_eq!(filter(vote, 10, Some(alice)), json!({ "authorId": { "equalTo": alice } })); assert_eq!( filter(vote, 10, None), json!({ "transfersByCommentId": { "some": { "toId": { "equalTo": vote }, "amount": { "greaterThanOrEqualTo": "10" } } } }) ); } }