@@ -109,7 +109,7 @@ public NexusAPI(Nexus nexus, Mempool mempool = null)
109
109
Nexus = nexus ;
110
110
Mempool = mempool ;
111
111
112
- var methodInfo = this . GetType ( ) . GetMethods ( BindingFlags . Public | BindingFlags . Instance ) ;
112
+ var methodInfo = GetType ( ) . GetMethods ( BindingFlags . Public | BindingFlags . Instance ) ;
113
113
114
114
foreach ( var entry in methodInfo )
115
115
{
@@ -145,23 +145,27 @@ private TransactionResult FillTransaction(Transaction tx)
145
145
var block = Nexus . FindBlockForTransaction ( tx ) ;
146
146
var chain = Nexus . FindChainForBlock ( block . Hash ) ;
147
147
148
- var result = new TransactionResult ( ) ;
149
- result . Txid = tx . Hash . ToString ( ) ;
150
- result . ChainAddress = chain . Address . Text ;
151
- result . ChainName = chain . Name ;
152
- result . Timestamp = block . Timestamp . Value ;
153
- result . BlockHeight = block . Height ;
154
- result . Script = tx . Script . Encode ( ) ;
148
+ var result = new TransactionResult
149
+ {
150
+ Txid = tx . Hash . ToString ( ) ,
151
+ ChainAddress = chain . Address . Text ,
152
+ ChainName = chain . Name ,
153
+ Timestamp = block . Timestamp . Value ,
154
+ BlockHeight = block . Height ,
155
+ Script = tx . Script . Encode ( )
156
+ } ;
155
157
156
158
var eventList = new List < EventResult > ( ) ;
157
159
158
160
var evts = block . GetEventsForTransaction ( tx . Hash ) ;
159
161
foreach ( var evt in evts )
160
162
{
161
- var eventEntry = new EventResult ( ) ;
162
- eventEntry . Address = evt . Address . Text ;
163
- eventEntry . Data = evt . Data . Encode ( ) ;
164
- eventEntry . Kind = evt . Kind . ToString ( ) ;
163
+ var eventEntry = new EventResult
164
+ {
165
+ Address = evt . Address . Text ,
166
+ Data = evt . Data . Encode ( ) ,
167
+ Kind = evt . Kind . ToString ( )
168
+ } ;
165
169
eventList . Add ( eventEntry ) ;
166
170
}
167
171
result . Events = eventList . ToArray ( ) ;
@@ -171,14 +175,14 @@ private TransactionResult FillTransaction(Transaction tx)
171
175
172
176
private BlockResult FillBlock ( Block block , Chain chain )
173
177
{
174
- // var chain = Nexus.FindChainForBlock(block.Hash);
175
- var result = new BlockResult ( ) ;
176
-
177
- result . Hash = block . Hash . ToString ( ) ;
178
- result . PreviousHash = block . PreviousHash . ToString ( ) ;
179
- result . Timestamp = block . Timestamp . Value ;
180
- result . Height = block . Height ;
181
- result . ChainAddress = block . ChainAddress . ToString ( ) ;
178
+ var result = new BlockResult
179
+ {
180
+ Hash = block . Hash . ToString ( ) ,
181
+ PreviousHash = block . PreviousHash . ToString ( ) ,
182
+ Timestamp = block . Timestamp . Value ,
183
+ Height = block . Height ,
184
+ ChainAddress = block . ChainAddress . ToString ( )
185
+ } ;
182
186
183
187
var minerAddress = Nexus . FindValidatorForBlock ( block ) ;
184
188
result . MinerAddress = minerAddress . Text ;
@@ -212,7 +216,7 @@ private ChainResult FillChain(Chain chain, bool fillChildren)
212
216
result . Address = chain . Address . Text ;
213
217
result . Height = chain . BlockHeight ;
214
218
215
- result . ParentAddress = ( chain . ParentChain != null ) ? chain . ParentChain . Name : null ;
219
+ result . ParentAddress = chain . ParentChain ? . Name ;
216
220
217
221
if ( fillChildren )
218
222
{
@@ -285,13 +289,15 @@ public IAPIResult GetBlockHeightFromChainAddress(string chainAddress)
285
289
return GetBlockHeight ( chain ) ;
286
290
}
287
291
288
- return new ErrorResult ( ) { error = "invalid address" } ;
292
+ return new ErrorResult { error = "invalid address" } ;
289
293
}
290
294
291
295
public IAPIResult GetBlockHeightFromChainName ( string chainName )
292
296
{
293
297
var chain = Nexus . FindChainByName ( chainName ) ;
294
- if ( chain == null ) return null ;
298
+
299
+ if ( chain == null ) return new ErrorResult { error = "invalid name" } ;
300
+
295
301
return GetBlockHeight ( chain ) ;
296
302
}
297
303
@@ -300,32 +306,32 @@ private IAPIResult GetBlockHeight(Chain chain)
300
306
{
301
307
if ( chain != null )
302
308
{
303
- return new SingleResult ( ) { value = chain . BlockHeight } ;
304
- }
305
- else
306
- {
307
- return new ErrorResult ( ) { error = "chain not found" } ;
309
+ return new SingleResult { value = chain . BlockHeight } ;
308
310
}
311
+
312
+ return new ErrorResult { error = "chain not found" } ;
309
313
}
310
314
311
315
[ APIInfo ( typeof ( int ) , "Returns the number of transactions of given block hash or error if given hash is invalid or is not found." ) ]
312
316
public IAPIResult GetBlockTransactionCountByHash ( string blockHash )
313
317
{
314
318
if ( Hash . TryParse ( blockHash , out var hash ) )
315
319
{
316
- var temp = Nexus . FindBlockByHash ( hash ) ? . TransactionHashes . Count ( ) ;
317
- int count = ( temp != null ) ? temp . Value : 0 ;
318
- return new SingleResult ( ) { value = count } ;
319
- }
320
- else
321
- {
322
- return new ErrorResult ( ) { error = "invalid block hash" } ;
320
+ var block = Nexus . FindBlockByHash ( hash ) ;
321
+
322
+ if ( block != null )
323
+ {
324
+ int count = block . TransactionHashes . Count ( ) ;
325
+
326
+ return new SingleResult { value = count } ;
327
+ }
323
328
}
329
+
330
+ return new ErrorResult { error = "invalid block hash" } ;
324
331
}
325
332
326
- // TODO serialized should not be an option but a instead a separate getRawBlock
327
333
[ APIInfo ( typeof ( BlockResult ) , "Returns information about a block by hash." ) ]
328
- public IAPIResult GetBlockByHash ( string blockHash , int serialized = 0 )
334
+ public IAPIResult GetBlockByHash ( string blockHash )
329
335
{
330
336
if ( Hash . TryParse ( blockHash , out var hash ) )
331
337
{
@@ -334,11 +340,24 @@ public IAPIResult GetBlockByHash(string blockHash, int serialized = 0)
334
340
var block = chain . FindBlockByHash ( hash ) ;
335
341
if ( block != null )
336
342
{
337
- if ( serialized == 0 ) // TODO why is this not a bool?
338
- {
339
- return FillBlock ( block , chain ) ;
340
- }
343
+ return FillBlock ( block , chain ) ;
344
+ }
345
+ }
346
+ }
341
347
348
+ return new ErrorResult ( ) { error = "invalid block hash" } ;
349
+ }
350
+
351
+ [ APIInfo ( typeof ( BlockResult ) , "Returns information about a block (encoded) by hash." ) ]
352
+ public IAPIResult GetRawBlockByHash ( string blockHash )
353
+ {
354
+ if ( Hash . TryParse ( blockHash , out var hash ) )
355
+ {
356
+ foreach ( var chain in Nexus . Chains )
357
+ {
358
+ var block = chain . FindBlockByHash ( hash ) ;
359
+ if ( block != null )
360
+ {
342
361
return new SingleResult ( ) { value = block . ToByteArray ( ) . Encode ( ) } ;
343
362
}
344
363
}
@@ -348,41 +367,61 @@ public IAPIResult GetBlockByHash(string blockHash, int serialized = 0)
348
367
}
349
368
350
369
[ APIInfo ( typeof ( BlockResult ) , "Returns information about a block by height and chain." ) ]
351
- public IAPIResult GetBlockByHeight ( string chainAddress , uint height )
370
+ public IAPIResult GetBlockByHeight ( string chainInput , uint height )
352
371
{
353
- var address = Address . FromText ( chainAddress ) ;
354
- var chain = Nexus . FindChainByAddress ( address ) ;
372
+ var chain = Nexus . FindChainByName ( chainInput ) ;
373
+
374
+ if ( chain == null )
375
+ {
376
+ if ( ! Address . IsValidAddress ( chainInput ) )
377
+ {
378
+ return new ErrorResult { error = "chain not found" } ;
379
+ }
380
+ chain = Nexus . FindChainByAddress ( Address . FromText ( chainInput ) ) ;
381
+ }
382
+
355
383
if ( chain == null )
356
384
{
357
- return new ErrorResult ( ) { error = "chain not found" } ;
385
+ return new ErrorResult { error = "chain not found" } ;
358
386
}
359
387
360
388
var block = chain . FindBlockByHeight ( height ) ;
389
+
361
390
if ( block != null )
362
391
{
363
392
return FillBlock ( block , chain ) ;
364
393
}
365
394
366
- return new ErrorResult ( ) { error = "block not found" } ;
395
+ return new ErrorResult { error = "block not found" } ;
367
396
}
368
397
369
398
[ APIInfo ( typeof ( BlockResult ) , "Returns information about a block by height and chain." ) ]
370
- public IAPIResult GetRawBlockByHeight ( string chainAddress , uint height )
399
+ public IAPIResult GetRawBlockByHeight ( string chainInput , uint height )
371
400
{
372
- var address = Address . FromText ( chainAddress ) ;
373
- var chain = Nexus . FindChainByAddress ( address ) ;
401
+ var chain = Nexus . FindChainByName ( chainInput ) ;
402
+
374
403
if ( chain == null )
375
404
{
376
- return new ErrorResult ( ) { error = "chain not found" } ;
405
+ if ( ! Address . IsValidAddress ( chainInput ) )
406
+ {
407
+ return new ErrorResult { error = "chain not found" } ;
408
+ }
409
+ chain = Nexus . FindChainByAddress ( Address . FromText ( chainInput ) ) ;
410
+ }
411
+
412
+ if ( chain == null )
413
+ {
414
+ return new ErrorResult { error = "chain not found" } ;
377
415
}
378
416
379
417
var block = chain . FindBlockByHeight ( height ) ;
418
+
380
419
if ( block != null )
381
420
{
382
- return new SingleResult ( ) { value = block . ToByteArray ( ) . Encode ( ) } ;
421
+ return new SingleResult { value = block . ToByteArray ( ) . Encode ( ) } ;
383
422
}
384
423
385
- return new ErrorResult ( ) { error = "block not found" } ;
424
+ return new ErrorResult { error = "block not found" } ;
386
425
}
387
426
388
427
[ APIInfo ( typeof ( TransactionResult ) , "Returns the information about a transaction requested by a block hash and transaction index." ) ]
@@ -391,29 +430,31 @@ public IAPIResult GetTransactionByBlockHashAndIndex(string blockHash, int index)
391
430
if ( Hash . TryParse ( blockHash , out var hash ) )
392
431
{
393
432
var block = Nexus . FindBlockByHash ( hash ) ;
433
+
394
434
if ( block == null )
395
435
{
396
- return new ErrorResult ( ) { error = "unknown block hash" } ;
436
+ return new ErrorResult { error = "unknown block hash" } ;
397
437
}
398
438
399
- var txHash = block . TransactionHashes . ElementAt ( index ) ;
439
+ var txHash = block . TransactionHashes . ElementAtOrDefault ( index ) ;
440
+
400
441
if ( txHash == null )
401
442
{
402
- return new ErrorResult ( ) { error = "unknown tx index" } ;
443
+ return new ErrorResult { error = "unknown tx index" } ;
403
444
}
404
445
405
446
return FillTransaction ( Nexus . FindTransactionByHash ( txHash ) ) ;
406
447
}
407
448
408
- return new ErrorResult ( ) { error = "invalid block hash" } ;
449
+ return new ErrorResult { error = "invalid block hash" } ;
409
450
}
410
451
411
452
[ APIInfo ( typeof ( AccountTransactionsResult ) , "Returns last X transactions of given address." ) ]
412
453
public IAPIResult GetAddressTransactions ( string addressText , int amountTx )
413
454
{
414
455
if ( amountTx < 1 )
415
456
{
416
- return new ErrorResult ( ) { error = "invalid amount" } ;
457
+ return new ErrorResult { error = "invalid amount" } ;
417
458
}
418
459
419
460
var result = new AccountTransactionsResult ( ) ;
@@ -423,16 +464,14 @@ public IAPIResult GetAddressTransactions(string addressText, int amountTx)
423
464
var plugin = Nexus . GetPlugin < AddressTransactionsPlugin > ( ) ;
424
465
425
466
result . Address = address . Text ;
426
- result . Amount = ( uint ) amountTx ;
467
+
427
468
var txs = plugin ? . GetAddressTransactions ( address ) .
428
469
Select ( hash => Nexus . FindTransactionByHash ( hash ) ) .
429
470
OrderByDescending ( tx => Nexus . FindBlockForTransaction ( tx ) . Timestamp . Value ) .
430
471
Take ( amountTx ) ;
431
472
432
- if ( txs != null )
433
- {
434
- result . Txs = txs . Select ( tx => FillTransaction ( tx ) ) . ToArray ( ) ;
435
- }
473
+ result . Amount = ( uint ) txs . Count ( ) ;
474
+ result . Txs = txs . Select ( FillTransaction ) . ToArray ( ) ;
436
475
437
476
return result ;
438
477
}
@@ -524,7 +563,7 @@ public IAPIResult SendRawTransaction(string txData)
524
563
{
525
564
if ( Mempool == null )
526
565
{
527
- return new ErrorResult ( ) { error = "No mempool" } ;
566
+ return new ErrorResult { error = "No mempool" } ;
528
567
}
529
568
530
569
var bytes = Base16 . Decode ( txData ) ;
@@ -533,26 +572,26 @@ public IAPIResult SendRawTransaction(string txData)
533
572
bool submited = Mempool . Submit ( tx ) ;
534
573
if ( ! submited )
535
574
{
536
- return new ErrorResult ( ) { error = "Mempool submission rejected" } ;
575
+ return new ErrorResult { error = "Mempool submission rejected" } ;
537
576
}
538
577
539
- return new SingleResult ( ) { value = tx . Hash . ToString ( ) } ;
578
+ return new SingleResult { value = tx . Hash . ToString ( ) } ;
540
579
}
541
580
542
581
[ APIInfo ( typeof ( TransactionResult ) , "Returns information about a transaction by hash." ) ]
543
582
public IAPIResult GetTransaction ( string hashText )
544
583
{
545
- Hash hash ;
546
-
547
- if ( Hash . TryParse ( hashText , out hash ) )
584
+ if ( Hash . TryParse ( hashText , out var hash ) )
548
585
{
549
586
var tx = Nexus . FindTransactionByHash ( hash ) ;
550
- return FillTransaction ( tx ) ;
551
- }
552
- else
553
- {
554
- return new ErrorResult ( ) { error = "Invalid hash" } ;
587
+
588
+ if ( tx != null )
589
+ {
590
+ return FillTransaction ( tx ) ;
591
+ }
555
592
}
593
+
594
+ return new ErrorResult { error = "Invalid hash" } ;
556
595
}
557
596
558
597
[ APIInfo ( typeof ( ChainResult [ ] ) , "Returns an array of chains with useful information." ) ]
0 commit comments