Skip to content

Commit 66843e5

Browse files
committed
Share network across staking suite
1 parent 573b43b commit 66843e5

1 file changed

Lines changed: 52 additions & 67 deletions

File tree

packages/shared/src/staking.ts

Lines changed: 52 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,45 +1743,30 @@ async function cancelDeferredSlashTestAsAdmin<
17431743
export function slashingTests<
17441744
TCustom extends Record<string, unknown> | undefined,
17451745
TInitStorages extends Record<string, Record<string, any>> | undefined,
1746-
>(chain: Chain<TCustom, TInitStorages>): RootTestTree {
1747-
let client!: Client<TCustom, TInitStorages>
1748-
let restoreSnapshot: () => Promise<void>
1746+
>(getClient: () => Client<TCustom, TInitStorages>): RootTestTree {
17491747
return {
17501748
kind: 'describe',
17511749
label: 'slashing tests',
1752-
beforeAll: async () => {
1753-
;[client] = await createNetworks(chain)
1754-
restoreSnapshot = captureSnapshot(client)
1755-
},
1756-
beforeEach: async () => {
1757-
await restoreSnapshot()
1758-
const blockNumber = (await client.api.rpc.chain.getHeader()).number.toNumber()
1759-
await client.dev.setHead(blockNumber)
1760-
},
1761-
afterAll: async () => {
1762-
await client.api.disconnect().catch(() => {})
1763-
await client.teardown().catch(() => {})
1764-
},
17651750
children: [
17661751
{
17671752
kind: 'test',
17681753
label: 'unapplied slash',
1769-
testFn: async () => await unappliedSlashTest(client),
1754+
testFn: async () => await unappliedSlashTest(getClient()),
17701755
},
17711756
{
17721757
kind: 'test',
17731758
label: 'cancel deferred slash with bad origin',
1774-
testFn: async () => await cancelDeferredSlashTestBadOrigin(client),
1759+
testFn: async () => await cancelDeferredSlashTestBadOrigin(getClient()),
17751760
},
17761761
{
17771762
kind: 'test',
17781763
label: 'cancel deferred slash as root',
1779-
testFn: async () => await cancelDeferredSlashTestAsRoot(client),
1764+
testFn: async () => await cancelDeferredSlashTestAsRoot(getClient()),
17801765
},
17811766
{
17821767
kind: 'test',
17831768
label: 'cancel deferred slash as admin',
1784-
testFn: async () => await cancelDeferredSlashTestAsAdmin(client),
1769+
testFn: async () => await cancelDeferredSlashTestAsAdmin(getClient()),
17851770
},
17861771
],
17871772
}
@@ -1790,70 +1775,55 @@ export function slashingTests<
17901775
export function baseStakingE2ETests<
17911776
TCustom extends Record<string, unknown> | undefined,
17921777
TInitStorages extends Record<string, Record<string, any>> | undefined,
1793-
>(chain: Chain<TCustom, TInitStorages>): RootTestTree {
1794-
let client!: Client<TCustom, TInitStorages>
1795-
let restoreSnapshot: () => Promise<void>
1778+
>(getClient: () => Client<TCustom, TInitStorages>): RootTestTree {
17961779
return {
17971780
kind: 'describe',
17981781
label: 'base staking tests',
1799-
beforeAll: async () => {
1800-
;[client] = await createNetworks(chain)
1801-
restoreSnapshot = captureSnapshot(client)
1802-
},
1803-
beforeEach: async () => {
1804-
await restoreSnapshot()
1805-
const blockNumber = (await client.api.rpc.chain.getHeader()).number.toNumber()
1806-
await client.dev.setHead(blockNumber)
1807-
},
1808-
afterAll: async () => {
1809-
await client.api.disconnect().catch(() => {})
1810-
await client.teardown().catch(() => {})
1811-
},
18121782
children: [
18131783
{
18141784
kind: 'test' as const,
18151785
label: 'trying to become a validator with no bonded funds fails',
1816-
testFn: async () => await validateNoBondedFundsFailureTest(client),
1786+
testFn: async () => await validateNoBondedFundsFailureTest(getClient()),
18171787
},
18181788
{
18191789
kind: 'test' as const,
18201790
label: 'trying to nominate with no bonded funds fails',
1821-
testFn: async () => await nominateNoBondedFundsFailureTest(client),
1791+
testFn: async () => await nominateNoBondedFundsFailureTest(getClient()),
18221792
},
18231793
{
18241794
kind: 'test' as const,
18251795
label: 'staking lifecycle',
1826-
testFn: async () => await stakingLifecycleTest(client),
1796+
testFn: async () => await stakingLifecycleTest(getClient()),
18271797
},
18281798
{
18291799
kind: 'test' as const,
18301800
label: 'test force unstaking of nominator',
1831-
testFn: async () => await forceUnstakeTest(client),
1801+
testFn: async () => await forceUnstakeTest(getClient()),
18321802
},
18331803
{
18341804
kind: 'test' as const,
18351805
label: 'set minimum validator commission',
1836-
testFn: async () => await setMinCommission(client),
1806+
testFn: async () => await setMinCommission(getClient()),
18371807
},
18381808
{
18391809
kind: 'test' as const,
18401810
label: 'set staking configs',
1841-
testFn: async () => await setStakingConfigsTest(client),
1811+
testFn: async () => await setStakingConfigsTest(getClient()),
18421812
},
18431813
{
18441814
kind: 'test' as const,
18451815
label: 'force apply validator commission',
1846-
testFn: async () => await forceApplyValidatorCommissionTest(client),
1816+
testFn: async () => await forceApplyValidatorCommissionTest(getClient()),
18471817
},
18481818
{
18491819
kind: 'test' as const,
18501820
label: 'modify validator count',
1851-
testFn: async () => await modifyValidatorCountTest(client),
1821+
testFn: async () => await modifyValidatorCountTest(getClient()),
18521822
},
18531823
{
18541824
kind: 'test' as const,
18551825
label: 'chill other',
1856-
testFn: async () => await chillOtherTest(client),
1826+
testFn: async () => await chillOtherTest(getClient()),
18571827
},
18581828
],
18591829
}
@@ -1865,30 +1835,15 @@ export function baseStakingE2ETests<
18651835
export function fastUnstakeTests<
18661836
TCustom extends Record<string, unknown> | undefined,
18671837
TInitStorages extends Record<string, Record<string, any>> | undefined,
1868-
>(chain: Chain<TCustom, TInitStorages>): RootTestTree {
1869-
let client!: Client<TCustom, TInitStorages>
1870-
let restoreSnapshot: () => Promise<void>
1838+
>(getClient: () => Client<TCustom, TInitStorages>): RootTestTree {
18711839
return {
18721840
kind: 'describe',
18731841
label: 'fast unstake',
1874-
beforeAll: async () => {
1875-
;[client] = await createNetworks(chain)
1876-
restoreSnapshot = captureSnapshot(client)
1877-
},
1878-
beforeEach: async () => {
1879-
await restoreSnapshot()
1880-
const blockNumber = (await client.api.rpc.chain.getHeader()).number.toNumber()
1881-
await client.dev.setHead(blockNumber)
1882-
},
1883-
afterAll: async () => {
1884-
await client.api.disconnect().catch(() => {})
1885-
await client.teardown().catch(() => {})
1886-
},
18871842
children: [
18881843
{
18891844
kind: 'test',
18901845
label: 'test fast unstake',
1891-
testFn: async () => await fastUnstakeTest(client, client.config.properties.addressEncoding),
1846+
testFn: async () => await fastUnstakeTest(getClient(), getClient().config.properties.addressEncoding),
18921847
},
18931848
],
18941849
}
@@ -1901,12 +1856,27 @@ export function fullStakingE2ETests<
19011856
TCustom extends Record<string, unknown> | undefined,
19021857
TInitStorages extends Record<string, Record<string, any>> | undefined,
19031858
>(chain: Chain<TCustom, TInitStorages>, testConfig: TestConfig): RootTestTree {
1904-
const basalTestTree = baseStakingE2ETests(chain)
1905-
const slashingTestTree = slashingTests(chain)
1859+
let client!: Client<TCustom, TInitStorages>
1860+
let restoreSnapshot: () => Promise<void>
1861+
const basalTestTree = baseStakingE2ETests(() => client)
1862+
const slashingTestTree = slashingTests(() => client)
19061863

19071864
return {
19081865
kind: 'describe' as const,
19091866
label: testConfig.testSuiteName,
1867+
beforeAll: async () => {
1868+
;[client] = await createNetworks(chain)
1869+
restoreSnapshot = captureSnapshot(client)
1870+
},
1871+
beforeEach: async () => {
1872+
await restoreSnapshot()
1873+
const blockNumber = (await client.api.rpc.chain.getHeader()).number.toNumber()
1874+
await client.dev.setHead(blockNumber)
1875+
},
1876+
afterAll: async () => {
1877+
await client.api.disconnect().catch(() => {})
1878+
await client.teardown().catch(() => {})
1879+
},
19101880
children: [basalTestTree, slashingTestTree],
19111881
}
19121882
}
@@ -1921,13 +1891,28 @@ export function completeStakingE2ETests<
19211891
TCustom extends Record<string, unknown> | undefined,
19221892
TInitStorages extends Record<string, Record<string, any>> | undefined,
19231893
>(chain: Chain<TCustom, TInitStorages>, testConfig: TestConfig): RootTestTree {
1924-
const basalTestTree = baseStakingE2ETests(chain)
1925-
const slashingTestTree = slashingTests(chain)
1926-
const fastUnstakeTestTree = fastUnstakeTests(chain)
1894+
let client!: Client<TCustom, TInitStorages>
1895+
let restoreSnapshot: () => Promise<void>
1896+
const basalTestTree = baseStakingE2ETests(() => client)
1897+
const slashingTestTree = slashingTests(() => client)
1898+
const fastUnstakeTestTree = fastUnstakeTests(() => client)
19271899

19281900
return {
19291901
kind: 'describe' as const,
19301902
label: testConfig.testSuiteName,
1903+
beforeAll: async () => {
1904+
;[client] = await createNetworks(chain)
1905+
restoreSnapshot = captureSnapshot(client)
1906+
},
1907+
beforeEach: async () => {
1908+
await restoreSnapshot()
1909+
const blockNumber = (await client.api.rpc.chain.getHeader()).number.toNumber()
1910+
await client.dev.setHead(blockNumber)
1911+
},
1912+
afterAll: async () => {
1913+
await client.api.disconnect().catch(() => {})
1914+
await client.teardown().catch(() => {})
1915+
},
19311916
children: [basalTestTree, slashingTestTree, fastUnstakeTestTree],
19321917
}
19331918
}

0 commit comments

Comments
 (0)