Skip to content

Commit 69e41a3

Browse files
authored
Merge pull request #2042 from opentensor/refactor-evm-test
refactor alpha test
2 parents 62c7192 + 37a2ed0 commit 69e41a3

6 files changed

+49
-115
lines changed

evm-tests/src/subtensor.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,4 +377,28 @@ export async function setTargetRegistrationsPerInterval(
377377
call: internal_tx.decodedCall,
378378
});
379379
await waitForTransactionWithRetry(api, tx, alice);
380+
}
381+
382+
// Disable admin freeze window and owner hyperparam rate limiting for tests
383+
export async function disableAdminFreezeWindowAndOwnerHyperparamRateLimit(api: TypedApi<typeof devnet>) {
384+
const alice = getAliceSigner()
385+
386+
const currentAdminFreezeWindow = await api.query.SubtensorModule.AdminFreezeWindow.getValue()
387+
if (currentAdminFreezeWindow !== 0) {
388+
// Set AdminFreezeWindow to 0
389+
const setFreezeWindow = api.tx.AdminUtils.sudo_set_admin_freeze_window({ window: 0 })
390+
const sudoFreezeTx = api.tx.Sudo.sudo({ call: setFreezeWindow.decodedCall })
391+
await waitForTransactionWithRetry(api, sudoFreezeTx, alice)
392+
}
393+
394+
const currentOwnerHyperparamRateLimit = await api.query.SubtensorModule.OwnerHyperparamRateLimit.getValue()
395+
if (currentOwnerHyperparamRateLimit !== BigInt(0)) {
396+
// Set OwnerHyperparamRateLimit to 0
397+
const setOwnerRateLimit = api.tx.AdminUtils.sudo_set_owner_hparam_rate_limit({ limit: BigInt(0) })
398+
const sudoOwnerRateTx = api.tx.Sudo.sudo({ call: setOwnerRateLimit.decodedCall })
399+
await waitForTransactionWithRetry(api, sudoOwnerRateTx, alice)
400+
}
401+
402+
assert.equal(0, await api.query.SubtensorModule.AdminFreezeWindow.getValue())
403+
assert.equal(BigInt(0), await api.query.SubtensorModule.OwnerHyperparamRateLimit.getValue())
380404
}

evm-tests/test/alpha.precompile.test.ts

Lines changed: 11 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import * as assert from "assert";
22

3-
import { getAliceSigner, getDevnetApi, waitForTransactionCompletion, convertPublicKeyToMultiAddress, getRandomSubstrateKeypair, getSignerFromKeypair } from "../src/substrate"
3+
import { getDevnetApi, getRandomSubstrateKeypair } from "../src/substrate"
44
import { getPublicClient } from "../src/utils";
5-
import { ETH_LOCAL_URL, SUB_LOCAL_URL } from "../src/config";
5+
import { ETH_LOCAL_URL } from "../src/config";
66
import { devnet } from "@polkadot-api/descriptors"
77
import { PublicClient } from "viem";
8-
import { PolkadotSigner, TypedApi } from "polkadot-api";
8+
import { TypedApi } from "polkadot-api";
99
import { toViemAddress, convertPublicKeyToSs58 } from "../src/address-utils"
1010
import { IAlphaABI, IALPHA_ADDRESS } from "../src/contracts/alpha"
11-
import { u64 } from "@polkadot-api/substrate-bindings";
12-
11+
import { forceSetBalanceToSs58Address, addNewSubnetwork, startCall } from "../src/subtensor";
1312
describe("Test Alpha Precompile", () => {
1413
// init substrate part
1514
const hotkey = getRandomSubstrateKeypair();
@@ -18,60 +17,20 @@ describe("Test Alpha Precompile", () => {
1817

1918
let api: TypedApi<typeof devnet>;
2019

21-
// sudo account alice as signer
22-
let alice: PolkadotSigner;
23-
2420
// init other variable
2521
let subnetId = 0;
2622

2723
before(async () => {
2824
// init variables got from await and async
2925
publicClient = await getPublicClient(ETH_LOCAL_URL)
3026
api = await getDevnetApi()
31-
alice = await getAliceSigner();
32-
33-
// Fund the hotkey account
34-
{
35-
const multiAddress = convertPublicKeyToMultiAddress(hotkey.publicKey)
36-
const internalCall = api.tx.Balances.force_set_balance({ who: multiAddress, new_free: BigInt(1e12) })
37-
const tx = api.tx.Sudo.sudo({ call: internalCall.decodedCall })
38-
39-
await waitForTransactionCompletion(api, tx, alice)
40-
.then(() => { })
41-
.catch((error) => { console.log(`transaction error ${error}`) });
42-
}
43-
44-
// Fund the coldkey account
45-
{
46-
const multiAddress = convertPublicKeyToMultiAddress(coldkey.publicKey)
47-
const internalCall = api.tx.Balances.force_set_balance({ who: multiAddress, new_free: BigInt(1e12) })
48-
const tx = api.tx.Sudo.sudo({ call: internalCall.decodedCall })
49-
50-
await waitForTransactionCompletion(api, tx, alice)
51-
.then(() => { })
52-
.catch((error) => { console.log(`transaction error ${error}`) });
53-
}
54-
55-
// Register a new subnet
56-
const signer = getSignerFromKeypair(coldkey)
57-
const registerNetworkTx = api.tx.SubtensorModule.register_network({ hotkey: convertPublicKeyToSs58(hotkey.publicKey) })
58-
await waitForTransactionCompletion(api, registerNetworkTx, signer)
59-
.then(() => { })
60-
.catch((error) => { console.log(`transaction error ${error}`) });
61-
62-
// Get the newly created subnet ID
63-
let totalNetworks = await api.query.SubtensorModule.TotalNetworks.getValue()
64-
assert.ok(totalNetworks > 1)
65-
subnetId = totalNetworks - 1
66-
67-
// Register a neuron on the subnet if needed
68-
let uid_count = await api.query.SubtensorModule.SubnetworkN.getValue(subnetId)
69-
if (uid_count === 0) {
70-
const tx = api.tx.SubtensorModule.burned_register({ hotkey: convertPublicKeyToSs58(hotkey.publicKey), netuid: subnetId })
71-
await waitForTransactionCompletion(api, tx, signer)
72-
.then(() => { })
73-
.catch((error) => { console.log(`transaction error ${error}`) });
74-
}
27+
28+
await forceSetBalanceToSs58Address(api, convertPublicKeyToSs58(hotkey.publicKey))
29+
await forceSetBalanceToSs58Address(api, convertPublicKeyToSs58(coldkey.publicKey))
30+
31+
let netuid = await addNewSubnetwork(api, hotkey, coldkey)
32+
await startCall(api, netuid, coldkey)
33+
7534
})
7635

7736
describe("Alpha Price Functions", () => {

evm-tests/test/neuron.precompile.reveal-weights.test.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as assert from "assert";
2-
import { getAliceSigner, getDevnetApi, getRandomSubstrateKeypair, waitForTransactionWithRetry } from "../src/substrate"
2+
import { getAliceSigner, getDevnetApi, getRandomSubstrateKeypair } from "../src/substrate"
33
import { devnet } from "@polkadot-api/descriptors"
44
import { PolkadotSigner, TypedApi } from "polkadot-api";
55
import { convertPublicKeyToSs58, convertH160ToSS58 } from "../src/address-utils"
@@ -14,6 +14,7 @@ import {
1414
forceSetBalanceToEthAddress, forceSetBalanceToSs58Address, addNewSubnetwork, setWeightsSetRateLimit, burnedRegister,
1515
setTempo, setCommitRevealWeightsInterval,
1616
startCall,
17+
disableAdminFreezeWindowAndOwnerHyperparamRateLimit,
1718
} from "../src/subtensor"
1819

1920
// hardcode some values for reveal hash
@@ -70,20 +71,7 @@ describe("Test neuron precompile reveal weights", () => {
7071
await startCall(api, netuid, coldkey)
7172

7273
console.log("test the case on subnet ", netuid)
73-
// Disable admin freeze window and owner hyperparam rate limiting for tests
74-
{
75-
const alice = getAliceSigner()
76-
77-
// Set AdminFreezeWindow to 0
78-
const setFreezeWindow = api.tx.AdminUtils.sudo_set_admin_freeze_window({ window: 0 })
79-
const sudoFreezeTx = api.tx.Sudo.sudo({ call: setFreezeWindow.decodedCall })
80-
await waitForTransactionWithRetry(api, sudoFreezeTx, alice)
81-
82-
// Set OwnerHyperparamRateLimit to 0
83-
const setOwnerRateLimit = api.tx.AdminUtils.sudo_set_owner_hparam_rate_limit({ limit: BigInt(0) })
84-
const sudoOwnerRateTx = api.tx.Sudo.sudo({ call: setOwnerRateLimit.decodedCall })
85-
await waitForTransactionWithRetry(api, sudoOwnerRateTx, alice)
86-
}
74+
await disableAdminFreezeWindowAndOwnerHyperparamRateLimit(api)
8775

8876
await setWeightsSetRateLimit(api, netuid, BigInt(0))
8977

evm-tests/test/neuron.precompile.set-weights.test.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as assert from "assert";
22

3-
import { getAliceSigner, getDevnetApi, getRandomSubstrateKeypair, waitForTransactionWithRetry } from "../src/substrate"
3+
import { getDevnetApi, getRandomSubstrateKeypair } from "../src/substrate"
44
import { devnet } from "@polkadot-api/descriptors"
55
import { TypedApi } from "polkadot-api";
66
import { convertH160ToSS58, convertPublicKeyToSs58, } from "../src/address-utils"
@@ -10,7 +10,8 @@ import { generateRandomEthersWallet } from "../src/utils"
1010
import {
1111
forceSetBalanceToSs58Address, forceSetBalanceToEthAddress, addNewSubnetwork, burnedRegister, setCommitRevealWeightsEnabled,
1212
setWeightsSetRateLimit,
13-
startCall
13+
startCall,
14+
disableAdminFreezeWindowAndOwnerHyperparamRateLimit
1415
} from "../src/subtensor"
1516

1617
describe("Test neuron precompile contract, set weights function", () => {
@@ -38,20 +39,7 @@ describe("Test neuron precompile contract, set weights function", () => {
3839
await burnedRegister(api, netuid, convertH160ToSS58(wallet.address), coldkey)
3940
const uid = await api.query.SubtensorModule.Uids.getValue(netuid, convertH160ToSS58(wallet.address))
4041
assert.notEqual(uid, undefined)
41-
// Disable admin freeze window and owner hyperparam rate limiting for tests
42-
{
43-
const alice = getAliceSigner()
44-
45-
// Set AdminFreezeWindow to 0
46-
const setFreezeWindow = api.tx.AdminUtils.sudo_set_admin_freeze_window({ window: 0 })
47-
const sudoFreezeTx = api.tx.Sudo.sudo({ call: setFreezeWindow.decodedCall })
48-
await waitForTransactionWithRetry(api, sudoFreezeTx, alice)
49-
50-
// Set OwnerHyperparamRateLimit to 0
51-
const setOwnerRateLimit = api.tx.AdminUtils.sudo_set_owner_hparam_rate_limit({ limit: BigInt(0) })
52-
const sudoOwnerRateTx = api.tx.Sudo.sudo({ call: setOwnerRateLimit.decodedCall })
53-
await waitForTransactionWithRetry(api, sudoOwnerRateTx, alice)
54-
}
42+
await disableAdminFreezeWindowAndOwnerHyperparamRateLimit(api)
5543
// disable reveal and enable direct set weights
5644
await setCommitRevealWeightsEnabled(api, netuid, false)
5745
await setWeightsSetRateLimit(api, netuid, BigInt(0))

evm-tests/test/staking.precompile.reward.test.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as assert from "assert";
2-
import { getAliceSigner, getDevnetApi, getRandomSubstrateKeypair, waitForTransactionWithRetry } from "../src/substrate"
2+
import { getDevnetApi, getRandomSubstrateKeypair } from "../src/substrate"
33
import { devnet } from "@polkadot-api/descriptors"
44
import { TypedApi } from "polkadot-api";
55
import { convertPublicKeyToSs58 } from "../src/address-utils"
@@ -8,7 +8,8 @@ import {
88
forceSetBalanceToSs58Address, addNewSubnetwork, burnedRegister,
99
setTxRateLimit, setTempo, setWeightsSetRateLimit, setSubnetOwnerCut, setMaxAllowedUids,
1010
setMinDelegateTake, setActivityCutoff, addStake, setWeight, rootRegister,
11-
startCall
11+
startCall,
12+
disableAdminFreezeWindowAndOwnerHyperparamRateLimit
1213
} from "../src/subtensor"
1314

1415
describe("Test neuron precompile reward", () => {
@@ -39,20 +40,7 @@ describe("Test neuron precompile reward", () => {
3940
await startCall(api, netuid, coldkey)
4041

4142
console.log("test the case on subnet ", netuid)
42-
// Disable admin freeze window and owner hyperparam rate limiting for tests
43-
{
44-
const alice = getAliceSigner()
45-
46-
// Set AdminFreezeWindow to 0
47-
const setFreezeWindow = api.tx.AdminUtils.sudo_set_admin_freeze_window({ window: 0 })
48-
const sudoFreezeTx = api.tx.Sudo.sudo({ call: setFreezeWindow.decodedCall })
49-
await waitForTransactionWithRetry(api, sudoFreezeTx, alice)
50-
51-
// Set OwnerHyperparamRateLimit to 0
52-
const setOwnerRateLimit = api.tx.AdminUtils.sudo_set_owner_hparam_rate_limit({ limit: BigInt(0) })
53-
const sudoOwnerRateTx = api.tx.Sudo.sudo({ call: setOwnerRateLimit.decodedCall })
54-
await waitForTransactionWithRetry(api, sudoOwnerRateTx, alice)
55-
}
43+
await disableAdminFreezeWindowAndOwnerHyperparamRateLimit(api)
5644

5745
await setTxRateLimit(api, BigInt(0))
5846
await setTempo(api, root_netuid, root_tempo)

evm-tests/test/subnet.precompile.hyperparameter.test.ts

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import * as assert from "assert";
22

3-
import { getDevnetApi, getRandomSubstrateKeypair, getAliceSigner, waitForTransactionWithRetry } from "../src/substrate"
3+
import { getDevnetApi, getRandomSubstrateKeypair } from "../src/substrate"
44
import { devnet } from "@polkadot-api/descriptors"
55
import { TypedApi } from "polkadot-api";
66
import { convertPublicKeyToSs58 } from "../src/address-utils"
77
import { generateRandomEthersWallet } from "../src/utils";
88
import { ISubnetABI, ISUBNET_ADDRESS } from "../src/contracts/subnet"
99
import { ethers } from "ethers"
10-
import { forceSetBalanceToEthAddress, forceSetBalanceToSs58Address } from "../src/subtensor"
10+
import { disableAdminFreezeWindowAndOwnerHyperparamRateLimit, forceSetBalanceToEthAddress, forceSetBalanceToSs58Address } from "../src/subtensor"
1111

1212
describe("Test the Subnet precompile contract", () => {
1313
// init eth part
@@ -26,20 +26,7 @@ describe("Test the Subnet precompile contract", () => {
2626
await forceSetBalanceToSs58Address(api, convertPublicKeyToSs58(hotkey2.publicKey))
2727
await forceSetBalanceToEthAddress(api, wallet.address)
2828

29-
// Disable admin freeze window and owner hyperparam rate limiting for tests
30-
{
31-
const alice = getAliceSigner()
32-
33-
// Set AdminFreezeWindow to 0
34-
const setFreezeWindow = api.tx.AdminUtils.sudo_set_admin_freeze_window({ window: 0 })
35-
const sudoFreezeTx = api.tx.Sudo.sudo({ call: setFreezeWindow.decodedCall })
36-
await waitForTransactionWithRetry(api, sudoFreezeTx, alice)
37-
38-
// Set OwnerHyperparamRateLimit to 0
39-
const setOwnerRateLimit = api.tx.AdminUtils.sudo_set_owner_hparam_rate_limit({ limit: BigInt(0) })
40-
const sudoOwnerRateTx = api.tx.Sudo.sudo({ call: setOwnerRateLimit.decodedCall })
41-
await waitForTransactionWithRetry(api, sudoOwnerRateTx, alice)
42-
}
29+
await disableAdminFreezeWindowAndOwnerHyperparamRateLimit(api)
4330
})
4431

4532
it("Can register network without identity info", async () => {

0 commit comments

Comments
 (0)