From fc8c1f205cc6a0ea0e45aa8fcade2a5c3ccb8424 Mon Sep 17 00:00:00 2001 From: Hugh Cunningham <57735705+hughy@users.noreply.github.com> Date: Tue, 26 Sep 2023 11:46:56 -0700 Subject: [PATCH] updates bridge-relay to use API for head state (#4319) * updates bridge-relay to use API for head state gets head hash from '/bridge/head' if head flag is not set posts head hash to '/bridge/head' after processing all deposits from a confirmed block * passes options with GET /bridge/head options include the required token --- .../src/commands/service/bridge/relay.ts | 10 +++++--- ironfish/src/webApi.ts | 25 +++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/ironfish-cli/src/commands/service/bridge/relay.ts b/ironfish-cli/src/commands/service/bridge/relay.ts index b24c3d89cf..ab823d3ec6 100644 --- a/ironfish-cli/src/commands/service/bridge/relay.ts +++ b/ironfish-cli/src/commands/service/bridge/relay.ts @@ -80,11 +80,13 @@ export default class BridgeRelay extends IronfishCommand { this.log('Watching with view key:', incomingViewKey) - // TODO: track chain state of relay in API + head = head ?? (await api.getBridgeHead()) + if (!head) { const chainInfo = await client.chain.getChainInfo() head = chainInfo.content.genesisBlockIdentifier.hash } + this.log(`Starting from head ${head}`) const response = client.chain.getTransactionStream({ @@ -115,12 +117,12 @@ export default class BridgeRelay extends IronfishCommand { if (buffer.length > confirmations) { const response = buffer.shift() Assert.isNotUndefined(response) - this.commit(api, response) + await this.commit(api, response) } } } - commit(api: WebApi, response: GetTransactionStreamResponse): void { + async commit(api: WebApi, response: GetTransactionStreamResponse): Promise { Assert.isNotUndefined(response) const transactions = response.transactions @@ -132,5 +134,7 @@ export default class BridgeRelay extends IronfishCommand { // TODO: call Eth bridge contract to mint } } + + await api.setBridgeHead(response.block.hash) } } diff --git a/ironfish/src/webApi.ts b/ironfish/src/webApi.ts index 1027e2fd2b..4fc5e69313 100644 --- a/ironfish/src/webApi.ts +++ b/ironfish/src/webApi.ts @@ -178,6 +178,31 @@ export class WebApi { await axios.post(`${this.host}/telemetry`, payload) } + async getBridgeHead(): Promise { + this.requireToken() + + const response = await axios + .get<{ hash: string }>(`${this.host}/bridge/head`, this.options()) + .catch((e) => { + // The API returns 404 for no head + if (IsAxiosError(e) && e.response?.status === 404) { + return null + } + + throw e + }) + + return response?.data.hash + } + + async setBridgeHead(head: string): Promise { + this.requireToken() + + const options = this.options({ 'Content-Type': 'application/json' }) + + await axios.post(`${this.host}/bridge/head`, { head }, options) + } + options(headers: Record = {}): AxiosRequestConfig { return { headers: {