Skip to content

Commit

Permalink
Merge pull request #18 from Cerebellum-Network/feature/add-entity-cre…
Browse files Browse the repository at this point in the history
…ated-at

Add `created_at` for DDC entities
  • Loading branch information
khssnv authored Aug 20, 2024
2 parents e50c17d + f0e6a1c commit c737ebd
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 35 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
/node_modules
/lib
/*Versions.jsonl
.env*
!.env.example
2 changes: 2 additions & 0 deletions .env → .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
DB_NAME=squid
DB_USER=postgres
DB_PASS=postgres
DB_HOST="127.0.0.1"
DB_PORT=5432
GQL_PORT=4350

Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@
/.idea

/data

.env*
!.env.example
15 changes: 15 additions & 0 deletions db/migrations/1724138300437-Data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = class Data1724138300437 {
name = 'Data1724138300437'

async up(db) {
await db.query(`ALTER TABLE "ddc_node" ADD "created_at_block_height" integer NOT NULL DEFAULT -1`)
await db.query(`ALTER TABLE "ddc_cluster" ADD "created_at_block_height" integer NOT NULL DEFAULT -1`)
await db.query(`ALTER TABLE "ddc_bucket" ADD "created_at_block_height" integer NOT NULL DEFAULT -1`)
}

async down(db) {
await db.query(`ALTER TABLE "ddc_node" DROP COLUMN "created_at_block_height"`)
await db.query(`ALTER TABLE "ddc_cluster" DROP COLUMN "created_at_block_height"`)
await db.query(`ALTER TABLE "ddc_bucket" DROP COLUMN "created_at_block_height"`)
}
}
6 changes: 6 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ type Account @entity {
type DdcCluster @entity {
id: ID!

createdAtBlockHeight: Int!

managerId: Account! @index

treasuryShare: BigInt!
Expand Down Expand Up @@ -41,6 +43,8 @@ enum DdcClusterStatus {
type DdcNode @entity {
id: ID!

createdAtBlockHeight: Int!

providerId: Account! @index
clusterId: DdcCluster

Expand All @@ -65,6 +69,8 @@ enum DdcNodeMode {
type DdcBucket @entity {
id: ID!

createdAtBlockHeight: Int!

ownerId: Account! @index
clusterId: DdcCluster! @index

Expand Down
3 changes: 3 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ processor.run(new TypeormDatabase({ supportHotBlocks: true }), async (ctx) => {
ddcClusterEntities.push(
new DdcCluster({
id: c.id,
createdAtBlockHeight: c.createdAtBlockHeight,
managerId: accounts.get(c.managerId),
treasuryShare: c.treasuryShare,
validatorsShare: c.validatorsShare,
Expand Down Expand Up @@ -168,6 +169,7 @@ processor.run(new TypeormDatabase({ supportHotBlocks: true }), async (ctx) => {
ddcNodesMap.get(node.id) ??
new DdcNode({
id: node.id,
createdAtBlockHeight: node.createdAtBlockHeight,
providerId: accounts.get(node.providerId),
})
nodeEntity.host = node.host
Expand Down Expand Up @@ -225,6 +227,7 @@ processor.run(new TypeormDatabase({ supportHotBlocks: true }), async (ctx) => {
ddcBucketEntities.push(
new DdcBucket({
id: bucket.bucketId.toString(),
createdAtBlockHeight: bucket.createdAtBlockHeight,
ownerId: accounts.get(bucket.ownerId),
clusterId: cluster,
isPublic: bucket.isPublic,
Expand Down
5 changes: 4 additions & 1 deletion src/model/generated/ddcBucket.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, ManyToOne as ManyToOne_, Index as Index_, BooleanColumn as BooleanColumn_, BigIntColumn as BigIntColumn_} from "@subsquid/typeorm-store"
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, IntColumn as IntColumn_, ManyToOne as ManyToOne_, Index as Index_, BooleanColumn as BooleanColumn_, BigIntColumn as BigIntColumn_} from "@subsquid/typeorm-store"
import {Account} from "./account.model"
import {DdcCluster} from "./ddcCluster.model"

Expand All @@ -11,6 +11,9 @@ export class DdcBucket {
@PrimaryColumn_()
id!: string

@IntColumn_({nullable: false})
createdAtBlockHeight!: number

@Index_()
@ManyToOne_(() => Account, {nullable: true})
ownerId!: Account
Expand Down
5 changes: 4 additions & 1 deletion src/model/generated/ddcCluster.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, ManyToOne as ManyToOne_, Index as Index_, BigIntColumn as BigIntColumn_, IntColumn as IntColumn_, OneToMany as OneToMany_} from "@subsquid/typeorm-store"
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, IntColumn as IntColumn_, ManyToOne as ManyToOne_, Index as Index_, BigIntColumn as BigIntColumn_, OneToMany as OneToMany_} from "@subsquid/typeorm-store"
import {Account} from "./account.model"
import {DdcClusterStatus} from "./_ddcClusterStatus"
import {DdcBucket} from "./ddcBucket.model"
Expand All @@ -13,6 +13,9 @@ export class DdcCluster {
@PrimaryColumn_()
id!: string

@IntColumn_({nullable: false})
createdAtBlockHeight!: number

@Index_()
@ManyToOne_(() => Account, {nullable: true})
managerId!: Account
Expand Down
5 changes: 4 additions & 1 deletion src/model/generated/ddcNode.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, ManyToOne as ManyToOne_, Index as Index_, StringColumn as StringColumn_, BooleanColumn as BooleanColumn_, IntColumn as IntColumn_, BigIntColumn as BigIntColumn_} from "@subsquid/typeorm-store"
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, IntColumn as IntColumn_, ManyToOne as ManyToOne_, Index as Index_, StringColumn as StringColumn_, BooleanColumn as BooleanColumn_, BigIntColumn as BigIntColumn_} from "@subsquid/typeorm-store"
import {Account} from "./account.model"
import {DdcCluster} from "./ddcCluster.model"
import {DdcNodeMode} from "./_ddcNodeMode"
Expand All @@ -12,6 +12,9 @@ export class DdcNode {
@PrimaryColumn_()
id!: string

@IntColumn_({nullable: false})
createdAtBlockHeight!: number

@Index_()
@ManyToOne_(() => Account, {nullable: true})
providerId!: Account
Expand Down
30 changes: 20 additions & 10 deletions src/processors/ddcBucketsProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { logEmptyStorage, logUnsupportedEventVersion, logUnsupportedStorageVersi
import { BaseProcessor } from './processor'

export interface DdcBucketInfo {
createdAtBlockHeight?: number
ownerId: string
clusterId: string
bucketId: bigint
Expand All @@ -22,7 +23,12 @@ export class DdcBucketsProcessor extends BaseProcessor<State> {
super(new Map<bigint, DdcBucketInfo>())
}

private async processDdcBucketsEvents(bucketId: bigint, block: BlockHeader) {
private async processDdcBucketsEvents(bucketId: bigint, block: BlockHeader, event: Event) {
let createdAtBlockHeight;
if (event.name === events.ddcCustomers.bucketCreated.name) {
createdAtBlockHeight = block.height
}

// TODO(khssnv)
// We can return to ascending versions check here and in the other processors when
// https://github.com/subsquid/squid-sdk/issues/334 fixed, possibly with
Expand All @@ -33,6 +39,7 @@ export class DdcBucketsProcessor extends BaseProcessor<State> {
const bucket = await storage.ddcCustomers.buckets.v54100.get(block, bucketId)
if (bucket) {
bucketInfo = {
createdAtBlockHeight: createdAtBlockHeight,
ownerId: bucket.ownerId,
clusterId: bucket.clusterId,
bucketId: bucketId,
Expand All @@ -48,6 +55,7 @@ export class DdcBucketsProcessor extends BaseProcessor<State> {
const bucket = await storage.ddcCustomers.buckets.v50000.get(block, bucketId)
if (bucket) {
bucketInfo = {
createdAtBlockHeight: createdAtBlockHeight,
ownerId: bucket.ownerId,
clusterId: bucket.clusterId,
bucketId: bucketId,
Expand All @@ -63,6 +71,7 @@ export class DdcBucketsProcessor extends BaseProcessor<State> {
const bucket = await storage.ddcCustomers.buckets.v48017.get(block, bucketId)
if (bucket) {
bucketInfo = {
createdAtBlockHeight: createdAtBlockHeight,
ownerId: bucket.ownerId,
clusterId: bucket.clusterId,
bucketId: bucketId,
Expand All @@ -78,6 +87,7 @@ export class DdcBucketsProcessor extends BaseProcessor<State> {
const bucket = await storage.ddcCustomers.buckets.v48013.get(block, bucketId)
if (bucket) {
bucketInfo = {
createdAtBlockHeight: createdAtBlockHeight,
ownerId: bucket.ownerId,
clusterId: bucket.clusterId,
bucketId: bucketId,
Expand Down Expand Up @@ -105,13 +115,13 @@ export class DdcBucketsProcessor extends BaseProcessor<State> {
case events.ddcCustomers.bucketCreated.name: {
if (events.ddcCustomers.bucketCreated.v48013.is(event)) {
const bucketId = events.ddcCustomers.bucketCreated.v48013.decode(event)
await this.processDdcBucketsEvents(bucketId, block)
await this.processDdcBucketsEvents(bucketId, block, event)
} else if (events.ddcCustomers.bucketCreated.v48800.is(event)) {
const bucketId = events.ddcCustomers.bucketCreated.v48800.decode(event).bucketId
await this.processDdcBucketsEvents(bucketId, block)
await this.processDdcBucketsEvents(bucketId, block, event)
} else if (events.ddcCustomers.bucketCreated.v54100.is(event)) {
const bucketId = events.ddcCustomers.bucketCreated.v54100.decode(event).bucketId
await this.processDdcBucketsEvents(bucketId, block)
await this.processDdcBucketsEvents(bucketId, block, event)
} else {
logUnsupportedEventVersion(event)
}
Expand All @@ -120,13 +130,13 @@ export class DdcBucketsProcessor extends BaseProcessor<State> {
case events.ddcCustomers.bucketUpdated.name: {
if (events.ddcCustomers.bucketUpdated.v48017.is(event)) {
const bucketId = events.ddcCustomers.bucketUpdated.v48017.decode(event)
await this.processDdcBucketsEvents(bucketId, block)
await this.processDdcBucketsEvents(bucketId, block, event)
} else if (events.ddcCustomers.bucketUpdated.v48800.is(event)) {
const bucketId = events.ddcCustomers.bucketUpdated.v48800.decode(event).bucketId
await this.processDdcBucketsEvents(bucketId, block)
await this.processDdcBucketsEvents(bucketId, block, event)
} else if (events.ddcCustomers.bucketUpdated.v54100.is(event)) {
const bucketId = events.ddcCustomers.bucketUpdated.v54100.decode(event).bucketId
await this.processDdcBucketsEvents(bucketId, block)
await this.processDdcBucketsEvents(bucketId, block, event)
} else {
logUnsupportedEventVersion(event)
}
Expand All @@ -135,7 +145,7 @@ export class DdcBucketsProcessor extends BaseProcessor<State> {
case events.ddcCustomers.bucketRemoved.name: {
if (events.ddcCustomers.bucketRemoved.v50000.is(event)) {
const bucketId = events.ddcCustomers.bucketRemoved.v50000.decode(event).bucketId
await this.processDdcBucketsEvents(bucketId, block)
await this.processDdcBucketsEvents(bucketId, block, event)
} else {
logUnsupportedEventVersion(event)
}
Expand All @@ -144,7 +154,7 @@ export class DdcBucketsProcessor extends BaseProcessor<State> {
case events.ddcCustomers.bucketTotalNodesUsageUpdated.name: {
if (events.ddcCustomers.bucketTotalNodesUsageUpdated.v54100.is(event)) {
const bucketId = events.ddcCustomers.bucketTotalNodesUsageUpdated.v54100.decode(event).bucketId
await this.processDdcBucketsEvents(bucketId, block)
await this.processDdcBucketsEvents(bucketId, block, event)
} else {
logUnsupportedEventVersion(event)
}
Expand All @@ -153,7 +163,7 @@ export class DdcBucketsProcessor extends BaseProcessor<State> {
case events.ddcCustomers.bucketTotalCustomersUsageUpdated.name: {
if (events.ddcCustomers.bucketTotalCustomersUsageUpdated.v54100.is(event)) {
const bucketId = events.ddcCustomers.bucketTotalCustomersUsageUpdated.v54100.decode(event).bucketId
await this.processDdcBucketsEvents(bucketId, block)
await this.processDdcBucketsEvents(bucketId, block, event)
} else {
logUnsupportedEventVersion(event)
}
Expand Down
Loading

0 comments on commit c737ebd

Please sign in to comment.