Skip to content

Commit b21a91f

Browse files
committed
fix conflict
2 parents b1e289f + 698188f commit b21a91f

45 files changed

Lines changed: 1127 additions & 367 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chain-extensions/src/mock.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ parameter_types! {
331331
pub const InitialDissolveNetworkScheduleDuration: u64 = 5 * 24 * 60 * 60 / 12; // Default as 5 days
332332
pub const InitialTaoWeight: u64 = 0; // 100% global weight.
333333
pub const InitialEmaPriceHalvingPeriod: u64 = 201_600_u64; // 4 weeks
334-
pub const DurationOfStartCall: u64 = 7 * 24 * 60 * 60 / 12; // Default as 7 days
334+
pub const InitialStartCallDelay: u64 = 7 * 24 * 60 * 60 / 12; // Default as 7 days
335335
pub const InitialKeySwapOnSubnetCost: u64 = 10_000_000;
336336
pub const HotkeySwapOnSubnetInterval: u64 = 15; // 15 block, should be bigger than subnet number, then trigger clean up for all subnets
337337
pub const MaxContributorsPerLeaseToRemove: u32 = 3;
@@ -402,7 +402,7 @@ impl pallet_subtensor::Config for Test {
402402
type InitialDissolveNetworkScheduleDuration = InitialDissolveNetworkScheduleDuration;
403403
type InitialTaoWeight = InitialTaoWeight;
404404
type InitialEmaPriceHalvingPeriod = InitialEmaPriceHalvingPeriod;
405-
type DurationOfStartCall = DurationOfStartCall;
405+
type InitialStartCallDelay = InitialStartCallDelay;
406406
type SwapInterface = pallet_subtensor_swap::Pallet<Self>;
407407
type KeySwapOnSubnetCost = InitialKeySwapOnSubnetCost;
408408
type HotkeySwapOnSubnetInterval = HotkeySwapOnSubnetInterval;

contract-tests/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"scripts": {
3-
"test": "mocha --timeout 999999 --retries 3 --file src/setup.ts --require ts-node/register test/*test.ts"
3+
"test": "TS_NODE_PREFER_TS_EXTS=1 TS_NODE_TRANSPILE_ONLY=1 mocha --timeout 999999 --retries 3 --file src/setup.ts --require ts-node/register --extension ts \"test/**/*.ts\""
44
},
55
"keywords": [],
66
"author": "",

contract-tests/src/subtensor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ export async function setSubtokenEnable(api: TypedApi<typeof devnet>, netuid: nu
296296
export async function startCall(api: TypedApi<typeof devnet>, netuid: number, keypair: KeyPair) {
297297
const registerBlock = Number(await api.query.SubtensorModule.NetworkRegisteredAt.getValue(netuid))
298298
let currentBlock = await api.query.System.Number.getValue()
299-
const duration = Number(await api.constants.SubtensorModule.DurationOfStartCall)
299+
const duration = Number(await api.constants.SubtensorModule.InitialStartCallDelay)
300300

301301
while (currentBlock - registerBlock <= duration) {
302302
await new Promise((resolve) => setTimeout(resolve, 2000));

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

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as assert from "assert";
2-
import { getAliceSigner, getDevnetApi, getRandomSubstrateKeypair } from "../src/substrate"
2+
import { getAliceSigner, getDevnetApi, getRandomSubstrateKeypair, waitForTransactionWithRetry } 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"
@@ -23,6 +23,16 @@ const values = [5];
2323
const salt = [9];
2424
const version_key = 0;
2525

26+
async function setStakeThreshold(
27+
api: TypedApi<typeof devnet>,
28+
alice: PolkadotSigner,
29+
minStake: bigint,
30+
) {
31+
const internalCall = api.tx.AdminUtils.sudo_set_stake_threshold({ min_stake: minStake })
32+
const tx = api.tx.Sudo.sudo({ call: internalCall.decodedCall })
33+
await waitForTransactionWithRetry(api, tx, alice)
34+
}
35+
2636
function getCommitHash(netuid: number, address: string) {
2737
const registry = new TypeRegistry();
2838
let publicKey = convertH160ToPublicKey(address);
@@ -53,7 +63,7 @@ describe("Test neuron precompile reveal weights", () => {
5363
const coldkey = getRandomSubstrateKeypair();
5464

5565
let api: TypedApi<typeof devnet>
56-
let commitEpoch: number;
66+
let commitEpoch: number | undefined;
5767

5868
// sudo account alice as signer
5969
let alice: PolkadotSigner;
@@ -86,11 +96,52 @@ describe("Test neuron precompile reveal weights", () => {
8696
assert.equal(uid, uids[0])
8797
})
8898

99+
async function ensureCommitEpoch(netuid: number, contract: ethers.Contract) {
100+
if (commitEpoch !== undefined) {
101+
return
102+
}
103+
104+
const ss58Address = convertH160ToSS58(wallet.address)
105+
const existingCommits = await api.query.SubtensorModule.WeightCommits.getValue(
106+
netuid,
107+
ss58Address
108+
)
109+
if (Array.isArray(existingCommits) && existingCommits.length > 0) {
110+
const entry = existingCommits[0]
111+
const commitBlockRaw =
112+
Array.isArray(entry) && entry.length > 1 ? entry[1] : undefined
113+
const commitBlock =
114+
typeof commitBlockRaw === "bigint"
115+
? Number(commitBlockRaw)
116+
: Number(commitBlockRaw ?? NaN)
117+
if (Number.isFinite(commitBlock)) {
118+
commitEpoch = Math.trunc(commitBlock / (100 + 1))
119+
return
120+
}
121+
}
122+
123+
await setStakeThreshold(api, alice, BigInt(0))
124+
const commitHash = getCommitHash(netuid, wallet.address)
125+
const tx = await contract.commitWeights(netuid, commitHash)
126+
await tx.wait()
127+
128+
const commitBlock = await api.query.System.Number.getValue()
129+
commitEpoch = Math.trunc(commitBlock / (100 + 1))
130+
}
131+
89132
it("EVM neuron commit weights via call precompile", async () => {
90133
let totalNetworks = await api.query.SubtensorModule.TotalNetworks.getValue()
91134
const subnetId = totalNetworks - 1
92135
const commitHash = getCommitHash(subnetId, wallet.address)
93136
const contract = new ethers.Contract(INEURON_ADDRESS, INeuronABI, wallet);
137+
138+
await setStakeThreshold(api, alice, BigInt(1))
139+
await assert.rejects(async () => {
140+
const tx = await contract.commitWeights(subnetId, commitHash)
141+
await tx.wait()
142+
})
143+
await setStakeThreshold(api, alice, BigInt(0))
144+
94145
try {
95146
const tx = await contract.commitWeights(subnetId, commitHash)
96147
await tx.wait()
@@ -120,6 +171,11 @@ describe("Test neuron precompile reveal weights", () => {
120171
// set interval epoch as 1, it is the minimum value now
121172
await setCommitRevealWeightsInterval(api, netuid, BigInt(1))
122173

174+
await ensureCommitEpoch(netuid, contract)
175+
if (commitEpoch === undefined) {
176+
throw new Error("commitEpoch should be set before revealing weights")
177+
}
178+
123179
while (true) {
124180
const currentBlock = await api.query.System.Number.getValue()
125181
const currentEpoch = Math.trunc(currentBlock / (100 + 1))
@@ -130,6 +186,19 @@ describe("Test neuron precompile reveal weights", () => {
130186
await new Promise(resolve => setTimeout(resolve, 1000))
131187
}
132188

189+
await setStakeThreshold(api, alice, BigInt(1))
190+
await assert.rejects(async () => {
191+
const tx = await contract.revealWeights(
192+
netuid,
193+
uids,
194+
values,
195+
salt,
196+
version_key
197+
);
198+
await tx.wait()
199+
})
200+
await setStakeThreshold(api, alice, BigInt(0))
201+
133202
const tx = await contract.revealWeights(
134203
netuid,
135204
uids,

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

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

3-
import { getDevnetApi, getRandomSubstrateKeypair } from "../src/substrate"
3+
import { getAliceSigner, getDevnetApi, getRandomSubstrateKeypair, waitForTransactionWithRetry } from "../src/substrate"
44
import { devnet } from "@polkadot-api/descriptors"
5-
import { TypedApi } from "polkadot-api";
6-
import { convertPublicKeyToSs58 } from "../src/address-utils"
5+
import { Binary, TypedApi, getTypedCodecs } from "polkadot-api";
6+
import { convertH160ToSS58, 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"
@@ -545,4 +545,37 @@ describe("Test the Subnet precompile contract", () => {
545545
assert.equal(valueFromContract, newValue)
546546
assert.equal(valueFromContract, onchainValue);
547547
})
548+
549+
it("Rejects subnet precompile calls when coldkey swap is scheduled (tx extension)", async () => {
550+
const totalNetwork = await api.query.SubtensorModule.TotalNetworks.getValue()
551+
const contract = new ethers.Contract(ISUBNET_ADDRESS, ISubnetABI, wallet);
552+
const netuid = totalNetwork - 1;
553+
554+
const coldkeySs58 = convertH160ToSS58(wallet.address)
555+
const newColdkeySs58 = convertPublicKeyToSs58(hotkey1.publicKey)
556+
const currentBlock = await api.query.System.Number.getValue()
557+
const executionBlock = currentBlock + 10
558+
559+
const codec = await getTypedCodecs(devnet);
560+
const valueBytes = codec.query.SubtensorModule.ColdkeySwapScheduled.value.enc([
561+
executionBlock,
562+
newColdkeySs58,
563+
])
564+
const key = await api.query.SubtensorModule.ColdkeySwapScheduled.getKey(coldkeySs58);
565+
566+
// Use sudo + set_storage since the swap-scheduled check only exists in the tx extension.
567+
const setStorageCall = api.tx.System.set_storage({
568+
items: [[Binary.fromHex(key), Binary.fromBytes(valueBytes)]],
569+
})
570+
const sudoTx = api.tx.Sudo.sudo({ call: setStorageCall.decodedCall })
571+
await waitForTransactionWithRetry(api, sudoTx, getAliceSigner())
572+
573+
const storedValue = await api.query.SubtensorModule.ColdkeySwapScheduled.getValue(coldkeySs58)
574+
assert.deepStrictEqual(storedValue, [executionBlock, newColdkeySs58])
575+
576+
await assert.rejects(async () => {
577+
const tx = await contract.setServingRateLimit(netuid, 100);
578+
await tx.wait();
579+
})
580+
})
548581
})

node/src/chain_spec/localnet.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,8 @@ fn localnet_genesis(
124124
"evmChainId": {
125125
"chainId": 42,
126126
},
127+
"subtensorModule": {
128+
"startCallDelay": 10,
129+
},
127130
})
128131
}

node/src/mev_shield/author.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,10 @@ impl Default for ShieldKeys {
9191
}
9292

9393
/// Shared context state.
94-
#[freeze_struct("62af7d26cf7c1271")]
94+
#[freeze_struct("245b565abca7d403")]
9595
#[derive(Clone)]
9696
pub struct ShieldContext {
9797
pub keys: Arc<Mutex<ShieldKeys>>,
98-
pub timing: TimeParams,
9998
}
10099

101100
/// Derive AEAD key directly from the 32‑byte ML‑KEM shared secret.
@@ -153,7 +152,6 @@ where
153152
{
154153
let ctx = ShieldContext {
155154
keys: Arc::new(Mutex::new(ShieldKeys::new())),
156-
timing: timing.clone(),
157155
};
158156

159157
let aura_keys: Vec<sp_core::sr25519::Public> = keystore.sr25519_public_keys(AURA_KEY_TYPE);

0 commit comments

Comments
 (0)