Skip to content

Commit dcdcf14

Browse files
committed
fix: bugged re-org logic introduced in microblock implementation #640
1 parent 95cd1be commit dcdcf14

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

Diff for: src/datastore/postgres-store.ts

+26-6
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,7 @@ export class PgDataStore
850850
const { orphanedMicroblocks } = await this.findUnanchoredMicroblocksAtChainTip(
851851
client,
852852
currentMicroblockTip.parent_index_block_hash,
853+
'',
853854
currentMicroblockTip
854855
);
855856
if (orphanedMicroblocks.length > 0) {
@@ -858,6 +859,7 @@ export class PgDataStore
858859
const { orphanedMicroblockTxs } = await this.handleMicroOrphan(
859860
client,
860861
currentMicroblockTip.parent_index_block_hash,
862+
'',
861863
true,
862864
orphanedMicroblocks
863865
);
@@ -915,11 +917,14 @@ export class PgDataStore
915917
`
916918
SELECT microblock_hash
917919
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)
919923
`,
920924
[
921925
hexToBuffer(data.block.parent_index_block_hash),
922926
data.microblocks.map(mb => hexToBuffer(mb.microblock_hash)),
927+
hexToBuffer(data.block.index_block_hash),
923928
]
924929
);
925930
const existingMicroblockHashes = new Set(
@@ -976,13 +981,15 @@ export class PgDataStore
976981
} = await this.findUnanchoredMicroblocksAtChainTip(
977982
client,
978983
data.block.parent_index_block_hash,
984+
data.block.index_block_hash,
979985
acceptedMicroblockTip
980986
);
981987

982988
if (orphanedMicroblocks.length > 0) {
983989
const { orphanedMicroblockTxs } = await this.handleMicroOrphan(
984990
client,
985991
data.block.parent_index_block_hash,
992+
data.block.index_block_hash,
986993
isCanonical,
987994
orphanedMicroblocks
988995
);
@@ -1004,13 +1011,15 @@ export class PgDataStore
10041011
SET microblock_canonical = true, canonical = $1, index_block_hash = $2, block_hash = $3
10051012
WHERE parent_index_block_hash = $4
10061013
AND microblock_hash = ANY($5)
1014+
AND (index_block_hash = $6 OR index_block_hash = '\\x'::bytea)
10071015
`,
10081016
[
10091017
isCanonical,
10101018
hexToBuffer(data.block.index_block_hash),
10111019
hexToBuffer(data.block.block_hash),
10121020
hexToBuffer(data.block.parent_index_block_hash),
10131021
acceptedMicroblocks.map(mb => hexToBuffer(mb)),
1022+
hexToBuffer(data.block.index_block_hash),
10141023
]
10151024
);
10161025
// 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
10271036
UPDATE txs
10281037
SET microblock_canonical = true, canonical = $1, index_block_hash = $2, block_hash = $3, burn_block_time = $4
10291038
WHERE parent_index_block_hash = $5
1039+
AND (index_block_hash = $2 OR index_block_hash = '\\x'::bytea)
10301040
AND microblock_hash = ANY($6)
10311041
RETURNING tx_id, microblock_hash
10321042
`,
@@ -1189,6 +1199,7 @@ export class PgDataStore
11891199
async handleMicroOrphan(
11901200
client: ClientBase,
11911201
parentIndexBlockHash: string,
1202+
indexBlockHash: string,
11921203
isCanonical: boolean,
11931204
orphanedMicroblocks: string[]
11941205
): Promise<{ orphanedMicroblockTxs: string[] }> {
@@ -1199,8 +1210,13 @@ export class PgDataStore
11991210
SET microblock_canonical = false
12001211
WHERE parent_index_block_hash = $1
12011212
AND microblock_hash = ANY($2)
1213+
AND (index_block_hash = $3 OR index_block_hash = '\\x'::bytea)
12021214
`,
1203-
[hexToBuffer(parentIndexBlockHash), orphanedMicroblocks.map(mb => hexToBuffer(mb))]
1215+
[
1216+
hexToBuffer(parentIndexBlockHash),
1217+
orphanedMicroblocks.map(mb => hexToBuffer(mb)),
1218+
hexToBuffer(indexBlockHash),
1219+
]
12041220
);
12051221
if (orphanMicroblocksQuery.rowCount !== orphanedMicroblocks.length) {
12061222
throw new Error(`Unexpected number of rows updated when setting microblock_canonical`);
@@ -1215,12 +1231,14 @@ export class PgDataStore
12151231
SET microblock_canonical = false, canonical = $1
12161232
WHERE parent_index_block_hash = $2
12171233
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
12191236
`,
12201237
[
12211238
isCanonical,
12221239
hexToBuffer(parentIndexBlockHash),
12231240
orphanedMicroblocks.map(mb => hexToBuffer(mb)),
1241+
hexToBuffer(indexBlockHash),
12241242
]
12251243
);
12261244
orphanedMbTxsQuery.rows.forEach(row => {
@@ -1262,6 +1280,7 @@ export class PgDataStore
12621280
async findUnanchoredMicroblocksAtChainTip(
12631281
client: ClientBase,
12641282
parentIndexBlockHash: string,
1283+
indexBlockHash: string,
12651284
microblockChainTip: DbMicroblock | undefined
12661285
): Promise<{ acceptedMicroblocks: string[]; orphanedMicroblocks: string[] }> {
12671286
// Get any microblocks that this anchor block is responsible for accepting or rejecting.
@@ -1272,8 +1291,9 @@ export class PgDataStore
12721291
SELECT ${MICROBLOCK_COLUMNS}
12731292
FROM microblocks
12741293
WHERE parent_index_block_hash = $1
1294+
AND (index_block_hash = $2 OR index_block_hash = '\\x'::bytea)
12751295
`,
1276-
[hexToBuffer(parentIndexBlockHash)]
1296+
[hexToBuffer(parentIndexBlockHash), hexToBuffer(indexBlockHash)]
12771297
);
12781298
const candidateMicroblocks = mbQuery.rows.map(row => this.parseMicroblockQueryResult(row));
12791299

@@ -2755,8 +2775,8 @@ export class PgDataStore
27552775
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19,
27562776
$20, $21, $22, $23, $24, $25, $26, $27, $28, $29, $30, $31, $32, $33, $34, $35, $36, $37
27572777
)
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
27602780
`,
27612781
[
27622782
hexToBuffer(tx.tx_id),

0 commit comments

Comments
 (0)