Skip to content

Commit

Permalink
adding update graffiti cli command and webapi call
Browse files Browse the repository at this point in the history
  • Loading branch information
patnir committed Oct 4, 2023
1 parent 3c70456 commit f2d361d
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
91 changes: 91 additions & 0 deletions ironfish-cli/src/commands/service/syncGraffiti.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import { RpcClient, WebApi } from '@ironfish/sdk'
import { Flags } from '@oclif/core'
import { IronfishCommand } from '../../command'
import { RemoteFlags } from '../../flags'

export default class SyncGraffiti extends IronfishCommand {
static hidden = true

static description = `
Upload blocks and mempool transactions to an HTTP API using IronfishApi
`

static flags = {
...RemoteFlags,
endpoint: Flags.string({
char: 'e',
parse: (input: string) => Promise.resolve(input.trim()),
required: false,
description: 'API host to sync to',
}),
token: Flags.string({
char: 't',
parse: (input: string) => Promise.resolve(input.trim()),
required: false,
description: 'API host token to authenticate with',
}),
head: Flags.string({
char: 's',
required: true,
description: 'The block hash to start updating from',
}),
stopSequence: Flags.integer({
char: 's',
required: true,
description: 'Block sequence up to which to sync',
}),
}

async start(): Promise<void> {
const { flags } = await this.parse(SyncGraffiti)

const apiHost = (flags.endpoint || process.env.IRONFISH_API_HOST || '').trim()
const apiToken = (flags.token || process.env.IRONFISH_API_TOKEN || '').trim()

if (!apiHost) {
this.log(
`No api host found to upload blocks and transactions to. You must set IRONFISH_API_HOST env variable or pass --endpoint flag.`,
)
this.exit(1)
}

if (!apiToken) {
this.log(
`No api token found to auth with the API. You must set IRONFISH_API_TOKEN env variable or pass --token flag.`,
)
this.exit(1)
}

this.log('Connecting to node...')

const client = await this.sdk.connectRpc()

const api = new WebApi({ host: apiHost, token: apiToken })

await this.syncBlockGraffiti(client, api, flags.head, flags.stopSequence)
}

async syncBlockGraffiti(
client: RpcClient,
api: WebApi,
head: string,
stopSequence: number,
): Promise<void> {
this.log(`Starting from head ${head}`)

const response = client.chain.followChainStream(head ? { head } : undefined)

for await (const content of response.contentStream()) {
// We're done syncing if we greater than the stop sequence entered
const block = content.block
if (block.sequence >= stopSequence) {
break
}

await api.updateBlockGraffiti(block.hash, block.graffiti)
}
}
}
6 changes: 6 additions & 0 deletions ironfish/src/webApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ export class WebApi {
return response?.data.hash || null
}

async updateBlockGraffiti(hash: string, graffiti: string): Promise<void> {
this.requireToken()
const options = this.options({ 'Content-Type': 'application/json' })
await axios.post(`${this.host}/blocks/update_graffiti`, { hash, graffiti }, options)
}

async blocks(blocks: FollowChainStreamResponse[]): Promise<void> {
this.requireToken()

Expand Down

0 comments on commit f2d361d

Please sign in to comment.