File tree Expand file tree Collapse file tree 2 files changed +35
-0
lines changed
Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -154,5 +154,28 @@ public void ThrowsRestApiClientException()
154154 Assert . Empty ( headers ) ;
155155 }
156156 }
157+
158+ [ Fact ]
159+ public async Task CanGetBlockHashByHeight ( )
160+ {
161+ using ( var builder = NodeBuilderEx . Create ( ) )
162+ {
163+ var node = builder . CreateNode ( ) ;
164+ var client = node . CreateRESTClient ( ) ;
165+ builder . StartAll ( ) ;
166+
167+ var genesisHash = await client . GetBlockHashByHeightAsync ( 0 ) ;
168+ Assert . Equal ( RegNetGenesisBlock . GetHash ( ) , genesisHash ) ;
169+
170+ var expectedHash = node . Generate ( 1 ) . FirstOrDefault ( ) ;
171+
172+ var actualHash = await client . GetBlockHashByHeightAsync ( 1 ) ;
173+ Assert . Equal ( expectedHash , actualHash ) ;
174+
175+ // Should throw exception for non-existent block height
176+ await Assert . ThrowsAsync < RestApiException > ( async ( ) =>
177+ await client . GetBlockHashByHeightAsync ( 2 ) ) ;
178+ }
179+ }
157180 }
158181}
Original file line number Diff line number Diff line change @@ -202,6 +202,18 @@ public async Task<UTxOutputs> GetUnspentOutputsAsync(IEnumerable<OutPoint> outPo
202202 return utxos ;
203203 }
204204
205+ /// <summary>
206+ /// Gets the block hash by height.
207+ /// </summary>
208+ /// <param name="height">The block height.</param>
209+ /// <returns>The block hash at the specified height.</returns>
210+ public async Task < uint256 > GetBlockHashByHeightAsync ( int height )
211+ {
212+ var result = await SendRequestAsync ( "blockhashbyheight" , RestResponseFormat . Json , height . ToString ( ) ) . ConfigureAwait ( false ) ;
213+ var o = JObject . Parse ( Encoding . UTF8 . GetString ( result , 0 , result . Length ) ) ;
214+ return uint256 . Parse ( ( string ) o [ "blockhash" ] ) ;
215+ }
216+
205217 public async Task < byte [ ] > SendRequestAsync ( string resource , RestResponseFormat format , params string [ ] parms )
206218 {
207219 var request = BuildHttpRequest ( resource , format , parms ) ;
You can’t perform that action at this time.
0 commit comments