-
Notifications
You must be signed in to change notification settings - Fork 14
feat: validate space strategies #513
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wa0x6e
wants to merge
58
commits into
master
Choose a base branch
from
feat-validate-space-strategies
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+505
−294
Open
Changes from 55 commits
Commits
Show all changes
58 commits
Select commit
Hold shift + click to select a range
fac6311
feat: validate space strategies
wa0x6e 09cde68
fix: duplicate request preventor for EIP-1271 requests (#515)
ChaituVR 611e081
chore: increase follow limit for Turbo users (#516)
bonustrack 4d8f1ad
fix: allow copeland on production (#517)
ChaituVR ae2a23f
feat: use unified API to check if SX space exists (#520)
Sekhmet badd12a
feat: add mnt, ape, curtis to networks whitelist (#521)
Sekhmet fd8ab6f
chore(deps): bump @snapshot-labs/snapshot.js from 0.12.54 to 0.12.55 …
dependabot[bot] f30be53
chore(deps): bump @snapshot-labs/snapshot.js from 0.12.55 to 0.12.56 …
dependabot[bot] 3104246
chore(deps): bump @snapshot-labs/snapshot.js from 0.12.56 to 0.12.57 …
dependabot[bot] c8121e9
feat: sync spaces custom domain with walletconnect allowed origins (#…
wa0x6e a1dee26
fix: prevent non-premium networks (#522)
ChaituVR a21aaca
fix: allow turbo spaces to create proposals with non-premuim networks…
ChaituVR da6af19
feat: use correct network when fetching shib space controller (#527)
wa0x6e ec30ecd
chore(deps): bump @snapshot-labs/snapshot.js from 0.12.57 to 0.12.58 …
dependabot[bot] b6d764d
feat: add turbo tracking (#523)
pscott cf3719a
chore(deps): bump @snapshot-labs/snapshot.js from 0.12.58 to 0.12.59 …
dependabot[bot] 589cf8b
chore(deps): bump @snapshot-labs/snapshot.js from 0.12.59 to 0.12.60 …
dependabot[bot] 61578e2
chore: use pineapple gateway to get JSON on poke (#535)
ChaituVR dcde46a
fix: skip indexing from outdated schnaps api (#537)
wa0x6e 5adf223
fix: assign fallback value (#538)
wa0x6e 815b897
fix: infer turbo status from turbo expiration date (#539)
wa0x6e fa1b786
chore: mark eden-online-override strategy as overriding
bonustrack 03b1ad7
chore(deps): bump @snapshot-labs/snapshot.js from 0.12.60 to 0.12.62 …
dependabot[bot] 35375df
chore(deps): bump @snapshot-labs/snapshot.js from 0.12.62 to 0.12.63 …
dependabot[bot] 1fc2775
chore(deps): bump @snapshot-labs/snapshot.js from 0.12.63 to 0.12.64 …
dependabot[bot] 94b079f
chore(deps): bump @snapshot-labs/snapshot.js from 0.12.64 to 0.12.65 …
dependabot[bot] c81c533
chore(deps): bump @snapshot-labs/snapshot.js from 0.12.65 to 0.13.0 (…
dependabot[bot] 155fe15
feat: add delay before finalizing proposal scores (#541)
wa0x6e f4d464f
refactor: remove unused requestEonKey function from shutter helper (#…
bigint b2a3b0c
feat: allow vote and proposal handling from starknet aliases (#542)
wa0x6e 4321bc9
chore(deps): bump @snapshot-labs/snapshot.js from 0.13.0 to 0.14.1 (#…
dependabot[bot] 190484e
fix: remove duplicate promisification of Pool and Connection in mysql…
bigint 71535a7
feat: allow starknet aliases to flag proposals (#550)
wa0x6e b7ea17c
fix: add split-delegation to list of override strategies (#547)
wa0x6e 7b25718
refactor: simplify retry logic in fetchWithRetry method (#553)
bigint 2df80e6
chore(deps): bump @snapshot-labs/snapshot.js from 0.14.1 to 0.14.2 (#…
dependabot[bot] 181ca38
fix: update `network` column to support starknet chain id (#558)
wa0x6e e8c87a2
chore(deps): bump @snapshot-labs/snapshot.js from 0.14.2 to 0.14.4 (#…
dependabot[bot] 4dbcfeb
feat: prevent spaces from setting themselves as parent or child (#560)
ChaituVR 1d1a24d
chore(deps): bump @snapshot-labs/snapshot.js from 0.14.4 to 0.14.5 (#…
dependabot[bot] c36e9d8
feat: add support for sonic domain name (#562)
wa0x6e 271152c
chore: add sonic-staked-balance to override strategy (#568)
ChaituVR 89b2188
chore: remove matic from sx (#573)
wa0x6e a45151a
chore(deps): bump @snapshot-labs/snapshot.js from 0.14.5 to 0.14.6 (#…
dependabot[bot] 5e73cc2
chore(deps): bump @snapshot-labs/pineapple from 1.1.0 to 1.2.0 (#564)
dependabot[bot] ca67b26
chore(deps): bump @snapshot-labs/snapshot.js from 0.14.6 to 0.14.7 (#…
dependabot[bot] 6195859
feat: validate space strategies
wa0x6e 7eee9a6
chore: lint fix
wa0x6e ff9cae4
feat: validate space strategies
wa0x6e b17284f
chore: lint fix
wa0x6e ec91679
Merge branch 'master' into feat-validate-space-strategies
wa0x6e 49162ef
refactor: better strategies handling
wa0x6e 18c9919
fix: stop infinite loop on app shutdown
wa0x6e 2eb90a5
refactor: remove magic number
wa0x6e e12e40c
fix: update error message to reflect disabled status
wa0x6e a8b3668
refactor: remove useless try/catch
wa0x6e c214894
test: reset mock after each test
wa0x6e 6c57cdf
feat: only allow override strategies on pro spaces (#581)
wa0x6e File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { URL } from 'url'; | ||
import { capture } from '@snapshot-labs/snapshot-sentry'; | ||
import snapshot from '@snapshot-labs/snapshot.js'; | ||
import log from './log'; | ||
|
||
type Strategy = { | ||
id: string; | ||
override: boolean; | ||
disabled: boolean; | ||
}; | ||
|
||
const RUN_INTERVAL = 60e3; | ||
const MAX_CONSECUTIVE_FAILS = 3; | ||
const URI = new URL( | ||
'/api/strategies', | ||
process.env.SCORE_API_URL ?? 'https://score.snapshot.org' | ||
).toString(); | ||
|
||
let consecutiveFailsCount = 0; | ||
let shouldStop = false; | ||
export let strategies: Record<Strategy['id'], Strategy> = {}; | ||
|
||
async function loadStrategies() { | ||
const res = await snapshot.utils.getJSON(URI); | ||
|
||
if (res.hasOwnProperty('error')) { | ||
capture(new Error('Failed to load strategies'), { | ||
contexts: { input: { uri: URI }, res } | ||
}); | ||
return true; | ||
} | ||
|
||
const strat = Object.values(res).map((strategy: any) => { | ||
strategy.id = strategy.key; | ||
strategy.override = strategy.dependOnOtherAddress || false; | ||
strategy.disabled = strategy.disabled || false; | ||
return strategy; | ||
}); | ||
|
||
strategies = Object.fromEntries(strat.map(strategy => [strategy.id, strategy])); | ||
} | ||
|
||
export async function run() { | ||
while (!shouldStop) { | ||
try { | ||
log.info('[strategies] Start strategies refresh'); | ||
await loadStrategies(); | ||
consecutiveFailsCount = 0; | ||
log.info('[strategies] End strategies refresh'); | ||
} catch (e: any) { | ||
consecutiveFailsCount++; | ||
|
||
if (consecutiveFailsCount >= MAX_CONSECUTIVE_FAILS) { | ||
capture(e); | ||
} | ||
log.error(`[strategies] failed to load ${JSON.stringify(e)}`); | ||
} | ||
|
||
// if stop() has been called after sleep started, | ||
// the loop will exit only after the sleep has completed | ||
await snapshot.utils.sleep(RUN_INTERVAL); | ||
} | ||
} | ||
|
||
export function stop() { | ||
log.info('[strategies] Stopping strategies refresh'); | ||
shouldStop = true; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import snapshot from '@snapshot-labs/snapshot.js'; | ||
import log from './log'; | ||
import { strategies } from './strategies'; | ||
|
||
const DEFAULT_SNAPSHOT_ENV: string = 'testnet'; | ||
|
||
export async function validateSpaceSettings( | ||
originalSpace: any, | ||
snapshotEnv = DEFAULT_SNAPSHOT_ENV | ||
): Promise<void> { | ||
const spaceType = originalSpace.turbo ? 'turbo' : 'default'; | ||
const space = snapshot.utils.clone(originalSpace); | ||
|
||
if (space?.deleted) return Promise.reject('space deleted, contact admin'); | ||
|
||
delete space.deleted; | ||
delete space.flagged; | ||
delete space.verified; | ||
delete space.turbo; | ||
delete space.hibernated; | ||
delete space.id; | ||
|
||
if (space.parent && space.parent === originalSpace.id) { | ||
return Promise.reject('space cannot be its own parent'); | ||
} | ||
|
||
if ( | ||
space.children && | ||
Array.isArray(space.children) && | ||
space.children.includes(originalSpace.id) | ||
) { | ||
return Promise.reject('space cannot be its own child'); | ||
} | ||
|
||
const schemaIsValid: any = snapshot.utils.validateSchema(snapshot.schemas.space, space, { | ||
spaceType, | ||
snapshotEnv | ||
}); | ||
|
||
if (schemaIsValid !== true) { | ||
log.warn('[writer] Wrong space format', schemaIsValid); | ||
const firstErrorObject: any = Object.values(schemaIsValid)[0]; | ||
if (firstErrorObject.message === 'network not allowed') { | ||
return Promise.reject(firstErrorObject.message); | ||
} | ||
return Promise.reject('wrong space format'); | ||
} | ||
|
||
const strategiesIds: string[] = space.strategies.map((strategy: any) => strategy.name); | ||
if (snapshotEnv !== 'testnet') { | ||
const hasTicket = strategiesIds.includes('ticket'); | ||
const hasVotingValidation = | ||
space.voteValidation?.name && !['any'].includes(space.voteValidation.name); | ||
|
||
if (hasTicket && !hasVotingValidation) { | ||
return Promise.reject('space with ticket requires voting validation'); | ||
} | ||
|
||
const hasProposalValidation = | ||
(space.validation?.name && space.validation.name !== 'any') || | ||
space.filters?.minScore || | ||
space.filters?.onlyMembers; | ||
|
||
if (!hasProposalValidation) { | ||
return Promise.reject('space missing proposal validation'); | ||
} | ||
} | ||
|
||
for (const id of strategiesIds) { | ||
const strategy = strategies[id]; | ||
|
||
if (!strategy) { | ||
return Promise.reject(`strategy "${id}" is not a valid strategy`); | ||
} | ||
|
||
if (strategy.disabled) { | ||
return Promise.reject(`strategy "${id}" is not available anymore`); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.