Skip to content

Commit

Permalink
Merge branch 'master' into feat-add-subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e authored Jan 7, 2025
2 parents 27f15bc + 640b7e5 commit f59b01b
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 23 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"@snapshot-labs/pineapple": "^1.1.0",
"@snapshot-labs/snapshot-metrics": "^1.4.1",
"@snapshot-labs/snapshot-sentry": "^1.5.5",
"@snapshot-labs/snapshot.js": "^0.12.32",
"@snapshot-labs/snapshot.js": "^0.12.41",
"bluebird": "^3.7.2",
"connection-string": "^1.0.1",
"cors": "^2.8.5",
Expand Down
1 change: 1 addition & 0 deletions src/helpers/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export async function sxSpaceExists(network: string, spaceId: string): Promise<b
matic: 'https://api.studio.thegraph.com/query/23545/sx-polygon/version/latest',
arb1: 'https://api.studio.thegraph.com/query/23545/sx-arbitrum/version/latest',
oeth: 'https://api.studio.thegraph.com/query/23545/sx-optimism/version/latest',
base: 'https://api.studio.thegraph.com/query/23545/sx-base/version/latest',
sn: 'https://api.snapshot.box',
'sn-sep': 'https://testnet-api.snapshot.box',
'linea-testnet':
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import snapshot from '@snapshot-labs/snapshot.js';
import { Response } from 'express';
import fetch from 'node-fetch';

const MAINNET_NETWORK_ID_WHITELIST = ['s', 'eth', 'matic', 'arb1', 'oeth', 'sn'];
const MAINNET_NETWORK_ID_WHITELIST = ['s', 'eth', 'matic', 'arb1', 'oeth', 'sn', 'base'];
const TESTNET_NETWORK_ID_WHITELIST = ['s-tn', 'sep', 'linea-testnet', 'sn-sep'];
const broviderUrl = process.env.BROVIDER_URL ?? 'https://rpc.snapshot.org';

Expand Down
9 changes: 5 additions & 4 deletions src/ingestor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { capture } from '@snapshot-labs/snapshot-sentry';
import snapshot from '@snapshot-labs/snapshot.js';
import hashTypes from '@snapshot-labs/snapshot.js/src/sign/hashedTypes.json';
import castArray from 'lodash/castArray';
import kebabCase from 'lodash/kebabCase';
import { getProposal, getSpace } from './helpers/actions';
import { isValidAlias } from './helpers/alias';
import envelope from './helpers/envelope.json';
Expand Down Expand Up @@ -60,7 +59,7 @@ export default async function ingestor(req) {
const formattedSignature = castArray(body.sig).join(',');
const ts = Date.now() / 1e3;
const over = 300;
const under = 60 * 60 * 24 * 3; // 3 days
const under = 60 * 60 * 24 * 6; // 6 days
const overTs = (ts + over).toFixed();
const underTs = (ts - under).toFixed();
const { domain, message, types } = body.data;
Expand Down Expand Up @@ -160,6 +159,7 @@ export default async function ingestor(req) {
body: message.body,
discussion: message.discussion || '',
choices: message.choices,
privacy: message.privacy || '',
labels: message.labels || [],
start: message.start,
end: message.end,
Expand All @@ -168,7 +168,7 @@ export default async function ingestor(req) {
plugins: JSON.parse(message.plugins)
},
type: message.type,
app: kebabCase(message.app || '')
app: message.app || ''
};
if (type === 'alias') payload = { alias: message.alias };
if (type === 'statement')
Expand All @@ -186,6 +186,7 @@ export default async function ingestor(req) {
name: message.title,
body: message.body,
discussion: message.discussion || '',
privacy: message.privacy || '',
choices: message.choices,
labels: message.labels || [],
metadata: {
Expand Down Expand Up @@ -217,7 +218,7 @@ export default async function ingestor(req) {
proposal: message.proposal,
choice,
reason: message.reason || '',
app: kebabCase(message.app || ''),
app: message.app || '',
metadata: jsonParse(message.metadata, {})
};
type = 'vote';
Expand Down
13 changes: 10 additions & 3 deletions src/writer/proposal.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { capture } from '@snapshot-labs/snapshot-sentry';
import snapshot from '@snapshot-labs/snapshot.js';
import networks from '@snapshot-labs/snapshot.js/src/networks.json';
import kebabCase from 'lodash/kebabCase';
import { validateSpaceSettings } from './settings';
import { getSpace } from '../helpers/actions';
import { ACTIVE_PROPOSAL_BY_AUTHOR_LIMIT, getSpaceLimits } from '../helpers/limits';
Expand Down Expand Up @@ -103,6 +102,10 @@ export async function verify(body): Promise<any> {
if (msg.payload.type !== space.voting.type) return Promise.reject('invalid voting type');
}

if (space.voting?.privacy !== 'any' && msg.payload.privacy) {
return Promise.reject('not allowed to set privacy');
}

try {
if (await isMalicious(msg.payload, space.id)) {
return Promise.reject('invalid proposal content');
Expand Down Expand Up @@ -207,6 +210,10 @@ export async function action(body, ipfs, receipt, id): Promise<void> {
const plugins = JSON.stringify(metadata.plugins || {});
const spaceNetwork = spaceSettings.network;
const proposalSnapshot = parseInt(msg.payload.snapshot || '0');
let privacy = spaceSettings.voting?.privacy || '';
if (privacy === 'any') {
privacy = msg.payload.privacy;
}

let quorum = spaceSettings.voting?.quorum || 0;
if (!quorum && spaceSettings.plugins?.quorum) {
Expand Down Expand Up @@ -238,9 +245,9 @@ export async function action(body, ipfs, receipt, id): Promise<void> {
end: parseInt(msg.payload.end || '0'),
quorum,
quorum_type: (quorum && spaceSettings.voting?.quorumType) || '',
privacy: spaceSettings.voting?.privacy || '',
privacy,
snapshot: proposalSnapshot || 0,
app: kebabCase(msg.payload.app || ''),
app: msg.payload.app,
scores: JSON.stringify([]),
scores_by_strategy: JSON.stringify([]),
scores_state: 'pending',
Expand Down
10 changes: 10 additions & 0 deletions src/writer/update-proposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export async function verify(body): Promise<any> {

if (proposal.author !== body.address) return Promise.reject('Not the author');

if (space.voting?.privacy !== 'any' && msg.payload.privacy) {
return Promise.reject('not allowed to set privacy');
}

const spaceUpdateError = getSpaceUpdateError({
type: msg.payload.type,
space
Expand All @@ -63,6 +67,11 @@ export async function action(body, ipfs): Promise<void> {
const updated = parseInt(msg.timestamp);
const metadata = msg.payload.metadata || {};
const plugins = JSON.stringify(metadata.plugins || {});
const spaceSettings = await getSpace(msg.space);
let privacy = spaceSettings.voting?.privacy || '';
if (privacy === 'any') {
privacy = msg.payload.privacy;
}

const proposal = {
ipfs,
Expand All @@ -74,6 +83,7 @@ export async function action(body, ipfs): Promise<void> {
discussion: msg.payload.discussion,
choices: JSON.stringify(msg.payload.choices),
labels: msg.payload.labels?.length ? JSON.stringify(msg.payload.labels) : null,
privacy,
scores: JSON.stringify([]),
scores_by_strategy: JSON.stringify([]),
flagged: +containsFlaggedLinks(msg.payload.body)
Expand Down
3 changes: 1 addition & 2 deletions src/writer/vote.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import snapshot from '@snapshot-labs/snapshot.js';
import kebabCase from 'lodash/kebabCase';
import { getProposal } from '../helpers/actions';
import log from '../helpers/log';
import db from '../helpers/mysql';
Expand Down Expand Up @@ -108,7 +107,7 @@ export async function action(body, ipfs, receipt, id, context): Promise<void> {
const created = parseInt(msg.timestamp);
const choice = JSON.stringify(msg.payload.choice);
const metadata = JSON.stringify(msg.payload.metadata || {});
const app = kebabCase(msg.payload.app || '');
const app = msg.payload.app;
const reason = msg.payload.reason || '';
const proposalId = msg.payload.proposal;

Expand Down
7 changes: 4 additions & 3 deletions test/e2e/api.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fetch from 'node-fetch';
import db from '../../src/helpers/mysql';
import proposalInput from '../fixtures/ingestor-payload/proposal.json';
import { spacesSqlFixtures } from '../fixtures/space';
import proposalsFixtures from '../fixtures/proposal';
import db from '../../src/helpers/mysql';
import { spacesSqlFixtures } from '../fixtures/space';

const HOST = `http://localhost:${process.env.PORT || 3003}`;
const SPACE_PREFIX = 'e2e-';
Expand Down Expand Up @@ -71,7 +71,8 @@ describe('POST /flag', () => {
plugins: JSON.stringify(proposal.plugins),
choices: JSON.stringify(proposal.choices),
scores: JSON.stringify(proposal.scores),
scores_by_strategy: JSON.stringify(proposal.scores_by_strategy)
scores_by_strategy: JSON.stringify(proposal.scores_by_strategy),
vp_value_by_strategy: JSON.stringify(proposal.vp_value_by_strategy || [])
}))
.map(async proposal => {
db.queryAsync('INSERT INTO snapshot_sequencer_test.proposals SET ?', proposal);
Expand Down
18 changes: 13 additions & 5 deletions test/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ CREATE TABLE spaces (
INDEX updated (updated)
);

-- Note: The `proposals` table schema might have some discrepancies
-- compared to the production database. This is due to legacy reasons
-- and the challenges associated with updating the schema because of its size.
-- `id` and `ipfs` columns should not have any default values.
CREATE TABLE proposals (
id VARCHAR(66) NOT NULL,
ipfs VARCHAR(64) NOT NULL,
Expand All @@ -36,8 +40,8 @@ CREATE TABLE proposals (
updated INT(11) DEFAULT NULL,
space VARCHAR(64) NOT NULL,
network VARCHAR(12) NOT NULL,
symbol VARCHAR(16) NOT NULL,
type VARCHAR(24) NOT NULL,
symbol VARCHAR(16) NOT NULL DEFAULT '',
type VARCHAR(24) NOT NULL DEFAULT '',
strategies JSON NOT NULL,
validation JSON NOT NULL,
plugins JSON NOT NULL,
Expand All @@ -49,17 +53,20 @@ CREATE TABLE proposals (
start INT(11) NOT NULL,
end INT(11) NOT NULL,
quorum DECIMAL(64,30) NOT NULL,
quorum_type VARCHAR(24) NOT NULL DEFAULT '',
quorum_type VARCHAR(24) DEFAULT '',
privacy VARCHAR(24) NOT NULL,
snapshot INT(24) NOT NULL,
app VARCHAR(24) NOT NULL,
scores JSON NOT NULL,
scores_by_strategy JSON NOT NULL,
scores_state VARCHAR(24) NOT NULL,
scores_state VARCHAR(24) NOT NULL DEFAULT '',
scores_total DECIMAL(64,30) NOT NULL,
scores_updated INT(11) NOT NULL,
scores_total_value DECIMAL(64,30) NOT NULL DEFAULT '0.000000000000000000000000000000',
vp_value_by_strategy json NOT NULL,
votes INT(12) NOT NULL,
flagged INT NOT NULL DEFAULT 0,
cb INT NOT NULL DEFAULT 0,
PRIMARY KEY (id),
INDEX ipfs (ipfs),
INDEX author (author),
Expand All @@ -73,7 +80,8 @@ CREATE TABLE proposals (
INDEX scores_state (scores_state),
INDEX scores_updated (scores_updated),
INDEX votes (votes),
INDEX flagged (flagged)
INDEX flagged (flagged),
INDEX cb (cb)
);

CREATE TABLE votes (
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1350,10 +1350,10 @@
dependencies:
"@sentry/node" "^7.81.1"

"@snapshot-labs/snapshot.js@^0.12.32":
version "0.12.32"
resolved "https://registry.yarnpkg.com/@snapshot-labs/snapshot.js/-/snapshot.js-0.12.32.tgz#a92522c8563cf9849706ba6ea5592672aba8adde"
integrity sha512-GRmGMjcR8qdRE94QydVQVPf93lKjJp+J/ISoSTVp3oxKrkBEzY0DZx9gFlysBqF/h69PEeykJWUOH1YfQocYHw==
"@snapshot-labs/snapshot.js@^0.12.41":
version "0.12.41"
resolved "https://registry.yarnpkg.com/@snapshot-labs/snapshot.js/-/snapshot.js-0.12.41.tgz#3a0a2866a78f14368eb0648ef2e22a8e66422b26"
integrity sha512-Deikl21gUzeY/2TE8xTNsHbwTNcSqvxDnrzA1LcLwyqAzA6tigwNBlqaS5E35v7t5z7Iz62j+CPEFzxv1ta6cA==
dependencies:
"@ensdomains/eth-ens-namehash" "^2.0.15"
"@ethersproject/abi" "^5.6.4"
Expand Down

0 comments on commit f59b01b

Please sign in to comment.