Neptune-Crypto / neptune-core

anonymous peer-to-peer cash
Apache License 2.0
24 stars 7 forks source link

add get_confirmations() RPC and add to dashboard #56

Closed dan-da closed 10 months ago

dan-da commented 11 months ago

Addresses #6. (partial)

The major objective of this PR is adding the get_confirmations RPC. A secondary objective is displaying the confirmations field in overview screen of the Dashboard. Other changes are ancillary.

Server:

Dashboard:

Notes:

Testing:

I wanted to create a unit test for get_confirmations RPC however it is unclear to me how to do so. There are a few unit tests in rpc_server.rs, but afaict none of them setup a wallet with a non-zero balance which would be a requirement for my test(s). If someone could provide such a setup fn, I would happily make use of it.

Sword-Smith commented 11 months ago

I'm afraid Alan and I gave you some wrong information. The description in #6 says " The number of blocks (=confirmations) since the most recent balance update". If the last balance update is a outgoing transaction (UTXOs being spent), then any of the wallet's monitored UTXOs could have been spent, not just the last one. The monitored UTXOs are ordered by the blockheight at which they were received, and this ordering does not change. The get_balance_height does actually return the relevant information that get_latest_balance_height needs, as the returned list from get_balance_height can be ordered by BlockHeight and then the last element in that ordered list should contain the latest update. Sorry that we didn't catch that when you showed you suggestion in #6.

I would suggest:

  1. Fix logic to return BlockHeight of latest balance update -- not latest incoming transaction as this PR's code does.
  2. Write a test demonstrating correct behavior, or find an existing test that sets up the relevant states and add a call to get_latest_balance_height in that.
  3. Verify that the block in question belongs to the canonical chain. You can use block_belongs_to_canonical_chain in archival_state.rs for this. If it does not, then find the next-most recent balance update etc. This point can also be addressed later, as you suggest, if you prefer that

u64 for blockheight should be more than enough. But why not just use the existing BlockHeight struct that is a wrapped BFieldElement which is a data structure that goes up to almost u64::MAX ($2^{64} - 2^{32} + 1$ to be precise.)

dan-da commented 11 months ago

Fix logic to return BlockHeight of latest balance update -- not latest incoming transaction as this PR's code does.

Done. I think. Please review. ;-)

duh, whoops! nice catch on that. Now that you point it out, of course the list must be traversed to find latest spent utxo. I do wonder though if it could be possible to store the latest spent in the wallet to make this lookup o(1). Anyway I still tried to make the fn as efficient as possible given that it is called by the Dashboard overview. Maybe premature optimization, I dunno.

Write a test demonstrating correct behavior, or find an existing test that sets up the relevant states and add a call to get_latest_balance_height in that.

I didn't see such a test in wallet_state.rs. Perhaps I need to look elsewhere, or maybe you can point me to one. Anyway, I've run out of time tonight, so will look at that next coding session. We can keep the PR open.

Verify that the block in question belongs to the canonical chain

haven't looked into this yet. I will take a look and if it seems simple/clear and it works I will do it, else leave for a followup PR.

why not just use the existing BlockHeight struct

yes! That was my initial instinct, but I was a bit confused because the Dashboard was initially written to accept a u64 and BlockHeight::sub() returns i128. And I wasn't sure how pedantic we are being about BlockHeight vs say BlockCount. ie, a strict interpretation could say that a Height is only to be used in context of a count from block 0. Anyway, with your blessing I changed it to use BlockHeight everywhere in latest commit.

extra: I added an impl Display for Sign that just prints "-" if sign is negative. This helped me debug print a list of utxo's with amount and I figure it may be worth keeping, but happy to remove if you don't like it.

dan-da commented 11 months ago

thx! super helpful comments. I'm away today, but will continue on it over the weekend.

dan-da commented 11 months ago

I think this is ready for another review. See commit messages for summary of changes.