Skip to content

Commit

Permalink
fix: only query finalized blocks in javascript client
Browse files Browse the repository at this point in the history
A difference between the behavior of the rust and javascript clients has
been observed which leads to StaleTableData errors in the javascript.
When no particular block was requested, the javascript client was simply
using the jsonrpc getStorage method without supplying a block. In this
case, the jsonrpc uses the latest block, which is not necessarily
finalized. On the other hand, the rust was implicitly requesting the
latest finalized block via subxt. This change makes it so the javascript
is only requesting finalized blocks too, which is important since only
events in finalized blocks are inserted to the database.
  • Loading branch information
tlovell-sxt committed Nov 25, 2024
1 parent fa48f07 commit d360eaa
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions node/src/index_tail.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,40 @@ export class SxTClient {
}
return authResponse.json();
}
async #getCommitment(commitmentKey, blockHash = null) {
const params = blockHash ? [commitmentKey, blockHash] : [commitmentKey]

const commitmentResponse = await postHttpRequest({
async #querySubstrateRpc(method, params = null) {
const response = await postHttpRequest({
url: this.substrateNodeURL,
headers: {
"Content-Type": "application/json",
},
data: {
id: 1,
jsonrpc: "2.0",
method: "state_getStorage",
method,
params,
},
});

if (!commitmentResponse.ok) {
if (!response.ok) {
throw new Error(
`Error querying RPC node: ${commitmentResponse.status}: ${commitmentResponse.statusText}`,
`Error querying RPC node: ${response.status}: ${response.statusText}`,
);
}

return commitmentResponse.json();
return response.json()
}
async #getFinalizedHead() {
response = await this.#querySubstrateRpc("chain_getFinalizedHead");

return response.json().result
}
async #getCommitment(commitmentKey, blockHash = null) {
if (!blockHash) {
blockHash = await this.#getFinalizedHead();
}

const commitmentResponse = await this.#querySubstrateRpc("state_getStorage", [commitmentKey, blockHash]);

return commitmentResponse;
}
async #getProof(accessToken, proverQuery) {
const proverResponse = await postHttpRequest({
Expand Down

0 comments on commit d360eaa

Please sign in to comment.