Skip to content

Commit

Permalink
[release] @subql/[email protected]
Browse files Browse the repository at this point in the history
* fix fetch and invalidate state

* fix sync state

* use base64 encoding for syncing states

* [release] @subql/[email protected]
  • Loading branch information
icezohu authored Apr 30, 2024
1 parent b04093c commit 5e11b84
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 21 deletions.
5 changes: 4 additions & 1 deletion packages/network-support/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.2.2] 2024-05-01

## [1.2.1] 2024-04-30

## [1.2.0] 2024-04-29
Expand All @@ -29,7 +31,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- It's a internal library.

[unreleased]: https://github.com/subquery/network-support/compare/v1.2.1...HEAD
[unreleased]: https://github.com/subquery/network-support/compare/v1.2.2...HEAD
[1.2.2]: https://github.com/subquery/network-support/releases/tag/v1.2.2
[1.2.1]: https://github.com/subquery/network-support/releases/tag/v1.2.1
[1.2.0]: https://github.com/subquery/network-support/releases/tag/v1.2.0
[1.1.2]: https://github.com/subquery/network-support/releases/tag/v1.1.2
Expand Down
2 changes: 1 addition & 1 deletion packages/network-support/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@subql/network-support",
"version": "1.2.1",
"version": "1.2.2",
"main": "dist/index.js",
"author": "SubQuery Pte Limited",
"license": "Apache-2.0",
Expand Down
13 changes: 5 additions & 8 deletions packages/network-support/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@

import { OrderManager } from './orderManager';
import { customFetch, generateUniqueId, Logger } from './utils';
import { ChannelState, OrderType } from './types';
import { OrderType } from './types';
import { ScoreType } from './scoreManager';
import { Base64 } from 'js-base64';
import { State } from './stateManager';

interface SystemError extends Error {
code?: string | undefined;
Expand Down Expand Up @@ -73,10 +72,10 @@ export function createFetch(
method: 'post',
body: init.body,
});
let state: State | ChannelState | undefined, res: object;
let res: object;
if (type === OrderType.flexPlan) {
[res, state] = orderManager.extractChannelState(
JSON.parse((await _res.text()) || '{}'),
[res] = orderManager.extractChannelState(
await _res.text(),
new Headers(_res.headers),
channelId
);
Expand All @@ -94,9 +93,7 @@ export function createFetch(
}

orderManager.updateScore(runner, ScoreType.SUCCESS);
if (type === OrderType.flexPlan && channelId && state) {
void orderManager.syncChannelState(channelId, state);
}

return {
status: _res.status,
headers: _res.headers,
Expand Down
17 changes: 11 additions & 6 deletions packages/network-support/src/orderManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ export class OrderManager {
private async refreshOrders() {
try {
const orders = await fetchOrders(this.authUrl, this.projectId, this.projectType, this.apikey);
if (orders.agreements) {
if (orders.agreements && orders.agreements.length > 0) {
this._agreements = orders.agreements;
}
if (orders.plans) {
if (orders.plans && orders.plans.length > 0) {
this._plans = orders.plans;
}
this.healthy = true;
Expand Down Expand Up @@ -263,8 +263,6 @@ export class OrderManager {
case ResponseFormat.Inline: {
const _state = headers.get('X-Channel-State');
assert(_state, 'invalid response, missing channel state');
const _signature = headers.get('X-Indexer-Sig') || '';
// assert(_signature, 'invalid response, missing channel signature');
let state: State | ChannelState;
try {
state = JSON.parse(Base64.decode(_state)) as ChannelState;
Expand All @@ -273,6 +271,9 @@ export class OrderManager {
authorization: _state,
} as State;
}
if (channelId) this.syncChannelState(channelId, state);
const _signature = headers.get('X-Indexer-Sig') || '';
assert(_signature, 'invalid response, missing channel signature');
return [typeof payload === 'string' ? JSON.parse(payload) : payload, state, _signature];
}
case undefined: {
Expand All @@ -281,8 +282,10 @@ export class OrderManager {
return [body, state, ''];
}
default:
if (typeof payload === 'object' && (payload as any).code) {
if (channelId) this.stateManager.invalidateState(channelId);
if (
(typeof payload === 'object' && (payload as any).code) ||
(typeof payload === 'string' && JSON.parse(payload).code)
) {
throw new Error(JSON.stringify(payload));
} else {
throw new Error('invalid X-Indexer-Response-Format');
Expand Down Expand Up @@ -334,6 +337,8 @@ export class OrderManager {
if (!this.plans) return;

const plans = await this.filterOrdersByRequestId(requestId, this.plans);
this.logger?.debug(`available plans count: ${plans.length}`);

if (!this.healthy || !plans?.length) return;

const plan = await this.selectRunner(plans);
Expand Down
22 changes: 17 additions & 5 deletions packages/network-support/src/stateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,14 @@ export class StateManager {
const stateUrl = new URL('/channel/state', this.authUrl);
try {
const res = await POST<{ spent: string }>(stateUrl.toString(), {
...state,
// ...state,
channelId,
auth: Base64.encode(JSON.stringify(state)),
block: BlockType.Single,
apikey: this.apikey,
});
if (res.spent) {
this.logger?.debug(`syncChannelState [single] succeed`);
// this.logger?.debug(`syncChannelState [single] succeed`);
} else {
this.logger?.debug(`syncChannelState [single] failed: ${JSON.stringify(res)}`);
}
Expand All @@ -103,15 +106,24 @@ export class StateManager {
return;
}
try {
const res = await this.requestState(channelId, BlockType.Multiple);
const stateUrl = new URL('/channel/state', this.authUrl);
const res = await POST<{ authorization: string }>(stateUrl.toString(), {
channelId,
auth: state.authorization,
block: BlockType.Multiple,
apikey: this.apikey,
});
// const res = await this.requestState(channelId, BlockType.Multiple);
if (res.authorization) {
const convertResult = this.tryConvertJson(res.authorization);
if (!convertResult.success) {
await this.setState(channelId, {
authorization: res.authorization,
});
this.logger?.debug(`syncChannelState [multiple] succeed`);
} else {
this.logger?.debug(`syncChannelState [multiple] failed: ${convertResult.error}`);
}
this.logger?.debug(`syncChannelState [multiple] succeed`);
} else {
this.logger?.debug(`syncChannelState [multiple] failed: ${JSON.stringify(res)}`);
}
Expand Down Expand Up @@ -143,7 +155,7 @@ export class StateManager {
}
}

private getActiveType(state: State): ActiveType {
getActiveType(state: State): ActiveType {
const data = Base64.toUint8Array(state.authorization);
return data[0] as ActiveType;
}
Expand Down

0 comments on commit 5e11b84

Please sign in to comment.