Skip to content

Commit

Permalink
Integrate JIT clusters with testing
Browse files Browse the repository at this point in the history
  • Loading branch information
shanejearley committed May 19, 2023
1 parent f958627 commit 5513cf2
Show file tree
Hide file tree
Showing 24 changed files with 615 additions and 831 deletions.
172 changes: 66 additions & 106 deletions common/data/src/mock/validator.store.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions common/ssv/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
dist/
16 changes: 16 additions & 0 deletions common/ssv/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "@casimir/ssv",
"private": "true",
"main": "src/index.ts",
"scripts": {
"build": "esbuild src/index.ts --bundle --minify --sourcemap --platform=node --target=esnext --outfile=dist/index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"ethers": "^5.7.2"
},
"devDependencies": {
"@types/node": "^17.0.38",
"esbuild": "^0.15.9"
}
}
3 changes: 3 additions & 0 deletions common/ssv/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { getCluster } from './providers/clusters'

export { getCluster }
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { ISSVNetwork } from '@casimir/ethereum/build/artifacts/types'
import { ethers } from 'ethers'

export interface ClusterInput {
/** SSV network contract */
ssv: ISSVNetwork & ethers.Contract
/** SSV network address */
networkAddress: string
/** Operator IDs */
operatorIds: number[]
/** JSON RPC node provider */
provider: ethers.JsonRpcProvider
provider: ethers.providers.JsonRpcProvider
/** Withdrawal address */
withdrawalAddress: string
}
95 changes: 95 additions & 0 deletions common/ssv/src/providers/clusters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { ethers } from 'ethers'
import { Cluster } from '@casimir/types'
import ISSVNetworkJson from '@casimir/ethereum/build/artifacts/src/vendor/interfaces/ISSVNetwork.sol/ISSVNetwork.json'
import { ClusterInput } from '../interfaces/ClusterInput'

/**
* Get cluster snapshot
* @param {ClusterInput} input - Operator IDs and withdrawal address
* @returns {Promise<Cluster>} Cluster snapshot
*/
export async function getCluster(input: ClusterInput): Promise<Cluster> {
const { provider, networkAddress, operatorIds, withdrawalAddress } = input

const ssv = new ethers.Contract(networkAddress, ISSVNetworkJson.abi, provider)

const DAY = 5400
const WEEK = DAY * 7
const MONTH = DAY * 30
const latestBlockNumber = await provider.getBlockNumber()
let step = MONTH
let cluster: Cluster | undefined
let biggestBlockNumber = 0

const eventList = [
'ClusterDeposited',
'ClusterWithdrawn',
'ValidatorAdded',
'ValidatorRemoved',
'ClusterLiquidated',
'ClusterReactivated'
]

// Todo create query filter for events

let fromBlock = latestBlockNumber - step
let toBlock = latestBlockNumber

while (!cluster && fromBlock > 0) {
try {
const result = await ssv.queryFilter('*', fromBlock, toBlock)

for (const item of result) {
const { args, blockNumber, event } = item

try {
const checkClusterEvent = eventList.map(e => e.split('(')[0]).includes(event as string)
if (!checkClusterEvent) continue
const checkOwner = args?.owner === withdrawalAddress
if (!checkOwner) continue
const checkOperators = JSON.stringify(args?.operatorIds.map((value: string) => Number(value))) === JSON.stringify(operatorIds)
if (!checkOperators) continue
const checkCluster = args?.cluster !== undefined
if (!checkCluster) continue

if (blockNumber > biggestBlockNumber) {
biggestBlockNumber = blockNumber
const [
validatorCount,
networkFeeIndex,
index,
balance,
active
] = args.cluster
cluster = {
validatorCount,
networkFeeIndex,
index,
balance,
active
}
}
} catch (e) {
console.error('ERROR FILTERING CLUSTER EVENTS', e)
}
}
toBlock = fromBlock
} catch (e) {
console.error(e)
if (step === MONTH) {
step = WEEK
} else if (step === WEEK) {
step = DAY
}
}
fromBlock = toBlock - step
}

return cluster || {
validatorCount: 0,
networkFeeIndex: 0,
index: 0,
balance: 0,
active: true
}
}
19 changes: 19 additions & 0 deletions common/ssv/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"target": "ESNext",
"strict": true,
"preserveConstEnums": true,
"noEmit": true,
"sourceMap": false,
"module": "CommonJS",
"moduleResolution": "node",
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"isolatedModules": true,
"resolveJsonModule": true
},
"include": [
"./src/*"
]
}
10 changes: 6 additions & 4 deletions common/types/src/interfaces/Cluster.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { ethers } from 'ethers'

export interface Cluster {
validatorCount: number
networkFeeIndex: number
index: number
balance: number
validatorCount: number | ethers.BigNumber
networkFeeIndex: number | ethers.BigNumber
index: number | ethers.BigNumber
balance: number | ethers.BigNumber
active: boolean
}
Loading

0 comments on commit 5513cf2

Please sign in to comment.