@@ -20,16 +20,42 @@ type ErrorType = {
2020
2121export async function validateDataForBlock ( blockNum : number , log = false ) {
2222 let success = true ;
23- const blockTxs = await TransactionStorage . collection . find ( { chain, network, blockHeight : blockNum } ) . toArray ( ) ;
23+ const [ block , blockTxs , blocksForHeight ] = await Promise . all ( [
24+ BitcoinBlockStorage . collection . findOne ( { chain, network, height : blockNum , processed : true } ) ,
25+ TransactionStorage . collection . find ( { chain, network, blockHeight : blockNum } ) . toArray ( ) ,
26+ BitcoinBlockStorage . collection . countDocuments ( {
27+ chain,
28+ network,
29+ height : blockNum ,
30+ processed : true
31+ } )
32+ ] ) ;
2433 const blockTxids = blockTxs . map ( t => t . txid ) ;
25- const coinsForTx = await CoinStorage . collection . find ( { chain, network, mintTxid : { $in : blockTxids } } ) . toArray ( ) ;
26- const mempoolTxs = await TransactionStorage . collection
27- . find ( { chain, network, blockHeight : - 1 , txid : { $in : blockTxids } } )
28- . toArray ( ) ;
34+ const firstHash = blockTxs [ 0 ] . blockHash ;
35+ const [ coinsForTx , mempoolTxs , blocksForHash ] = await Promise . all ( [
36+ CoinStorage . collection . find ( { chain, network, mintTxid : { $in : blockTxids } } ) . toArray ( ) ,
37+ TransactionStorage . collection . find ( { chain, network, blockHeight : - 1 , txid : { $in : blockTxids } } ) . toArray ( ) ,
38+ BitcoinBlockStorage . collection . countDocuments ( { chain, network, hash : firstHash } )
39+ ] ) ;
2940
3041 const seenTxs = { } as { [ txid : string ] : ITransaction } ;
3142 const errors = new Array < ErrorType > ( ) ;
3243
44+ if ( ! block || block . transactionCount != blockTxs . length ) {
45+ const error = {
46+ model : 'block' ,
47+ err : true ,
48+ type : 'CORRUPTED_BLOCK' ,
49+ payload : { blockNum }
50+ } ;
51+
52+ errors . push ( error ) ;
53+
54+ if ( log ) {
55+ console . log ( JSON . stringify ( error ) ) ;
56+ }
57+ }
58+
3359 for ( const tx of mempoolTxs ) {
3460 success = false ;
3561 const error = { model : 'transaction' , err : true , type : 'DUPE_TRANSACTION' , payload : { tx, blockNum } } ;
@@ -125,12 +151,6 @@ export async function validateDataForBlock(blockNum: number, log = false) {
125151 }
126152 }
127153
128- const blocksForHeight = await BitcoinBlockStorage . collection . countDocuments ( {
129- chain,
130- network,
131- height : blockNum ,
132- processed : true
133- } ) ;
134154 if ( blocksForHeight === 0 ) {
135155 success = false ;
136156 const error = {
@@ -161,7 +181,6 @@ export async function validateDataForBlock(blockNum: number, log = false) {
161181 //blocks with same hash
162182 if ( blockTxs . length > 0 ) {
163183 const hashFromTx = blockTxs [ 0 ] . blockHash ;
164- const blocksForHash = await BitcoinBlockStorage . collection . countDocuments ( { chain, network, hash : hashFromTx } ) ;
165184 if ( blocksForHash > 1 ) {
166185 success = false ;
167186 const error = { model : 'block' , err : true , type : 'DUPE_BLOCKHASH' , payload : { hash : hashFromTx , blockNum } } ;
0 commit comments