|
| 1 | +import * as dotenv from 'dotenv'; |
| 2 | +dotenv.config(); |
| 3 | +import { createReadStream } from 'fs'; |
| 4 | +import { createInterface } from 'readline'; |
| 5 | +import { assertEnvironmentVariable } from '@wormhole-foundation/wormhole-monitor-common/src/utils'; |
| 6 | +import { BigtableDatabase } from '../src/databases/BigtableDatabase'; |
| 7 | +import ora from 'ora'; |
| 8 | +import { makeSignedVAAsRowKey } from '../src/databases/utils'; |
| 9 | +import { ChainId } from '@certusone/wormhole-sdk'; |
| 10 | + |
| 11 | +// This script writes all VAAs from a csv file compatible with the guardian `sign-existing-vaas-csv` admin command to bigtable |
| 12 | + |
| 13 | +const CHUNK_SIZE = 10000; |
| 14 | + |
| 15 | +interface SignedVAAsRow { |
| 16 | + key: string; |
| 17 | + data: { |
| 18 | + info: { |
| 19 | + bytes: { value: Buffer; timestamp: '0' }; |
| 20 | + }; |
| 21 | + }; |
| 22 | +} |
| 23 | + |
| 24 | +(async () => { |
| 25 | + try { |
| 26 | + const vaaCsvFilename = assertEnvironmentVariable('VAA_CSV_FILE'); |
| 27 | + |
| 28 | + const bt = new BigtableDatabase(); |
| 29 | + if (!bt.bigtable) { |
| 30 | + throw new Error('bigtable is undefined'); |
| 31 | + } |
| 32 | + const vaaTableId = assertEnvironmentVariable('BIGTABLE_SIGNED_VAAS_TABLE_ID'); |
| 33 | + const instance = bt.bigtable.instance(bt.instanceId); |
| 34 | + const vaaTable = instance.table(vaaTableId); |
| 35 | + |
| 36 | + const fileStream = createReadStream(vaaCsvFilename, { encoding: 'utf8' }); |
| 37 | + |
| 38 | + const rl = createInterface({ |
| 39 | + input: fileStream, |
| 40 | + crlfDelay: Infinity, |
| 41 | + }); |
| 42 | + // Note: we use the crlfDelay option to recognize all instances of CR LF |
| 43 | + // ('\r\n') in input.txt as a single line break. |
| 44 | + |
| 45 | + let rows: SignedVAAsRow[] = []; |
| 46 | + let numWritten = 0; |
| 47 | + let log = ora('Writing VAAs to bigtable...').start(); |
| 48 | + for await (const line of rl) { |
| 49 | + const split = line.split(','); |
| 50 | + const key = split[0]; |
| 51 | + const vaa = split[1]; |
| 52 | + const splitKey = key.split(':'); |
| 53 | + const chain = Number(splitKey[0]); |
| 54 | + const emitter = splitKey[1]; |
| 55 | + const sequence = splitKey[2]; |
| 56 | + const rowKey = makeSignedVAAsRowKey(chain as ChainId, emitter, sequence); |
| 57 | + rows.push({ |
| 58 | + key: rowKey, |
| 59 | + data: { |
| 60 | + info: { |
| 61 | + bytes: { value: Buffer.from(vaa, 'hex'), timestamp: '0' }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }); |
| 65 | + if (rows.length == CHUNK_SIZE) { |
| 66 | + await vaaTable.insert(rows); |
| 67 | + numWritten += rows.length; |
| 68 | + log.text = `Wrote ${numWritten} VAAs`; |
| 69 | + rows = []; |
| 70 | + } |
| 71 | + } |
| 72 | + if (rows.length > 0) { |
| 73 | + await vaaTable.insert(rows); |
| 74 | + numWritten += rows.length; |
| 75 | + } |
| 76 | + log.succeed(`Wrote ${numWritten} VAAs`); |
| 77 | + } catch (e) { |
| 78 | + console.error(e); |
| 79 | + } |
| 80 | +})(); |
0 commit comments