@@ -83,31 +83,29 @@ export class LightBlockCache {
83
83
} else if ( content . type === "disconnected" ) {
84
84
logger . warn ( `Removing block ${ content . block . sequence } ...` ) ;
85
85
const block = lightBlock ( content ) ;
86
- await this . db . put ( "head" , block . previousBlockHash ) ;
87
- await this . db . put ( "headSequence" , ( block . sequence - 1 ) . toString ( ) ) ;
88
- await this . db . del ( block . sequence ) ;
89
- await this . db . del ( block . hash ) ;
86
+ await this . putHead ( block . previousBlockHash , block . sequence - 1 ) ;
87
+ await this . del ( block . sequence . toString ( ) ) ;
88
+ await this . del ( block . hash . toString ( "hex" ) ) ;
90
89
}
91
90
}
92
91
}
93
92
}
94
93
95
94
private async rollbackHead ( ) : Promise < void > {
96
- let head = ( await this . getHeadSequence ( ) ) - 1 ;
97
- if ( ! head ) {
95
+ let headSequence = ( await this . getHeadSequence ( ) ) - 1 ;
96
+ if ( ! headSequence ) {
98
97
logger . error ( "Head sequence is not set. Cannot rollback." ) ;
99
98
return ;
100
99
}
101
100
let block = null ;
102
101
while ( ! block ) {
103
- block = await this . getBlockBySequence ( head ) ;
102
+ block = await this . getLightBlockBySequence ( headSequence ) ;
104
103
if ( ! block ) {
105
- head -= 1 ;
104
+ headSequence -= 1 ;
106
105
}
107
106
}
108
- await this . db . put ( "headSequence" , head . toString ( ) ) ;
109
- await this . db . put ( "head" , block . hash ) ;
110
- logger . info ( `Rolled back head to block sequence ${ head } ` ) ;
107
+ await this . putHead ( block . hash , headSequence ) ;
108
+ logger . info ( `Rolled back head to block sequence ${ headSequence } ` ) ;
111
109
}
112
110
113
111
async cacheBlock ( block : LightBlock ) : Promise < void > {
@@ -118,14 +116,12 @@ export class LightBlockCache {
118
116
) ;
119
117
}
120
118
const hash = block . hash ;
121
- await this . db . put ( hash , LightBlock . encode ( block ) . finish ( ) ) ;
122
- await this . db . put ( block . sequence . toString ( ) , hash ) ;
119
+ await this . putLightBlock ( block ) ;
123
120
const finalizedSequence = await this . getFinalizedBlockSequence ( ) ;
124
121
if ( block . sequence - this . finalityBlockCount > finalizedSequence ) {
125
122
this . putFinalizedBlockSequence ( block . sequence - this . finalityBlockCount ) ;
126
123
}
127
- await this . db . put ( "head" , hash ) ;
128
- await this . db . put ( "headSequence" , block . sequence . toString ( ) ) ;
124
+ await this . putHead ( hash , block . sequence ) ;
129
125
}
130
126
131
127
async getFinalizedBlockSequence ( ) : Promise < number > {
@@ -135,23 +131,32 @@ export class LightBlockCache {
135
131
: this . finalityBlockCount + 1 ;
136
132
}
137
133
138
- async getHead ( ) : Promise < Buffer | null > {
139
- const head = await this . get ( "head" ) ;
140
- return head ? Buffer . from ( head ) : null ;
134
+ async putFinalizedBlockSequence ( sequence : number ) : Promise < void > {
135
+ await this . put ( "finalizedBlockSequence" , Buffer . from ( sequence . toString ( ) ) ) ;
141
136
}
142
137
143
- async putFinalizedBlockSequence ( sequence : number ) : Promise < void > {
144
- await this . db . put ( "finalizedBlockSequence" , sequence . toString ( ) ) ;
138
+ async putHead ( hash : Buffer , sequence : number ) : Promise < void > {
139
+ await this . put ( "head" , hash ) ;
140
+ await this . put ( "headSequence" , Buffer . from ( sequence . toString ( ) ) ) ;
145
141
}
146
142
147
- async getBlockByHash ( hash : string ) : Promise < LightBlock | null > {
148
- const block = await this . get ( hash ) ;
149
- return block ? LightBlock . decode ( block ) : null ;
143
+ async getLightBlock ( hash : Buffer ) : Promise < LightBlock | null > {
144
+ try {
145
+ const data = await this . get ( hash . toString ( "hex" ) ) ;
146
+ if ( ! data ) return null ;
147
+ return LightBlock . decode ( data ) ;
148
+ } catch ( e ) {
149
+ return null ;
150
+ }
150
151
}
151
152
152
- async getBlockBySequence ( sequence : number ) : Promise < LightBlock | null > {
153
+ async getLightBlockBySequence ( sequence : number ) : Promise < LightBlock | null > {
153
154
const hash = await this . get ( sequence . toString ( ) ) ;
154
- return hash ? await this . getBlockByHash ( hash . toString ( ) ) : null ;
155
+ return hash ? await this . getLightBlock ( hash ) : null ;
156
+ }
157
+
158
+ async getHead ( ) : Promise < Buffer | null > {
159
+ return this . get ( "head" ) ;
155
160
}
156
161
157
162
async getHeadSequence ( ) : Promise < number > {
@@ -160,7 +165,7 @@ export class LightBlockCache {
160
165
return Number ( head . toString ( ) ) ;
161
166
}
162
167
163
- async get ( key : string ) : Promise < Uint8Array | null > {
168
+ async get ( key : string ) : Promise < Buffer | null > {
164
169
try {
165
170
const data = await this . db . get ( key ) ;
166
171
return data ;
@@ -169,10 +174,17 @@ export class LightBlockCache {
169
174
}
170
175
}
171
176
172
- async put ( key : string , value : Uint8Array | string ) : Promise < void > {
177
+ private async put ( key : string , value : Buffer ) : Promise < void > {
173
178
await this . db . put ( key , value ) ;
174
179
}
175
180
181
+ async putLightBlock ( block : LightBlock ) : Promise < void > {
182
+ const key = block . hash . toString ( "hex" ) ;
183
+ const value = LightBlock . encode ( block ) . finish ( ) ;
184
+ await this . put ( block . sequence . toString ( ) , block . hash ) ;
185
+ await this . put ( key , Buffer . from ( value ) ) ;
186
+ }
187
+
176
188
async del ( key : string ) : Promise < void > {
177
189
await this . db . del ( key ) ;
178
190
}
0 commit comments