494 Commits

Author SHA1 Message Date
jholdstock
ee4a440534 multi: Move vspd to internal package. 2023-09-16 08:29:17 +01:00
jholdstock
5bce5e6cfc vspd: Trigger database backups in main run func. 2023-09-16 08:29:17 +01:00
jholdstock
6c245b8418 vspd: Separate vspd and webapi.
The vspd Run func is now blocking and returns when its provided context
is cancelled. This means it is invoked/canceled in the same way as the
webapi Run func.

webapi is now created in the main run func rather than within vspd,
further decoupling these components.
2023-09-16 08:29:17 +01:00
jholdstock
ba3c7bf6d9 vspd: Create essential resources in main run func.
Creating the essential DB/RPC resources which are required for both
webapi and vspd in the main func allows the two components to be
decoupled.
2023-09-16 08:29:17 +01:00
jholdstock
61b6460014 webapi: Use existing context for server shutdown.
The server will shutdown cleanly even if the passed context is already
canceled, so there is no need to pass a new context and blindly guess at
how long the server may need to stop.
2023-09-16 07:54:24 +01:00
jholdstock
f881179024 webapi: Make Run func blocking.
This updates the Run func to make it blocking. When the provided context
is canceled the server is cleanly shut down and the func returns.
2023-09-16 07:54:24 +01:00
jholdstock
0742e0ff1a webapi: Split Start func into New and Run funcs.
Separating the single func grants the calling code greater control over
the webapi lifecycle.
2023-09-16 07:54:24 +01:00
jholdstock
a7bb0cd9d7 webapi: Rename server to WebAPI.
Exporting this struct is a step towards breaking up the Start func into
a New func and Run func, enabling calling code to use:

    api := webapi.New()
    api.Run()

WebAPI is a more suitable name than server because it matches the
package name, and also it helps to distinguish WebAPI from the HTTP
server it uses internally (ie. webapi.server rather than server.server).
2023-09-16 07:54:24 +01:00
jholdstock
8a8cbe47b9 webapi: Create server when all components ready.
It's generally good practice to first create everything that can fail
and then create the final instance at once with the results versus doing
it piecemeal.

Piecemeal creation is typically more error prone and, while not a huge
concern here, it also ends up needlessly creating objects that are just
thrown away in the event of a later error.
2023-09-16 07:54:24 +01:00
jholdstock
a9f517f787 multi: Don't use RPC to determine DCP0010 status.
The activation heights are known for mainnet and testnet, so they can be
hard-coded and RPC does not need to be used.
2023-09-14 20:45:24 +01:00
jholdstock
280dc391be webapi: Keep DB and RPC clients inside cache.
Storing references to the database/dcrd/dcrwallet clients inside the
cache struct means they don't need to be passed in every time the cache
is updated.
2023-09-14 20:43:09 +01:00
jholdstock
e26c8db199 vspd: Copy VSP fee validation func from wallet.
This is such a trivial func that there is no need to import it from
wallet, it can just be copied into this project.
2023-09-14 20:42:22 +01:00
jholdstock
d1766c362e webapi: Add Listen to webapi Config.
No need for this value to be passed in separately, it can be passed in
the same as every other config value.
2023-09-14 20:42:12 +01:00
jholdstock
a33bcbcbfd multi: Reuse CurrentVoteVersion helper.
Moving CurrentVoteVersion to the config package allows it to be reused
in vote-validator.
2023-09-14 20:39:49 +01:00
jholdstock
d5d1c3239c multi: Move netparams to internal.
A new internal package named config contains all of the hard-coded, network specific values used by vspd.
2023-09-14 20:39:49 +01:00
jholdstock
17fe1d7db1 vspd: Americanize "cancelled".
Consistent with the go stdlib (e.g. context.Canceled).
2023-09-13 09:10:10 +01:00
jholdstock
8038a0ea46 vspd: Move start/stop logging to main func.
This ensures that the startup/shutdown messages are always the
first/last thing to be logged.
2023-09-13 09:10:10 +01:00
jholdstock
e38032ba2f vspd: Run deferred tasks on os.Exit.
Any tasks deferred in the main func will not execute if os.Exit is
called. Moving application logic down into a new func which is called by
main works around this limitation.
2023-09-13 09:10:10 +01:00
jholdstock
3967d9e24e webapi: Don't export funcs/vars unnecessarily. 2023-09-13 09:03:05 +01:00
jholdstock
b5ffecd280 version: Don't export funcs/vars unnecessarily. 2023-09-13 09:03:05 +01:00
jholdstock
3d4fb6ab99 version: Move package to internal. 2023-09-13 09:03:05 +01:00
jholdstock
7ad309c9aa webapi: Move package to internal. 2023-09-13 09:03:05 +01:00
jholdstock
841c8ba115 vspd: Enable checkWalletConsistency interrupting.
Add a Context parameter to checkWalletConsistency so it can be canceled
when a shutdown is requested.
2023-09-08 09:59:00 +01:00
jholdstock
97e7b01afe vspd: Enable blockConnected interrupting.
Check the status of the Context in blockConnected so it can be canceled
when a shutdown is requested.
2023-09-08 09:59:00 +01:00
jholdstock
ad7c587699 vspd: Enable findSpentTickets interrupting.
Add a Context parameter to findSpentTickets so it can be canceled when a
shutdown is requested.
2023-09-08 09:59:00 +01:00
jholdstock
b0f79e56f5 vspd: Exit if shutdown requested during startup.
vspd runs some tasks on startup - database consistency checks, catching
up with missed blocks, etc. If a shutdown is requested while these tasks
are running, vspd should stop immediately rather than continuing to
start the web API server and other background tasks.
2023-09-08 09:59:00 +01:00
jholdstock
b5909817e4 multi: Rename shutdownCtx to ctx.
The more verbose name was helpful in the past when some code was
handling more than one context, however that is no longer the case due
to refactoring so reverting to the more concise name makes sense.
2023-09-08 09:59:00 +01:00
jholdstock
8df00752c0 vspd: Consolidate background task timers.
Using a single select loop for background tasks removes a lot of
duplicated boilerplate code and helps to simplify shutdown logic. This
does reduce the amount of things which can run in parallel, but that
isn't of concern for vspd. The web server still runs in its own
goroutine so its responsiveness won't be affected.
2023-09-08 09:59:00 +01:00
jholdstock
cfc34a3adc vspd: Extract const time period for dcrd check. 2023-09-08 09:59:00 +01:00
jholdstock
030a4c6874 webapi: Display testnet/simnet in homepage banner.
Don't hardcode the banner to testnet because it is also displayed for
simnet.
2023-09-06 17:14:53 +01:00
jholdstock
939f094104 vspd: Stop scanning when ticket spender found.
Once the spender of a given ticket is found, the rest of the blocks
don't need to be checked.
2023-09-05 15:55:32 +01:00
jholdstock
2af3d4eca7 vspd: Fix OOB panic in spent ticket scanner.
Range expressions are only evaluated once before beginning a for loop,
which makes them unsuitable for loops which manually alter the index.
2023-09-05 15:55:32 +01:00
jholdstock
7d1b7deb01 multi: Remove GetBlockHeaderVerbose.
A slight performance improvement becase vspd can do everything it needs
without the verbose block header.
2023-09-04 16:51:27 +01:00
jholdstock
fb47e65ef3 webapi: Use GetBlockCount not GetBestBlockHeader.
GetBestBlockHeader is a more demanding RPC so using GetBlockCount offers
a slight performance improvement.
2023-09-04 16:51:27 +01:00
JoeGruff
61911a3f3b config: Add back simnet. 2023-09-04 16:51:17 +01:00
jholdstock
96b99f35d7 webapi: Add expired/missed to /vspinfo. 2023-09-04 16:51:09 +01:00
jholdstock
54e243526e multi: Display expired/missed tickets on webpage.
Revoked ticket count is replaced by separate counts for expired/missed
tickets.

/vspinfo API response remains unchanged.
2023-09-04 16:47:00 +01:00
jholdstock
fc1f7f2955 webapi: Fix revoked % calculation. 2023-09-04 16:47:00 +01:00
jholdstock
1dbcb785df webapi: Display testnet as a banner.
A banner notice is now displayed when a VSP is running on testnet
rather than including it in the row of statistics.
2023-09-04 16:47:00 +01:00
Jamie Holdstock
50c343e177
vspd: Use filter.MatchAny instead of filter.Match.
This brings a notable performance improvement to spent ticket scanning
without changing functionality. Can cut scanning time in half.
2023-09-04 16:46:24 +01:00
jholdstock
0c5016ae62 types: Add expired/missed to /vspinfo response.
Adding fields for expired/missed to /vspinfo response, deprecate
revoked.

This will need a minor version bump in the types module.
2023-08-31 09:18:03 +01:00
jholdstock
a52034cda7 multi: Update revoked tickets to expired/missed.
Any tickets in the database which are currently revoked should be
updated to either expired or missed.

This is achieved with a heuristic based on the expiry height and
revoke height of the tickets. It is not guaranteed to be 100% correct
but should be pretty close.

The web api is not yet updated to reflect this change, missed/expired
tickets will continue to be counted as revoked.
2023-08-29 10:12:30 +01:00
jholdstock
2fc60321e0 database: Return explicit nil instead of err.
No functional change because err is already checked and known to be nil,
but explicitly returning nil is easier/quicker to interpret when
reading.
2023-08-26 09:33:40 +01:00
jholdstock
9be203c923 multi: Find voted/revoked tickets with GCS filters
Use dcrd and GCS filters to find voted/revoked tickets rather than using
the dcrwallet TicketInfo RPC.

Using TicketInfo was a bit flakey because wallets do not always
correctly detect votes/revokes, and as a result VSP admins may notice
that with this change vspd detects some historic voted/revoked tickets
which TicketInfo never detected.
2023-08-26 09:33:30 +01:00
jholdstock
618cfc7cf1 vspd: Remove simnet params.
Simnet params were added in the very first commit of vspd purely because
of copy/pasting from another project.

vspd has never actually been tested on simnet, so its possible
(probable) that it won't even operate correctly without additional dev
work. If we want simnet to be a properly supported network it can be
re-added and tested in the future.
2023-08-26 09:33:30 +01:00
jholdstock
fe8e63a295 build: Use latest golangci-linter. 2023-08-24 07:52:40 +01:00
jholdstock
a4d21d7838 build: Add extra linters.
Some of these cover points which have recently been highlighted in
reviews, such as contexts contained in structs, slices initialized with
zero length, and periods missing from the end of comments.
2023-08-24 07:52:40 +01:00
jholdstock
5f81b0b40e database: Ensure comments end with period. 2023-08-24 07:52:40 +01:00
jholdstock
c052365be8 client: Use %w to wrap error correctly. 2023-08-24 07:52:40 +01:00
jholdstock
e80ebfea13 rpc: Do not store contexts in structs.
Documentation for the context package suggests that Contexts should
never be stored in structs.
2023-08-24 07:52:40 +01:00