@@ -572,48 +572,46 @@ protected internal virtual JToken GetCandidates()
572572 {
573573 script = sb . EmitDynamicCall ( NativeContract . NEO . Hash , "getCandidates" ) . ToArray ( ) ;
574574 }
575- StackItem [ ] resultstack ;
575+
576+ StackItem [ ] resultStack ;
576577 try
577578 {
578579 using var engine = ApplicationEngine . Run ( script , snapshot , settings : system . Settings , gas : settings . MaxGasInvoke ) ;
579- resultstack = engine . ResultStack . ToArray ( ) ;
580+ resultStack = engine . ResultStack . ToArray ( ) ;
580581 }
581582 catch
582583 {
583584 throw new RpcException ( RpcError . InternalServerError . WithData ( "Can't get candidates." ) ) ;
584585 }
585586
586- JObject json = new ( ) ;
587+ // GetCandidates should return a 1-element array.
588+ // If the behavior is unexpected, throw an exception(rather than returning an empty result), and the UT will find it.
589+ if ( resultStack . Length != 1 ) // A empty array even if no candidate
590+ throw new RpcException ( RpcError . InternalServerError . WithData ( "Unexpected GetCandidates result." ) ) ;
591+
587592 try
588593 {
589- if ( resultstack . Length > 0 )
590- {
591- JArray jArray = new ( ) ;
592- var validators = NativeContract . NEO . GetNextBlockValidators ( snapshot , system . Settings . ValidatorsCount )
593- ?? throw new RpcException ( RpcError . InternalServerError . WithData ( "Can't get next block validators." ) ) ;
594+ var validators = NativeContract . NEO . GetNextBlockValidators ( snapshot , system . Settings . ValidatorsCount )
595+ ?? throw new RpcException ( RpcError . InternalServerError . WithData ( "Can't get next block validators." ) ) ;
594596
595- foreach ( var item in resultstack )
597+ var candidates = ( Array ) resultStack [ 0 ] ;
598+ var result = new JArray ( ) ;
599+ foreach ( Struct ele in candidates )
600+ {
601+ var publickey = ele [ 0 ] . GetSpan ( ) . ToHexString ( ) ;
602+ result . Add ( new JObject ( )
596603 {
597- var value = ( Array ) item ;
598- foreach ( Struct ele in value )
599- {
600- var publickey = ele [ 0 ] . GetSpan ( ) . ToHexString ( ) ;
601- json [ "publickey" ] = publickey ;
602- json [ "votes" ] = ele [ 1 ] . GetInteger ( ) . ToString ( ) ;
603- json [ "active" ] = validators . ToByteArray ( ) . ToHexString ( ) . Contains ( publickey ) ;
604- jArray . Add ( json ) ;
605- json = new ( ) ;
606- }
607- return jArray ;
608- }
604+ [ "publickey" ] = publickey ,
605+ [ "votes" ] = ele [ 1 ] . GetInteger ( ) . ToString ( ) ,
606+ [ "active" ] = validators . ToByteArray ( ) . ToHexString ( ) . Contains ( publickey ) ,
607+ } ) ;
609608 }
609+ return result ;
610610 }
611611 catch
612612 {
613- throw new RpcException ( RpcError . InternalServerError . WithData ( "Can't get next block validators " ) ) ;
613+ throw new RpcException ( RpcError . InternalServerError . WithData ( "Can't get candidates. " ) ) ;
614614 }
615-
616- return json ;
617615 }
618616
619617 /// <summary>
0 commit comments