Skip to content

Commit

Permalink
fix: rely on ids not ordering for batch query
Browse files Browse the repository at this point in the history
  • Loading branch information
samsiegart committed Jun 20, 2023
1 parent 9ec9785 commit 60c9b25
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
8 changes: 5 additions & 3 deletions src/rpc/src/batchQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ export const batchVstorageQuery = (
const options = {
method: 'POST',
body: JSON.stringify(
paths.map(path => ({
paths.map((path, index) => ({
jsonrpc: '2.0',
id: 1,
id: index,
method: 'abci_query',
params: { path: `/custom/vstorage/${path[0]}/${path[1]}` },
})),
Expand All @@ -33,7 +33,9 @@ export const batchVstorageQuery = (
.then(res => res.json())
.then(res =>
Object.fromEntries(
(Array.isArray(res) ? res : [res]).map((entry, index) => {
(Array.isArray(res) ? res : [res]).map(entry => {
const { id: index } = entry;

if (entry.result.response.code) {
return [
pathToKey(paths[index]),
Expand Down
16 changes: 12 additions & 4 deletions src/rpc/test/chainStorageWatcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ describe('makeAgoricChainStorageWatcher', () => {

fetch.mockResolvedValue(
createFetchResponse([
{ value: expected1, kind: AgoricChainStoragePathKind.Data },
{ value: expected2, kind: AgoricChainStoragePathKind.Children },
{ value: expected1, kind: AgoricChainStoragePathKind.Data, id: 0 },
{ value: expected2, kind: AgoricChainStoragePathKind.Children, id: 1 },
]),
);

Expand All @@ -63,7 +63,7 @@ describe('makeAgoricChainStorageWatcher', () => {
body: JSON.stringify([
{
jsonrpc: '2.0',
id: 1,
id: 0,
method: 'abci_query',
params: { path: `/custom/vstorage/data/${path}` },
},
Expand All @@ -84,6 +84,7 @@ describe('makeAgoricChainStorageWatcher', () => {
fetch.mockResolvedValue(
createFetchResponse([
{
id: 0,
value: expected1,
kind: AgoricChainStoragePathKind.Data,
blockHeight: 123,
Expand All @@ -110,6 +111,7 @@ describe('makeAgoricChainStorageWatcher', () => {
fetch.mockResolvedValue(
createFetchResponse([
{
id: 0,
value: expected2,
kind: AgoricChainStoragePathKind.Data,
blockHeight: 456,
Expand All @@ -129,6 +131,7 @@ describe('makeAgoricChainStorageWatcher', () => {
fetch.mockResolvedValue(
createFetchResponse([
{
id: 0,
value: expected1,
kind: AgoricChainStoragePathKind.Children,
},
Expand All @@ -154,6 +157,7 @@ describe('makeAgoricChainStorageWatcher', () => {
fetch.mockResolvedValue(
createFetchResponse([
{
id: 0,
value: expected2,
kind: AgoricChainStoragePathKind.Children,
},
Expand All @@ -172,6 +176,7 @@ describe('makeAgoricChainStorageWatcher', () => {
fetch.mockResolvedValue(
createFetchResponse([
{
id: 0,
value: null,
kind: AgoricChainStoragePathKind.Children,
code: 6,
Expand Down Expand Up @@ -199,6 +204,7 @@ describe('makeAgoricChainStorageWatcher', () => {
fetch.mockResolvedValue(
createFetchResponse([
{
id: 0,
value: expected1,
kind: AgoricChainStoragePathKind.Children,
},
Expand Down Expand Up @@ -234,12 +240,13 @@ const createFetchResponse = (
blockHeight?: number;
code?: number;
log?: string;
id: number;
}[],
) => ({
json: () =>
new Promise(res =>
res(
values.map(({ kind, value, blockHeight, code = 0, log }) => {
values.map(({ kind, value, blockHeight, code = 0, log, id }) => {
const data =
kind === AgoricChainStoragePathKind.Children
? { children: value }
Expand All @@ -251,6 +258,7 @@ const createFetchResponse = (
};

return {
id,
result: {
response: {
value: window.btoa(JSON.stringify(data)),
Expand Down

0 comments on commit 60c9b25

Please sign in to comment.