@@ -170,7 +170,9 @@ private void OnGetStateHeight()
170170 [ ConsoleCommand ( "get proof" , Category = "StateService" , Description = "Get proof of key and contract hash" ) ]
171171 private void OnGetProof ( UInt256 rootHash , UInt160 scriptHash , string key )
172172 {
173- if ( _system is null || _system . Settings . Network != StateServiceSettings . Default . Network ) throw new InvalidOperationException ( "Network doesn't match" ) ;
173+ if ( _system is null || _system . Settings . Network != StateServiceSettings . Default . Network )
174+ throw new InvalidOperationException ( "Network doesn't match" ) ;
175+
174176 try
175177 {
176178 ConsoleHelper . Info ( "Proof: " , GetProof ( rootHash , scriptHash , Convert . FromBase64String ( key ) ) ) ;
@@ -186,8 +188,7 @@ private void OnVerifyProof(UInt256 rootHash, string proof)
186188 {
187189 try
188190 {
189- ConsoleHelper . Info ( "Verify Result: " ,
190- VerifyProof ( rootHash , Convert . FromBase64String ( proof ) ) ) ;
191+ ConsoleHelper . Info ( "Verify Result: " , VerifyProof ( rootHash , Convert . FromBase64String ( proof ) ) ) ;
191192 }
192193 catch ( RpcException e )
193194 {
@@ -196,9 +197,8 @@ private void OnVerifyProof(UInt256 rootHash, string proof)
196197 }
197198
198199 [ RpcMethod ]
199- public JToken GetStateRoot ( JArray _params )
200+ public JToken GetStateRoot ( uint index )
200201 {
201- var index = Result . Ok_Or ( ( ) => uint . Parse ( _params [ 0 ] . AsString ( ) ) , RpcError . InvalidParams . WithData ( $ "Invalid state root index: { _params [ 0 ] } ") ) ;
202202 using var snapshot = StateStore . Singleton . GetSnapshot ( ) ;
203203 var stateRoot = snapshot . GetStateRoot ( index ) . NotNull_Or ( RpcError . UnknownStateRoot ) ;
204204 return stateRoot . ToJson ( ) ;
@@ -233,7 +233,7 @@ private string GetProof(Trie trie, StorageKey skey)
233233
234234 private string GetProof ( UInt256 rootHash , UInt160 scriptHash , byte [ ] key )
235235 {
236- ( ! StateServiceSettings . Default . FullState && StateStore . Singleton . CurrentLocalRootHash != rootHash ) . False_Or ( RpcError . UnsupportedState ) ;
236+ CheckRootHash ( rootHash ) ;
237237
238238 using var store = StateStore . Singleton . GetStoreSnapshot ( ) ;
239239 var trie = new Trie ( store , rootHash ) ;
@@ -242,12 +242,10 @@ private string GetProof(UInt256 rootHash, UInt160 scriptHash, byte[] key)
242242 }
243243
244244 [ RpcMethod ]
245- public JToken GetProof ( JArray _params )
245+ public JToken GetProof ( UInt256 rootHash , UInt160 scriptHash , string key )
246246 {
247- var rootHash = Result . Ok_Or ( ( ) => UInt256 . Parse ( _params [ 0 ] . AsString ( ) ) , RpcError . InvalidParams . WithData ( $ "Invalid root hash: { _params [ 0 ] } ") ) ;
248- var scriptHash = Result . Ok_Or ( ( ) => UInt160 . Parse ( _params [ 1 ] . AsString ( ) ) , RpcError . InvalidParams . WithData ( $ "Invalid script hash: { _params [ 1 ] } ") ) ;
249- var key = Result . Ok_Or ( ( ) => Convert . FromBase64String ( _params [ 2 ] . AsString ( ) ) , RpcError . InvalidParams . WithData ( $ "Invalid key: { _params [ 2 ] } ") ) ;
250- return GetProof ( rootHash , scriptHash , key ) ;
247+ var keyBytes = Result . Ok_Or ( ( ) => Convert . FromBase64String ( key ) , RpcError . InvalidParams . WithData ( $ "Invalid key: { key } ") ) ;
248+ return GetProof ( rootHash , scriptHash , keyBytes ) ;
251249 }
252250
253251 private string VerifyProof ( UInt256 rootHash , byte [ ] proof )
@@ -269,15 +267,15 @@ private string VerifyProof(UInt256 rootHash, byte[] proof)
269267 }
270268
271269 [ RpcMethod ]
272- public JToken VerifyProof ( JArray _params )
270+ public JToken VerifyProof ( UInt256 rootHash , string proof )
273271 {
274- var rootHash = Result . Ok_Or ( ( ) => UInt256 . Parse ( _params [ 0 ] . AsString ( ) ) , RpcError . InvalidParams . WithData ( $ "Invalid root hash: { _params [ 0 ] } " ) ) ;
275- var proofBytes = Result . Ok_Or ( ( ) => Convert . FromBase64String ( _params [ 1 ] . AsString ( ) ) , RpcError . InvalidParams . WithData ( $ "Invalid proof: { _params [ 1 ] } ") ) ;
272+ var proofBytes = Result . Ok_Or (
273+ ( ) => Convert . FromBase64String ( proof ) , RpcError . InvalidParams . WithData ( $ "Invalid proof: { proof } ") ) ;
276274 return VerifyProof ( rootHash , proofBytes ) ;
277275 }
278276
279277 [ RpcMethod ]
280- public JToken GetStateHeight ( JArray _params )
278+ public JToken GetStateHeight ( )
281279 {
282280 return new JObject ( )
283281 {
@@ -289,50 +287,39 @@ public JToken GetStateHeight(JArray _params)
289287 private ContractState GetHistoricalContractState ( Trie trie , UInt160 scriptHash )
290288 {
291289 const byte prefix = 8 ;
292- StorageKey skey = new KeyBuilder ( NativeContract . ContractManagement . Id , prefix ) . Add ( scriptHash ) ;
293- return trie . TryGetValue ( skey . ToArray ( ) , out var value ) ? value . AsSerializable < StorageItem > ( ) . GetInteroperable < ContractState > ( ) : null ;
290+ var skey = new KeyBuilder ( NativeContract . ContractManagement . Id , prefix ) . Add ( scriptHash ) ;
291+ return trie . TryGetValue ( skey . ToArray ( ) , out var value )
292+ ? value . AsSerializable < StorageItem > ( ) . GetInteroperable < ContractState > ( )
293+ : null ;
294294 }
295295
296296 private StorageKey ParseStorageKey ( byte [ ] data )
297297 {
298- return new ( )
299- {
300- Id = BinaryPrimitives . ReadInt32LittleEndian ( data ) ,
301- Key = data . AsMemory ( sizeof ( int ) ) ,
302- } ;
298+ return new ( ) { Id = BinaryPrimitives . ReadInt32LittleEndian ( data ) , Key = data . AsMemory ( sizeof ( int ) ) } ;
303299 }
304300
305- [ RpcMethod ]
306- public JToken FindStates ( JArray _params )
301+ private void CheckRootHash ( UInt256 rootHash )
307302 {
308- var rootHash = Result . Ok_Or ( ( ) => UInt256 . Parse ( _params [ 0 ] . AsString ( ) ) , RpcError . InvalidParams . WithData ( $ "Invalid root hash: { _params [ 0 ] } ") ) ;
309- ( ! StateServiceSettings . Default . FullState && StateStore . Singleton . CurrentLocalRootHash != rootHash ) . False_Or ( RpcError . UnsupportedState ) ;
303+ var fullState = StateServiceSettings . Default . FullState ;
304+ var current = StateStore . Singleton . CurrentLocalRootHash ;
305+ ( ! fullState && current != rootHash )
306+ . False_Or ( RpcError . UnsupportedState . WithData ( $ "fullState:{ fullState } ,current:{ current } ,rootHash:{ rootHash } ") ) ;
307+ }
310308
311- var scriptHash = Result . Ok_Or ( ( ) => UInt160 . Parse ( _params [ 1 ] . AsString ( ) ) , RpcError . InvalidParams . WithData ( $ "Invalid script hash: { _params [ 1 ] } ") ) ;
312- var prefix = Result . Ok_Or ( ( ) => Convert . FromBase64String ( _params [ 2 ] . AsString ( ) ) , RpcError . InvalidParams . WithData ( $ "Invalid prefix: { _params [ 2 ] } ") ) ;
313- var key = Array . Empty < byte > ( ) ;
314- if ( 3 < _params . Count )
315- key = Result . Ok_Or ( ( ) => Convert . FromBase64String ( _params [ 3 ] . AsString ( ) ) , RpcError . InvalidParams . WithData ( $ "Invalid key: { _params [ 3 ] } ") ) ;
309+ [ RpcMethod ]
310+ public JToken FindStates ( UInt256 rootHash , UInt160 scriptHash , byte [ ] prefix , byte [ ] key = null , int count = 0 )
311+ {
312+ CheckRootHash ( rootHash ) ;
316313
317- int count = StateServiceSettings . Default . MaxFindResultItems ;
318- if ( 4 < _params . Count )
319- count = Result . Ok_Or ( ( ) => int . Parse ( _params [ 4 ] . AsString ( ) ) , RpcError . InvalidParams . WithData ( $ "Invalid count: { _params [ 4 ] } ") ) ;
320- if ( StateServiceSettings . Default . MaxFindResultItems < count )
321- count = StateServiceSettings . Default . MaxFindResultItems ;
314+ key ??= [ ] ;
315+ count = count <= 0 ? StateServiceSettings . Default . MaxFindResultItems : count ;
316+ count = Math . Min ( count , StateServiceSettings . Default . MaxFindResultItems ) ;
322317
323318 using var store = StateStore . Singleton . GetStoreSnapshot ( ) ;
324319 var trie = new Trie ( store , rootHash ) ;
325320 var contract = GetHistoricalContractState ( trie , scriptHash ) . NotNull_Or ( RpcError . UnknownContract ) ;
326- var pkey = new StorageKey ( )
327- {
328- Id = contract . Id ,
329- Key = prefix ,
330- } ;
331- var fkey = new StorageKey ( )
332- {
333- Id = pkey . Id ,
334- Key = key ,
335- } ;
321+ var pkey = new StorageKey ( ) { Id = contract . Id , Key = prefix } ;
322+ var fkey = new StorageKey ( ) { Id = pkey . Id , Key = key } ;
336323
337324 var json = new JObject ( ) ;
338325 var jarr = new JArray ( ) ;
@@ -364,16 +351,12 @@ public JToken FindStates(JArray _params)
364351 }
365352
366353 [ RpcMethod ]
367- public JToken GetState ( JArray _params )
354+ public JToken GetState ( UInt256 rootHash , UInt160 scriptHash , byte [ ] key )
368355 {
369- var rootHash = Result . Ok_Or ( ( ) => UInt256 . Parse ( _params [ 0 ] . AsString ( ) ) , RpcError . InvalidParams . WithData ( $ "Invalid root hash: { _params [ 0 ] } ") ) ;
370- ( ! StateServiceSettings . Default . FullState && StateStore . Singleton . CurrentLocalRootHash != rootHash ) . False_Or ( RpcError . UnsupportedState ) ;
356+ CheckRootHash ( rootHash ) ;
371357
372- var scriptHash = Result . Ok_Or ( ( ) => UInt160 . Parse ( _params [ 1 ] . AsString ( ) ) , RpcError . InvalidParams . WithData ( $ "Invalid script hash: { _params [ 1 ] } ") ) ;
373- var key = Result . Ok_Or ( ( ) => Convert . FromBase64String ( _params [ 2 ] . AsString ( ) ) , RpcError . InvalidParams . WithData ( $ "Invalid key: { _params [ 2 ] } ") ) ;
374358 using var store = StateStore . Singleton . GetStoreSnapshot ( ) ;
375359 var trie = new Trie ( store , rootHash ) ;
376-
377360 var contract = GetHistoricalContractState ( trie , scriptHash ) . NotNull_Or ( RpcError . UnknownContract ) ;
378361 var skey = new StorageKey ( )
379362 {
0 commit comments