@@ -850,6 +850,7 @@ export class PgDataStore
850
850
const { orphanedMicroblocks } = await this . findUnanchoredMicroblocksAtChainTip (
851
851
client ,
852
852
currentMicroblockTip . parent_index_block_hash ,
853
+ '' ,
853
854
currentMicroblockTip
854
855
) ;
855
856
if ( orphanedMicroblocks . length > 0 ) {
@@ -858,6 +859,7 @@ export class PgDataStore
858
859
const { orphanedMicroblockTxs } = await this . handleMicroOrphan (
859
860
client ,
860
861
currentMicroblockTip . parent_index_block_hash ,
862
+ '' ,
861
863
true ,
862
864
orphanedMicroblocks
863
865
) ;
@@ -915,11 +917,14 @@ export class PgDataStore
915
917
`
916
918
SELECT microblock_hash
917
919
FROM microblocks
918
- WHERE parent_index_block_hash = $1 AND microblock_hash = ANY($2)
920
+ WHERE parent_index_block_hash = $1
921
+ AND microblock_hash = ANY($2)
922
+ AND (index_block_hash = $3 OR index_block_hash = '\\x'::bytea)
919
923
` ,
920
924
[
921
925
hexToBuffer ( data . block . parent_index_block_hash ) ,
922
926
data . microblocks . map ( mb => hexToBuffer ( mb . microblock_hash ) ) ,
927
+ hexToBuffer ( data . block . index_block_hash ) ,
923
928
]
924
929
) ;
925
930
const existingMicroblockHashes = new Set (
@@ -976,13 +981,15 @@ export class PgDataStore
976
981
} = await this . findUnanchoredMicroblocksAtChainTip (
977
982
client ,
978
983
data . block . parent_index_block_hash ,
984
+ data . block . index_block_hash ,
979
985
acceptedMicroblockTip
980
986
) ;
981
987
982
988
if ( orphanedMicroblocks . length > 0 ) {
983
989
const { orphanedMicroblockTxs } = await this . handleMicroOrphan (
984
990
client ,
985
991
data . block . parent_index_block_hash ,
992
+ data . block . index_block_hash ,
986
993
isCanonical ,
987
994
orphanedMicroblocks
988
995
) ;
@@ -1004,13 +1011,15 @@ export class PgDataStore
1004
1011
SET microblock_canonical = true, canonical = $1, index_block_hash = $2, block_hash = $3
1005
1012
WHERE parent_index_block_hash = $4
1006
1013
AND microblock_hash = ANY($5)
1014
+ AND (index_block_hash = $6 OR index_block_hash = '\\x'::bytea)
1007
1015
` ,
1008
1016
[
1009
1017
isCanonical ,
1010
1018
hexToBuffer ( data . block . index_block_hash ) ,
1011
1019
hexToBuffer ( data . block . block_hash ) ,
1012
1020
hexToBuffer ( data . block . parent_index_block_hash ) ,
1013
1021
acceptedMicroblocks . map ( mb => hexToBuffer ( mb ) ) ,
1022
+ hexToBuffer ( data . block . index_block_hash ) ,
1014
1023
]
1015
1024
) ;
1016
1025
// Note: this assumes the stacks-node will never send the same combination of (microblock_hash, parent_index_block_hash) more than once.
@@ -1027,6 +1036,7 @@ export class PgDataStore
1027
1036
UPDATE txs
1028
1037
SET microblock_canonical = true, canonical = $1, index_block_hash = $2, block_hash = $3, burn_block_time = $4
1029
1038
WHERE parent_index_block_hash = $5
1039
+ AND (index_block_hash = $2 OR index_block_hash = '\\x'::bytea)
1030
1040
AND microblock_hash = ANY($6)
1031
1041
RETURNING tx_id, microblock_hash
1032
1042
` ,
@@ -1189,6 +1199,7 @@ export class PgDataStore
1189
1199
async handleMicroOrphan (
1190
1200
client : ClientBase ,
1191
1201
parentIndexBlockHash : string ,
1202
+ indexBlockHash : string ,
1192
1203
isCanonical : boolean ,
1193
1204
orphanedMicroblocks : string [ ]
1194
1205
) : Promise < { orphanedMicroblockTxs : string [ ] } > {
@@ -1199,8 +1210,13 @@ export class PgDataStore
1199
1210
SET microblock_canonical = false
1200
1211
WHERE parent_index_block_hash = $1
1201
1212
AND microblock_hash = ANY($2)
1213
+ AND (index_block_hash = $3 OR index_block_hash = '\\x'::bytea)
1202
1214
` ,
1203
- [ hexToBuffer ( parentIndexBlockHash ) , orphanedMicroblocks . map ( mb => hexToBuffer ( mb ) ) ]
1215
+ [
1216
+ hexToBuffer ( parentIndexBlockHash ) ,
1217
+ orphanedMicroblocks . map ( mb => hexToBuffer ( mb ) ) ,
1218
+ hexToBuffer ( indexBlockHash ) ,
1219
+ ]
1204
1220
) ;
1205
1221
if ( orphanMicroblocksQuery . rowCount !== orphanedMicroblocks . length ) {
1206
1222
throw new Error ( `Unexpected number of rows updated when setting microblock_canonical` ) ;
@@ -1215,12 +1231,14 @@ export class PgDataStore
1215
1231
SET microblock_canonical = false, canonical = $1
1216
1232
WHERE parent_index_block_hash = $2
1217
1233
AND microblock_hash = ANY($3)
1218
- RETURNING tx_id, microblock_hash
1234
+ AND index_block_hash = $4
1235
+ RETURNING tx_id, microblock_hash, block_hash
1219
1236
` ,
1220
1237
[
1221
1238
isCanonical ,
1222
1239
hexToBuffer ( parentIndexBlockHash ) ,
1223
1240
orphanedMicroblocks . map ( mb => hexToBuffer ( mb ) ) ,
1241
+ hexToBuffer ( indexBlockHash ) ,
1224
1242
]
1225
1243
) ;
1226
1244
orphanedMbTxsQuery . rows . forEach ( row => {
@@ -1262,6 +1280,7 @@ export class PgDataStore
1262
1280
async findUnanchoredMicroblocksAtChainTip (
1263
1281
client : ClientBase ,
1264
1282
parentIndexBlockHash : string ,
1283
+ indexBlockHash : string ,
1265
1284
microblockChainTip : DbMicroblock | undefined
1266
1285
) : Promise < { acceptedMicroblocks : string [ ] ; orphanedMicroblocks : string [ ] } > {
1267
1286
// Get any microblocks that this anchor block is responsible for accepting or rejecting.
@@ -1272,8 +1291,9 @@ export class PgDataStore
1272
1291
SELECT ${ MICROBLOCK_COLUMNS }
1273
1292
FROM microblocks
1274
1293
WHERE parent_index_block_hash = $1
1294
+ AND (index_block_hash = $2 OR index_block_hash = '\\x'::bytea)
1275
1295
` ,
1276
- [ hexToBuffer ( parentIndexBlockHash ) ]
1296
+ [ hexToBuffer ( parentIndexBlockHash ) , hexToBuffer ( indexBlockHash ) ]
1277
1297
) ;
1278
1298
const candidateMicroblocks = mbQuery . rows . map ( row => this . parseMicroblockQueryResult ( row ) ) ;
1279
1299
@@ -2755,8 +2775,8 @@ export class PgDataStore
2755
2775
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19,
2756
2776
$20, $21, $22, $23, $24, $25, $26, $27, $28, $29, $30, $31, $32, $33, $34, $35, $36, $37
2757
2777
)
2758
- -- ON CONFLICT ON CONSTRAINT unique_tx_id_index_block_hash
2759
- -- DO NOTHING
2778
+ ON CONFLICT ON CONSTRAINT unique_tx_id_index_block_hash
2779
+ DO NOTHING
2760
2780
` ,
2761
2781
[
2762
2782
hexToBuffer ( tx . tx_id ) ,
0 commit comments