Commit Graph

625 Commits

Author SHA1 Message Date
Lorenz Leutgeb e4d23fe56a
cli: Introduce `cob [create|update]` 2025-04-28 16:19:02 +02:00
Fintan Halpenny a90aabb1fe node: rate limiter for channel reads
This introduces rate limits for the `ChannelReader` to limit DDoS attacks and
attempts to upload repositories that are larger than a node is will to permit.

The limiter sets the total number of bytes it is will to accept in a single
exchange, defaulting to 500MB. This means that initial fetches will prevent
large repositories, but is plenty for new packfile data to be sent in subsequent
fetch exchanges.

The limit can be configured within the node's config file, under the limits.
2025-04-25 20:27:51 +02:00
Fintan Halpenny 6dcd56275e
node: upload-pack inter-thread communication
If an upload-pack is of a considerable size, the writer thread will be busy
writing bytes to the receiving side. During this period, the receiving side will
not be sending any bytes.

To ensure that the reader does not exit while the writing thread is performing
writes, the reading side will check when the last time a write was performed. If
it has not reached the timeout, then it can continue attempting to read.
Otherwise, it will break out of the loop; killing the upload-pack process safely.
2025-04-23 16:25:17 +02:00
Fintan Halpenny 96637aca7e
chore: update rust-toolchain
Update the rust toolchain version to 1.85.

Note that this is not the latest, however, it is the latest available in nixpgs,
so this version is good enough for now.
2025-04-17 10:39:34 +02:00
Fintan Halpenny c847a16e14 radicle: return iterator types for db policies
Instead of collecting the results and returning the collection as an
iterator, introduce `FollowPolicies` and `SeedPolicies` types, which
implement `Iterator`.

Note that a call in `service` changed due to the lifetime borrow. It
simply `collect`s into a `Vec` for the time being.
2025-04-11 12:35:55 +02:00
Fintan Halpenny 7de82b5025 radicle: use `Alias` in `follow`
Ensure that the validated `Alias` type is used for inserting into the
policy database.
2025-04-11 12:35:55 +02:00
cloudhead 3ad84420bd
cli: Implement `rad cob migrate`
Adds the new `migrate` sub-command to migrate the COB database.
Checks for version when opening the COBs database and suggests the
command on out of date version.

Also adds the 2nd migration to the COBs cache, which updates the patch
JSON schema.
2024-11-22 21:27:45 +01:00
Fintan Halpenny 91914d9345
radicle: improve quorum copy
The `no quorum found` error can be opaque and lead to a lot of confusion. It is
better to give more information for this error so that it leads to easier
debugging, since it can be quite a common warning/error.

Instead of a single error, there are two cases provided: `NoCandidates` and
`Diverging`, to capture the two different variants of errors that can occur.

`Display` implementations are provided for both to provide more information.
2024-11-18 12:32:44 +01:00
Fintan Halpenny e412168be3
node: do not fail on `set_head`
The failure to set a HEAD ref, due to quourum, during a fetch should not fail
the fetch entirely.

Instead, log the success and failure, or return an error in other cases.

In fact, any failure to set the head could have resulted in notifications not
being sent, nor caching to occur.
2024-11-18 12:32:40 +01:00
Fintan Halpenny f9c3523183
e2e: improve flake in missing_delegate_default_branch
The `missing_delegate_default_branch` test was flakey and would fail checking if
Bob had the `rid` in storage.

This can be improved by waiting to ensure that Bob has a `RefsFetched` event
before performing the check.
2024-11-11 11:36:09 +01:00
Fintan Halpenny eb095c109b
fetch: allow missing default branch
When fetching, the validation logic would prune a delegate if it was missing the
default branch. Originally, this was done to prevent any broken state, but later
it was realised that this can be too restrictive and would not allow nodes to
recover into an improved state.

Remove this check so thtat nodes can still fetch from each other when the
default branch is missing.

An e2e test is added to show that nodes can still fetch from each other and
still receive other references, even when a delegate is missing their default
branch.
2024-11-06 16:47:01 +01:00
cloudhead f6aa46a283
cli: Try to connect to seeds specified as options
When fetching with a `--seed` specified on the CLI, try to connect to it
if not already connected.
2024-11-05 12:18:12 +01:00
Fintan Halpenny de1958fab0 radicle: refactor doc
The aim of this change is to make the `Doc` type more safe to use by approaching
the design via [Parse don't validate][[0]] approach.

The problem with the previous approach was that all field were `pub` and thus a
`Doc<Verified>` could easily be mutated and serialized. Granted, the code that
used the serialization would tend to verify the `Doc` first, however, this
approach *ensures* that only a verified `Doc` can be serialized. It also meant
that trying to add new data that would follow the parse approach would require
more generic parameters on top of the existing `PhantomData` parameter, i.e. we
need to do something like: `Doc<RawField, V> -> Doc<ValidField, V>`.

The new approach splits the type into two separate types: `RawDoc` and `Doc`.
The former is allowed to be mutated at will, and uses types that are less
strict. The latter is the valid type that can only be constructed by validating
a `RawDoc` (or the `initial` constructor). The `Doc` type's fields can then only
be accessed by read-only methods.

Solves the problems above by only allowing mutations to `RawDoc`, as well as,
new fields being added to `RawDoc` which are then validated via
`RawDoc::verified`.

[0]: https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/
2024-10-22 14:58:12 +01:00
cloudhead 989edacd56
Include new `rad/root` in signed refs
We ensure that a `rad/root` ref is included in the signed refs file
under `rad/sigrefs` for all remotes. This prevents a certain kind of
"grafting" attack where signed refs can be copied between repositories,
by having the peer sign over the identity root together with the data refs.

When verifying signed refs, we ensure that the ref is present and points
to an identity branch root that matches the repository identity containing
the signed refs.

Alternatives: lots of alternatives were considered, but this one doesn't
introduce any changes to the signing. The `rad/id/root` name was
considered but is invalid due to `rad/id`.
2024-10-21 16:32:49 +02:00
cloudhead 034eb41860
node: Ensure private RIDs don't leak in gossip
Though refs of private repositories are not announced publicly, they can
sometimes be relayed to nodes that are not in the allow list.

We fix this by always checking the visibility of a repository before
sending a refs announcement of it to a peer.

Discovered-by: Adrian Duke <adrian.duke@gmail.com>
2024-10-08 13:28:09 +02:00
Fintan Halpenny f244d89e56
node: check policy before visibility
In the `is_authorized` helper, the logic checks the policy and the visibility of
the repository.

If the policy is set to block, the function can return before getting the
repository and the identity document. This improves the check, since the
repository and identity document may be missing if the repository is blocked, so
it would return a different error other than the expected unauthorized error.
2024-09-12 17:47:55 +02:00
cloudhead a61affa9fb
node: Don't fail if we can't load a COB to cache
This change simply logs an error if there is a problem loading a COB
from storage, instead of bailing on the cache update process.

We also ensure that objects that can't be loaded from storage are
removed from the cache.
2024-08-09 11:59:53 +02:00
cloudhead c6a8a00055
Update `rust-toolchain` to 1.80
Fix new clippy warnings.
2024-08-06 14:37:21 +02:00
cloudhead 0e08452323
node: Ensure we don't prune our own routes
Filter out routing entries of our own node when prunning.
2024-08-06 14:17:12 +02:00
cloudhead 84e3ba1482
Validate project names properly in all places
We were only validating names when passed as a flag to `rad init
--name`.
2024-07-31 16:05:31 +02:00
Derick Eddington 1c46f19592 node: Fix `main` to print early errors
Otherwise, previously, when an error occurred before `logger::init()`
then no message about the error would be shown, because `log` ignores
messages generated before the logger is initialized.  E.g. when
`$RAD_HOME/config.json` is missing, invoking `radicle-node`
directly (i.e. not via `rad node start`) would fail without any
explanation being logged nor printed.  There also are some other
possible errors that can occur before `logger::init()` where no message
would've been shown.

The fix is to detect if the logger is enabled, which it won't be when it
hasn't been initialized, and if not then fallback to printing directly
to stderr.

To show the lower-level source of an error, like previously, and to
avoid now needing more conditionals with more format strings for all
possibilities of `err.source().is_some()` and `log_enabled!()`, the
"alternate" form (`{:#}`) of formatting `anyhow::Error` is now used.
This also introduces a change in behavior such that the entire chain of
source errors will now be shown, instead of only the first in the chain,
which seems more desirable for errors that cause fatal exiting of
`radicle-node`.  Note that this form still formats as only a single
line, like previously.

The prefix "Error: " is used for the new fallback printing, because in
this case it's not a log message (though, it might be written to the log
file), and because that prefix is consistent with how Rust errors that
cause immediate termination are usually printed directly.  For the
opposite case, the "Fatal: " prefix serves to distinguish it as being
fatal among the many other various log messages.

While it would be possible to instead return `anyhow::Result<()>` from
`main`, to achieve the printing of early errors (and exiting with
failure code), that would cause undesirable duplication, for non-early
errors that occur after `logger::init()`, of the error message where it
would be written to both the log file and to stderr which often is the
same as the log file.

Signed-off-by: Derick Eddington <kcired@pm.me>
2024-07-22 12:37:57 -07:00
Fintan Halpenny 29e681210b
node: test multiple offline inits
Ensure that when a node initialises multiple repositories, those
repositories can successfully be fetched by another peer once they
connect.

Signed-off-by: Fintan Halpenny <fintan.halpenny@gmail.com>
X-Clacks-Overhead: GNU Terry Pratchett
2024-07-22 10:48:10 +02:00
Lorenz Leutgeb 723e2741f5
node: Socket Activation with systemd 2024-07-22 10:44:22 +02:00
Derick Eddington 6f8d75a00d
node: Fix "signals" thread
Fix "signals" thread to continue upon other signals.

Otherwise, previously, when one of the non-shutdown signals was
received, that would cause the "signals" thread to finish, but then a
subsequent signal that should cause shutdown would not because the
thread no longer existed to do so.  E.g. when SIGHUP or SIGWINCH was
received first and ignored and then SIGTERM or SIGINT was received next,
the program should still be shutdown, but it wouldn't be.  This is now
fixed by simply looping to continue handling subsequent signals.

Logging is now done for:
- Receiving of SIGHUP, at log level `debug`, because the default action
  for that signal would be to terminate the process but that is not done
  by `radicle_signals`.  Someone sending that signal might want to debug
  why the process isn't being terminated.
- Disconnection of the signal-notifications channel, at log level
  `warn`, because, even though that should be impossible, if it somehow
  occurs then a warning is warranted.

Logging is not done for receiving of SIGWINCH, because the default
action for that signal would be to ignore it, which is what
`radicle-node` does.

A wildcard pattern is not used in the match arms, so that any future
changes to the `Signal` variants will require reviewing their handling
in the "signals" thread.

Signed-off-by: Derick Eddington <kcired@pm.me>
2024-07-22 10:38:42 +02:00
cloudhead 4a497fa66f
node: Move the session lookup outside the loop
Small code change that was not possible before.
2024-07-16 19:58:47 +02:00
cloudhead 189046bb5d
node: Randomize order of missing inventories 2024-07-16 19:58:47 +02:00
cloudhead aac23dde0c
node: Add a max capacity to fetch queues 2024-07-16 19:58:44 +02:00
cloudhead 3c1c35f259
node: Have multiple fetch queues
In the current design it's possible for one peer to fill the fetch queue
so that fetches from other peers are delayed.

To improve fairness, we move to a queue per peer. We then try to dequeue
from all peers in a random order for good measure.
2024-07-16 17:11:46 +02:00
cloudhead 27eff8095d
node: Don't depend on `gix` directly
This avoids pulling in the same dependency twice.
2024-07-16 15:57:27 +02:00
Alexis Sellier 4b65c22305
node: Fix logging on maintain connections
We were comparing the available peers with the target, so the comparison
would always succeed. We now compare it with the missing count, and use
debug logging since it's not something of concern yet.
2024-07-04 12:03:42 +02:00
cloudhead a79ca5e8fc
node: Constrain worker channel size
To avoid buffering large amounts of data in the process, we set the
worker channel size to `64`. Keep in mind that this is one
`ChannelEvent`, not one byte. `ChannelEvent::Data` can already contain
an arbitrary amount of data via its `Vec<u8>`, but in practice the
maximum is 8192, due to the use of `io::copy`.

We also remove an unused function, and move the flushing to the
`ChannelFlushWriter`.
2024-07-03 14:15:45 +02:00
cloudhead f7d8f1b8da
radicle: Distinguish seeding policy types
We create a separate type for the default seeding policy, that
serializes differently and is only used in the node configuraiton.
2024-06-26 11:57:04 +02:00
cloudhead bbb292c8e6
node: Don't panic on connection logic error
It seems like the underlying network library has a bug, and transport
handovers don't always happen.
2024-06-26 11:31:07 +02:00
cloudhead 765fc48c84
node: Fix edge case in persistent peer connections
If two peers are persistent for each other and experience
disconnections, it's possible that an outbound connection is turned into
an inbound one. But if we don't set that correctly, we'll eventually run
into a crash where an already connected peer re-connects, since the
disconnection logic checks for the link direction.
2024-06-24 14:29:00 +02:00
cloudhead f8c6cf8bf9
node: Fix Tor full-proxy mode support
We ensure that the global proxy is used as the connection address if
set.
2024-06-21 16:16:51 +02:00
cloudhead 6f91f2fb8f
clippy: Fix a few warnings 2024-06-20 15:10:39 +02:00
cloudhead 589c375624
build: Update env vars for build process
Use the standard `SOURCE_DATE_EPOCH` instead of `GIT_COMMIT_TIME`.
Set `GIT_HEAD` properly.
2024-06-20 10:47:50 +02:00
cloudhead 6598e77456
node: Log IP ban rejections 2024-06-19 20:38:23 +02:00
cloudhead dd7a0b3524
node: Revise fetch dequeue behavior
The fetch queue could sometimes not drain due to skipped fetches not
signaling that to the dequeue loop.

We now return a `bool` from `try_fetch` and keep trying to dequeue
fetches until one of them actually is initiated.

Additionally, we try to dequeue fetches at a regular interval
(`IDLE_INTERVAL`) to ensure that the queue is drained.
2024-06-19 15:31:00 +02:00
cloudhead 47c9f792e6
node: Handle connection state discrepancies
There are cases where the service state doesn't match the state
of the underlying wire protocol. We remedy this by transitioning peers
to a "connected" state when a message is received, if they are in a
"connecting" state.

The long term solution to this will likely be to merge the service and
protocol layer so that there are no inconsistencies.

The other case happens when a persistent peer is in "disconnected" state
and attempts an inbound connection to us.
2024-06-18 15:55:28 +02:00
cloudhead a1c9a927c2
node: Keep track of last announce separately
Fixes a bug where we try to announce the inventory over and over even
though there's nothing to announce.
2024-06-18 13:58:48 +02:00
cloudhead 484cf0220c
node: Don't panic if we can't decode announcements
Bubble up the error instead.

Also, as part of the migration, delete existing announcements that won't
decode properly.
2024-06-13 21:38:30 +02:00
cloudhead 83786fbd80
node: Add a more elaborate message extension test
This required adding a type parameter to `Frame` to be able to test
different message types.
2024-06-13 11:28:09 +02:00
cloudhead a89a300390
node: Pass `Alias` by reference 2024-06-13 11:28:09 +02:00
cloudhead 76edc0c523
node: Add agent and version to node announcement
1. Adds a user-agent string to the node announcement. This lets us keep
   track of what software version everyone is running. Especially useful
   to see what percentage of the network has upgraded to a new release.

  For old nodes, this defaults to the string `/radicle/`.

  The user agent format is roughly based on BIP 0014.

2. Advertize protocol version in node announcement.

  By advertizing the protocol version, nodes can decide to only connect to
  peers with a compatible version. The default and current version is `1`.

  We bundle these two changes as they both modify the node announcement
  and node database.

3. Swap the order of the fields in announcements to make the message
   extensible.
2024-06-13 11:28:09 +02:00
cloudhead 5c0d1b10e0
node: Make sure all channels we use are bounded
This prevents potential memory leaks. We also ensure that sends fail
instead of blocking, in case the channels are full.

Additionally, we add some metrics to report on channel size.
2024-06-12 19:00:23 +02:00
cloudhead 614f3e7b77
node: Keep private repos out of inventory
Since repos can go from public to private, make sure that we remove them
from the routing table on startup.
2024-06-12 15:36:34 +02:00
cloudhead 50379548c2
node: Don't announce inventory redundantly
Keep track of last announcement timestamp and don't re-announce if it
matches.
2024-06-12 15:36:34 +02:00
cloudhead a6b5c723c8
node: Rename certain functions for clarity 2024-06-12 15:36:34 +02:00
cloudhead b24f8a30d6
node: Update inventory when unseeding
Previously, our advertized inventory was not always correct: when we
stopped seeding a repository, it was still advertised as part of our
inventory until node restart, because the `Storage` type doesn't have
access to seeding policies.

In this change, we remove the concept of inventory from `Storage` and
make the authoritative place for it the routing table in our database.

To make this work, we have to add our node to the database on profile
creation, to not violate the foreign key constraint on the routing
table. Hence, the tests are changed to include our alias which is now
always available.
2024-06-12 15:36:34 +02:00