@@ -12,22 +12,19 @@ import { contracts } from "../utils/contracts";
1212import { conetProvider } from "../utils/constants" ;
1313import { initProfileTokens , postToEndpoint } from "../utils/utils" ;
1414import async from "async" ;
15- import { getAllRegions } from "./regions" ;
1615
1716let allNodes : nodes_info [ ] = [ ] ;
1817let closestNodes : nodes_info [ ] = [ ] ;
1918let allRegions : string [ ] = [ ]
2019let cCNTPcurrentTotal = 0 ;
21- let miningAddress = "" ;
22- let miningConnection : any = null ;
23- let mining_epoch = 0 ;
20+
2421let epoch = 0 ;
2522let getAllNodesProcess = false ;
26- let getEntryNodesProcess = false ;
27- let miningProfile : profile | null = null ;
23+
24+ let entryNodes : nodes_info [ ] = [ ]
2825let currentScanNodeNumber = 0
2926let maxNodes = 0
30- let closestRegion : ClosestRegion [ ] = [ ]
27+ let testRegion : ClosestRegion [ ] = [ ]
3128const postToEndpointGetBody : (
3229 url : string ,
3330 post : boolean ,
@@ -74,7 +71,7 @@ const getRandomNodeFromRegion: (region: string) => nodes_info = (
7471} ;
7572
7673const testClosestRegion = async ( callback : ( ) => void ) => {
77- closestRegion = [ ] ;
74+ testRegion = [ ] ;
7875
7976 async . mapLimit ( allRegions , allRegions . length , async ( r : string , next ) => {
8077 const node = getRandomNodeFromRegion ( r ) ;
@@ -86,11 +83,11 @@ const testClosestRegion = async (callback: () => void) => {
8683 await postToEndpointGetBody ( url , false , null ) ;
8784 const endTime = new Date ( ) . getTime ( ) ;
8885 const delay = endTime - startTime ;
89- closestRegion . push ( { node, delay } ) ;
86+ testRegion . push ( { node, delay } ) ;
9087 } , err => {
9188 console . log ( `testClosestRegion success!` )
92- closestRegion . sort ( ( a , b ) => a . delay - b . delay ) ;
93- closestRegion . forEach ( n => {
89+ testRegion . sort ( ( a , b ) => a . delay - b . delay ) ;
90+ testRegion . forEach ( n => {
9491 closestNodes . push ( n . node )
9592 } )
9693 callback ( )
@@ -101,11 +98,11 @@ const testClosestRegion = async (callback: () => void) => {
10198
10299const getAllNodes = async (
103100 _allRegions : Region [ ] ,
104- setClosestRegion : ( region : ClosestRegion ) => void ,
101+ setClosestRegion : ( entryNodes : nodes_info [ ] ) => void ,
105102 callback :( allnodes : nodes_info [ ] ) => void
106103) => {
107104 if ( getAllNodesProcess ) {
108- setClosestRegion ( closestRegion [ 0 ] ) ;
105+ setClosestRegion ( entryNodes ) ;
109106 return ;
110107 }
111108 getAllNodesProcess = true ;
@@ -180,32 +177,21 @@ const getAllNodes = async (
180177 allRegions = Array . from ( country . keys ( ) )
181178 testClosestRegion ( ( ) => {
182179 maxNodes = currentScanNodeNumber ;
183- setClosestRegion ( closestRegion [ 0 ] ) ;
180+ const country = testRegion [ 0 ] . node . country
181+ const entryRegionNodes = allNodes . filter ( n => n . country === country )
182+ do {
183+ const index = Math . floor ( Math . random ( ) * entryRegionNodes . length )
184+ const node = entryRegionNodes [ index ]
185+ if ( node ?. ip_addr ) {
186+ entryNodes . push ( node )
187+ }
188+ } while ( entryNodes . length < 5 )
189+ setClosestRegion ( entryNodes ) ;
184190 callback ( allNodes ) ;
185191 } ) ;
186192
187193} ;
188194
189- const getRandomNodeV2 : ( index : number ) => null | nodes_info = ( index = - 1 ) => {
190- const totalNodes = closestNodes . length - 1 ;
191- if ( ! totalNodes ) {
192- return null ;
193- }
194-
195- const nodeNumber = Math . floor ( Math . random ( ) * totalNodes ) ;
196- if ( index > - 1 && nodeNumber === index ) {
197- console . log (
198- `getRandomNodeV2 nodeNumber ${ nodeNumber } == index ${ index } REUNING AGAIN!`
199- ) ;
200- return getRandomNodeV2 ( index ) ;
201- }
202-
203- const node = closestNodes [ nodeNumber ] ;
204- console . log (
205- `getRandomNodeV2 Guardian_Nodes length =${ closestNodes . length } nodeNumber = ${ nodeNumber } `
206- ) ;
207- return node ;
208- } ;
209195
210196const createGPGKey = async ( passwd : string , name : string , email : string ) => {
211197 const userId = {
@@ -235,23 +221,23 @@ const startMiningV2 = async (
235221 }
236222
237223 startMiningV2Process = true
238- miningAddress = profile . keyID . toLowerCase ( ) ;
239224
240225
241- if ( ! closestRegion . length ) {
242- console . log ( "totalNodes is empty" ) ;
226+ if ( ! entryNodes . length ) {
227+ console . log ( "entryNodes is empty" ) ;
243228 startMiningV2Process = false
244229 return ;
245230 }
246- const connectNode = closestRegion [ 0 ] . node
231+ const connectNode = entryNodes [ 0 ]
247232
248233
249234 if ( ! connectNode ) {
250235 startMiningV2Process = false
251236 console . log ( "connectNode is empty" ) ;
252237 return ;
253238 }
254- const entryRegion = connectNode . country
239+
240+
255241 if ( ! profile ?. pgpKey ) {
256242 const key = await createGPGKey ( "" , "" , "" ) ;
257243 profile . pgpKey = {
@@ -274,7 +260,7 @@ const startMiningV2 = async (
274260
275261 const url = `https://${ connectNode . domain } /post` ;
276262
277- miningConnection = postToEndpointSSE (
263+ postToEndpointSSE (
278264 url ,
279265 true ,
280266 { data : postData ?. requestData ?. [ 0 ] } ,
@@ -299,10 +285,9 @@ const startMiningV2 = async (
299285
300286 console . log ( "_startMiningV2 success" , _data ) ;
301287 const response : nodeResponse = JSON . parse ( _data ) ;
302- mining_epoch = epoch ;
288+
303289
304290 if ( first ) {
305- miningProfile = profile ;
306291 first = false ;
307292
308293 cCNTPcurrentTotal = parseFloat ( "0" ) ;
@@ -326,7 +311,8 @@ const startMiningV2 = async (
326311 name : "cCNTP" ,
327312 } ;
328313 }
329- const entryNode = getRandomNodeFromRegion ( entryRegion ) ;
314+ const index = Math . floor ( Math . random ( ) * entryNodes . length - 1 )
315+ const entryNode = entryNodes [ index ] ;
330316 const kk = parseFloat ( response . rate ) ;
331317 response . rate = isNaN ( kk ) ? "" : kk . toFixed ( 8 ) ;
332318 response . currentCCNTP = (
0 commit comments