Commit Graph

426 Commits

Author SHA1 Message Date
Jason Rhinelander 0cecc6749b Remove wallet2.h includes
This includes aren't needed, and wallet2.h is an extremely heavy header
to include.
2020-08-07 17:14:02 -03:00
Jason Rhinelander 4b3bdc0ac5
Service node contribution fixes (#1215)
This fixes a few issues surrounding required contributions:

- #1210 broke reserved registrations (including the operator stake)
  because it wasn't properly recognizing a contribution into a reserved
  slot.

- As part of the above, the overstaking protection now applies to the
  reserved amount + any free contribution room.  (So, if you have a
  reserved spot of 8000 and there is 2000 free, but someone else fills
  1000 of it before your stake gets mined, any stake above 9090 (9000 +
  1%) will fall through without being locked up.

- The required contribution calculation was only taking into account
  contributions but not unfilled, reserved contributions (issue #1137).
  This fixes it to count unfilled, reserved spots as contributions for
  the purposes of calculating the minimum contribution.  This is applied
  in the wallet immediately, but for the chain it is not enforced until
  HF16.

- Eliminates some potential edge cases if a stake contains multiple
  stake outputs.  The wallet won't generate these, but they are
  allowed on chain and could result in an incompletely staked service
  node with no contributor slots remaining.  The minimum contribution
  math effectively assumes a single output for calculating the minimum
  contribution (but if we have multiple outputs we would chew up two
  slots).  Disallow such stakes entirely beginning in HF16.

- Operator stake included in the registration tx had some weird edge
  cases.  The wallet wouldn't produce these, but they should be
  disallowed at the blockchain level:
  - multi-output stakes were allowed (as above).
  - the registration stake only had to be >= 25%, even if the operator
    reserved a larger amount.
  - the registration stake could be for someone *other* than the
    operator.
  Beginning in HF16 the stake included in the registration must be >=
  the operator-reserved amount, and must be staked by the operator
  wallet, and must be a single output.
2020-08-07 16:37:01 +10:00
Jason Rhinelander 03d9f49abb
Recommission credit (#1204)
This changes the credit rules for recommission: instead of being
restored with no credit at all, with this you get restored with 2 blocks
of credit lost (up to all of your credit) for each block of decommission
time you had.

For example, if you had 1000 blocks of credit and are decommissioned for
150 blocks, you'll get recommissioned with 700 blocks of credit.  If you
were decommissioned for 500 or more blocks then you'll get
decommissioned with 0 blocks of credit.

This applies the effects immediately rather than waiting for the hard
fork.  Because this isn't a consensus-breaking change it seems easier to
not worry about the HF rules.  The consequences are that the rules that
get applied to determining how much credit you have will depend on the
nodes involved in testing up until the hard fork: you *may* be subject
to the harsher v7 rules, or you may fall under v8 rules if enough of the
testing quorum has upgraded, but you'll never end up worse than the v7
rules.
2020-08-06 12:24:57 +10:00
Jason Rhinelander 441c21b40d
Add overstaking prevention (#1210)
This prevents staking transactions from being accepted if they overstake
the available contribution room by more than 1%.  This is to prevent a
case that has happened a few times where there are competing partial
stakes submitted for the same SN at the same time (i.e. before a block
gets mined with the stakes).  For example:

- Operator registers service node with 30% contribution
- Staker 1 submits stake with 40% contribution
- Staker 2 submits stake with 60% contribution

The wallet avoids stake 2 if the 40% has been accepted into a block, but
doesn't if it is still in the mempool.  Later, when the contributions
get mined, both stakes are admitted because whichever one goes first
doesn't complete the stake, and the second one is still valid (since
there is a spot, and since it contributes >= the required amount).

Whichever stake gets added to a block second, however, will only be
counted as a contribution of the available amount.  So, for example, if
stake 1 gets added first and then stake 2 gets added you'll end up with
an active service node of:

- operator has 30% contributed and locked
- staker 1 has 40% contributed and locked
- staker 2 has 30% contributed but 60% locked.

This commit adds an upper bound for an acceptable stake that is 101% of
the available contribution room so that, in the above situation,
whichever stake gets added first will be a contribution and the second
one will fall through as an ordinary transaction back to the staker's
wallet so that the staker the re-contribute the proper amount.
2020-08-03 10:10:40 +10:00
Jason Rhinelander 54ca96b997 Fix warnings and unintended copies
- pessimizing move in wallet2 prevents copy ellision

- various for loops were creating copies (clang-10 now warns about
  this).  Mostly this is because they had the type wrong when looping
  through a map: the iterator type of a `map<K, V>` is `pair<const K,
  V>` not `pair<K, V>`.  Replaced them with C++17:

    for (const auto& [key, val] : var)

  which is so much nicer.

- cryptonote::core did not have a virtual destructor, but had virtual
  methods (causing both a warning, and likely a crash if we ever have
  something inheriting from it held in a unique_ptr<core>).

- core() constructor still had explicit even though it lost the single
  argument.

- test code class had a `final` destructor but wasn't marked final.  (It
  also has a billion superfluous `virtual` declarations but I left them
  in place because it's just test code).
2020-07-02 12:52:13 -03:00
Jason Rhinelander fdbd4bc07b DRY out tx_extra API a little; convert extra macros to constexprs
The tx extra getting/setting API is a little non-DRY, so this dries it
out with some small changes:

- Added get_field_from_tx_extra that takes a specific tx_extra lvalue
reference and fetches it.  This eliminates a bunch of nearly duplicate
code that calls parse - find - return.

- Add three new functions to add tagged data:

  - add_tagged_data_to_tx_extra is a raw function that takes a
    tag and string_view and appends it to tx_extra

  - add_tx_extra<T>(tx_extra, val) lets you copy `val` directly as a
    `T`.  `val` does *not* have to be the same as `T` (`T` is not
    deduced) but rather just has to have the same size.  This makes it
    very easy to add simple tx_extra values such as `tx_extra_pub_key`
    from just a pub key without needing to wrap it first:

        add_tx_extra<tx_extra_pub_key>(tx.extra, regular_old_pubkey);

  - add_tx_extra<T>(tx, val) - very light wrapper around the above that
    takes a transaction lvalue directly (rather than the extra vector)
    for slight convenience.

These additions allow removing several now-unneeded methods and simplify
some code around the tx extra handling.

This commit also converts the TX_EXTRA_* macros into constexprs.
2020-07-02 12:52:12 -03:00
Jason Rhinelander e7d056edf1 boost::variant -> std::variant
A huge amount of this is repetitive:

- `boost::get<T>(variant)` becomes `std::get<T>(variant)`
- `boost::get<T>(variant_ptr)` becomes `std::get_if<T>(variant_ptr)`
- `variant.type() == typeid(T)` becomes `std::holds_alternative<T>(variant)`

There are also some simplifications to visitors using simpler stl
visitors, or (simpler still) generic lambdas as visitors.

Also adds boost serialization serializers for std::variant and
std::optional.
2020-07-02 12:52:12 -03:00
Jason Rhinelander a506790f1c Serialization (part2): application 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 83dd656e74 C++17
Switch loki dev branch to C++17 compilation, and update the code with
various C++17 niceties.

- stop including the (deprecated) lokimq/string_view.h header and
instead switch everything to use std::string_view and `""sv` instead of
`""_sv`.

- std::string_view is much nicer than epee::span, so updated various
loki-specific code to use it instead.

- made epee "portable storage" serialization accept a std::string_view
instead of const lvalue std::string so that we can avoid copying.

- switched from mapbox::variant to std::variant

- use `auto [a, b] = whatever()` instead of `T1 a; T2 b; std::tie(a, b)
= whatever()` in a couple places (in the wallet code).

- switch to std::lock(...) instead of boost::lock(...) for simultaneous
lock acquisition.  boost::lock() won't compile in C++17 mode when given
locks of different types.

- removed various pre-C++17 workarounds, e.g. for fold expressions,
unused argument attributes, and byte-spannable object detection.

- class template deduction means lock types no longer have to specify
the mutex, so `std::unique_lock<std::mutex> lock{mutex}` can become
`std::unique_lock lock{mutex}`.  This will make switching any mutex
types (e.g. from boost to std mutexes) far easier as you just have to
update the type in the header and everything should work.  This also
makes the tools::unique_lock and tools::shared_lock methods redundant
(which were a sort of poor-mans-pre-C++17 way to eliminate the
redundancy) so they are now gone and replaced with direct unique_lock or
shared_lock constructions.

- Redid the LNS validation using a string_view; instead of using raw
char pointers the code now uses a string view and chops off parts of the
view as it validates.  So, for instance, it starts with "abcd.loki",
validates the ".loki" and chops the view to "abcd", then validates the
first character and chops to "bcd", validates the last and chops to
"bc", then can just check everything remaining for is-valid-middle-char.

- LNS validation gained a couple minor validation checks in the process:
  - slightly tightened the requirement on lokinet addresses to require
    that the last character of the mapped address is 'y' or 'o' (the
    last base32z char holds only one significant bit).
  - In parse_owner_to_generic_owner made sure that the owner value has
    the correct size (otherwise we could up end not filling or
    overfilling the pubkey buffer).

- Replaced base32z/base64/hex conversions with lokimq's versions which
have a nicer interface, are better optimized, and don't depend on epee.
2020-07-02 12:52:12 -03:00
Jason Rhinelander e3bca6cbc3 Silence maybe-uninitialized warnings 2020-06-14 23:55:48 -03:00
Doyle 2e42202e94 Merge commit 'dcff02e4c3b1214143b19685361c3c6cffa62be7' into MergeUpstream3 2020-05-26 15:15:25 +10:00
Doyle ec044e7b96 Loki: Fix tests and bugs introduced in merging 2020-05-22 16:32:42 +10:00
cryptonote.social 616003fb33 show amount for blacklisted stakes (#1140)
* fix segfault when exiting cli wallet due to running poll thread

* fix whitespace

* style

* support for amount of blacklisted stakes in wallets

* bump version number for blacklist entries with amounts

* default initialize ints in key_image_blacklist_entry

* whitespace
2020-05-11 18:45:15 -03:00
Jason Rhinelander 0e3f173c7f RPC overhaul
High-level details:

This redesigns the RPC layer to make it much easier to work with,
decouples it from an embedded HTTP server, and gets the vast majority of
the RPC serialization and dispatch code out of a very commonly included
header.

There is unfortunately rather a lot of interconnected code here that
cannot be easily separated out into separate commits.  The full details
of what happens here are as follows:

Major details:
- All of the RPC code is now in a `cryptonote::rpc` namespace; this
  renames quite a bit to be less verbose: e.g. CORE_RPC_STATUS_OK
  becomes `rpc::STATUS_OK`, and `cryptonote::COMMAND_RPC_SOME_LONG_NAME`
  becomes `rpc::SOME_LONG_NAME` (or just SOME_LONG_NAME for code already
  working in the `rpc` namespace).
- `core_rpc_server` is now completely decoupled from providing any
  request protocol: it is now *just* the core RPC call handler.
- The HTTP RPC interface now lives in a new rpc/http_server.h; this code
  handles listening for HTTP requests and dispatching them to
  core_rpc_server, then sending the results back to the caller.
- There is similarly a rpc/lmq_server.h for LMQ RPC code; more details
  on this (and other LMQ specifics) below.
- RPC implementing code now returns the response object and throws when
  things go wrong which simplifies much of the rpc error handling.  They
  can throw anything; generic exceptions get logged and a generic
  "internal error" message gets returned to the caller, but there is
  also an `rpc_error` class to return an error code and message used by
  some json-rpc commands.
- RPC implementing functions now overload `core_rpc_server::invoke`
  following the pattern:

    RPC_BLAH_BLAH::response core_rpc_server::invoke(RPC_BLAH_BLAH::request&& req, rpc_context context);

  This overloading makes the code vastly simpler: all instantiations are
  now done with a small amount of generic instantiation code in a single
  .cpp rather than needing to go to hell and back with a nest of epee
  macros in a core header.
- each RPC endpoint is now defined by the RPC types themselves,
  including its accessible names and permissions, in
  core_rpc_server_commands_defs.h:
  - every RPC structure now has a static `names()` function that returns
    the names by which the end point is accessible.  (The first one is
    the primary, the others are for deprecated aliases).
  - RPC command wrappers define their permissions and type by inheriting
    from special tag classes:
    - rpc::RPC_COMMAND is a basic, admin-only, JSON command, available
      via JSON RPC.  *All* JSON commands are now available via JSON RPC,
      instead of the previous mix of some being at /foo and others at
      /json_rpc.  (Ones that were previously at /foo are still there for
      backwards compatibility; see `rpc::LEGACY` below).
    - rpc::PUBLIC specifies that the command should be available via a
      restricted RPC connection.
    - rpc::BINARY specifies that the command is not JSON, but rather is
      accessible as /name and takes and returns values in the magic epee
      binary "portable storage" (lol) data format.
    - rpc::LEGACY specifies that the command should be available via the
      non-json-rpc interface at `/name` for backwards compatibility (in
      addition to the JSON-RPC interface).
- some epee serialization got unwrapped and de-templatized so that it
  can be moved into a .cpp file with just declarations in the .h.  (This
  makes a *huge* difference for core_rpc_server_commands_defs.h and for
  every compilation unit that includes it which previously had to
  compile all the serialization code and then throw all by one copy away
  at link time).  This required some new macros so as to not break a ton
  of places that will use the old way putting everything in the headers;
  The RPC code uses this as does a few other places; there are comments
  in contrib/epee/include/serialization/keyvalue_serialization.h as to
  how to use it.
- Detemplatized a bunch of epee/storages code.  Most of it should have
  have been using templates at all (because it can only ever be called
  with one type!), and now it isn't.  This broke some things that didn't
  properly compile because of missing headers or (in one case) a messed
  up circular dependency.
- Significantly simplified a bunch of over-templatized serialization
  code.
- All RPC serialization definitions is now out of
  core_rpc_server_commands_defs.h and into a single .cpp file
  (core_rpc_server_commands_defs.cpp).
- core RPC no longer uses the disgusting
  BEGIN_URI_MAP2/MAP_URI_BLAH_BLAH macros.  This was a terrible design
  that forced slamming tons of code into a common header that didn't
  need to be there.
- epee::struct_init is gone.  It was a horrible hack that instiated
  multiple templates just so the coder could be so lazy and write
  `some_type var;` instead of properly value initializing with
  `some_type var{};`.
- Removed a bunch of useless crap from epee.  In particular, forcing
  extra template instantiations all over the place in order to nest
  return objects inside JSON RPC values is no longer needed, as are a
  bunch of stuff related to the above de-macroization of the code.
- get_all_service_nodes, get_service_nodes, and get_n_service_nodes are
  now combined into a single `get_service_nodes` (with deprecated
  aliases for the others), which eliminates a fair amount of
  duplication.  The biggest obstacle here was getting the requested
  fields reference passed through: this is now done by a new ability to
  stash a context in the serialization object that can be retrieved by a
  sub-serialized type.

LMQ-specifics:

- The LokiMQ instance moves into `cryptonote::core` rather than being
  inside cryptonote_protocol.  Currently the instance is used both for
  qnet and rpc calls (and so needs to be in a common place), but I also
  intend future PRs to use the batching code for job processing
  (replacing the current threaded job queue).
- rpc/lmq_server.h handles the actual LMQ-request-to-core-RPC glue.
  Unlike http_server it isn't technically running the whole LMQ stack
  from here, but the parallel name with http_server seemed appropriate.
- All RPC endpoints are supported by LMQ under the same names as defined
  generically, but prefixed with `rpc.` for public commands and `admin.`
  for restricted ones.
- service node keys are now always available, even when not running in
  `--service-node` mode: this is because we want the x25519 key for
  being able to offer CURVE encryption for lmq RPC end-points, and
  because it doesn't hurt to have them available all the time.  In the
  RPC layer this is now called "get_service_keys" (with
  "get_service_node_key" as an alias) since they aren't strictly only
  for service nodes.  This also means code needs to check
  m_service_node, and not m_service_node_keys, to tell if it is running
  as a service node.  (This is also easier to notice because
  m_service_node_keys got renamed to `m_service_keys`).
- Added block and mempool monitoring LMQ RPC endpoints: `sub.block` and
  `sub.mempool` subscribes the connection for new block and new mempool
  TX notifications.  The latter can notify on just blink txes, or all
  new mempool txes (but only new ones -- txes dumped from a block don't
  trigger it).  The client gets pushed a [`notify.block`, `height`,
  `hash`] or [`notify.tx`, `txhash`, `blob`] message when something
  arrives.

Minor details:
- rpc::version_t is now a {major,minor} pair.  Forcing everyone to pack
  and unpack a uint32_t was gross.
- Changed some macros to constexprs (e.g. CORE_RPC_ERROR_CODE_...).
  (This immediately revealed a couple of bugs in the RPC code that was
  assigning CORE_RPC_ERROR_CODE_... to a string, and it worked because
  the macro allows implicit conversion to a char).
- De-templatizing useless templates in epee (i.e. a bunch of templated
  types that were never invoked with different types) revealed a painful
  circular dependency between epee and non-epee code for tor_address and
  i2p_address.  This crap is now handled in a suitably named
  `net/epee_network_address_hack.cpp` hack because it really isn't
  trivial to extricate this mess.
- Removed `epee/include/serialization/serialize_base.h`.  Amazingly the
  code somehow still all works perfectly with this previously vital
  header removed.
- Removed bitrotted, unused epee "crypted_storage" and
  "gzipped_inmemstorage" code.
- Replaced a bunch of epee::misc_utils::auto_scope_leave_caller with
  LOKI_DEFERs.  The epee version involves quite a bit more instantiation
  and is ugly as sin.  Also made the `loki::defer` class invokable for
  some edge cases that need calling before destruction in particular
  conditions.
- Moved the systemd code around; it makes much more sense to do the
  systemd started notification as in daemon.cpp as late as possible
  rather than in core (when we can still have startup failures, e.g. if
  the RPC layer can't start).
- Made the systemd short status string available in the get_info RPC
  (and no longer require building with systemd).
- during startup, print (only) the x25519 when not in SN mode, and
  continue to print all three when in SN mode.
- DRYed out some RPC implementation code (such as set_limit)
- Made wallet_rpc stop using a raw m_wallet pointer
2020-05-11 18:44:45 -03:00
Doyle 35621a0979 Switch to exception based error reporting 2020-04-24 13:39:27 +10:00
Doyle ff2949934d SN: Move signature validating code to function and use in wallet2 & sn 2020-04-24 11:46:38 +10:00
Doyle 6e12ab3665 Add overload for tx_get_staking_components that accepts a txid 2020-04-24 10:18:12 +10:00
Doyle f3767a5d7e Classify staking transactions in show/export_transfers 2020-04-23 16:50:25 +10:00
Doyle 8f58ce001f get_contribution -> tx_get_staking_components and make public 2020-04-23 16:44:08 +10:00
Jason Rhinelander 8dc42c1a97
Warn and refuse to submit proofs if we detect duplicate ed/x25519 keys (#1121) 2020-04-14 10:53:40 +10:00
Doyle c433a15b8a Min uptime version=7.0.0, centralise defn to other version vars 2020-03-12 10:47:24 +11:00
Maxim Shishmarev 5cdf090539
Add storage server lokimq port (#1056)
* Add storage server lokimq port

* Don't hash storage lmq port until HF15

* Parse storage lmq port from ping
2020-03-11 12:35:36 +11:00
Doyle a22157862b Update txtype activation hardforks 2020-02-13 11:07:46 +11:00
Doyle c7c32896fd Remove unused function rescan_starting_from_curr..
This has been combined into load_missing_blocks_into_loki_subsystem
which also handles loading blocks into LNS, but doesn't handle rescanning yet.
2020-02-13 11:07:46 +11:00
Doyle 632f6a6e83 Rebase fix, blink_lns -> blink, v15 and reserve types for protocol 2020-02-13 11:07:46 +11:00
Doyle d33c937dd4 Start move away from hooks, combine rescanning of lns and snl to one function
Call service_node_lists's block added hook function manually instead of
hooking the hook. There's a common operation between the 2 subsystems,
Loki Name Service and Service Node List, in which they generate state
based off incoming blocks from the blockchain.

Upon profiling, the slowest factor of this is retrieving the blocks from
the DB in batches of a 1000 rather than the processing of the blocks in
the subsystems. So flatten out the hierarchy so that we can retrieve the
batch and simultaneously process the blocks in both subsystems on the
same fetch request.

A complete removal of the hook system so we have more flexibility at the
callsite can be done in a later PR, to avoid too many unneccesary
changes in the LNS PR.
2020-02-13 11:07:46 +11:00
Doyle dc69d237e6 Add support for Loki Name Service on the backend 2020-02-13 11:07:46 +11:00
Jason Rhinelander bf13bbfb26 Quorumnet - fix premature x25519 cache expiry
x25519 -> pubkey cache entries weren't being updated on new but
unchanged proofs, so would eventually expire and cause SNs to stop
authenticating new quorumnet connections as being from SNs.
2020-01-13 13:53:00 -04:00
Doyle 1b750a6c38
Service Node List reset, should reset all transient fields (#953)
* Service Node List reset, should reset all transient fields

* Don't clear proofs/x25519 map on reset for robustness
2020-01-09 13:38:18 +11:00
Jason Rhinelander 16288433ca Add hardfork at height 442333
Estimated time: Thu Jan  9 00:00:00 2020 UTC
2019-12-26 13:30:06 -04:00
Jason Rhinelander 63b52422f6 macos workaround: use boost::shared_mutex
The MacOSX 10.11 SDK we use is broken AF: it lies about supporting
C++14, but really only upgraded the headers but not the library itself,
so using std::shared_timed_mutex just results in a linking failure.

Upgrading the SDK is a huge pain (I tried, failed, and gave up), so for
now temporarily switch to boost::shared_mutex until we sort out the
macOS build disaster.
2019-12-23 15:02:20 -04:00
Jason Rhinelander 78ded3af70 Do txn handling via TXN_BLOCK_PREFIX macro (#982)
The macro handles the case where we can be called from either inside or
outside a block batch transaction.
2019-12-16 08:44:53 +10:00
Jason Rhinelander 8b1df0ef4c Fix deadlock
The m_blockchain lock added in #975 was causing deadlocks because we
have ordered `m_blockchain -> ... -> m_sn_mutex` lock sequences but
`proof.store()` was adding a `m_sn_mutex -> ... -> m_blockchain`
sequence when called when receiving an uptime proof.  Fix it by also
taking out an m_blockchain lock where we take out the `m_sn_mutex`.
2019-12-11 23:29:52 -04:00
Jason Rhinelander 55bd5941aa Add helper header/func to obtain unique locks
auto locks = tools::unique_locks(mutex1, mutex2, ...);

gives you a tuple of unique_locks and obtains the locks atomically.

    auto lock = tools::unique_lock(lock1);

is essentially the same as:

    std::unique_lock<decltype(lock1)> lock{lock1};

but less ugly (and extends nicely to the plural version).
2019-12-11 23:28:05 -04:00
Jason Rhinelander 19f59acfd8 Work around broken database interface
Apparently it's not safe to use "transactions" in the LMDB without first
taking out an exclusive lock on the Blockchain class.  Transactions
apparently aren't actually transactions, they are just fatal errors that
forever deadlock the database if you try to take two write transactions
at once.

Work around this terrible design by adding the appropriate magic
incantation.
2019-12-11 11:51:36 -04:00
Doyle 9ea0c136b5 Update service node info to v4, no data conversions necessary 2019-12-11 14:27:59 +11:00
Jason Rhinelander 9d7a53840e Set minimum version for v14
This will need to be updated with the final mainnet release version.
2019-12-10 16:44:49 -04:00
Jason Rhinelander d0abfdd092 Clear our own proof timestamp when loading
This restores the more aggressive sending of uptime proofs after a
restart.
2019-12-09 22:27:33 -04:00
Jason Rhinelander 28171488f9 Log warning when ed25519 -> x25519 conversion fails 2019-12-09 22:26:41 -04:00
Jason Rhinelander 0829df4855 Iterate directly on service node info map 2019-12-09 22:26:41 -04:00
Jason Rhinelander 7904e64bae Don't reset proof data when rescanning
This would wipe out proof data if the rescan crossed a registration, and
wipe out the timestamp if it crossed a decommission.  We don't want to
do either when rescanning: our latest proof data would have already been
reset the first time we cross the registration/decomm transactions.
2019-12-09 22:26:41 -04:00
Jason Rhinelander b3d6150fa9 Store uptime proof data separately from states
This extracts uptime proof data entirely from service node states,
instead storing (some) proof data as its own first-class object in the
code and backed by the database.  We now persistently store:

- timestamp
- version
- ip & ports
- ed25519 pubkey

and update it every time a proof is received.  Upon restart, we load all
the proofs from the database, which means we no longer lose last proof
received times on restart, and always have the most recently received ip
and port information (rather than only having whatever it was in the
most recently stored state, which could be ~12 blocks ago).  It also
means we don't need to spam the network with proofs for up to half an
hour after a restart because we now know when we last sent (and
received) our own proof before restarting.

This separation required some restructuring: most notably changing
`state_t` to be constructed with a `service_node_list` pointer, which it
needs both directly and for BlockchainDB access.  Related to this is
also eliminating the separate BlockchainDB pointer stored in
service_node_list in favour of just accessing it via m_blockchain (which
now has a new `has_db()` method to detect when it has been initialized).
2019-12-09 22:26:38 -04:00
Jason Rhinelander c8ca6a0cd7 Eliminate state_t lookup temporaries (C++14)
Looking up by height currently goes via a mostly-empty implicitly
created state_t, which is wasteful but was necessary with C++11.  C++14
`std::set`s let us avoid this by making `state_t` and heights directly
comparable for set lookups.
2019-12-09 22:25:44 -04:00
Doyle f815ed5c8d Store more quorums than just the immutable checkpoint 2019-12-10 11:06:28 +11:00
Doyle 5bcb45aadc Merge branch 'master' into dev 2019-12-04 16:03:21 +11:00
Doyle 291ba112b4 enum_top, use burn hf in sanity check, fill new construction data 2019-12-04 15:17:55 +11:00
Doyle 1eb45e523e Build/core test fixes, incoporate staking changes from LNS
- Fix assert to use version_t::_count in service_node_list.cpp
- Incoporate staking changes from LNS which revamp construct tx to
  derive more of the parameters from hf_version and type. This removes
  extraneous parameters that can be derived elsewhere.

  Also delegate setting loki_construct_tx_params into wallet2 atleast,
  so that callers into the wallet logic, like rpc server, simplewallet
  don't need bend backwards to get the HF version whereas the wallet has
  dedicated functions for determining the HF.
2019-12-04 15:17:55 +11:00
Jason Rhinelander 0a7397b8aa Rewrite travis-ci code
- Don't use a trusty distro to load an ubuntu docker of a bionic image
  which builds a bunch of dependencies that are already available as
  system packages.

- Replace arm64 cross compiled build with native full build

- Remove armhf build; it's possible but not that simple and not that
  useful for every build.

- Add s390x (big endian) and pp64le builds.  These and the arm64 build
  are built partially with -j1 and partially with -j2 because the build
  environment for the non-amd64 arches only has 4GB memory, which is
  barely enough for wallet2.cpp by itself.

- Build on bionic and xenial

- Add a clang build

- Add a debug build

- Build tests on main bionic build (don't run them; we can explore that
  later)

- Do macos build on macos, and build on 10.13 and 10.14 (with the
  intention of updating the latter to 10.15 once available on travis);
  cross-compiling it from linux was stupid.
2019-12-03 00:51:09 -04:00
Doyle 8220f5bd1a Don't check alt quorums when processing blocks on the mainchain
If you accept a block on the main chain due to an alternative quorum,
eventually that alternative quorum will get pruned as it corresponds to an
alternative block and not the current block.

This should not be accepted via the main chain, but instead the block
should be passed through the alt chain block added hook.
2019-12-03 11:48:46 +11:00
Doyle 148531f199 Query state archive when processing state changes 2019-12-03 11:26:30 +11:00
Jason Rhinelander d7a2acf028 Promote SN state change errors to MERROR
These are quite serious: if they occur there's a good chance that the
local SN state data is wrong.  (At default logging they aren't printed
at all, and really should be).

This also fixes and adds more detail in the the "height is not stored"
error.
2019-12-02 18:21:16 -04:00
Jason Rhinelander 20c07f8e8d Promote SN state change errors to MERROR
These are quite serious: if they occur there's a good chance that the
local SN state data is wrong.  (At default logging they aren't printed
at all, and really should be).

This also fixes and adds more detail in the the "height is not stored"
error.
2019-12-02 18:19:39 -04:00
Jason Rhinelander 8090ee3481 Move randomness into new common/random.h
This adds a thread-local, pre-seeded rng at `tools::rng` (to avoid the
multiple places we are creating + seeding such an RNG currently).

This also moves the portable uniform value and generic shuffle code
there as well as neither function is specific to service nodes and this
seems a logical place for them.
2019-11-27 14:07:52 -04:00
Jason Rhinelander d66e6e9e3f Align hashable data structures
We don't impose any alignment on hashable types, but this means the
hashing function is doing invalid misaligned access when converting to a
size_t.  This aligns all of the primitive data types (crypto::hash,
public keys, etc.) to the same alignment as size_t.

That cascades into a few places in epee which only allow byte spanning
types that have byte alignment when what it really requires is just that
the type has no padding.  In C++17 this is exactly the purpose of
std::has_unique_object_representations, but that isn't available (or
even implementable) in C++14 so add specializations for the type that
need it to tell epee that we know those types are properly packed and
that it can safely use them as bytes.

Related to this, monero/epee also misuses `is_standard_layout` when the
purpose is actually `is_trivially_copyable`, so fixed that too.  (You
need the latter but don't need the former for a type to be safely
memcpy'able; the only purpose of `is_standard_layout` is when you need
to be sure your structs are compatible with C structs which is
irrelevant here).
2019-11-27 14:07:52 -04:00
Jason Rhinelander dd7a4104b5 Blink
This is the bulk of the work for blink.  There is two pieces yet to come
which will follow shortly, which are: the p2p communication of blink
transactions (which needs to be fully synchronized, not just shared,
unlike regular mempool txes); and an implementation of fee burning.

Blink approval, multi-quorum signing, cli wallet and node support for
submission denial are all implemented here.

This overhauls and fixes various parts of the SNNetwork interface to fix
some issues (particularly around non-SN communication with SNs, which
wasn't working).

There are also a few sundry FIXME's and TODO's of other minor details
that will follow shortly under cleanup/testing/etc.
2019-11-27 14:07:52 -04:00
Jason Rhinelander 5d28c9cf8d Switch service_node_keys to plain pointer instead of shared_ptr
The owning pointer is stashed in a unique_ptr so that it gets properly
destroyed; everything else gets a plain pointer to it.

(Ideally it would be std::optional instead, but that's not until C++17)
2019-11-27 14:07:52 -04:00
Jason Rhinelander d0573c11b3 Add blink quorums 2019-11-27 14:07:52 -04:00
Jason Rhinelander 8a276b362f Store loki version as std::array<u16,3> everywhere
Currently we store it as various different things: 3 separate ints, 2
u16s, 3 separate u16s, and a vector of u16s.  This unifies all version
values to a `std::array<uint16_t,3>`.

- LOKI_VERSION_{MAJOR,MINOR,PATCH} are now just LOKI_VERSION
- The previous LOKI_VERSION (C-string of the version) is now renamed
  LOKI_VERSION_STR

A related change included here is that the restricted RPC now returns
the major version in the get_info rpc call instead of an empty string
(e.g. "5" instead of "").  There is almost certainly enough difference
in the RPC results to distinguish major versions already so this doesn't
seem like it actually leaks anything significant.
2019-11-27 14:07:52 -04:00
Jason Rhinelander 1b5234e6a6 Rename `testing_quorum` class to just `quorum`
testing_quorum is a bit of a misnomer: it only does testing when used
for an obligations quorum, but it is also used for checkpointing and
soon Blink.
2019-11-27 14:07:52 -04:00
Jason Rhinelander 57e64b7d1c Add quorumnet comms
This adds vote relaying via quorumnet.

- The main glue between existing code and quorumnet code is in
  cryptonote_protocol/quorumnet.{h,cpp}
- Uses function pointers assigned at initialization to call into the
  glue without making cn_core depend on p2p
- Adds ed25519 and quorumnet port to uptime proofs and serialization.
- Added a second uptime proof signature using the ed25519 key to that
  SNs are proving they actually have it (otherwise they could spoof
  someone else's pubkey).
- Adds quorumnet port, defaulting to zmq rpc port + 1.  quorumnet
  listens on the p2p IPv4 address (but broadcasts the `public_ip` to the
  network).
- Derives x25519 when received or deserialized.
- Converted service_info_info::version_t into a scoped enum (with the
  same underlying type).
- Added contribution_t::version_t scoped enum.  It was using
  service_info_info::version for a 0 value which seemed rather wrong.

Random small details:
- Renamed internal `arg_sn_bind_port` for consistency
- Moved default stagenet ports from 38153-38155 to 38056-38058, and add the default stagenet
  quorumnet port at 38059 (this keeps the range contiguous; otherwise the next stagenet default port
  would collide with the testnet p2p port).
2019-11-27 14:07:45 -04:00
Doyle 0b8e21fb18
Aggegrate init service node info instead of setting members individually (#934)
* Aggegrate init service node info instead of setting members individually

* Initialise in header instead of aggregate
2019-11-27 14:40:56 +11:00
Jason Rhinelander 34480c6f23 Merge branch 'pk25519-rpc-interfaces' into lokinet-integration 2019-11-26 21:47:07 -04:00
1point21gigabytes 40946bd92f explicitly specify UTC (#931) 2019-11-25 15:30:12 +11:00
Jason Rhinelander f6bd747d94 Add optional get_n_sns RPC request; remove get_all_sn_pubkeys RPC
This allows a `"if_block_not_equal": "hash"` parameter to be given to
`get_n_service_nodes` which, if the given value matches the current
top block hash, skips building and returning a reply.

This is primarily aimed at lokinet which polls fairly frequenty for an
update but where the vast majority of those polls contain no new
information.

It also removes the get_all_service_nodes_public_keys RPC request
completely as lokinet was the only consumer of it (particularly unlikely
that anyone else was using this because it was returning the keys
base32z-encoded).
2019-11-08 03:01:08 -04:00
Jason Rhinelander a9294cdbef Remove boost::value_initialized and BOOST_FOREACH (#921)
Neither of these have a place in modern C++11; boost::value_initialized
is entirely superseded by `Type var{};` which does value initialization
(or default construction if a default constructor is defined).  More
problematically, each `boost::value_initialized<T>` requires
instantiation of another wrapping templated type which is a pointless
price to pay the compiler in C++11 or newer.

Also removed is the AUTO_VAL_INIT macro (which is just a simple macro
around constructing a boost::value_initialized<T>).

BOOST_FOREACH is a similarly massive pile of code to implement
C++11-style for-each loops. (And bizarrely it *doesn't* appear to fall
back to C++ for-each loops even when under a C++11 compiler!)

This removes both entirely from the codebase.
2019-11-01 09:26:58 +11:00
Doyle e949e15c6e
Remove hard-forking code on the boundary of 12 and 13 (#919) 2019-10-30 14:50:34 +11:00
Doyle 973f9de780
Merge pull request #901 from loki-project/dev
Merge v5.1.2 to master
2019-10-16 14:29:19 +10:00
Doyle 5fd673ae07
Guard against unexpected infinite loop, early out alt state cull (#896)
* Guard against unexpected infinite loop, early out alt state cull

* Remove unused code
2019-10-16 14:02:07 +10:00
Doyle 788603caaa Distinguish pop_blocks from reorg in detach hook 2019-10-16 10:25:04 +11:00
Jason Rhinelander 4b8c62b711 Fix version printing in info message
If major/minor are uint8_t then printing them tries to print characters
rather than numbers.  Changing them to uint16_t seems the easiest fix
(and agrees with the type we use for storing version parts in proofs).
2019-10-09 17:57:55 -03:00
Doyle fbd5c5eb60
Bump minor patch to 1 and enforce uptime proof minor version (#881)
* Bump minor patch to 1 in preparation for release

* Hardfork 13 date for mainnet
2019-10-09 12:54:16 +11:00
Jason Rhinelander 4edb53d317 Fix version_t count assert
Use an alias to simplify a little.
2019-10-08 01:21:22 -03:00
Jason Rhinelander 5f4b611d38 Delay SN info duplication
This required a bit of reworking - without the duplication we only have
a const iterator so can't reuse it later for a non-const iterator, so
store the found contributor by index instead.
2019-10-08 01:21:07 -03:00
Jason Rhinelander c00e8221cc Add SN ed25519/x25519 pubkeys + quorumnet port in proofs
This generates a ed25519 keypair (and from it derives a x25519 keypair)
and broadcasts the ed25519 pubkey in HF13 uptime proofs.

This auxiliary key will be used both inside lokid (starting in HF14) in
places like the upcoming quorumnet code where we need a standard
pub/priv keypair that is usable in external tools (e.g. sodium) without
having to reimplement the incompatible (though still 25519-based) Monero
pubkey format.

This pulls it back into HF13 from the quorumnet code because the
generation code is ready now, and because there may be opportunities to
use this outside of lokid (e.g. in the storage server and in lokinet)
before HF14.  Broadcasting it earlier also allows us to be ready to go
as soon as HF14 hits rather than having to wait for every node to have
sent a post-HF14 block uptime proof.

For a similar reason this adds a placeholder for the quorumnet port in
the uptime proof: currently the value is just set to 0 and ignored, but
allowing it to be passed will allow upgraded loki 6.x nodes to start
sending it to each other without having to wait for the fork height so
that they can start using it immediately when HF14 begins.
2019-10-07 22:09:17 -03:00
Jason Rhinelander 2c52c1c020 Add unified key container
This puts the SN pubkey/privkey into a single struct (which have
ed25519/x25519 keys added to it in the future), which simplifies various
places to just pass the struct rather than storing and passing the
pubkey and privkey separately.
2019-10-07 22:09:17 -03:00
Jason Rhinelander 45d62f1763 Code cleanup & minor reorganization
- Replace a bunch of repetitive proof rejection rules with a macro +
  HF requirement version loop.
- Remove mostly unused MAX_KEY_IMAGES_PER_CONTRIBUTOR variable.  It has
  always been set to 1 (and so doesn't actually do anything).  If we
  wanted to increase it at this point we'd have to add multiple
  HF-guarded versions of it and HF-version guard the code anyway, so
  seems simpler to just drop it.
- Simplify the max contributor/max contribution logic and merge it in
  with the existing contributor code (this is made easier by the above
  dropped variable).
2019-10-07 22:09:17 -03:00
Jason Rhinelander dd6de52378 Add --dev-allow-local-ip flag for allowing local network public-ip
This is very useful for testing.  Also adds a suitable scary warning at
startup when used.
2019-10-07 22:09:17 -03:00
Jason Rhinelander a3ebf5eba6 Reward fudge factor (#867)
* Add a 1 atomic unit fudge factor on reward calculations

If the thread's rounding mode is different (because RandomX changed it
in the current thread or in the thread that generated the block) then
the reward calculation can be up to 1 atomic unit off.  This lets those
pass rather than rejecting the block.

(I tested LOKI's reward calculations up to height 10M to verify that the
maximum error in reward values is never off by more than 1).

* Remove partial_block_reward

This variable is not actually used anywhere.

* Add 1ULP tolerance for SN reward checks

* Rewrite confusing logic and error

- fix the broken error message which was printing "base(base+fees)" instead
  of "total(base+fees)".
- improve the error message wording to actually indicate what the values
  signify.
- don't use base_reward as a temporary value (the intermediate value
  assigned to it is completely replaced later anyway).
- use some better variable names to describe what is happening here

* Add check for overflow
2019-10-04 13:57:14 +10:00
Doyle 127b505558 Make empty quorum if height is checkpointable but insufficient nodes
This is so that we can validly print an error in quorum_cop on the
heights that are actually not being stored in the daemon VS a service
node network that has insufficient nodes to form the quorum.

Update core test to enforce this constraint
2019-09-23 14:42:29 +10:00
Doyle f94328bf37
Switch to using validators for checkpoints (#850)
* Add temporary HF12/13 code to switch to validators for checkpointing

* Don't use target height for verifying vote age

You wouldn't have the quorum calculated to verify the vote in the first place.

* Update core tests to use validators for checkpoints
2019-09-20 15:54:00 +10:00
Doyle bcbaaeef93
Enforce checkpoint quorums min size and soft fork it for testnet (#842)
* Enforce checkpoint quorums min size from HF13 onwards

* Add core_test for checkpoint quorum min size
2019-09-20 12:46:34 +10:00
Doyle c41bfbdfe4 Add +detail to print_sn|print_n_status, display checkpoint and reachability 2019-09-19 17:42:19 +10:00
Doyle a94c1587d1 Remove sychronization check on receipt of uptime
If a peer views the destination peer as not synchronizing, then the
destination peer should just accept the uptime proof, rather than accept
it and then conditionally relay it depending on whether or not you are
synchronizing at the point of attempting to relay (which you could
transition into synchronizing state interim between accepting and
attempting to relay the proof).
2019-09-19 16:58:09 +10:00
Doyle 409cf4faee Relay proofs back to source, but don't relay your proof again
Otherwise we get into a ping-pong situation as follows
Node1 sends uptime ->
Node2 receives uptime and relays it back to Node1 for acknowledgement ->
Node1 receives it, handle_uptime_proof returns true to acknowledge ->
Node1 tries to resend to the same peers again

Instead, if we receive our own uptime proof, then acknowledge but don't
send on. If the we are missing an uptime proof it will have been
submitted automatically by the daemon itself instead of using my own
proof relayed by other nodes.
2019-09-18 15:37:32 +10:00
Doyle aedf023dc9 Add message on receipt of your own uptime proof from network 2019-09-18 15:21:59 +10:00
Doyle cbd22735d7
Add hard fork date for testnet to v13 (#833) 2019-09-13 16:31:58 +10:00
Doyle adc7c9aa02
Don't verify checkpoints until after HF13 (#834) 2019-09-13 13:13:40 +10:00
Doyle 54af5572ea
Don't allow duplicate ip changes for the same height (#831) 2019-09-11 16:14:24 +10:00
Doyle 50228e2a0e Merge branch 'dev' into AlternativeQuorums 2019-09-10 12:31:03 +10:00
Doyle 39987729ab
Update soft-forking checks to be effective HF13 (#825)
Code review

m_oldest_allowable_alternative_block -> m_immutable_height
2019-09-10 10:04:44 +10:00
Doyle 0b92e9ba89 Code review 2019-09-09 11:14:19 +10:00
Doyle 8cf7e0f5ca Add migration code for service node state format updates
Also gate some troublesome slow changes that slow down pre-hf12 changes
2019-09-06 14:41:55 +10:00
Doyle d2b619ea9a Revamp core_tests linear_chain_generator to support state_t
Remove using crypto, using cryptonote, using epee in chaingen

Add loki prefix to data structures, cleanup warts

Remove the need for get_static_keys(), remove useless functions and
attempt to remove some pointless function overloading that puts too many
levels of indirection and makes it harder to follow code execution.

Rehaul tests further, persist test generator when replaying through core

The class used to generate the testing events to replay through core is
now preserved. This subtle change simplifies a big chunk of core testing
in that we can remove the need for using callbacks to track events in
the events vector.

Add preliminary checkpoint/alt service node tests

Allow inlining of fail cases for test conditions

Add more checkpoint core tests

add_block in generator is parameterised with checkpoints

Add test to check chain with equal checkpoints

Add test for chain reorging when recieving enough votes for checkpoint
2019-09-06 14:41:55 +10:00
Doyle 35c84359d4 Handle quorums needing to be stored in the previous state_t 2019-09-06 14:41:55 +10:00
Doyle 62c0da64e3 Remove Blockchain dependency in service_node_list::state_t 2019-09-06 14:41:55 +10:00
Doyle b4efc82e6b Remove using select_winner twice in block reward check
Also promote the std::pair of <address, portions> to a struct for
readability.

Readjust block_winner to ensure test case passes
2019-09-06 14:41:55 +10:00
Doyle 4724a7436f Incorporate checkpoint validation into syncing step
Fix checkpoint verification check and add messages
2019-09-06 14:41:55 +10:00
Doyle 15ff209db0 Add alt block added hook
Move service node list methods to state_t methods

Add querying state from alt blocks and put key image parsing into function

Incorporate hash into state_t to find alt states

Add a way to query alternate testing quorums

Rebase cleanup
2019-09-06 14:41:55 +10:00
Maxim Shishmarev b91e690a62 Add storage server reachability field; test it with quorums (#820)
* Add storage server reachability field; test it with quorums

* Improve method names

* Address review comments
2019-09-02 13:06:15 +10:00
Doyle add6051a8f
make get_checkpoints_range faster by using lmdb cursor as iterator (#819)
* make get_checkpoints_range faster by using lmdb cursor as iterator

* Update clamp to be more compliant with convention
2019-08-30 13:54:49 +10:00
Doyle 9fe3097e4d
Separate long term service node state into archive from recent history set (#815) 2019-08-27 13:28:46 +10:00
Maxim Shishmarev 14881db565 Fix decommissioned snodes not leaving swarms (#812) 2019-08-22 13:14:25 +10:00
Doyle 35fa83a4dc
Merge pull request #803 from Doy-lee/ServiceNodeWinnerIncorrectFix
Service node winner incorrect fix
2019-08-17 15:07:03 +10:00
doyle 1905836c3a Code review
only_serialize_quorums should set only_store_quorums flag

Simplify detach logic since we don't need constants anymore

Move state storage comment to ::store

Fix off by one in detach on recent states
2019-08-17 14:59:45 +10:00
Doyle 83f69a791c Serialize long term storage separate from short term 2019-08-17 14:55:42 +10:00
Doyle f009d31b90 Don't version code because adding only_loaded_quorums breaks compat 2019-08-17 14:55:42 +10:00
Doyle 7ab6b16c6b Fix service node winner incorrect error
If we are re-deriving states we still also need the historical quorums
such that we can process incoming blocks with state changes. Without it,
state changes will be ignored and skipped causing inconsistent state in
the service node list.

This mandates a rescan and stores the most recent in state_ts, and only
just quorums for states preceeding 10k intervals. We can no longer store
the 1st most recent state in the DB as that will be missing quorums as
well for similar reason when rederiving on start-up.
2019-08-17 14:55:42 +10:00
Jason Rhinelander 24f26bd184 Fix 4.0.4 uptime proof transmission after recomm
When a node gets recommissioned in 4.0.4 we reset its timestamp to the
current time to delay obligations checks for newly recommissioned nodes,
but this reset caused problems:

- the code runs not only when a fresh block is received, but also when
  syncing or rescanning, and so time(NULL) gets used to update the
  node's timestamp even if it is an old record, and since proof info is
  shared across states, the affects the current state.

- as a result of the above, a just-rescanned node that has been
  decommissioned at some point in the past will think it has just sent a
  proof, and so won't send any proofs for an hour.

- A just-rescanned node won't accept or relay any proofs for any node
  that was recommissioned in its scan for the first half an hour, but this
  lack of relaying can cause chaos in getting uptime proofs out across the
  network, especially while we still have 4.0.3 nodes that need it.

To address the first issue, this switches the recommissioning to use the
block timestamp rather than the current timestamp.  This *will* be
slightly delayed in the case of current blocks (since a block timestamp
is the time the pool *started* working on the block, which is generally
the time the previous block was found on the network), but even with an
exceptionally long block delay (e.g. 20 minutes) we are still fending
off obligations checks for 1h40m.

That would partially fix issues 2 and 3, but we actually don't want a
recommissioning to look like a received uptime proof for a couple
reasons:

- When we haven't actually received an uptime proof it's confusing to
  report that we have (at the recommission time) and may mask an
  underlying issue of a node that isn't actually sending proofs for some
  reason (which might be more common for a node that has just been
  decommissioned/recommissioned).  There's also a related weird state
  here for nodes that have come on recently: they think the SN is
  active, but have 0's for IP and storage server port.

- 4.0.3 nodes don't get the updated timestamp and so really need the
  proof to come through even when the 4.0.4 nodes don't think it's
  important/acceptable.

So to also fix these, this commits adds an "effective_timestamp" to the
proof info: if it is larger than the actual timestamp field, we use it
instead of the actual one for obligations checking.  On a recommission,
we update only the effective field so that we can delay obligations
checking for a couple of hours without delaying actual proofs info going
over the network.
2019-08-16 15:04:49 -03:00
Doyle 5f1c18ab34 Remove rollback code and old serialization method, mandate rescan 2019-08-16 15:34:27 +10:00
Doyle 8fdf2a7819 Fix service node winner incorrect error
If we are re-deriving states we still also need the historical quorums
such that we can process incoming blocks with state changes. Without it,
state changes will be ignored and skipped causing inconsistent state in
the service node list.

This mandates a rescan and stores the most recent in state_ts, and only
just quorums for states preceeding 10k intervals. We can no longer store
the 1st most recent state in the DB as that will be missing quorums as
well for similar reason when rederiving on start-up.
2019-08-16 15:30:36 +10:00
Jason Rhinelander 0cea0970e9 Print good/bad winners in incorrect winner error 2019-08-15 20:29:35 -03:00
Doyle 9ab22f7404
Assert should be after info.is_active() is true (#799) 2019-08-15 16:03:32 +10:00
Doyle 84993544e1 Regression core tests, missing txs on fork, segfault on detach
We can't delete all state change TX's on blockchain increment incase the
state change came from an alternative block. We must keep them around
even if they would conflict with another state change because if we
switch chains, they need to be present for the block to still be
considered valid.

Add a .size() check when checking for is_migrated_from_v403 because when
reinitialise is true, m_state_history does not necessarily have any
elements (in tests atleast).
2019-08-13 13:31:14 +10:00
Jason Rhinelander 7bc764588b Fix storage states over-reserved size 2019-08-13 00:05:21 -03:00
Jason Rhinelander 59a2175f47 Use shared_ptr storage for service_node_info
This converts the stored service_node_info value into a
`shared_ptr<const service_node_info>` rather than a plain
`service_node_info`.  This yields a huge performance benefit by
significantly eliminating the vast majority of service_node_info
construction, destruction, and copying.

Most of the time when we copy a service_node_info nothing in it has
changed, which means we're storing exactly the same thing; this means an
extra construction for every SN info on every block *and* an extra
destruction when we cull old stored history.  By using a
shared_ptr, the vast majority of those constructions and destructions
are eliminated.

The immediately previous commit (upon which this one builds) already
reduced a full rescan from 180s to 171s; this commit further reduces
that time to 104s, or about 42% reduced from the rescan time required
before this pair of commits.  (All timings are from the dev.lokinet.org
box, tested over multiple runs with the entire lmdb cached in memory).

With the shared_ptr approach, we only make a copy when a change is
actually needed: because of infrequent (at the per-SN level) events like
a state_change, received reward, contribution, etc.  The contained
reference is deliberately `const` so that values are not changeable;
there's a new function that does an explicit copying duplication,
returning the new non-const and storing the const ref in the shared
pointer.

Related to this is a small change (and fix) to how proof info and
public_ip/storage_port are stored: rather than store the values in the
service_node_info struct itself, they now gets stored in a shared_ptr
inside the service_node_info that intentionally gets shared among all
copies of the service_node_info (that is, a SN info copy deliberately
copies the pointer rather than the values).  This also moves the ip/port
values into the proof struct, since that seemed much easier than
maintaining a separate shared_ptr for each value.

Previously, because these were stored as values in the service_node_info
they would actually get rolled back in the event of a reorg, but that
seems highly undesirable: you would end up rolling back to the old
values of the uptime proof and ip address (for example), but that should
not happen: those values are not dependent on the blockchain and so
should not be affected by a reorg/rollback.  With this change they
aren't since there is only one actual proof stored.

Note that the shared storage here only applies to in-memory states;
states loaded from the db will still be duplicated.
2019-08-11 22:35:00 -03:00
Jason Rhinelander d4635e5344 Optimizations and simplifications
This commit makes various simplifications and optimizations, mainly in
the service node list code.

All in all, this shaves about 5% of the CPU time used for re-processing
the entire service node list.

In particular:

- changed m_state_history from a std::vector to a std::set that sorts on
  height.  This is responsible for the bulk of the CPU reduction by
  significant reducing the amount of work required for checkpoint
  culling, which has to shuffle a lot of `state_t`s around when removing
  from the midde of a vector.

- the above also allows replacing the binary-search `std::lower_bound`
  complexity with a much simpler `find()`.

- since the history is now always sorted, removed the error messages
  related to sorting that either can't happen (on store) or don't matter
  (on load).

- Added some converting constructors to simplify code (for example, a
  `state_t` can now be very usefully constructed from an r-value
  `state_serialized`).

- Many construct + moves (and a couple construct + copy) were replaced
  with in-place constructions.

- removed some unused variables
2019-08-11 22:35:00 -03:00
Doyle 74ee386f6c
Make 4.0.4 service node blob backwards compatible (#786)
Ordinarily on shut down we would discard the quorum states that are
converted and serialised once on initial start up with the new software.
If this is lost, we would not be able to process incoming votes or any
state changes and essentially halt without forcing a way to rescan the
service node list back to a consistent state.
2019-08-12 09:53:05 +10:00
Doyle a0c327bcdf
Handle multi state changes in the pool (#781)
* Update state transition check to account for height and universally set timestamp on recommission

Reject invalidated state changes by their height after HF13

* Prune invalidated state changes on blockchain increment

Simplify check_tx_inputs for state_changes by using service node list

Instead of querying the last 60 historical blocks for every transaction,
use the service node list and determine the state of the service node
and if it can transition to its new state.

We also now enforce at hardfork 13, that the network cannot commit
transactions to the network if they would have been invalidated by
a newer state change that already is already on the blockchain.

This is backwards compatible all the way back to hardfork 9.

Greatly simplify state change tx pruning on block added

Use the new stricter rules for pruning state changes in the txpool

We can do so because pruning the TX pool won't cause issues at the
protocol level, but the more people we can upgrade the better network
behaviour we get in terms of propagating more intrinsically correct
ordering of state changes to other peers.

* Don't generate state changes if not valid, disallow voting if node is non-votable
2019-08-12 09:48:47 +10:00
Doyle fcbe578067
Remove boundary HF12 uptime proof code (#785) 2019-08-09 12:52:39 +10:00
Doyle 17a0b7c381
Only store service node list every 10k AND Height-N blocks (#761)
* Store the oldest state in the MAX_SHORT_TERM_STATE_HISTORY window

Rederive the rest on startup, this way we only need to store states for
10k intervals + 1 state every block temporarily removing the overhead of
having to serialise 30+(every 10k interval) states every block which was
taking upto 15ms~ per block on mainnet.

* Store quorums into the state class

This is setup work to allow only storing one service node list state and
rederiving the rest of the state at runtime in memory without having to
waste space storing the extra 360 quorums every block which is the
biggest bottleneck with syncing the blockchain adding around ~15ms per
block after Service Nodes are activated.

* Derive quorums using correct state, record duration of scan

* Don't fail load if quorums empty, save progress on rescan

Storing quorums in quorum states is now optional because quorums are
stored with the state objects. Quorums are only stored in the dedicated
data structure if you requested to store quorum history so for most
users this will be empty.

* Store state upto quorum lifetime to match previous behaviour

* Make store every 10k states more robust

* Corectly use lower bound to find quorum

* Code review
2019-08-02 11:07:37 +10:00
Doyle 1155232048
Demote error message for missing quorum in handling non-standard txes (#769) 2019-07-31 12:22:07 +10:00
Doyle ead8997e21
Don't query the blockchain for hardfork which causes major slowdown (#764) 2019-07-27 00:55:39 +10:00
Doyle 05a7b97db1
Fix off by 1 when checking state_t heights causing sync failure (#759) 2019-07-25 14:35:36 +10:00
Doyle 234d2eeb16 Guard against invalid serialisation and change deque to vector 2019-07-22 15:16:29 +10:00
Doyle 4a30aecf36 Fix pop-blocks edge case before the first 10k interval 2019-07-22 14:21:35 +10:00
Doyle 7f9b5837ac ~5ms per block speed up, make serialised struct static
Avoid constant reallocation.
2019-07-22 12:36:14 +10:00
Doyle 707f43fe58 Store historical states for every 10k blocks and better detach 2019-07-22 12:36:12 +10:00
Doyle e7f44e4554 Increase the age of service node rollbacks we keep
Increase the range to mitigate frequent rescanning when reorganising,
technically with checkpointing we only need up to the 2nd oldest
checkpoint, but I'm sure there's some case we're forgetting that would
benefit from a larger rollback range.

Also considering there were only 160 events in the month with a mass
deregistration- keeping 2 days worth of rollbacks around is not going to
be too expensive.

Start storing service node list state for past blocks

Separate quorums and reduce storage to 1 days worth

Don't use pointer, declare m_state by value

Fix rebase to dev

Fix double serialise of service node state and expensive blob copy

std::move quorums into storage, reserve vector size

Don't +1 checkpoint finality height

Code review

Code review 2
2019-07-22 12:35:47 +10:00
Jason Rhinelander ae1adf38b2 Fix rebase mistake from #702 (#751) 2019-07-18 10:12:25 +10:00
Jason Rhinelander 5b819ba558 (Low priority) Add disabled-by-default quorum storage support (#702)
* Add disabled-by-default quorum storage support

This adds support for storing N blocks of recent expired quorum state
history in lokid (and dumping to/from the lmdb).

This isn't useful for regular nodes (and that's why we don't store it)
but is incredibly useful for the block explorer to be able to report
*which* node got deregistered/decommissioned/etc. by a given
state_change tx.

* Fix quorum states copy
2019-07-17 16:01:54 +10:00
Jason Rhinelander 0bac540cdf Push buffer subtraction into get_testing_quorum
As recent bugs have demonstrated, requiring the caller to have offset
the quorum by 11 is highly error prone.  This commit moves the offset
into `get_testing_quorum` itself so that any caller asking for the
quorum for vote height 12345 just asks for it rather than having to
remember to subtract 11 to get at the correct generating block.
2019-07-12 23:24:53 -03:00
Doyle 30fc4612b9 Move the refill of votes to recommission because we still update vote counts during decommission 2019-07-12 15:00:57 +10:00
Jason Rhinelander 22970cd51e Skip non-interval height checkpoints when loading
Because of how the serialization code works, these gets stored as
quorums with empty members, but we don't want them at all so just skip
loading them when the height says it should be empty.
2019-07-11 20:51:00 -03:00
Jason Rhinelander 846e2e03d6 Only create checkpoint quorum for heights divisible by CHECKPOINT_INTERVAL
This was causing mass checkpoint deregistrations for missed votes
because 3/4 of the checkpointing "quorums" being identified were not
(and not supposed to be) voted on.
2019-07-11 18:27:32 -03:00
Doyle fd0b2bb3db
Merge pull request #712 from Doy-lee/CheckpointingFixes
Switch to a circular buffer for recording checkpointing votes
2019-07-11 16:07:58 +10:00
Doyle 09ce929a91 Reset uptime to 0 on decommission, so node must submit uptime to recommission 2019-07-11 14:21:59 +10:00
Doyle 3571d9e524 Update softfork date for testnet quorum change 2019-07-11 14:21:59 +10:00
Doyle bdf8829f6b Switch to a circular buffer for recording checkpointing votes 2019-07-11 14:21:59 +10:00
Doyle ca37156c38
Allow state changes if non conflicting states (#718)
* Only dupe check state changes against the latest valid change

* Check the service node info for dupe state change

* Gate dupe state changes behind HF12

* Actually properly gate dupe state change and revert breaking changes

* Use is_decommissioned() to get service node state, change msg log level
2019-07-09 13:59:42 +10:00
Doyle 17ba8d88cf
Resolve pubkeys from state change in dupe check in the tx_pool (#707)
* Resolve public keys from state changes for the TX pool to avoid dupes

* Update copy pasted error message for quorum index OOB

* Code review
2019-07-04 17:25:02 +10:00
Jason Rhinelander c64121f224 Fix segfault on exit with bad SN args (#690)
service_node_list::store() was called on destruction, but destruction
can happen before it is set up with the database pointer (hence the
segfault).  Fix it to just silently return if not set up yet.
2019-06-29 14:25:52 +10:00
Doyle e6b5ef558a
Service node checkpointing deregistration (#672)
* Add deregistration of checkpoints by checking how many votes are missed

Move uptime proofs and add checkpoint counts in the service_node_list
because we typically prune uptime proofs by time, but it seems we want
to switch to a model where we persist proof data until the node expires
otherwise currently we would prune uptime entries and potentially our
checkpoint vote counts which would cause premature deregistration as the
expected vote counts start mismatching with the number of received
votes.

* Revise deregistration

* Fix test breakages

* uint16_t for port, remove debug false, min votes to 2 in integration mode

* Fix integration build
2019-06-27 17:05:44 +10:00
Jason Rhinelander 91def132f1 Replace growable vector with fixed-size array
We don't really need to know the IP history; it's sufficient just to
know that there were at least two different recent IPs, so just store
two.
2019-06-26 23:25:52 -03:00
Jason Rhinelander 6130c1b976 Add penalty for switching IPs
This adds a new obligations quorum vote "ip_change_penalty" that gets
triggered if the quorum has received multiple IPs advertised in uptime
proofs from a service node in the past 24 hours.  Upon reception of such
a transaction the SN gets bumped to the bottom of the reward list.
2019-06-26 01:24:59 -03:00
Jason Rhinelander 661bfe3f1b Fix recommissioning not reinserting SN at the back of the reward list 2019-06-26 01:14:52 -03:00
Jason Rhinelander 3489ae1e72 Replace is_fully_funded() calls with is_active()
The vast majority of the time `is_active()` is what we actually want.
Most of these are just a name change, but there is also one important
fix here to the next-winner list which needs to use active, not
fully-funded.
2019-06-26 01:14:52 -03:00
Doyle fdc340b0ee
Service node checkpointing: rebased on relaxed deregistration (#662)
* core: do not commit half constructed batch db txn

* Add defer macro

* Revert dumb extra copy/move change

* Fix pop_blocks not calling hooks, fix BaseTestDB missing prototypes

* Merge ServiceNodeCheckpointing5 branch, syncing and integration fixes

* Update tests to compile with relaxed-registration changes

* Get back to feature parity pre-relaxed registration changes

* Remove debug changes noticed in code review and some small bugs
2019-06-26 14:00:05 +10:00
Jason Rhinelander 6d6541670e Relax deregistration rules
The replaces the deregistration mechanism with a new state change
mechanism (beginning at the v12 fork) which can change a service node's
network status via three potential values (and is extensible in the
future to handle more):

- deregistered -- this is the same as the existing deregistration; the
SN is instantly removed from the SN list.
- decommissioned -- this is a sort of temporary deregistration: your SN
remains in the service node list, but is removed from the rewards list
and from any network duties.
- recommissioned -- this tx is sent by a quorum if they observe a
decommissioned SN sending uptime proofs again.  Upon reception, the SN
is reactivated and put on the end of the reward list.

Since this is broadening the quorum use, this also renames the relevant
quorum to a "obligations" quorum (since it validates SN obligations),
while the transactions are "state_change" transactions (since they
change the state of a registered SN).

The new parameters added to service_node_rules.h control how this works:

    // Service node decommissioning: as service nodes stay up they earn "credits" (measured in blocks)
    // towards a future outage.  A new service node starts out with INITIAL_CREDIT, and then builds up
    // CREDIT_PER_DAY for each day the service node remains active up to a maximum of
    // DECOMMISSION_MAX_CREDIT.
    //
    // If a service node stops sending uptime proofs, a quorum will consider whether the service node
    // has built up enough credits (at least MINIMUM): if so, instead of submitting a deregistration,
    // it instead submits a decommission.  This removes the service node from the list of active
    // service nodes both for rewards and for any active network duties.  If the service node comes
    // back online (i.e. starts sending the required performance proofs again) before the credits run
    // out then a quorum will reinstate the service node using a recommission transaction, which adds
    // the service node back to the bottom of the service node reward list, and resets its accumulated
    // credits to 0.  If it does not come back online within the required number of blocks (i.e. the
    // accumulated credit at the point of decommissioning) then a quorum will send a permanent
    // deregistration transaction to the network, starting a 30-day deregistration count down.

This commit currently includes values (which are not necessarily
finalized):
- 8 hours (240 blocks) of credit required for activation of a
decommission (rather than a deregister)
- 0 initial credits at registration
- a maximum of 24 hours (720 blocks) of credits
- credits accumulate at a rate that you hit 24 hours of credits after 30
days of operation.

Miscellaneous other details of this PR:

- a new TX extra tag is used for the state change (including
deregistrations).  The old extra tag has no version or type tag, so
couldn't be reused.  The data in the new tag is slightly more
efficiently packed than the old deregistration transaction, so it gets
used for deregistrations (starting at the v12 fork) as well.

- Correct validator/worker selection required generalizing the shuffle
function to be able to shuffle just part of a vector.  This lets us
stick any down service nodes at the end of the potential list, then
select validators by only shuffling the part of the index vector that
contains active service indices.  Once the validators are selected, the
remainder of the list (this time including decommissioned SN indices) is
shuffled to select quorum workers to check, thus allowing decommisioned
nodes to be randomly included in the nodes to check without being
selected as a validator.

- Swarm recalculation was not quite right: swarms were recalculated on
SN registrations, even if those registrations were include shared node
registrations, but *not* recalculated on stakes.  Starting with the
upgrade this behaviour is fixed (swarms aren't actually used currently
and aren't consensus-relevant so recalculating early won't hurt
anything).

- Details on decomm/dereg are added to RPC info and print_sn/print_sn_status

- Slightly improves the % of reward output in the print_sn output by
rounding it to two digits, and reserves space in the output string to
avoid excessive reallocations.

- Adds various debugging at higher debug levels to quorum voting (into
all of voting itself, vote transmission, and vote reception).

- Reset service node list internal data structure version to 0.  The SN
list has to be rescanned anyway at upgrade (its size has changed), so we
might as well reset the version and remove the version-dependent
serialization code.  (Note that the affected code here is for SN states
in lmdb storage, not for SN-to-SN communication serialization).
2019-06-23 22:37:53 -03:00
Jason Rhinelander 4f721c0098 Make tx type and version scoped enums
This converts the transaction type and version to scoped enum, giving
type safety and making the tx type assignment less error prone because
there is no implicit conversion or comparison with raw integers that has
to be worried about.

This ends up converting any use of `cryptonote::transaction::type_xyz`
to `cryptonote::transaction::txtype::xyz`.  For version, names like
`transaction::version_v4` become `cryptonote::txversion::v4_tx_types`.

This also allows/includes various other simplifications related to or
enabled by this change:
- handle `is_deregister` dynamically in serialization code (setting
  `type::standard` or `type::deregister` rather than using a
  version-determined union)
- `get_type()` is no longer needed with the above change: it is now
  much simpler to directly access `type` which will always have the
  correct value (even for v2 or v3 transaction types).  And though there
  was an assertion on the enum value, `get_type()` was being used only
  sporadically: many places accessed `.type` directly.
- the old unscoped enum didn't have a type but was assumed castable
  to/from `uint16_t`, which technically meant there was potential
  undefined behaviour when deserializing any type values >= 8.
- tx type range checks weren't being done in all serialization paths;
  they are now.  Because `get_type()` was not used everywhere (lots of
  places simply accessed `.type` directory) these might not have been
  caught.
- `set_type()` is not needed; it was only being used in a single place
  (wallet2.cpp) and only for v4 txes, so the version protection code was
  never doing anything.
- added a std::ostream << operator for the enum types so that they can be
  output with `<< tx_type <<` rather than needing to wrap it in
  `type_to_string(tx_type)` everywhere.  For the versions, you get the
  annotated version string (e.g. 4_tx_types) rather than just the number
  4.
2019-06-19 17:47:03 -03:00
Doyle 9f12ae0c0a
Service Node Checkpointing: More reliable transportation of votes, culling of old checkpoints (#632)
* Initial updates to allow syncing of checkpoints in protocol_handler

* Handle checkpoints in prepare_handle_incoming_blocks

* Debug changes for testing, cancel changes later

* Add checkpoint pruning code

* Reduce DB checkpoint accesses, sync votes for up to 60 blocks

* Remove duplicate lifetime variable

* Move parsing checkpoints to above early return for consistency

* Various integration test fixes

- Don't try participate in quorums at startup if you are not a service node
- Add lock for when potentially writing to the DB
- Pre-existing batch can be open whilst updating checkpoint so can't use
  DB guards
- Temporarily emit the ban message using msgwriter instead of logs

* Integration mode bug fixes

- Always deserialize empty quorums so get_testing_quorum only fails when
an actual critical error has occurred
- Cache the last culled height so we don't needlessly query the DB over
the same heights trying to delete already-deleted checkpoints
- Submit votes when new blocks arrive for more reliable vote
transportation

* Undo debug changes for testing

* Update incorrect DB message and stale comment
2019-06-13 18:01:08 +10:00
Maxim Shishmarev 0c23035562 Incorporate service node ip address into uptime proofs (#622)
* Incorporate service node ip address into uptime proofs; expose them using rpc

* Check that storage server port is specified in service-node mode

* Remove problematic const, rename argument name for storage port, update comments

* Validate ip address when receive uptime proof

* Better argument names and descriptions
2019-06-04 14:15:16 +10:00
Doyle 8af377d2b8
Unify and move responsibility of voting to quorum_cop (#615)
* Unify checkpointing and uptime quorums

* Begin making checkpoints cull old votes/checkpoints

* Begin rehaul of service node code out of core, to assist checkpoints

* Begin overhaul of votes to move resposibility into quorum_cop

* Update testing suite to work with the new system

* Remove vote culling from checkpoints and into voting_pool

* Fix bugs making integration deregistration fail

* Votes don't always specify an index in the validators

* Update tests for validator index member change

* Rename deregister to voting, fix subtle hashing bug

Update the deregister hash derivation to use uint32_t as originally set
not uint64_t otherwise this affects the result and produces different
results.

* Remove un-needed nettype from vote pool

* PR review, use <algorithms>

* Rename uptime_deregister/uptime quorums to just deregister quorums

* Remove unused add_deregister_vote, move side effect out of macro
2019-05-31 11:06:42 +10:00
Doyle 47e5ab779c Merge branch 'master' into BackportVersion3Patch6ToDev 2019-05-27 10:16:13 +10:00
Doyle 62a5d7abe5 Merge branch 'master' into dev 2019-05-27 10:05:14 +10:00
Doyle 170e841a12
Merge pull request #590 from Doy-lee/LokiMergeUpstream
Loki Merge Upstream 2019-05-07 2bf855e
2019-05-16 13:15:49 -04:00
Maxim Shishmarev 55c14bc672 sort sn list before applying updates; only assign swarms to fully funded snodes (#605) 2019-05-14 10:14:28 -04:00
Jason Rhinelander 27bc2aa11d Add more status messages when rescanning SNs (#603) 2019-05-12 10:07:52 +10:00
Doyle cbfb285630
Add key_image_unlock rollback event (#594)
* Add key_image_unlock rollback event

* Update printed text for failure to rollback image unlock
2019-05-09 18:25:45 +10:00
Doyle f19e937a05 Merge commit '46c477a' into LokiMergeUpstream 2019-05-01 17:32:57 +10:00
Doyle cc0d51078c Merge dev into upstream 2019-05-01 16:01:17 +10:00
Doyle 2715050afe Merge branch 'master' into dev 2019-04-26 13:56:40 +10:00
Doyle 0d4f2361ee
Ensure clear() also clears out the key image blacklist (#573) 2019-04-26 13:19:08 +10:00
Doyle 6024539e13
Allow storing multiple quorum types (#562) 2019-04-23 10:53:17 +10:00
Doyle 8d7d90c374 Merge commit 'ed6aa76cca69e4f6d0b84eb55ef7061dc4b6fc77' into LokiMergeUpstream 2019-04-11 15:08:26 +10:00
sachaaaaa 18208cf138 Bufferless swarm assignment logic (#518)
* unit tests and core tests for new queueless swarm assignment logic

* new bufferless swarm assignment logic

* new lines

* Address reviews

* Improve node stealing logic

* unit tests for additional functions

* get_excess_pool to return early if the threshold is lower than MIN_SWARM_SIZE

* Address reviews

* another round of reviews

* Move new_swarm_vector inside loop

* Rebase changes: moved get_new_swarm_id to service_node_swarm.cpp

* reserve vector size for all_ids in get_new_swarm_id
2019-04-08 12:03:47 +10:00
Doyle f154d309e6
Merge pull request #535 from loki-project/master
Backport 3.0.3 + 3.0.4 changes to dev
2019-04-03 15:36:28 +11:00
Maxim Shishmarev cbd57e0f95 new algorithm for generating ids for new swarms (#546)
* new algorithm for generating ids for new swarms

* fixed typo

* simplify algorithm
2019-04-03 12:40:58 +11:00
sachaaaaa 2109f3bd46 Remove nested shadowed variables (#542) 2019-04-01 15:03:56 +11:00
Doyle 33d17607c0
Account for delayed portions subtraction in converted registration args (#531) 2019-03-28 19:22:16 +11:00
Doyle 1b07d10493
Prepare registration dust failure minimal fix (#523)
* Add the minimal fix for prepare registration failing by dust amounts

* Add dust redistribution at final step

* Comment and clean up useless lines

* Add fudge factor for dust amounts

* Avoid possible overflow with portions if contributor 1 is insufficient
2019-03-28 18:18:23 +11:00
Doyle 75c521dd31
Merge pull request #516 from loki-project/master
Backport forced rescan on v3 change to dev
2019-03-21 16:32:13 +11:00
Doyle af66c7a847
Merge pull request #513 from Doy-lee/ForceRefresh
Force a rescan of the list
2019-03-21 13:47:41 +11:00
Doyle 57e6fe09f9 Force a rescan of the list 2019-03-21 13:46:32 +11:00
Doyle 24ac69fe36
Merge pull request #512 from loki-project/master
Backport master changes to dev
2019-03-21 12:55:40 +11:00
Doyle 9f232a159b Incorporate HF version into staking requirement algo change 2019-03-21 11:05:32 +11:00
Doyle 40c08391e1
Remove old line referring to autostake (not relevant) (#505) 2019-03-20 10:18:30 +10:00
Maxim Shishmarev aaa338838e core tests: update priority for service nodes selected as winners (#506) 2019-03-19 17:29:27 +10:00
Maxim Shishmarev 4be45d29cf Fix SN expirations and deregistrations in core tests (#493) 2019-03-18 09:48:50 +10:00
Doyle 29a793f9c0
Implement the relaxed contribution rules on the back end (#481)
* Implement the relaxed contribution rules on the back end

* Code review adjustments
2019-03-13 05:35:02 +00:00
Doyle 9ddba6de5f
Demote error messages not relevant to user to level 1 (#484) 2019-03-13 05:34:46 +00:00
Doyle bc47ecf05b Update version naming for infinite staking 2019-02-25 18:05:20 +11:00
Doyle 43436b081d
Infinite Staking Part 2 (#406)
* Cleanup and undoing some protocol breakages

* Simplify expiration of nodes

* Request unlock schedules entire node for expiration

* Fix off by one in expiring nodes

* Undo expiring code for pre v10 nodes

* Fix RPC returning register as unlock height and not checking 0

* Rename key image unlock height const

* Undo testnet hardfork debug changes

* Remove is_type for get_type, fix missing var rename

* Move serialisable data into public namespace

* Serialise tx types properly

* Fix typo in no service node known msg

* Code review

* Fix == to >= on serialising tx type

* Code review 2

* Fix tests and key image unlock

* Add command to print locked key images

* Update ui to display lock stakes, query in print cmd blacklist

* Modify print stakes to be less slow

* Remove autostaking code

* Refactor staking into sweep functions

It appears staking was derived off stake_main written separately at
implementation at the beginning. This merges them back into a common
code path, after removing autostake there's only some minor differences.

It also makes sure that any changes to sweeping upstream are going to be
considered in the staking process which we want.

* Display unlock height for stakes

* Begin creating output blacklist

* Make blacklist output a migration step

* Implement get_output_blacklist for lmdb

* In wallet output selection ignore blacklisted outputs

* Apply blacklisted outputs to output selection

* Fix broken tests, switch key image unlock

* Fix broken unit_tests

* Begin change to limit locked key images to 4 globally

* Revamp prepare registration for new min contribution rules

* Fix up old back case in prepare registration

* Remove debug code

* Cleanup debug code and some unecessary changes

* Fix migration step on mainnet db

* Fix blacklist outputs for pre-existing DB's

* Remove irrelevant note

* Tweak scanning addresses for locked stakes

Since we only now allow contributions from the primary address we can
skip checking all subaddress + lookahead to speed up wallet scanning

* Define macro for SCNu64 for Mingw

* Fix failure on empty DB

* Add missing error msg, remove contributor from stake

* Improve staking messages

* Flush prompt to always display

* Return the msg from stake failure and fix stake parsing error

* Tweak fork rules for smaller bulletproofs

* Tweak pooled nodes minimum amounts

* Fix crash on exit, there's no need to store on destructor

Since all information about service nodes is derived from the blockchain
and we store state every time we receive a block, storing in the
destructor is redundant as there is no new information to store.

* Make prompt be consistent with CLI

* Check max number of key images from per user to node

* Implement error message on get_output_blacklist failure

* Remove resolved TODO's/comments

* Handle infinite staking in print_sn

* Atoi->strtol, fix prepare_registration, virtual override, stale msgs
2019-02-14 12:12:57 +11:00
Doyle 1ad7658c77
Developer build target and minor static analysis (#409)
* Faster build target for development, some static analysis fixes

* Avoid linking wallet_rpc_server in dev build
2019-02-07 12:14:00 +11:00
doy-lee 0f560e0d62 Merge branch 'dev' into LokiMergeUpstream 2019-01-30 17:15:31 +11:00
doy-lee bebecbd0a6 Merge branch 'dev' into LokiMergeUpstream 2019-01-30 16:59:47 +11:00
doy-lee 590e4594fb Fix bulletproof broken tests 2019-01-30 14:44:00 +11:00
Doyle 8ae8d9804a
Fix assert compiled out in debug causing werror unused var (#405) 2019-01-29 11:34:33 +11:00
Doyle 3a7b6b59eb
Infinite Staking Part 1 (#387)
* Remove dead branches in hot-path check_tx_inputs

Also renames #define for mixins to better match naming convention

* Shuffle around some more code into common branches

* Fix min/max tx version rules, since there 1 tx v2 on v9 fork

* First draft infinite staking implementation

* Actually generate the right key image and expire appropriately

* Add framework to lock key images after expiry

* Return locked key images for nodes, add request unlock option

* Introduce transaction types for key image unlock

* Update validation steps to accept tx types, key_image_unlock

* Add mapping for lockable key images to amounts

* Change inconsistent naming scheme of contributors

* Create key image unlock transaction type and process it

* Update tx params to allow v4 types and as a result construct_tx*

* Fix some serialisation issues not sending all the information

* Fix dupe tx extra tag causing incorrect deserialisation

* Add warning comments

* Fix key image unlocks parsing error

* Simplify key image proof checks

* Fix rebase errors

* Correctly calculate the key image unlock times

* Blacklist key image on deregistration

* Serialise key image blacklist

* Rollback blacklisted key images

* Fix expiry logic error

* Disallow requesting stake unlock if already unlocked client side

* Add double spend checks for key image unlocks

* Rename get_staking_requirement_lock_blocks

To staking_initial_num_lock_blocks

* Begin modifying output selection to not use locked outputs

* Modify output selection to avoid locked/blacklisted key images

* Cleanup and undoing some protocol breakages

* Simplify expiration of nodes

* Request unlock schedules entire node for expiration

* Fix off by one in expiring nodes

* Undo expiring code for pre v10 nodes

* Fix RPC returning register as unlock height and not checking 0

* Rename key image unlock height const

* Undo testnet hardfork debug changes

* Remove is_type for get_type, fix missing var rename

* Move serialisable data into public namespace

* Serialise tx types properly

* Fix typo in no service node known msg

* Code review

* Fix == to >= on serialising tx type

* Code review 2

* Fix tests and key image unlock

* Add additional test, fix assert

* Remove debug code in wallet

* Fix merge dev problem
2019-01-25 14:15:52 +11:00
Doyle 101f1799eb
All Service Node Keys as Base32z RPC Call (#404)
* create get_service_node_list rpc call

currently does nothing, just a shell (that compiles)

* implement get_all_service_node_keys rpc call

* change get_all_service_node_keys rpc to use json rpc

also change the result to be vector of hex strings rather than binary keys

* Make nodes be plural, add hex to base32z for keys

Base32z for Lokinet internal usage

* Add option to return fully funded service nodes only

* Add nullptr check for conversion

* Add assert for incorrect usage of to base32z
2019-01-25 10:50:42 +11:00
Maxim Shishmarev 715f3d3698 allow less than 25% staking with large existing contributions (#339)
* allow less than 25% staking with large existing contributions

* remove readline depencency in wallet2
2019-01-22 10:44:30 +11:00
doy-lee cbd8193ba2 Fix destruction of service node list locking destroyed mutex 2019-01-18 16:16:23 +11:00
Maxim Shishmarev f9a1883ce2 Allow rpc call to get registration command to use human readable input (#343)
* Allow `prepare_registration` to work on command-line

This allows you to use `lokid prepare_registration` to generate a
registration command via RPC commands to the lokid, which is
particularly useful when the lokid is running non-interactively as a
system service.

Without this it is necessary to stop the lokid service, start it
manually, run the prepare_registration, then stop lokid and restart it
via the system service.

This also rewrites the description of the prepare_registration command to
remove the reference to saving to disk.

* make getting registration command more user-friendly via rpc

* better error message for get registration command rpc

* move get_service_node_registration_cmd into prepare_registration

* show error message on unsuccessful registration in simplewallet as well
2019-01-11 13:28:21 +11:00
doy-lee 7464673c64 Merge branch 'dev' into LokiMergeUpstream 2019-01-07 11:56:23 +11:00
Doyle baa100a345
Remove loki namespace and merge into service_nodes (#334) 2018-12-21 11:33:05 +11:00
doy-lee df347f23e0 Merge branch 'dev' into LokiMergeUpstream 2018-12-19 10:53:35 +11:00
Maxim Shishmarev 303ac6cdcb Add an rpc call for getting batched quorum states (#345)
* add rpc call for getting batched quorum states

* add rpc call to get all service nodes
2018-12-18 17:50:49 +11:00
doy-lee 5a4ca2c4b2 Replace gmtime calls with epee::misc_utils::get_gmt_time 2018-12-11 17:42:06 +11:00
doy-lee 40506127a4 Merge commit '7e957c1' into LokiMergeUpstream 2018-12-11 13:46:35 +11:00
Maxim Shishmarev d22055bfcd Implementation of swarms using a queue/buffer (#299)
* Added implementation of swarms using a buffer

* fixed bug in get_new_swarm_id

* use correct hf version

* removed comments
2018-11-16 10:32:56 +11:00