Skip to content

Commit

Permalink
add serial id
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolas Haimerl authored and Nikolas Haimerl committed Feb 10, 2025
1 parent 5b3f3fa commit b9cee98
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 10 deletions.
12 changes: 2 additions & 10 deletions backend/lib/look-up-payload-cids.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,14 @@ async function updatePayloadCidInActiveDeal (pgPool, deal, newPayloadRetrievalSt
const updateQuery = `
UPDATE active_deals
SET payload_cid = $1, payload_retrievability_state = $2, last_payload_retrieval_attempt = $3
WHERE activated_at_epoch = $4 AND miner_id = $5 AND client_id = $6 AND piece_cid = $7 AND piece_size = $8 AND term_start_epoch = $9 AND term_min = $10 AND term_max = $11 AND sector_id = $12
WHERE id = $4
`
try {
await pgPool.query(updateQuery, [
newPayloadCid,
newPayloadRetrievalState,
lastRetrievalAttemptTimestamp,
deal.activated_at_epoch,
deal.miner_id,
deal.client_id,
deal.piece_cid,
deal.piece_size,
deal.term_start_epoch,
deal.term_min,
deal.term_max,
deal.sector_id
deal.id
])
} catch (error) {
throw Error(util.format('Error updating payload of deal: ', deal), { cause: error })
Expand Down
1 change: 1 addition & 0 deletions backend/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Value } from '@sinclair/typebox/value'
*/
export function convertBlockEventToActiveDealDbEntry (blockEvent) {
return Value.Parse(ActiveDealDbEntry, {
id: undefined, // Auto-generated primary key
activated_at_epoch: blockEvent.height,
miner_id: blockEvent.event.provider,
client_id: blockEvent.event.client,
Expand Down
3 changes: 3 additions & 0 deletions backend/test/deal-observer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('deal-observer-backend', () => {

beforeEach(async () => {
await pgPool.query('DELETE FROM active_deals')
await pgPool.query('ALTER SEQUENCE active_deals_id_seq RESTART WITH 1')
})

it('adds new FIL+ deals from built-in actor events to storage', async () => {
Expand All @@ -43,6 +44,7 @@ describe('deal-observer-backend', () => {
await storeActiveDeals([dbEntry], pgPool)
const actualData = await loadDeals(pgPool, 'SELECT * FROM active_deals')
const expectedData = {
id: 1,
activated_at_epoch: event.height,
miner_id: eventData.provider,
client_id: eventData.client,
Expand Down Expand Up @@ -132,6 +134,7 @@ describe('deal-observer-backend built in actor event observer', () => {

beforeEach(async () => {
await pgPool.query('DELETE FROM active_deals')
await pgPool.query('ALTER SEQUENCE active_deals_id_seq RESTART WITH 1')
})
it('stores all retrievable active deals if database is empty', async () => {
await observeBuiltinActorEvents(pgPool, makeRpcRequest, 10, 0)
Expand Down
7 changes: 7 additions & 0 deletions backend/test/look-up-payload-cids.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ describe('deal-observer-backend look up payload CIDs', () => {

beforeEach(async () => {
await pgPool.query('DELETE FROM active_deals')
await pgPool.query('ALTER SEQUENCE active_deals_id_seq RESTART WITH 1')
const startEpoch = 4622129
for (let blockHeight = startEpoch; blockHeight < startEpoch + 10; blockHeight++) {
await fetchAndStoreActiveDeals(blockHeight, pgPool, makeRpcRequest)
Expand Down Expand Up @@ -103,6 +104,7 @@ describe('deal-observer-backend piece indexer payload retrieval', () => {

beforeEach(async () => {
await pgPool.query('DELETE FROM active_deals')
await pgPool.query('ALTER SEQUENCE active_deals_id_seq RESTART WITH 1')
})
it('piece indexer does not retry to fetch missing payloads if the last retrieval was too recent', async (t) => {
const returnPayload = false
Expand All @@ -113,6 +115,7 @@ describe('deal-observer-backend piece indexer payload retrieval', () => {
}

const deal = Value.Parse(ActiveDealDbEntry, {
id: undefined,
miner_id: 1,
piece_cid: pieceCid,
client_id: 1,
Expand All @@ -128,6 +131,7 @@ describe('deal-observer-backend piece indexer payload retrieval', () => {
})

await storeActiveDeals([deal], pgPool)
deal.id = 1
assert.deepStrictEqual((await loadDeals(pgPool, 'SELECT * FROM active_deals'))[0], deal)
// The payload is unretrievable and the last retrieval timestamp should be updated
await lookUpPayloadCids(fetchMinerId, getDealPayloadCid, pgPool, 10000, now)
Expand All @@ -150,6 +154,7 @@ describe('deal-observer-backend piece indexer payload retrieval', () => {
}
// If we set the last_payload_retrieval_attempt to a value more than three days ago the piece indexer should try again to fetch the payload CID
const deal = Value.Parse(ActiveDealDbEntry, {
id: undefined,
miner_id: 1,
piece_cid: pieceCid,
client_id: 1,
Expand All @@ -170,6 +175,7 @@ describe('deal-observer-backend piece indexer payload retrieval', () => {
// This is the second attempt that failed to fetch the payload CID so the deal should be marked as unretrievable
deal.payload_retrievability_state = PayloadRetrievabilityState.TerminallyUnretrievable
deal.last_payload_retrieval_attempt = new Date(now)
deal.id = 1
assert.deepStrictEqual((await loadDeals(pgPool, 'SELECT * FROM active_deals'))[0], deal)
// Now the piece indexer should no longer call the payload request for this deal
await lookUpPayloadCids(fetchMinerId, getDealPayloadCid, pgPool, 10000, now)
Expand All @@ -185,6 +191,7 @@ describe('deal-observer-backend piece indexer payload retrieval', () => {
}
// If we set the last_payload_retrieval_attempt to a value more than three days ago the piece indexer should try again to fetch the payload CID
const deal = Value.Parse(ActiveDealDbEntry, {
id: 1,
miner_id: 1,
piece_cid: pieceCid,
client_id: 1,
Expand Down
1 change: 1 addition & 0 deletions db/lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const PayloadRetrievabilityState = {
const PayloadRetrievabilityStateType = Type.Enum(PayloadRetrievabilityState)

const ActiveDealDbEntry = Type.Object({
id: Type.Optional(Type.Number()),
activated_at_epoch: Type.Number(),
miner_id: Type.Number(),
client_id: Type.Number(),
Expand Down
1 change: 1 addition & 0 deletions db/migrations/011.do.add-serial-index-to-active-deals.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE active_deals ADD COLUMN id SERIAL PRIMARY KEY;

0 comments on commit b9cee98

Please sign in to comment.