Skip to content

Commit 75cecaf

Browse files
bug fixing and improved rpc API overall
1 parent 0cb3cc6 commit 75cecaf

File tree

2 files changed

+145
-88
lines changed

2 files changed

+145
-88
lines changed

Phantasma.API/NexusAPI.cs

+112-73
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public NexusAPI(Nexus nexus, Mempool mempool = null)
109109
Nexus = nexus;
110110
Mempool = mempool;
111111

112-
var methodInfo = this.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance);
112+
var methodInfo = GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance);
113113

114114
foreach (var entry in methodInfo)
115115
{
@@ -145,23 +145,27 @@ private TransactionResult FillTransaction(Transaction tx)
145145
var block = Nexus.FindBlockForTransaction(tx);
146146
var chain = Nexus.FindChainForBlock(block.Hash);
147147

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+
};
155157

156158
var eventList = new List<EventResult>();
157159

158160
var evts = block.GetEventsForTransaction(tx.Hash);
159161
foreach (var evt in evts)
160162
{
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+
};
165169
eventList.Add(eventEntry);
166170
}
167171
result.Events = eventList.ToArray();
@@ -171,14 +175,14 @@ private TransactionResult FillTransaction(Transaction tx)
171175

172176
private BlockResult FillBlock(Block block, Chain chain)
173177
{
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+
};
182186

183187
var minerAddress = Nexus.FindValidatorForBlock(block);
184188
result.MinerAddress = minerAddress.Text;
@@ -212,7 +216,7 @@ private ChainResult FillChain(Chain chain, bool fillChildren)
212216
result.Address = chain.Address.Text;
213217
result.Height = chain.BlockHeight;
214218

215-
result.ParentAddress = (chain.ParentChain != null) ? chain.ParentChain.Name : null;
219+
result.ParentAddress = chain.ParentChain?.Name;
216220

217221
if (fillChildren)
218222
{
@@ -285,13 +289,15 @@ public IAPIResult GetBlockHeightFromChainAddress(string chainAddress)
285289
return GetBlockHeight(chain);
286290
}
287291

288-
return new ErrorResult() { error = "invalid address" };
292+
return new ErrorResult { error = "invalid address" };
289293
}
290294

291295
public IAPIResult GetBlockHeightFromChainName(string chainName)
292296
{
293297
var chain = Nexus.FindChainByName(chainName);
294-
if (chain == null) return null;
298+
299+
if (chain == null) return new ErrorResult { error = "invalid name" };
300+
295301
return GetBlockHeight(chain);
296302
}
297303

@@ -300,32 +306,32 @@ private IAPIResult GetBlockHeight(Chain chain)
300306
{
301307
if (chain != null)
302308
{
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 };
308310
}
311+
312+
return new ErrorResult { error = "chain not found" };
309313
}
310314

311315
[APIInfo(typeof(int), "Returns the number of transactions of given block hash or error if given hash is invalid or is not found.")]
312316
public IAPIResult GetBlockTransactionCountByHash(string blockHash)
313317
{
314318
if (Hash.TryParse(blockHash, out var hash))
315319
{
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+
}
323328
}
329+
330+
return new ErrorResult { error = "invalid block hash" };
324331
}
325332

326-
// TODO serialized should not be an option but a instead a separate getRawBlock
327333
[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)
329335
{
330336
if (Hash.TryParse(blockHash, out var hash))
331337
{
@@ -334,11 +340,24 @@ public IAPIResult GetBlockByHash(string blockHash, int serialized = 0)
334340
var block = chain.FindBlockByHash(hash);
335341
if (block != null)
336342
{
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+
}
341347

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+
{
342361
return new SingleResult() { value = block.ToByteArray().Encode() };
343362
}
344363
}
@@ -348,41 +367,61 @@ public IAPIResult GetBlockByHash(string blockHash, int serialized = 0)
348367
}
349368

350369
[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)
352371
{
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+
355383
if (chain == null)
356384
{
357-
return new ErrorResult() { error = "chain not found" };
385+
return new ErrorResult { error = "chain not found" };
358386
}
359387

360388
var block = chain.FindBlockByHeight(height);
389+
361390
if (block != null)
362391
{
363392
return FillBlock(block, chain);
364393
}
365394

366-
return new ErrorResult() { error = "block not found" };
395+
return new ErrorResult { error = "block not found" };
367396
}
368397

369398
[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)
371400
{
372-
var address = Address.FromText(chainAddress);
373-
var chain = Nexus.FindChainByAddress(address);
401+
var chain = Nexus.FindChainByName(chainInput);
402+
374403
if (chain == null)
375404
{
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" };
377415
}
378416

379417
var block = chain.FindBlockByHeight(height);
418+
380419
if (block != null)
381420
{
382-
return new SingleResult() { value = block.ToByteArray().Encode() };
421+
return new SingleResult { value = block.ToByteArray().Encode() };
383422
}
384423

385-
return new ErrorResult() { error = "block not found" };
424+
return new ErrorResult { error = "block not found" };
386425
}
387426

388427
[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)
391430
if (Hash.TryParse(blockHash, out var hash))
392431
{
393432
var block = Nexus.FindBlockByHash(hash);
433+
394434
if (block == null)
395435
{
396-
return new ErrorResult() { error = "unknown block hash" };
436+
return new ErrorResult { error = "unknown block hash" };
397437
}
398438

399-
var txHash = block.TransactionHashes.ElementAt(index);
439+
var txHash = block.TransactionHashes.ElementAtOrDefault(index);
440+
400441
if (txHash == null)
401442
{
402-
return new ErrorResult() { error = "unknown tx index" };
443+
return new ErrorResult { error = "unknown tx index" };
403444
}
404445

405446
return FillTransaction(Nexus.FindTransactionByHash(txHash));
406447
}
407448

408-
return new ErrorResult() { error = "invalid block hash" };
449+
return new ErrorResult { error = "invalid block hash" };
409450
}
410451

411452
[APIInfo(typeof(AccountTransactionsResult), "Returns last X transactions of given address.")]
412453
public IAPIResult GetAddressTransactions(string addressText, int amountTx)
413454
{
414455
if (amountTx < 1)
415456
{
416-
return new ErrorResult() { error = "invalid amount" };
457+
return new ErrorResult { error = "invalid amount" };
417458
}
418459

419460
var result = new AccountTransactionsResult();
@@ -423,16 +464,14 @@ public IAPIResult GetAddressTransactions(string addressText, int amountTx)
423464
var plugin = Nexus.GetPlugin<AddressTransactionsPlugin>();
424465

425466
result.Address = address.Text;
426-
result.Amount = (uint)amountTx;
467+
427468
var txs = plugin?.GetAddressTransactions(address).
428469
Select(hash => Nexus.FindTransactionByHash(hash)).
429470
OrderByDescending(tx => Nexus.FindBlockForTransaction(tx).Timestamp.Value).
430471
Take(amountTx);
431472

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();
436475

437476
return result;
438477
}
@@ -524,7 +563,7 @@ public IAPIResult SendRawTransaction(string txData)
524563
{
525564
if (Mempool == null)
526565
{
527-
return new ErrorResult() { error = "No mempool" };
566+
return new ErrorResult { error = "No mempool" };
528567
}
529568

530569
var bytes = Base16.Decode(txData);
@@ -533,26 +572,26 @@ public IAPIResult SendRawTransaction(string txData)
533572
bool submited = Mempool.Submit(tx);
534573
if (!submited)
535574
{
536-
return new ErrorResult() { error = "Mempool submission rejected" };
575+
return new ErrorResult { error = "Mempool submission rejected" };
537576
}
538577

539-
return new SingleResult() { value = tx.Hash.ToString() };
578+
return new SingleResult { value = tx.Hash.ToString() };
540579
}
541580

542581
[APIInfo(typeof(TransactionResult), "Returns information about a transaction by hash.")]
543582
public IAPIResult GetTransaction(string hashText)
544583
{
545-
Hash hash;
546-
547-
if (Hash.TryParse(hashText, out hash))
584+
if (Hash.TryParse(hashText, out var hash))
548585
{
549586
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+
}
555592
}
593+
594+
return new ErrorResult { error = "Invalid hash" };
556595
}
557596

558597
[APIInfo(typeof(ChainResult[]), "Returns an array of chains with useful information.")]

0 commit comments

Comments
 (0)