Skip to content

Commit

Permalink
Store proposal value as base64-encoded protobuf.
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSaso committed Jan 8, 2024
1 parent 0fd5837 commit 4152c3a
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 15 deletions.
9 changes: 6 additions & 3 deletions src/core/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
FormulaPrefetch,
FormulaPrefetchTransformations,
FormulaProposalGetter,
FormulaProposalObject,
FormulaProposalsGetter,
FormulaSlashEventsGetter,
FormulaTransformationDateGetter,
Expand Down Expand Up @@ -1138,7 +1139,8 @@ export const getEnv = ({
await onFetch?.([event])

return {
...event.value,
id: event.proposalId,
value: event.value,
version: event.version,
}
}
Expand Down Expand Up @@ -1208,11 +1210,12 @@ export const getEnv = ({
(acc, { proposalId, value, version }) => ({
...acc,
[proposalId]: {
...value,
id: proposalId,
value,
version,
},
}),
{} as Record<string, Record<string, any>>
{} as Record<string, FormulaProposalObject>
)
}

Expand Down
12 changes: 9 additions & 3 deletions src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,18 @@ export type FormulaBalancesGetter = (
address: string
) => Promise<Record<string, string> | undefined>

export type FormulaProposalObject = {
id: string
value: string
version: string
}

export type FormulaProposalGetter = (
proposalId: string
) => Promise<Record<string, any> | undefined>
) => Promise<FormulaProposalObject | undefined>

export type FormulaProposalsGetter = () => Promise<
Record<string, Record<string, any>> | undefined
Record<string, FormulaProposalObject> | undefined
>

export type Env<Args extends Record<string, string> = {}> = {
Expand Down Expand Up @@ -481,7 +487,7 @@ export type ParsedGovStateEvent = {
blockHeight: string
blockTimeUnixMs: string
blockTimestamp: Date
value: Record<string, any>
value: string
version: string
}

Expand Down
8 changes: 4 additions & 4 deletions src/data/formulas/generic/gov.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GenericFormula } from '@/core'
import { FormulaProposalObject, GenericFormula } from '@/core'

export const proposal: GenericFormula<
Record<string, any> | undefined,
FormulaProposalObject | undefined,
{ proposalId: string }
> = {
compute: async ({ getProposal, args: { proposalId } }) => {
Expand All @@ -15,7 +15,7 @@ export const proposal: GenericFormula<

export const proposals: GenericFormula<
{
proposals: Record<string, any>[]
proposals: FormulaProposalObject[]
total: number
},
{
Expand Down Expand Up @@ -52,7 +52,7 @@ export const proposals: GenericFormula<

export const reverseProposals: GenericFormula<
{
proposals: Record<string, any>[]
proposals: FormulaProposalObject[]
total: number
},
{
Expand Down
19 changes: 19 additions & 0 deletions src/db/migrations/20240108103200-change-gov-value-to-string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { QueryInterface } from 'sequelize'
import { DataType } from 'sequelize-typescript'

module.exports = {
async up(queryInterface: QueryInterface) {
await queryInterface.bulkDelete('GovStateEvents', {})
await queryInterface.changeColumn('GovStateEvents', 'value', {
type: DataType.TEXT('long'),
allowNull: false,
})
},

async down(queryInterface: QueryInterface) {
await queryInterface.changeColumn('GovStateEvents', 'value', {
type: DataType.JSONB,
allowNull: false,
})
},
}
5 changes: 3 additions & 2 deletions src/db/models/GovStateEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ export class GovStateEvent extends DependendableEventModel {
@Column(DataType.DATE)
blockTimestamp!: Date

// Base64-encoded protobuf
@AllowNull(false)
@Column(DataType.JSONB)
value!: any
@Column(DataType.TEXT('long'))
value!: string

@AllowNull(false)
@Column(DataType.STRING)
Expand Down
8 changes: 5 additions & 3 deletions src/scripts/export/handlers/gov.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fromBase64 } from '@cosmjs/encoding'
import { fromBase64, toBase64 } from '@cosmjs/encoding'
import retry from 'async-await-retry'
import { Sequelize } from 'sequelize'

Expand Down Expand Up @@ -58,11 +58,13 @@ export const gov: HandlerMaker<ParsedGovStateEvent> = async () => {
let value: any
let version
try {
value = ProposalV1.toAmino(ProposalV1.decode(valueData))
value = toBase64(ProposalV1.toProto(ProposalV1.decode(valueData)))
version = 'v1'
} catch {
try {
value = ProposalV1Beta1.toAmino(ProposalV1Beta1.decode(valueData))
value = toBase64(
ProposalV1Beta1.toProto(ProposalV1Beta1.decode(valueData))
)
version = 'v1beta1'
} catch {
return
Expand Down

0 comments on commit 4152c3a

Please sign in to comment.