@@ -2,6 +2,7 @@ import { Bigtable } from '@google-cloud/bigtable';
2
2
import { PubSub } from '@google-cloud/pubsub' ;
3
3
import { Chain , chainToChainId } from '@wormhole-foundation/sdk-base' ;
4
4
import {
5
+ Mode ,
5
6
assertEnvironmentVariable ,
6
7
chunkArray ,
7
8
sleep ,
@@ -38,16 +39,25 @@ export class BigtableDatabase extends Database {
38
39
firestoreDb : FirebaseFirestore . Firestore ;
39
40
latestCollectionName : string ;
40
41
latestNTTCollectionName : string ;
42
+ latestFTCollectionName : string ;
41
43
pubsubSignedVAATopic : string ;
44
+ collectionNameByMode : { [ key in Mode ] : string } ;
42
45
pubsub : PubSub ;
43
46
constructor ( ) {
44
47
super ( ) ;
45
48
this . msgTableId = assertEnvironmentVariable ( 'BIGTABLE_TABLE_ID' ) ;
46
49
this . signedVAAsTableId = assertEnvironmentVariable ( 'BIGTABLE_SIGNED_VAAS_TABLE_ID' ) ;
47
50
this . vaasByTxHashTableId = assertEnvironmentVariable ( 'BIGTABLE_VAAS_BY_TX_HASH_TABLE_ID' ) ;
48
51
this . instanceId = assertEnvironmentVariable ( 'BIGTABLE_INSTANCE_ID' ) ;
52
+ // TODO: make these const?
49
53
this . latestCollectionName = assertEnvironmentVariable ( 'FIRESTORE_LATEST_COLLECTION' ) ;
50
54
this . latestNTTCollectionName = assertEnvironmentVariable ( 'FIRESTORE_LATEST_NTT_COLLECTION' ) ;
55
+ this . latestFTCollectionName = assertEnvironmentVariable ( 'FIRESTORE_LATEST_FT_COLLECTION' ) ;
56
+ this . collectionNameByMode = {
57
+ vaa : this . latestCollectionName ,
58
+ ntt : this . latestNTTCollectionName ,
59
+ ft : this . latestFTCollectionName ,
60
+ } ;
51
61
this . pubsubSignedVAATopic = assertEnvironmentVariable ( 'PUBSUB_SIGNED_VAA_TOPIC' ) ;
52
62
try {
53
63
this . bigtable = new Bigtable ( ) ;
@@ -63,11 +73,15 @@ export class BigtableDatabase extends Database {
63
73
}
64
74
}
65
75
66
- async getLastBlockByChain ( chain : Chain , isNTT : boolean ) : Promise < string | null > {
76
+ async getLastBlockByChain ( chain : Chain , mode : Mode ) : Promise < string | null > {
67
77
const chainId = chainToChainId ( chain ) ;
68
- const lastObservedBlock = isNTT
69
- ? this . firestoreDb . collection ( this . latestNTTCollectionName ) . doc ( chainId . toString ( ) )
70
- : this . firestoreDb . collection ( this . latestCollectionName ) . doc ( chainId . toString ( ) ) ;
78
+
79
+ const collectionName = this . collectionNameByMode [ mode ] ;
80
+ if ( ! collectionName ) {
81
+ throw new Error ( `Unknown mode: ${ mode } ` ) ;
82
+ }
83
+
84
+ const lastObservedBlock = this . firestoreDb . collection ( collectionName ) . doc ( chainId . toString ( ) ) ;
71
85
const lastObservedBlockByChain = await lastObservedBlock . get ( ) ;
72
86
const blockKeyData = lastObservedBlockByChain . data ( ) ;
73
87
const lastBlockKey = blockKeyData ?. lastBlockKey ;
@@ -79,16 +93,19 @@ export class BigtableDatabase extends Database {
79
93
return null ;
80
94
}
81
95
82
- async storeLatestBlock ( chain : Chain , lastBlockKey : string , isNTT : boolean ) : Promise < void > {
96
+ async storeLatestBlock ( chain : Chain , lastBlockKey : string , mode : Mode ) : Promise < void > {
83
97
if ( this . firestoreDb === undefined ) {
84
98
this . logger . error ( 'no firestore db set' ) ;
85
99
return ;
86
100
}
87
101
const chainId = chainToChainId ( chain ) ;
88
102
this . logger . info ( `storing last block=${ lastBlockKey } for chain=${ chainId } ` ) ;
89
- const lastObservedBlock = isNTT
90
- ? this . firestoreDb . collection ( this . latestNTTCollectionName ) . doc ( `${ chainId . toString ( ) } ` )
91
- : this . firestoreDb . collection ( this . latestCollectionName ) . doc ( `${ chainId . toString ( ) } ` ) ;
103
+ const collectionName = this . collectionNameByMode [ mode ] ;
104
+ if ( ! collectionName ) {
105
+ throw new Error ( `Unknown mode: ${ mode } ` ) ;
106
+ }
107
+
108
+ const lastObservedBlock = this . firestoreDb . collection ( collectionName ) . doc ( chainId . toString ( ) ) ;
92
109
await lastObservedBlock . set ( { lastBlockKey } ) ;
93
110
}
94
111
@@ -159,7 +176,7 @@ export class BigtableDatabase extends Database {
159
176
if ( blockKeys . length ) {
160
177
const lastBlockKey = blockKeys [ blockKeys . length - 1 ] ;
161
178
this . logger . info ( `for chain=${ chain } , storing last bigtable block=${ lastBlockKey } ` ) ;
162
- await this . storeLatestBlock ( chain , lastBlockKey , false ) ;
179
+ await this . storeLatestBlock ( chain , lastBlockKey , 'vaa' ) ;
163
180
}
164
181
}
165
182
}
0 commit comments