Skip to content

Commit

Permalink
use v1.7.0 for creation
Browse files Browse the repository at this point in the history
  • Loading branch information
HananINouman committed Mar 14, 2024
1 parent 54423ba commit 0a31856
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 115 deletions.
22 changes: 0 additions & 22 deletions src/ajv.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,10 @@
import Ajv, { type ErrorObject } from 'ajv'
import { ETHER_TO_GWEI } from './constants'

function validDpositAmounts (data: boolean, deposits: string[]): boolean {
let sum = 0
for (let i = 0; i < deposits.length; i++) {
const amount = parseInt(deposits[i])
if (amount % ETHER_TO_GWEI !== 0 || amount > 32 * ETHER_TO_GWEI) {
return false
}
sum += amount
}
if (sum !== 32 * ETHER_TO_GWEI) {
return false
} else {
return true
}
}

export function validatePayload (
data: any,
schema: any,
): ErrorObject[] | undefined | null | boolean {
const ajv = new Ajv()
ajv.addKeyword({
keyword: 'validDpositAmounts',
validate: validDpositAmounts,
errors: true,
})
const validate = ajv.compile(schema)
const isValid = validate(data)
if (!isValid) {
Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export const signEnrPayload = (

export const DKG_ALGORITHM = 'default'

export const CONFIG_VERSION = 'v1.8.0'
export const CONFIG_VERSION = 'v1.7.0'

export const SDK_VERSION = pjson.version

Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ export class Client extends Base {
timestamp: new Date().toISOString(),
threshold: Math.ceil((2 * newCluster.operators.length) / 3),
num_validators: newCluster.validators.length,
deposit_amounts: newCluster.deposit_amounts ? newCluster.deposit_amounts : ['32000000000']
}

try {
Expand Down
8 changes: 0 additions & 8 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,6 @@ export const definitionSchema = {
required: ['fee_recipient_address', 'withdrawal_address'],
},
},
deposit_amounts: {
type: 'array',
items: {
type: 'string',
pattern: '^[0-9]+$',
},
validDpositAmounts: true
},
},
required: ['name', 'operators', 'validators'],
}
8 changes: 0 additions & 8 deletions src/verification/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ export const clusterConfigOrDefinitionHash = (
): string => {
let definitionType, val

if (semver.eq(cluster.version, 'v1.6.0')) {
definitionType = clusterDefinitionContainerTypeV1X6(configOnly)
val = hashClusterDefinitionV1X6(cluster, configOnly)
return (
'0x' + Buffer.from(definitionType.hashTreeRoot(val).buffer).toString('hex')
)
}

if (semver.eq(cluster.version, 'v1.7.0')) {
definitionType = clusterDefinitionContainerTypeV1X7(configOnly)
val = hashClusterDefinitionV1X7(cluster, configOnly)
Expand Down
79 changes: 24 additions & 55 deletions test/methods.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,23 @@ describe('Cluster Client', () => {
.mockReturnValue(Promise.resolve({ config_hash: mockConfigHash }))

const configHash =
await clientInstance.createClusterDefinition(clusterConfigV1X8)
await clientInstance.createClusterDefinition(clusterConfigV1X7)
expect(configHash).toEqual(mockConfigHash)
})

test('acceptClusterDefinition should return cluster definition', async () => {
clientInstance['request'] = jest
.fn()
.mockReturnValue(Promise.resolve(clusterLockV1X8.cluster_definition))
.mockReturnValue(Promise.resolve(clusterLockV1X7.cluster_definition))

const clusterDefinition = await clientInstance.acceptClusterDefinition(
{
enr: clusterLockV1X8.cluster_definition.operators[0].enr,
version: clusterLockV1X8.cluster_definition.version,
enr: clusterLockV1X7.cluster_definition.operators[0].enr,
version: clusterLockV1X7.cluster_definition.version,
},
clusterLockV1X8.cluster_definition.config_hash,
clusterLockV1X7.cluster_definition.config_hash,
)
expect(clusterDefinition).toEqual(clusterLockV1X8.cluster_definition)
expect(clusterDefinition).toEqual(clusterLockV1X7.cluster_definition)
})

test('createClusterDefinition should throw an error on invalid operators', async () => {
Expand All @@ -60,7 +60,7 @@ describe('Cluster Client', () => {
.mockReturnValue(Promise.resolve({ config_hash: mockConfigHash }))
try {
await clientInstance.createClusterDefinition({
...clusterConfigV1X8,
...clusterConfigV1X7,
operators: [],
})
} catch (error: any) {
Expand All @@ -70,37 +70,9 @@ describe('Cluster Client', () => {
}
})

test('createClusterDefinition should accept a configuration without deposit_amounts ', async () => {
clientInstance['request'] = jest
.fn()
.mockReturnValue(Promise.resolve({ config_hash: mockConfigHash }))

const configHash = await clientInstance.createClusterDefinition({
...clusterConfigV1X7,
})

expect(configHash).toEqual(mockConfigHash)
})

test('createClusterDefinition should throw on not valid deposit_amounts ', async () => {
clientInstance['request'] = jest
.fn()
.mockReturnValue(Promise.resolve({ config_hash: mockConfigHash }))
try {
await clientInstance.createClusterDefinition({
...clusterConfigV1X7,
deposit_amounts: ['34000000']
})
} catch (error: any) {
expect(error.message).toEqual(
"Schema compilation errors', must pass \"validDpositAmounts\" keyword validation",
)
}
})

test('validatePayload should throw an error on empty schema', async () => {
try {
validatePayload({ ...clusterConfigV1X8, operators: [] }, '')
validatePayload({ ...clusterConfigV1X7, operators: [] }, '')
} catch (error: any) {
expect(error.message).toEqual('schema must be object or boolean')
}
Expand All @@ -109,29 +81,26 @@ describe('Cluster Client', () => {
test('getClusterdefinition should return cluster definition if config hash exist', async () => {
clientInstance['request'] = jest
.fn()
.mockReturnValue(Promise.resolve(clusterLockV1X8.cluster_definition))
.mockReturnValue(Promise.resolve(clusterLockV1X7.cluster_definition))

const clusterDefinition = await clientInstance.getClusterDefinition(
clusterLockV1X8.cluster_definition.config_hash,
clusterLockV1X7.cluster_definition.config_hash,
)

expect(clusterDefinition.deposit_amounts?.length).toEqual(
clusterLockV1X8.cluster_definition.deposit_amounts.length,
)
expect(clusterDefinition.config_hash).toEqual(
clusterLockV1X8.cluster_definition.config_hash,
clusterLockV1X7.cluster_definition.config_hash,
)
})

test('getClusterLock should return lockFile if exist', async () => {
clientInstance['request'] = jest
.fn()
.mockReturnValue(Promise.resolve(clusterLockV1X8))
.mockReturnValue(Promise.resolve(clusterLockV1X7))

const clusterLock = await clientInstance.getClusterLock(
clusterLockV1X8.cluster_definition.config_hash,
clusterLockV1X7.cluster_definition.config_hash,
)
expect(clusterLock.lock_hash).toEqual(clusterLockV1X8.lock_hash)
expect(clusterLock.lock_hash).toEqual(clusterLockV1X7.lock_hash)
})

test('request method should set user agent header', async () => {
Expand Down Expand Up @@ -171,7 +140,7 @@ describe('Cluster Client without a signer', () => {

test('createClusterDefinition should throw an error without signer', async () => {
try {
await clientInstance.createClusterDefinition(clusterConfigV1X8)
await clientInstance.createClusterDefinition(clusterConfigV1X7)
} catch (err: any) {
expect(err.message).toEqual('Signer is required in createClusterDefinition')
}
Expand All @@ -181,10 +150,10 @@ describe('Cluster Client without a signer', () => {
try {
await clientInstance.acceptClusterDefinition(
{
enr: clusterLockV1X8.cluster_definition.operators[0].enr,
version: clusterLockV1X8.cluster_definition.version,
enr: clusterLockV1X7.cluster_definition.operators[0].enr,
version: clusterLockV1X7.cluster_definition.version,
},
clusterLockV1X8.cluster_definition.config_hash,
clusterLockV1X7.cluster_definition.config_hash,
)
} catch (err: any) {
expect(err.message).toEqual('Signer is required in acceptClusterDefinition')
Expand All @@ -194,25 +163,25 @@ describe('Cluster Client without a signer', () => {
test('getClusterdefinition should return cluster definition if config hash exist', async () => {
clientInstance['request'] = jest
.fn()
.mockReturnValue(Promise.resolve(clusterLockV1X8.cluster_definition))
.mockReturnValue(Promise.resolve(clusterLockV1X7.cluster_definition))

const clusterDefinition = await clientInstance.getClusterDefinition(
clusterLockV1X8.cluster_definition.config_hash,
clusterLockV1X7.cluster_definition.config_hash,
)
expect(clusterDefinition.config_hash).toEqual(
clusterLockV1X8.cluster_definition.config_hash,
clusterLockV1X7.cluster_definition.config_hash,
)
})

test('getClusterLock should return lockFile if exist', async () => {
clientInstance['request'] = jest
.fn()
.mockReturnValue(Promise.resolve(clusterLockV1X8))
.mockReturnValue(Promise.resolve(clusterLockV1X7))

const clusterLock = await clientInstance.getClusterLock(
clusterLockV1X8.cluster_definition.config_hash,
clusterLockV1X7.cluster_definition.config_hash,
)
expect(clusterLock.lock_hash).toEqual(clusterLockV1X8.lock_hash)
expect(clusterLock.lock_hash).toEqual(clusterLockV1X7.lock_hash)
})

test.each([{ version: 'v1.7.0', clusterLock: clusterLockV1X7 }, { version: 'v1.8.0', clusterLock: clusterLockV1X8 }])(
Expand Down
36 changes: 16 additions & 20 deletions test/sdk-package-test/cluster.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import request from 'supertest'
import dotenv from 'dotenv'
import { clusterConfigV1X8, clusterLockV1X7, clusterLockV1X8, enr } from './fixtures'
import { clusterConfigV1X7, clusterLockV1X7, clusterLockV1X8, enr } from './fixtures'
import {
client,
updateClusterDef,
Expand Down Expand Up @@ -32,7 +32,7 @@ describe('Cluster Definition', () => {
})

beforeAll(async () => {
configHash = await client.createClusterDefinition(clusterConfigV1X8)
configHash = await client.createClusterDefinition(clusterConfigV1X7)
})

it('should post a cluster definition and return confighash', async () => {
Expand All @@ -41,7 +41,7 @@ describe('Cluster Definition', () => {

it('should throw on post a cluster without a signer', async () => {
try {
await clientWithoutAsigner.createClusterDefinition(clusterConfigV1X8)
await clientWithoutAsigner.createClusterDefinition(clusterConfigV1X7)
} catch (err: any) {
expect(err.message).toEqual('Signer is required in createClusterDefinition')
}
Expand Down Expand Up @@ -71,9 +71,9 @@ describe('Cluster Definition', () => {

it('should update the cluster which the operator belongs to', async () => {
const signerAddress = await signer.getAddress()
clusterConfigV1X8.operators.push({ address: signerAddress })
clusterConfigV1X7.operators.push({ address: signerAddress })

secondConfigHash = await client.createClusterDefinition(clusterConfigV1X8)
secondConfigHash = await client.createClusterDefinition(clusterConfigV1X7)

const definitionData: ClusterDefintion =
await client.acceptClusterDefinition(
Expand Down Expand Up @@ -108,7 +108,7 @@ describe('Cluster Definition', () => {

describe('Poll Cluster Lock', () => {
// Test polling getClusterLock through mimicing the whole flow using obol-api endpoints
const { definition_hash: _, ...rest } = clusterLockV1X8.cluster_definition
const { definition_hash: _, ...rest } = clusterLockV1X7.cluster_definition
const clusterWithoutDefHash = rest
const clientWithoutAsigner = new Client({
baseUrl: 'https://obol-api-nonprod-dev.dev.obol.tech',
Expand All @@ -126,7 +126,7 @@ describe('Poll Cluster Lock', () => {
const pollReqIntervalId = setInterval(async function () {
try {
const lockFile = await client.getClusterLock(
clusterLockV1X8.cluster_definition.config_hash,
clusterLockV1X7.cluster_definition.config_hash,
)
if (lockFile?.lock_hash) {
clearInterval(pollReqIntervalId)
Expand All @@ -144,8 +144,8 @@ describe('Poll Cluster Lock', () => {
}, 5000)
}),
(async () => {
await updateClusterDef(clusterLockV1X8.cluster_definition)
await publishLockFile(clusterLockV1X8)
await updateClusterDef(clusterLockV1X7.cluster_definition)
await publishLockFile(clusterLockV1X7)
})(),
])
expect(lockObject).toHaveProperty('lock_hash')
Expand All @@ -158,7 +158,7 @@ describe('Poll Cluster Lock', () => {
const pollReqIntervalId = setInterval(async function () {
try {
const lockFile = await clientWithoutAsigner.getClusterLock(
clusterLockV1X8.cluster_definition.config_hash,
clusterLockV1X7.cluster_definition.config_hash,
)
if (lockFile?.lock_hash) {
clearInterval(pollReqIntervalId)
Expand All @@ -175,8 +175,8 @@ describe('Poll Cluster Lock', () => {
}, 5000)
}),
(async () => {
await updateClusterDef(clusterLockV1X8.cluster_definition)
await publishLockFile(clusterLockV1X8)
await updateClusterDef(clusterLockV1X7.cluster_definition)
await publishLockFile(clusterLockV1X7)
})(),
])
expect(lockObject).toHaveProperty('lock_hash')
Expand All @@ -185,14 +185,10 @@ describe('Poll Cluster Lock', () => {
it('should fetch the cluster definition for the configHash', async () => {
const clusterDefinition: ClusterDefintion =
await client.getClusterDefinition(
clusterLockV1X8.cluster_definition.config_hash,
clusterLockV1X7.cluster_definition.config_hash,
)

expect(clusterDefinition.deposit_amounts?.length).toEqual(
clusterLockV1X8.cluster_definition.deposit_amounts.length,
)
expect(clusterDefinition.config_hash).toEqual(
clusterLockV1X8.cluster_definition.config_hash,
clusterLockV1X7.cluster_definition.config_hash,
)
})

Expand All @@ -204,8 +200,8 @@ describe('Poll Cluster Lock', () => {
})

afterAll(async () => {
const configHash = clusterLockV1X8.cluster_definition.config_hash
const lockHash = clusterLockV1X8.lock_hash
const configHash = clusterLockV1X7.cluster_definition.config_hash
const lockHash = clusterLockV1X7.lock_hash

await request(app)
.delete(`/lock/${lockHash}`)
Expand Down

0 comments on commit 0a31856

Please sign in to comment.