Skip to content

Commit 8f3f12c

Browse files
authored
Merge pull request #2364 from hirosystems/develop
release to master
2 parents 3ffcc11 + 32f879f commit 8f3f12c

12 files changed

+307
-135
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* eslint-disable camelcase */
2+
3+
exports.shorthands = undefined;
4+
5+
exports.up = pgm => {
6+
pgm.sql(`
7+
UPDATE burnchain_rewards
8+
SET canonical = COALESCE(
9+
(
10+
SELECT canonical
11+
FROM blocks
12+
WHERE blocks.burn_block_hash = burnchain_rewards.burn_block_hash
13+
LIMIT 1
14+
),
15+
false
16+
)
17+
`);
18+
};
19+
20+
exports.down = pgm => {};

src/datastore/helpers.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,6 +1363,10 @@ export function newReOrgUpdatedEntities(): ReOrgUpdatedEntities {
13631363
};
13641364
}
13651365

1366+
export function removeNullBytes(str: string): string {
1367+
return str.replace(/\x00/g, '');
1368+
}
1369+
13661370
/**
13671371
* Priority queue for parallel Postgres write query execution. This helps performance because it
13681372
* parallelizes the work postgres.js has to do when serializing JS types to PG types.

src/datastore/pg-store.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2051,19 +2051,25 @@ export class PgStore extends BasePgStore {
20512051
>`
20522052
WITH ordered_pox_events AS (
20532053
SELECT
2054-
stacker, pox_addr, amount_ustx, unlock_burn_height::integer, tx_id,
2054+
stacker, pox_addr, amount_ustx, unlock_burn_height::integer, tx_id, name,
20552055
block_height, microblock_sequence, tx_index, event_index
20562056
FROM ${sql(args.poxTable)}
20572057
WHERE
2058-
canonical = true AND microblock_canonical = true AND
2059-
name = ${SyntheticPoxEventName.DelegateStx} AND delegate_to = ${args.delegator} AND
2060-
block_height <= ${args.blockHeight} AND block_height > ${args.afterBlockHeight} AND
2061-
(unlock_burn_height > ${args.burnBlockHeight} OR unlock_burn_height IS NULL)
2058+
canonical = true
2059+
AND microblock_canonical = true
2060+
AND delegate_to = ${args.delegator}
2061+
AND name IN (
2062+
${SyntheticPoxEventName.DelegateStx},
2063+
${SyntheticPoxEventName.RevokeDelegateStx}
2064+
)
2065+
AND block_height <= ${args.blockHeight}
2066+
AND block_height > ${args.afterBlockHeight}
2067+
AND (unlock_burn_height > ${args.burnBlockHeight} OR unlock_burn_height IS NULL)
20622068
ORDER BY stacker, block_height DESC, microblock_sequence DESC, tx_index DESC, event_index DESC
20632069
),
20642070
distinct_rows AS (
20652071
SELECT DISTINCT ON (stacker)
2066-
stacker, pox_addr, amount_ustx, unlock_burn_height, tx_id,
2072+
stacker, pox_addr, amount_ustx, unlock_burn_height, tx_id, name,
20672073
block_height, microblock_sequence, tx_index, event_index
20682074
FROM ordered_pox_events
20692075
ORDER BY stacker, block_height DESC, microblock_sequence DESC, tx_index DESC, event_index DESC
@@ -2072,6 +2078,7 @@ export class PgStore extends BasePgStore {
20722078
stacker, pox_addr, amount_ustx, unlock_burn_height, block_height::integer, tx_id,
20732079
COUNT(*) OVER()::integer AS total_rows
20742080
FROM distinct_rows
2081+
WHERE name = ${SyntheticPoxEventName.DelegateStx}
20752082
ORDER BY block_height DESC, microblock_sequence DESC, tx_index DESC, event_index DESC
20762083
LIMIT ${args.limit}
20772084
OFFSET ${args.offset}

src/datastore/pg-write-store.ts

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ import {
7878
validateZonefileHash,
7979
newReOrgUpdatedEntities,
8080
PgWriteQueue,
81+
removeNullBytes,
8182
} from './helpers';
8283
import { PgNotifier } from './pg-notifier';
8384
import { MIGRATIONS_DIR, PgStore } from './pg-store';
@@ -203,6 +204,13 @@ export class PgWriteStore extends PgStore {
203204
}
204205

205206
async storeRawEventRequest(eventPath: string, payload: any): Promise<void> {
207+
if (eventPath === '/new_block' && typeof payload === 'object') {
208+
for (const tx of payload.transactions) {
209+
if ('vm_error' in tx && tx.vm_error) {
210+
tx.vm_error = removeNullBytes(tx.vm_error);
211+
}
212+
}
213+
}
206214
await this.sqlWriteTransaction(async sql => {
207215
const insertResult = await sql<
208216
{
@@ -1930,35 +1938,8 @@ export class PgWriteStore extends PgStore {
19301938
};
19311939
}
19321940

1933-
async updateBurnchainRewards({
1934-
burnchainBlockHash,
1935-
burnchainBlockHeight,
1936-
rewards,
1937-
}: {
1938-
burnchainBlockHash: string;
1939-
burnchainBlockHeight: number;
1940-
rewards: DbBurnchainReward[];
1941-
}): Promise<void> {
1941+
async updateBurnchainRewards({ rewards }: { rewards: DbBurnchainReward[] }): Promise<void> {
19421942
return await this.sqlWriteTransaction(async sql => {
1943-
const existingRewards = await sql<
1944-
{
1945-
reward_recipient: string;
1946-
reward_amount: string;
1947-
}[]
1948-
>`
1949-
UPDATE burnchain_rewards
1950-
SET canonical = false
1951-
WHERE canonical = true AND
1952-
(burn_block_hash = ${burnchainBlockHash}
1953-
OR burn_block_height >= ${burnchainBlockHeight})
1954-
`;
1955-
1956-
if (existingRewards.count > 0) {
1957-
logger.warn(
1958-
`Invalidated ${existingRewards.count} burnchain rewards after fork detected at burnchain block ${burnchainBlockHash}`
1959-
);
1960-
}
1961-
19621943
for (const reward of rewards) {
19631944
const values: BurnchainRewardInsertValues = {
19641945
canonical: true,
@@ -2062,7 +2043,9 @@ export class PgWriteStore extends PgStore {
20622043
token_transfer_memo: tx.token_transfer_memo ?? null,
20632044
smart_contract_clarity_version: tx.smart_contract_clarity_version ?? null,
20642045
smart_contract_contract_id: tx.smart_contract_contract_id ?? null,
2065-
smart_contract_source_code: tx.smart_contract_source_code ?? null,
2046+
smart_contract_source_code: tx.smart_contract_source_code
2047+
? removeNullBytes(tx.smart_contract_source_code)
2048+
: null,
20662049
contract_call_contract_id: tx.contract_call_contract_id ?? null,
20672050
contract_call_function_name: tx.contract_call_function_name ?? null,
20682051
contract_call_function_args: tx.contract_call_function_args ?? null,
@@ -2085,7 +2068,7 @@ export class PgWriteStore extends PgStore {
20852068
execution_cost_runtime: tx.execution_cost_runtime,
20862069
execution_cost_write_count: tx.execution_cost_write_count,
20872070
execution_cost_write_length: tx.execution_cost_write_length,
2088-
vm_error: tx.vm_error ?? null,
2071+
vm_error: tx.vm_error ? removeNullBytes(tx.vm_error) : null,
20892072
}));
20902073

20912074
let count = 0;
@@ -3615,6 +3598,11 @@ export class PgWriteStore extends PgStore {
36153598
if (orphanedBlockResult.length > 0) {
36163599
const orphanedBlocks = orphanedBlockResult.map(b => parseBlockQueryResult(b));
36173600
for (const orphanedBlock of orphanedBlocks) {
3601+
await sql`
3602+
UPDATE burnchain_rewards
3603+
SET canonical = false
3604+
WHERE canonical = true AND burn_block_hash = ${orphanedBlock.burn_block_hash}
3605+
`;
36183606
const microCanonicalUpdateResult = await this.updateMicroCanonical(sql, {
36193607
isCanonical: false,
36203608
blockHeight: orphanedBlock.block_height,

src/datastore/redis-notifier.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export class RedisNotifier {
5454
},
5555
};
5656
logger.info(message, 'RedisNotifier broadcasting index progress message');
57-
await this.redis.rpush(this.queue, JSON.stringify(message));
57+
await this.redis.xadd(this.queue, '*', 'data', JSON.stringify(message));
5858
}
5959

6060
async close() {

src/event-stream/event-server.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,6 @@ async function handleBurnBlockMessage(
121121
return slotHolder;
122122
});
123123
await db.updateBurnchainRewards({
124-
burnchainBlockHash: burnBlockMsg.burn_block_hash,
125-
burnchainBlockHeight: burnBlockMsg.burn_block_height,
126124
rewards: rewards,
127125
});
128126
await db.updateBurnchainRewardSlotHolders({

tests/2.5/pox-4-delegate-revoked-stacking.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import {
2424
fetchGet,
2525
readOnlyFnCall,
2626
standByForPoxCycle,
27-
standByForPoxCycleEnd,
2827
standByForTx,
2928
standByForTxSuccess,
3029
testEnv,
@@ -207,6 +206,19 @@ describe('PoX-4 - Delegate Revoked Stacking', () => {
207206
);
208207
const delegatedAmount = BigInt(getDelegationInfo.data['amount-ustx'].value);
209208
expect(delegatedAmount).toBe(DELEGATE_HALF_AMOUNT);
209+
210+
// validate pool delegations
211+
const stackersRes: any = await fetchGet(`/extended/v1/pox4/${POOL.stxAddr}/delegations`);
212+
expect(stackersRes).toBeDefined();
213+
expect(stackersRes.total).toBe(1);
214+
expect(stackersRes.results).toHaveLength(1);
215+
expect(stackersRes.results[0]).toEqual({
216+
amount_ustx: DELEGATE_HALF_AMOUNT.toString(),
217+
pox_addr: STACKER.btcTestnetAddr,
218+
stacker: STACKER.stxAddr,
219+
tx_id: delegateStxDbTx.tx_id,
220+
block_height: delegateStxDbTx.block_height,
221+
});
210222
});
211223

212224
test('Perform delegate-stack-stx', async () => {
@@ -311,6 +323,12 @@ describe('PoX-4 - Delegate Revoked Stacking', () => {
311323
const coreBalanceInfo = await testEnv.client.getAccount(STACKER.stxAddr);
312324
expect(BigInt(coreBalanceInfo.locked)).toBe(DELEGATE_HALF_AMOUNT);
313325
expect(coreBalanceInfo.unlock_height).toBeGreaterThan(0);
326+
327+
// validate pool delegation no longer exists
328+
const stackersRes: any = await fetchGet(`/extended/v1/pox4/${POOL.stxAddr}/delegations`);
329+
expect(stackersRes).toBeDefined();
330+
expect(stackersRes.total).toBe(0);
331+
expect(stackersRes.results).toHaveLength(0);
314332
});
315333

316334
test('Try to perform delegate-stack-stx - while revoked', async () => {

tests/api/burnchain.test.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,9 @@ describe('burnchain tests', () => {
201201
reward_index: 2,
202202
};
203203
await db.updateBurnchainRewards({
204-
burnchainBlockHash: reward1.burn_block_hash,
205-
burnchainBlockHeight: reward1.burn_block_height,
206204
rewards: [reward1, reward2],
207205
});
208206
await db.updateBurnchainRewards({
209-
burnchainBlockHash: reward3.burn_block_hash,
210-
burnchainBlockHeight: reward3.burn_block_height,
211207
rewards: [reward3, reward4, reward5],
212208
});
213209

@@ -282,18 +278,12 @@ describe('burnchain tests', () => {
282278
reward_index: 0,
283279
};
284280
await db.updateBurnchainRewards({
285-
burnchainBlockHash: reward1.burn_block_hash,
286-
burnchainBlockHeight: reward1.burn_block_height,
287281
rewards: [reward1],
288282
});
289283
await db.updateBurnchainRewards({
290-
burnchainBlockHash: reward2.burn_block_hash,
291-
burnchainBlockHeight: reward2.burn_block_height,
292284
rewards: [reward2],
293285
});
294286
await db.updateBurnchainRewards({
295-
burnchainBlockHash: reward3.burn_block_hash,
296-
burnchainBlockHeight: reward3.burn_block_height,
297287
rewards: [reward3],
298288
});
299289
const rewardResult = await supertest(api.server).get(
@@ -320,8 +310,6 @@ describe('burnchain tests', () => {
320310
reward_index: 0,
321311
};
322312
await db.updateBurnchainRewards({
323-
burnchainBlockHash: reward1.burn_block_hash,
324-
burnchainBlockHeight: reward1.burn_block_height,
325313
rewards: [reward1],
326314
});
327315
const rewardResult = await supertest(api.server).get(`/extended/v1/burnchain/rewards/${addr1}`);
@@ -360,8 +348,6 @@ describe('burnchain tests', () => {
360348
reward_index: 0,
361349
};
362350
await db.updateBurnchainRewards({
363-
burnchainBlockHash: reward1.burn_block_hash,
364-
burnchainBlockHeight: reward1.burn_block_height,
365351
rewards: [reward1],
366352
});
367353
const rewardResult = await supertest(api.server).get(
@@ -402,8 +388,6 @@ describe('burnchain tests', () => {
402388
reward_index: 0,
403389
};
404390
await db.updateBurnchainRewards({
405-
burnchainBlockHash: reward1.burn_block_hash,
406-
burnchainBlockHeight: reward1.burn_block_height,
407391
rewards: [reward1],
408392
});
409393
const rewardResult = await supertest(api.server).get(
@@ -444,8 +428,6 @@ describe('burnchain tests', () => {
444428
reward_index: 0,
445429
};
446430
await db.updateBurnchainRewards({
447-
burnchainBlockHash: reward1.burn_block_hash,
448-
burnchainBlockHeight: reward1.burn_block_height,
449431
rewards: [reward1],
450432
});
451433
const rewardResult = await supertest(api.server).get(

0 commit comments

Comments
 (0)