Skip to content

Commit

Permalink
updates bridge-relay to use API for head state (#4319)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
hughy authored Sep 26, 2023
1 parent 1834d4b commit fc8c1f2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
10 changes: 7 additions & 3 deletions ironfish-cli/src/commands/service/bridge/relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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<void> {
Assert.isNotUndefined(response)

const transactions = response.transactions
Expand All @@ -132,5 +134,7 @@ export default class BridgeRelay extends IronfishCommand {
// TODO: call Eth bridge contract to mint
}
}

await api.setBridgeHead(response.block.hash)
}
}
25 changes: 25 additions & 0 deletions ironfish/src/webApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,31 @@ export class WebApi {
await axios.post(`${this.host}/telemetry`, payload)
}

async getBridgeHead(): Promise<string | undefined> {
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<void> {
this.requireToken()

const options = this.options({ 'Content-Type': 'application/json' })

await axios.post(`${this.host}/bridge/head`, { head }, options)
}

options(headers: Record<string, string> = {}): AxiosRequestConfig {
return {
headers: {
Expand Down

0 comments on commit fc8c1f2

Please sign in to comment.