Commit graph

909 commits

Author SHA1 Message Date
Jason Rhinelander 23cecefe6f Merge remote-tracking branch 'origin/master' into master-to-dev 2020-09-21 19:09:14 -03:00
Jason Rhinelander f765c67f6e Tweak how get_service_nodes works for fields/all
This changes `get_service_nodes` fields behaviour to make the fields.all
field a bit more intuitive by defaulting it to true if `fields` is
entirely omitted (as before), but now defaulting to false if `fields`
is explicitly given but `fields.all` is omitted.

This means a request such as `{"fields":{"service_node_pubkey":true}}`
now returns just pubkeys, while previously it returned everything (which
was surprising behaviour).  To make it return just pubkeys you'd have to
request: `{"fields":{"all":false,"service_node_pubkey":true}}`.

`all` is still there so that you can explicitly request everything if
desired.
2020-08-18 12:22:35 -03:00
Jason Rhinelander bec07d6c0a Add std::optional KV serialization + clean up kv serialization overloads
An std::optional lets you distinguish between a value that isn't
provided at all (std::optional will be empty) versus one that is
provided as a default value (for example, a std::string specified as
empty).

On serialization, a null optional is not serialized at all; on
deserialization the optional is reset if omitted, otherwise set to the
specified value.

This also cleans up the serialization code to use simpler C++17 `if
constexpr`s rather than SFINAE overloads for the different serialization
cases.
2020-08-17 02:44:17 -03:00
Jason Rhinelander e1d1366a27 Add missing header needed for std::byte 2020-08-10 14:47:09 -03:00
Jason Rhinelander 899d1e4eaf Wallet RPC server modernization
- Replaces the wallet RPC classes with ones like the core RPC server
(with serialization code moved into a new .cpp file).
  - Restricted commands are now carried through the RPC serialization
    types (by inheriting from RESTRICTED instead of RPC_COMMAND) and
    restrictions are handled in one place rather than being handled in
    each of the 49 restricted endpoints.  This differs a little from how
    the core http server works (which has a PUBLIC base class) because
    for the wallet rpc server unrestricted really doesn't mean "public",
    it means something closer to view-only.
  - GET_TRANSFERS_CSV is now restricted (it looks like an oversight that
    it wasn't before since GET_TRANSFERS is restricted)
  - GET_ADDRESS_BOOK_ENTRY is now restricted.  Since restricted mode is
    meant to provide something like view-only access, it doesn't make
    much sense that address book entries were available.

- Use uWebSockets to provide the wallet RPC server HTTP functionality.
  This version is quite a bit simpler than the core RPC version since it
  doesn't support multithreaded (parallel) requests, and so we don't
  have to worry about queuing jobs.

- Converted all the numeric wallet rpc error codes defines to constexprs

- Changed how endpoints get called; previous this was called:

    bool on_some_endpoint(const wallet_rpc::COMMAND_RPC_SOME_ENDPOINT::request& req, wallet_rpc::COMMAND_RPC_SOME_ENDPOINT::response& res, epee::json_rpc::error& er, const connection_context *ctx = NULL)

  This PR changes it similarly to how core_rpc_server's endpoints work:

    wallet_rpc::SOME_ENDPOINT invoke(wallet_rpc::COMMAND_RPC_SOME_ENDPOINT::request&& req);

  That is:
  - the response is now returned
  - the request is provided by mutable rvalue reference
  - the error object is gone (just throw instead)
  - the connection_context is gone (it was not used at all by any wallet
    rpc endpoint).

- Consolidated most of the (identical) exception handling to the RPC
  method invocation point rather than needing to repeat it in each
  individual endpoint.  This means each endpoint's `invoke` method can
  now just throw (or not catch) exceptions.  Some try/catches are still
  there because they are converting one type of exception into another,
  but the generic ones that return a generic error are gone.

- Removed all remaining epee http code.

- DRYed out some wallet rpc code.
2020-08-07 17:14:03 -03:00
Jason Rhinelander fb0aff57f6 Replace epee http client with curl-based client
In short: epee's http client is garbage, standard violating, and
unreliable.

This completely removes the epee http client support and replaces it
with cpr, a curl-based C++ wrapper.  rpc/http_client.h wraps cpr for RPC
requests specifically, but it is also usable directly.

This replacement has a number of advantages:

- requests are considerably more reliable.  The epee http client code
  assumes that a connection will be kept alive forever, and returns a
  failure if a connection is ever closed.  This results in some very
  annoying things: for example, preparing a transaction and then waiting
  a long tim before confirming it will usually result in an error
  communication with the daemon.  This is just terribly behaviour: the
  right thing to do on a connection failure is to resubmit the request.

- epee's http client is broken in lots of other ways: for example, it
  tries throwing SSL at the port to see if it is HTTPS, but this is
  protocol violating and just breaks (with a several second timeout) on
  anything that *isn't* epee http server (for example, when lokid is
  behind a proxying server).

- even when it isn't doing the above, the client breaks in other ways:
  for example, there is a comment (replaced in this PR) in the Trezor PR
  code that forces a connection close after every request because epee's
  http client doesn't do proper keep-alive request handling.

- it seems noticeably faster to me in practical use in this PR; both
  simple requests (for example, when running `lokid status`) and
  wallet<->daemon connections are faster, probably because of crappy
  code in epee.  (I think this is also related to the throw-ssl-at-it
  junk above: the epee client always generates an ssl certificate during
  static initialization because it might need one at some point).

- significantly reduces the amount of code we have to maintain.

- removes all the epee ssl option code: curl can handle all of that just
  fine.

- removes the epee socks proxy code; curl can handle that just fine.
  (And can do more: it also supports using HTTP/HTTPS proxies).

- When a cli wallet connection fails we know show why it failed (which
  now is an error message from curl), which could have all sorts of
  reasons like hostname resolution failure, bad ssl certificate, etc.
  Previously you just got a useless generic error that tells you
  nothing.

Other related changes in this PR:

- Drops the check-for-update and download-update code.  To the best of
my knowledge these have never been supported in loki-core and so it
didn't seem worth the trouble to convert them to use cpr for the
requests.

- Cleaned up node_rpc_proxy return values: there was an inconsistent mix
  of ways to return errors and how the returned strings were handled.
  Instead this cleans it up to return a pair<bool, val>, which (with
  C++17) can be transparently captured as:

    auto [success, val] = node.whatever(req);

  This drops the failure message string, but it was almost always set to
  something fairly useless (if we want to resurrect it we could easily
  change the first element to be a custom type with a bool operator for
  success, and a `.error` attribute containing some error string, but
  for the most part the current code wasn't doing much useful with the
  failure string).

- changed local detection (for automatic trusted daemon determination)
  to just look for localhost, and to not try to resolve anything.
  Trusting non-public IPs does not work well (e.g. with lokinet where
  all .loki addresses resolve to a local IP).

- ssl fingerprint option is removed; this isn't supported by curl
  (because it is essentially just duplicating what a custom cainfo
  bundle does)

- --daemon-ssl-allow-chained is removed; it wasn't a useful option (if
  you don't want chaining, don't specify a cainfo chain).

- --daemon-address is now a URL instead of just host:port.  (If you omit
  the protocol, http:// is prepended).

- --daemon-host and --daemon-port are now deprecated and produce a
  warning (in simplewallet) if used; the replacement is to use
  --daemon-address.

- --daemon-ssl is deprecated; specify --daemon-address=https://whatever
  instead.

- the above three are now hidden from --help

- reordered the wallet connection options to make more logical sense.
2020-08-07 17:14:03 -03:00
Doyle a142a510ac
Merge pull request #1205 from jagerman/rpc-fixes
Miscellaneous daemon and rpc fixes
2020-08-03 10:36:09 +10:00
Jason Rhinelander cac49dfbb6 JSON: use .put and .write instead of << 2020-08-01 20:07:52 -03:00
Jason Rhinelander a82edadd35 Fix badly formatted bytes < 0x20
The conversion here was becoming an integer and thus inserting a
formatted integer rather than a char value (e.g. 0x02 would end up as
"\u00050" instead of "\u0002").
2020-08-01 20:04:15 -03:00
Jason Rhinelander 52faa1a2e1 Make daemon interactive commands ordered again 2020-08-01 12:37:25 -03:00
Jason Rhinelander 07293ebf60 Address PR feedback 2020-07-02 12:56:37 -03:00
Jason Rhinelander 6135f01bd4 Boost <= 1.65 compatibility fix 2020-07-02 12:56:37 -03:00
Jason Rhinelander f09857de2b More string_view (mostly in wallet code)
This started out with a few string_view simplifications, but unwinding
the string_view calls resulted in quite a few changes.

- use constexpr string_view instead of macros for constants

- use constexpr ints for other macros instead of constants (to be
consistent with the string_view constexprs); also eliminated a couple of
unused variables.

- convert amount parsing to string_view, and rewrite it with comments to
be far less confusing.  (Previously it was remove a "." from a string,
chopping 0s off the end then putting them back on which was hard to
follow).

- fixed some not-quite-right amount parsing unit tests

- replace a bunch of string code (including lots of code allocating new
strings with .substr on a string) with std::string_view.

- avoid using C functions for strings

- convert epee http uri conversion methods to string_view (and fix one
typoed func name "conver" -> "convert")

- Add a `tools::hex_to_type` that converts hex directly into a (simple)
type, with hex and length verification for the type, and use it to
eliminate a bunch of intermediate std::string conversions.

- Convert a bunch of error-prone raw pointer string appends into
more type-safe `tools::view_guts(val)` calls.  (In particular view_guts
always gets the sizeof() correct, while the individual call could easily
put the wrong type in the `sizeof()`).
2020-07-02 12:55:28 -03:00
Jason Rhinelander 443c5e8cea Purge epee::critical_crap and CRITICAL_CRAP
This purges epee::critical_region/epee::critical_section and the awful
CRITICAL_REGION_LOCAL and CRITICAL_REGION_LOCAL1 and
CRITICAL_REGION_BEGIN1 and all that crap from epee code.

This wrapper class around a mutex is just painful, macro-infested
indirection that accomplishes nothing (and, worse, forces all using code
to use a std::recursive_mutex even when a different mutex type is more
appropriate).

This commit purges it, replacing the "critical_section" mutex wrappers
with either std::mutex, std::recursive_mutex, or std::shared_mutex as
appropriate.  I kept anything that looked uncertain as a
recursive_mutex, simple cases that obviously don't recurse as
std::mutex, and simple cases with reader/writing mechanics as a
shared_mutex.

Ideally all the recursive_mutexes should be eliminated because a
recursive_mutex is almost always a design flaw where someone has let the
locking code get impossibly tangled, but that requires a lot more time
to properly trace down all the ways the mutexes are used.

Other notable changes:

- There was one NIH promise/future-like class here that was used in
example one place in p2p/net_node; I replaced it with a
std::promise/future.

- moved the mutex for LMDB resizing into LMDB itself; having it in the
abstract base class is bad design, and also made it impossible to make a
moveable base class (which gets used for the fake db classes in the test
code).
2020-07-02 12:52:13 -03:00
Jason Rhinelander c27334e282 Remove unused lock sleep variable
Besides being a useless idea, this variable doesn't actually do anything
anymore, even in upstream Monero since it got removed from
CRITICAL_REGION_LOCAL.
2020-07-02 12:52:13 -03:00
Jason Rhinelander 48a0dc9eef More string_view niceties
- Adds a tools::trim that operates on a string_view to replace boost
string trim usage.

- Replaces some boost::split's with tools::split's

- updates vercmp to operate on string_views instead of c strings
2020-07-02 12:52:13 -03:00
Jason Rhinelander 24f084a73c Miscellaneous header updates via iwyu
Adds some missing required headers and removes some unnecessary ones.
2020-07-02 12:52:13 -03:00
Jason Rhinelander 723d66ad59 Miscellaneous fixes
Adds a small handful of missing headers, signature updates, etc. to make
it compile again.
2020-07-02 12:52:13 -03:00
Jason Rhinelander 141b709ead Update boost::placeholders
There are some boost::bind's left here that don't look simple to replace
with lambdas, so leave them (but add the required boost::placeholders);
remove a now unused one as well.
2020-07-02 12:52:13 -03:00
Jason Rhinelander 0706727000 Replace epee::byte_slice with much simpler shared_sv
byte_slice was an abomination that used more than 400 lines of code to
implement what can be implemented in about 20 lines of code with a
std::shared_ptr<std::string> and a std::string_view and a couple
convenience methods.

Much of the levin noise generation using it used it gratuitously: they
always allocate a new string, but then return that wrapped it in a
byte_slice abomination because... well, there's no reason at all except
apparently that the byte_slice author wanted to push byte_slice into
places it didn't belong at all (even if you accept the overbuilt
monstrosity).  Those methods now take a string_view and return a string,
which is what they should have done in the first place.
2020-07-02 12:52:13 -03:00
Jason Rhinelander 5009de02a5 Readline clearing fixes & cowsay replacement
Replaces the silly cow with an ASCII Loki logo:

            .
          oooo
        oooo
      oooo   .           You Loki Wallet has been locked to
    oooo    oooo         protect you while you were away.
      oooo    oooo
        oooo    oooo     (Use `set inactivity-lock-timeout 0`
          oooo    oooo   to disable this inactivity timeout)
            .   oooo
              oooo
            oooo
             .

Also includes a few related miscellaneous updates to how we suspend
input and clear the screen.
2020-07-02 12:52:13 -03:00
Jason Rhinelander cc04770b85 DRY wallet inactivity timer
Replaces the horrible callback wrapper that had to be called from each
and every command callback (and was randomly missing from some) to reset
the inactivity timer with a generic one in the console handler that
allows specifying pre-callback and post-callback commands.
2020-07-02 12:52:13 -03:00
Jason Rhinelander 0de3c64a77 Post-merge: Serialization updates
Updates some old serialization code that crept in with the Monero
upstream merge.
2020-07-02 12:52:13 -03:00
Jason Rhinelander 2be43025c7 Post-merge: std::chrono updates
Replaces reintroduced boost::chronos and posix_times with std::chrono.

Also rewrote the time printing parts of the performance_tests code to do
it better use std::chrono types instead of uint64_ts.
2020-07-02 12:52:13 -03:00
Jason Rhinelander 9ca1056f7f Post-merge updates: epee::is_byte_spannable
Fixes the temporary workarounds added for pre-C++17 is_byte_spannable.
2020-07-02 12:52:13 -03:00
Jason Rhinelander 2357bd6a57 Post-merge updates: std::function
Replaces some boost::functions, and removes some C-style ancient
`std::function<Return(void)>` with C++-style `std::function<Return()>`.

Edit: There's also a map -> unordered_map change hidden in here because
it was in the middle of the other changes and is minor.
2020-07-02 12:52:13 -03:00
Jason Rhinelander 3cc53aa06a Post-merge updates: std::string_view 2020-07-02 12:52:13 -03:00
Jason Rhinelander 778cb89620 Post-merge updates: std::optional 2020-07-02 12:52:13 -03:00
Jason Rhinelander 2a34775543 Post-merge: Remove boost::bind
Removes some more boost::bind's that came in with the upstream merge.
2020-07-02 12:52:13 -03:00
Jason Rhinelander e04d81615c epee/portable_storage: move code to .cpp
There's a bunch of code in inline functions in the header for no good
reason.  Move it to a .cpp.
2020-07-02 12:52:13 -03:00
Jason Rhinelander 95a52c0f25 uuid unordered_map changes
- Use std::unordered_map in epee by giving it a hasher

- Fix the hasher in block_queue to invoke boost's proper hasher.
2020-07-02 12:52:13 -03:00
Jason Rhinelander 86986bd703 Replace boost::bind with lambdas 2020-07-02 12:52:13 -03:00
Jason Rhinelander 718a9496fb Replace boost chrono & posix_time with stl chrono 2020-07-02 12:52:13 -03:00
Jason Rhinelander 515b96e20b Replace (most) boost::thread with std::thread
This removes some boost thread interruption code, but that code is
highly unlikely to have done anything: you cannot preemptively interrupt
a thread, and pretty much anywhere a thread would get stuck is in I/O,
where boost's (voluntary) thread interruption cannot follow.

It also removes thread attributes because they have really limited
usefulness.  There *was* a reason to introduce them many years ago (when
musl libc ran into a stack limit problem with libunbound) but musl
increased the default stack size long since.
2020-07-02 12:52:13 -03:00
Jason Rhinelander 9a9ef745b7 Remove unused call_sys_cmd function 2020-07-02 12:52:13 -03:00
Jason Rhinelander c8e82b44da epee time helper: remove cruft, move to src/
There's a bunch of cruft in here that isn't used anywhere; delete it,
and move the two remaining functions to a .cpp file since this header
gets included almost everywhere.

Also simplify the duration printer to avoid needing boost.
2020-07-02 12:52:13 -03:00
Jason Rhinelander 0c427512f6 Make sure we don't blob serialize padded types
The histogram data, in particular, is not blob serializable: it is a 16
byte struct on most 64-bit architectures, and a 12 byte struct on most
32-bit architectures.
2020-07-02 12:52:13 -03:00
Jason Rhinelander 65a37c65c1 Remove horrible logging macros 2020-07-02 12:52:13 -03:00
Jason Rhinelander d8a29a029b Replace boost::bind's with lambda
Lambdas are often more efficient than boost::bind/std::bind, and pretty
much never less efficient.
2020-07-02 12:52:13 -03:00
Jason Rhinelander d0862eb74b Replace boost internal atomic writes with std::atomic's 2020-07-02 12:52:13 -03:00
Jason Rhinelander 37a341ff6b Remove boost regex dependencies
All boost::regex use is now replaced with std::regex.
2020-07-02 12:52:13 -03:00
Jason Rhinelander 49c693d0a9 Split up common/util.h
common/util.h has become something of a dumping ground of random
functions.  This splits them up a little by moving the filesystem bits
to common/file.h, the sha256sum functions to common/sha256sum.h, and the
(singleton) signal handler to common/signal_handler.h.
2020-07-02 12:52:12 -03:00
Jason Rhinelander 008558ba54 Header updates
Adding/removing a few required/unnneeded headers.
2020-07-02 12:52:12 -03:00
Jason Rhinelander 19ca0e21ee Replace a few boost::bind's with lambda 2020-07-02 12:52:12 -03:00
Jason Rhinelander ae344c2534 epee serialization code: std::variant & C++17 modernization
This overhauls the (unpleasant) epee "portable storage" interface to be
a bit easier to work with.  The diff is fairly large because of the
amount of type indirection that was being used, but at its core does
this simplifications:

- `hsection` was just a typedef for a `section*`, so just use `section*`
instead to not pretend it the value is something it isn't.

- use std::variant instead of boost::variant to store the portable
storage items.

- don't make the variant list recursive with itself since the
serialization doesn't support that anyway (i.e. it doesn't support a
list of lists).

- greatly simplified the variant visiting by using generic lambdas.

- the array traversal interface was a horrible mess.  Replaced it with
a simpler custom iterator approach.

- replaced the `selector<bool>` templated class with templated function.
So for example,

    epee::serialization::selector<is_store>::serialize_t_val_as_blob(...)

becomes:

    epee::serialization::perform_serialize_blob<is_store>(bytes, stg, parent_section, "addr");

and similar for the other types of serializing.
2020-07-02 12:52:12 -03:00
Jason Rhinelander e02545ca4b boost->std: mutex, locks; C++17 lock vars
Changes all boost mutexes, locks, and condition_variables to their stl
equivalents.

Changes all lock_guard/unique_lock/shared_lock to not specify the mutex
type (C++17), e.g.

    std::lock_guard foo{mutex};

instead of

    std::lock_guard<oh::um::what::mutex> foo{mutex};

Also changes some related boost::thread calls to std::thread, and some
related boost chrono calls to stl chrono.

boost::thread isn't changed here to std::thread because some of the
instances rely on some boost thread extensions.
2020-07-02 12:52:12 -03:00
Jason Rhinelander 96354a0e0f boost::optional -> std::optional 2020-07-02 12:52:12 -03:00
Jason Rhinelander 87b147c954 De-analize epee 2020-07-02 12:52:12 -03:00
Jason Rhinelander 6dc5d9ac68 boost::shared_ptr -> std::shared_ptr 2020-07-02 12:52:12 -03:00
Jason Rhinelander b9448febdf Replace boost::string_ref with std::string_view
Also a few related small places in here where a `const std::string&`
made mode sense to be a std::string_view.
2020-07-02 12:52:12 -03:00