11import * as assert from "assert" ;
2- import { getAliceSigner , getDevnetApi , getRandomSubstrateKeypair } from "../src/substrate"
2+ import { getAliceSigner , getDevnetApi , getRandomSubstrateKeypair , waitForTransactionWithRetry } from "../src/substrate"
33import { devnet } from "@polkadot-api/descriptors"
44import { PolkadotSigner , TypedApi } from "polkadot-api" ;
55import { convertPublicKeyToSs58 , convertH160ToSS58 } from "../src/address-utils"
@@ -23,6 +23,16 @@ const values = [5];
2323const salt = [ 9 ] ;
2424const 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+
2636function 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 ,
0 commit comments