1
0
mirror of https://github.com/ACINQ/eclair.git synced 2024-11-20 02:27:32 +01:00
Commit Graph

656 Commits

Author SHA1 Message Date
Pierre-Marie Padiou
9507ba99fa
Fix regression in Commitments.availableForSend (#1107)
We must consider `nextRemoteCommit` when applicable.

This is a regression caused in #784. The core bug only exists when we
have a pending unacked `commit_sig`, but since we only send the
`AvailableBalanceChanged` event when sending a signature (not when
receiving a revocation), actors relying on this event to know the
current available balance (e.g. the `Relayer`) will have a wrong
value in-between two outgoing sigs.
2019-08-30 18:44:26 +02:00
araspitzu
74af0304bd
Move http APIs to subproject eclair-node (#1102)
* Move Service and FormParamExtractor to eclair-node

* Move dependency akka-http-json4s into eclair-node

* Move json serializers to eclair-node
2019-08-30 09:44:24 +02:00
Pierre-Marie Padiou
d67ba48fc0
Add a sync whitelist (#954)
We will only sync with whilelisted peer. If the whitelist is empty then
we sync with everyone.
2019-08-29 12:50:37 +02:00
araspitzu
8d1354a21d
Use unsigned comparison for 'maxHtlcValueInFlightMsat' (#1105)
* Use unsigned comparison for HtlcValueTooHighInFlight
2019-08-29 11:58:10 +02:00
Bastien Teinturier
46e4873562
Add more numeric utilities to MilliSatoshi (#1103)
Add comparisons and postfix operators.
Update most of the codebase to leverage those.
2019-08-29 07:19:01 +00:00
Pierre-Marie Padiou
8f7a415f5a
Rework router data structures (#902)
Instead of using two separate maps (for channels and channel_updates), we now use a single map, which groups channel+channel_updates. This is also true for data storage, resulting in the removal of the channel_updates table.
2019-08-28 16:58:49 +02:00
Pierre-Marie Padiou
2f42538660
Extended queries optional (#899)
This is the implementation of https://github.com/lightningnetwork/lightning-rfc/pull/557.

* Correctly handle multiple channel_range_replies

The scheme we use to keep tracks of channel queries with each peer would forget about
missing data when several channel_range_replies are sent back for a single channel_range_queries.

* RoutingSync: remove peer entry properly

* Remove peer entry on our sync map only when we've received
a `reply_short_channel_ids_end` message.
* Make routing sync test more explicit

* Routing Sync: rename Sync.count to Sync.totalMissingCount

* Do not send channel queries if we don't want to sync

* Router: clean our sync state when we (re)connect to a peer

We must clean up leftovers for the previous session and start the sync process again.

* Router: reset sync state on reconnection

When we're reconnected to a peer we will start a new sync process and should reset our sync
state with that peer.

* Extended Queries: use TLV format for optional data

Optional query extensions now use TLV instead of a custom format.
Flags are encoded as varint instead of bytes as originally proposed. With the current proposal they will all fit on a single byte, but will be
much easier to extends this way.

* Optional TLVs are represented as a list, not an optional list

TLVs that extend regular LN messages can be represented as a TlvStream and not an Option[TlvStream] since we don't need
to explicitely terminate the stream (either by preprending its length or using a specific terminator) as we do in Onion TLVs.

No TLVs simply means that the TLV stream is empty.

* TLV Stream: Implement a generic "get" method for TLV fields

If a have a TLV stream of type MyTLV which is a subtype of TLV, and MyTLV1 and MYTLV2 are both
subtypes of MyTLV then we can use stream.get[MyTLV1] to get the TLV record of type MYTLV1 (if any)
in our TLV stream.

* Use extended range queries on regtest and testnet

We will use them on mainnet as soon as https://github.com/lightningnetwork/lightning-rfc/pull/557 has been merged.

* Channel range queries: send back node announcements if requested (#1108)

This PR adds support for sending back node announcements when replying to channel range queries:
- when explicitly requested (bit is set in the optional query flag)
- when query flags are not used and a channel announcement is sent (as per the BOLTs)

A new configuration option `request-node-announcements` has been added in the `router` section. If set to true, we
will request node announcements when we receive a channel id (through channel range queries) that we don't know of.
This is a setting that we will probably turn off on mobile devices.

* Extended Channel Queries: add CL interop test
2019-08-28 15:03:55 +02:00
Bastien Teinturier
0b18abfc54
Typed cltv expiry (#1104)
Untyped cltv expiry was confusing: delta and absolute expiries really need to be handled differently.
Even variable names were sometimes misleading.
Now the compiler will help us catch errors early.
2019-08-26 13:15:33 +00:00
Pierre-Marie Padiou
a406d2fcea
Publish transactions during transitions (#1089)
Follow up to #1082.

The goal is to be able to publish transactions only after we have
persisted the state. Otherwise we may run into corner cases like [1]
where a refund tx has been published, but we haven't kept track of it
and generate a different one (with different fees) the next time.

As a side effect, we can now remove the special case that we were
doing when publishing the funding tx, and remove the `store` function.

NB: the new `calling` transition method isn't restricted to publishing
transactions but that is the only use case for now.

[1] https://github.com/ACINQ/eclair-mobile/issues/206
2019-08-26 15:02:56 +02:00
Fabrice Drouin
290ac3dbb2
Route computation: fix fee check (#1101)
* Route computation: fix fee check

Fee check during route computation is:
- fee is below maximum value
- OR fee is below amout * maximum percentage

The second check was buggy and route computation would failed when fees we above maximum value but below maximum percentage of amount being paid.
2019-08-20 15:56:56 +02:00
araspitzu
4929febbd1
Typed amounts (#1088)
* Type all amounts used in eclair

* Add eclair.MilliSatoshi class

* Use bitcoin-lib 0.14

* Add specialized codecs for Satoshi/MilliSatoshi

* Rename 'toSatoshi' to 'truncateToSatoshi' to highlight it's a precision-losing conversion
2019-08-07 17:37:38 +02:00
araspitzu
648415771e
Update list of commands in eclair-cli help (#1091)
* Add missing API endpoints to eclair-cli help
2019-07-29 10:32:05 +02:00
araspitzu
d88d376cf0
Use correct cost comparison when evaluating candidate channels (#1090)
* Use correct cost comparison when evaluating if the amount we're sending is within the range htlcMinimumMsat/htlcMaximumMsat of the candidate channel
2019-07-26 17:15:08 +02:00
araspitzu
131f50adf4
Configurable transaction confirmation target (#1083)
* Use feeEstimator in NodeParams, remove all calls to Globals.feeratePerKw

* Introduce FeeConf object and config block for confirmation targets, remove unused 'smartfeeNBlocks'

* Use a custom confirmation target for commitment transaction

* Use a custom confirmation target for funding transaction

* Use custom confirmation target for mutual close transaction

* Use custom confirmation target for claim transactions

* Add confirmation target block 144

* Use block target = 12 as default for claim transactions
2019-07-25 19:06:18 +02:00
Pierre-Marie Padiou
e62adf2dea
Made using/storing/sending consistent (#1082)
We know can now do `goto(STATE) using DATA storing() sending msg1 msg2`.
2019-07-24 10:03:44 +02:00
Bastien Teinturier
93d9369f90
Variable-length onion payloads (#976)
Add support for variable-length onion payloads at the Sphinx (cryptographic) layer.
This is currently unused as we keep using the legacy format by default (this will be changed in a later commit).
This commit also refactors quite heavily the Sphinx file.
2019-07-23 12:22:29 +02:00
Bastien Teinturier
189b11e9bf
Handle fulfill not acked upstream (#1079)
When we want to fulfill an HTLC but the upstream peer is unresponsive, we must close the channel if we get too close to the HTLC timeout on their side.
Otherwise we risk an on-chain race condition between our HTLC success transaction and their HTLC timeout transaction, which could result in a loss of funds.
2019-07-23 11:11:40 +02:00
Pierre-Marie Padiou
00df431472
Replace traits by bitfield for ChannelVersion (#1073)
We now store the channel version on a 8B bitfield prefixed by `0x01`
2019-07-15 13:11:20 +02:00
Bastien Teinturier
62946a0426
Switch varint codec to big-endian. (#1075) 2019-07-15 11:55:40 +02:00
Pierre-Marie Padiou
c1a7b4fe50
Added a channel version to Commitments object (#1059)
In a backward-compatible way, by using the fact that the first object of
a legacy `Commitments` was a public key, starting from 0x02 or 0x03.
2019-07-12 13:36:33 +02:00
Bastien Teinturier
1621e393dd
TLV improvements and full spec compatibility (#1069)
This PR adds support for truncated integers as defined in the spec.
The test vectors are updated to include all test vectors from rusty's spec PR.
It also provides many changes to the tlv and tlv stream classes:

- The tlv trait doesn't need a type field, the codec should handle that
- A TLV stream should be scoped to a specific subtrait of tlv
- Stream validation is done inside the codec instead of the tlv stream: it makes it more convenient for application layers to create tlv streams and manipulate them
2019-07-11 17:25:14 +02:00
araspitzu
5f4a2ebf3e
Wrap all routes in toStrictEntity (#1032)
* Wrap all routes in toStrictEntity
2019-07-09 16:49:55 +02:00
Fabrice Drouin
7d89dd01a1
Electrum: update checkpoints (#1067)
Checkpoints generated on July 9th 2019
2019-07-09 16:00:18 +02:00
Pierre-Marie Padiou
e5c5a4cfbc
Handle unknown fields in network announcements (#1047)
All the data contained in `node_announcement`, `channel_announcement`
and `channel_update` is to be included in the signature, including
unknown trailing fields. We were ignoring them, causing signature
verification to fail when there was unknown fields.

In the case of `channel_update` there is a backward compatibility issue
to handle, because when persisting channel data in state `NORMAL`, we
used to store the `channel_update` followed by other data, and without
prefixing it with size information.

To work around that we use the same trick as before, based on an
additional discriminator codec.
2019-07-08 17:14:01 +02:00
Bastien Teinturier
22548733e6
Add a few improvements to tlv. (#1065)
Hide some internals (tlvFallback, generictlv codecs).
Add a length-prefixed tlv stream codec.
2019-07-08 10:05:18 +02:00
Pierre-Marie Padiou
683294ee57
Use a RelayResult class instead of an Either (#1064)
It is functionnaly the same but it's cleaner and removes the need for
tuples in the success case.
2019-07-04 14:06:05 +02:00
Fabrice Drouin
bbe07c2c42
Set version to 0.3.2-SNAPSHOT (#1062) 2019-07-04 10:00:44 +02:00
Fabrice Drouin
6906ecb403
Set version to v0.3.1 (#1061) 2019-07-03 17:23:12 +02:00
araspitzu
20ea9d0e1d
Reject payments for expired invoices (#1057)
* Reject payments for expired invoices
2019-07-03 15:52:03 +02:00
Pierre-Marie Padiou
32145e8d6a
Connect immediately on restart, then wait (#1040)
* connect immediately on restart, then wait

This is to allow herd effect when we restart the app and have numerous
peers.

Also removed the unnecessary transition and cleaned up delay
computation.

* always reconnect immediately when disconnected

Whether we go to this state from startup, or after getting disconnected.

It makes the transition logic simpler, and the potential herd effect at
startup is inevitable anyway since our peers will try to reconnect too.

* add randomization when reconnecting

* randomize delay for first reconnection attempt after startup

* make some parameters configurable
2019-07-03 13:15:20 +02:00
Pierre-Marie Padiou
f724efaa76
Close the channel if the funding tx never confirms (#1034)
If we are fundee and after 5 days the funding isn't even in the mempool,
then we give up waiting and consider the channel closed. Note that if
the funding tx stays unconfirmed forever we won't give up waiting.

If we are funder, we never give up until the funding tx is double spent,
and we periodically republish it.

This applies to states `WAIT_FOR_FUNDING_CONFIRMED` and `CLOSING` (and
also `OFFLINE`/`SYNCING` when underlying state is
`WAIT_FOR_FUNDING_CONFIRMED`).

Also, added a generic way of passing context to `ElectrumClient`
requests/responses.

Fixes #1029.
2019-07-03 12:00:00 +02:00
araspitzu
667444cd0e
Check 'initialblockdownload' from bitcoind during startup (#1058) 2019-07-02 17:31:53 +02:00
Bastien Teinturier
1cc14aeda3
Add TLV and TLV stream codec support (#1045)
TLV (tag-length-value) types and TLV streams have been defined in the following spec PR: https://github.com/lightningnetwork/lightning-rfc/pull/607

New Lightning Messages should use TLV extensively instead of ad-hoc per-message encoding. This also allows ignoring unknown odd TLV types, which lets implementers safely test new features on mainnet without impacting legacy nodes. It also allows type re-use which speeds up new features development.

Also cleaned-up and refactored common codecs.
2019-07-02 11:53:14 +02:00
Anton Kumaigorodski
c4f07e8742 Add more data to usablebalances API method (#1053)
Now it additionally returns remote `nodeId` and `shortChannelId` for each channel. Also negative balances are represented as `0`s.
2019-07-02 11:20:02 +02:00
Anton Kumaigorodski
a35d50d4c8 Add balances API method (#962)
Currently balances can be obtained from `channels` call but this requires a lot of work on caller side and also some specific knowledge (reserves, commit tx fee, in-flight payments), so this new `balances` endpoint only returns a correct balance info for each channel.
2019-06-26 12:05:51 +02:00
araspitzu
de50cc437e Remove old service and related docs (#1046) 2019-06-24 17:46:55 +02:00
Pierre-Marie Padiou
316ba02ff8
Ack unhandled lightning messages (#1042)
In the event when we receive an unexpected message, the `Peer` was just logging a warning and not sending an `Ack` to the `TransportHandler`. This resulted in a stuck connection, because no more data was read on the connection.

Fixes #1037.
2019-06-20 11:40:01 +02:00
Pierre-Marie Padiou
38696b56e4
Minor fixes (#1043)
* fixed javadoc

* make bitcoind only bind on localhost for tests
2019-06-19 11:36:30 +02:00
Bastien Teinturier
a53145a6da
Electrum: add data to TxIdFromPosResponse. (#1038)
To make the response usable, we need to copy the request input (block height and transaction position).
2019-06-17 12:10:10 +02:00
Anton Kumaigorodski
cb1196cf95 Correctly decode requests without multipliers (#1020)
Since MAX_PAYMENT_AMOUNT has been removed we may now get payment requests without multiplier which means a whole bitcoin is requested.
2019-06-14 15:01:10 +02:00
Pierre-Marie Padiou
a3563e38bb
Removed cached codec (#1036)
It turns out that performance gains of the cached codec are not that
great, and they come at a cost of significant pressure on the GC. In
other words: premature optimization.

When removed, the heap usage becomes very stable, which is much better
than hypothetical performance gains.

Fixes #1031.
2019-06-14 14:04:15 +02:00
Bastien Teinturier
b4adff2dc4 Electrum: add id_from_pos (#1035)
This allows getting a transaction id from a short channel id.
2019-06-14 13:25:37 +02:00
Fabrice Drouin
31022ceca5
Use compact encoding for signatures (64 bytes) instead of DER (#1014)
* use 64B representation instead of DER for sigs

It is more compact, and as an added bonus it frees us from the
completely unrelated Bitcoin-specific `0x01` trailing sig hash.

Note that we already used the 64B representation for storage everywhere,
except in `ChannelCodecs.htlcTxAndSigsCodec`, which required a backward
compatibility codec. Added a nonreg test for this.

* Use updated secp256k1 JNI bindings

* Replace scalar with private key and point with public key

We now use the simplified/unified design proposed in bitcoin-lib where:
- there are no more specific types for scalar/point
- private and public keys are compressed unless explicitly requested

* Generate and use 32 bytes seeds (and not 33)

We used serialized random private keys which were represented a 33 bytes (with a 01 suffix).
Using random 32 bytes values is more consistent.
We must make sure that upgraded apps that already have a 33 bytes seed will still generate the same secrets, which is why LocalKeyManager still uses the 01 suffix when needed
2019-06-14 11:52:01 +02:00
araspitzu
199d6041c5 Output txid and serialized tx in transaction json serializer (#1033) 2019-06-13 18:10:09 +02:00
Pierre-Marie Padiou
db334380b3
Clean pending htlcs db (#1027)
We store `CMD_FULFILL_HTLC`/`CMD_FAIL_HTLC`/`CMD_FAIL_MALFORMED_HTLC` in
a database (see `CommandBuffer`) because we don't want to lose
preimages, or to forget to fail incoming htlcs, which would lead to
unwanted channel closings.

But we currently only clean up this database on success, and because of
the way our watcher works, in a scenario where a downstream channel has
gone to the blockchain, it may send several times the same command. Only
the first one will be acked and cleaned up by the upstream channel,
causing the remaining commands to stay forever in the "pending relay
db".

With this change we clean up the commands when they fail too.

We also clean up the pending relay db on startup.
2019-06-13 18:08:34 +02:00
Pierre-Marie Padiou
849b6bd22b
Smarter restore logic for closing channels (#1015)
If the closing type is known:
 - there is no need to watch the funding tx because it has already
 been spent and the spending tx has already reached min_depth
 - there is no need to attempt to publish transactions for other
 type of closes.

* differentiate current/next remote close

We can still match on the trait `RemoteClose` if we don't need that
level of precision.
2019-06-12 12:29:31 +02:00
araspitzu
818199e8f4
Peer reconnection address from node announcements (#1009)
* Use node announcements as fallback to load peer addresses during startup

* Add NetworkDb.getNode to retrieve a node_announcement by nodeId

* When connecting to a peer use node_announcement as fallback for its IP address

* Support connection to peer via pubKey

* Increase finite max of exponential backoff time to 1h.

* Add peer disconnect API call
2019-06-11 17:39:52 +02:00
araspitzu
9087ceeb5f Add feeThresholdSat and maxFeePct parameters to payments API (#1023) 2019-06-11 15:34:47 +02:00
Pierre-Marie Padiou
f180e54b03
Close channel if funding tx times out (#1016)
This only happens when we are fundee. We *could* have some funds at
stake if there was a non-zero `push_msat`, but we already allows 5 days
for the funding tx to confirm so the best option is probably to forget
about the channel.
2019-06-11 14:31:23 +02:00
Pierre-Marie Padiou
b33c9ecaac
Smarter relay logic (#1011)
When relaying a payment, we look in the onion to find the next
`shortChannelId`. But we may choose a different channel (to the same
node), because the requested channel may not have enough balance, of for
some other reasons, as permitted by the spec.

Currently we limit ourselves to only two attempts: one with a
"preferred" channel, and one with the originally requested channel if is
different from the preferred one. This has drawbacks, because if we have
multiple channels to the same node, we may not be able to relay a
payment if the "preferred" channel is currently unavailable (e.g.
because of an htlc in-flight value that is too high).

We now retry as many times as there are available channels, in our order
of preference, and if all fail, then we return a failure message for the
originally requested channel.
2019-06-03 19:10:14 +02:00
Fabrice Drouin
cd78c9ecdd
Electrum: update server list (#1022)
* Electrum: update server list

* Electrum: set client name to 3.3.6
2019-06-03 13:45:51 +02:00
Dominique
3e46475f9f Initialize global feerate with default from conf (#1019)
`Globals.feeratePerKB` is an atomic reference initialized to `null` and
is asynchronously set by the fee provider only once it's ready. This
means that it is possible to retrieve a null object from feeratePerKB,
scenario that must be handled separately to prevent any issues.

This commit now initialize `Globals.feeratePerKB` with the default
values set in the configuration file. This makes sure that the
feerate is always set to a meaningful value.
2019-06-03 09:44:06 +02:00
Pierre-Marie Padiou
c4b7ade038
Removed PaymentRequest.MAX_AMOUNT (#1018)
It was obsole since
068b0bccf9.

Note that we use a signed long, but it doesn't matter since 2^64
milli-satoshis is greater than the total number of bitcoins.
2019-05-28 14:14:17 +02:00
Pierre-Marie Padiou
b82d7211cc
Set handleCommandError to WARN (#1012)
Those are not fatal errors for channels.
2019-05-27 17:33:20 +02:00
Fabrice Drouin
648f57276a Electrum: consistently retrieve wallet transactions (#1010)
* Electrum: update mainnet and testnet servers list

* Electrum: request missing history transactions on reconnect

Upon connection/reconnection, ask for transactions that are included in our history but
which we don't have and don't have a pending request for.

* Electrum: add disconnect/reconnect tests

Simulate disconnections and check that wallet eventually gets it history and transactions.
2019-05-23 16:36:54 +02:00
Pierre-Marie Padiou
92e2d21eab
Make htlc-reaper a top-level actor (#1013)
The way we handle requests to `Switchboard` assume that all children are `Peer`s. This led the `peer` API request to timeout.
2019-05-21 17:36:29 +02:00
araspitzu
101bcd7c50
Send to route (#952)
* Add /sendtoroute API and functionality

* Do not use extractor pattern in PaymentLifecycle::SendPaymentToRoute

* /sendtoroute: support route parameter as comma separated list

* Add test for 'sendtoroute' in EclairImplSpec
2019-05-20 15:20:53 +02:00
araspitzu
cfaa5508eb
Increase Service and EclairImpl test coverage (#1004)
* /payinvoice: assert the override amount is being used in the invoice

* /updaterelayfee: assert the values passed through the API call are forwarder to EclairImpl

* 'receive' API: test for fallback address usage and parameters passing

* 'close' API: test for parameters handling

* 'networkFees': test for default parameters

* Add test dependency mockito-scala, rewrite a test using the mock framework

* Factor out query filter parsing in EclairImpl, Add test for networkFees/audit/allinvoice

* Move getDefaultTimestampFilters in companion object, group together EclairImpl-scoped classes
2019-05-20 14:22:09 +02:00
araspitzu
80340c1fbf
API: optionally use a user supplied preimage when creating invoices (#965)
* Add support for custom preimage in /createinvoice API

* Add test for using a custom preimage in payment request
2019-05-17 17:11:38 +02:00
Pierre-Marie Padiou
8718959164
Improved test coverage of io package (#996)
* improved test coverage of `NodeURI`

* improved test coverage of `Peer`
2019-05-10 19:53:50 +02:00
Fabrice Drouin
847d0999c8
Release v0.3 (#994)
* gui: include javafx native libraries for windows, mac, linux

* Release v0.3

* Set version to 0.3.1-SNAPSHOT
2019-05-10 11:02:08 +02:00
Pierre-Marie Padiou
081dec15ae
Updated license header (#992) 2019-05-09 16:40:37 +02:00
Fabrice Drouin
6cc1f87940
Use bitcoind fee estimator first (#987)
* use bitcoind fee provider first

* set default `smooth-feerate-window`=6

* Configuration: increase fee rate mismatch threshold

We wil accept fee rates that up to 8x bigger or smaller than our local fee rate
2019-05-09 15:19:25 +02:00
Pierre-Marie Padiou
e26c145e89
Accept commit_sig without changes (#988)
LND sometimes sends a new signature without any changes, which is a
(harmless) spec violation.

Note that the test was previously not failing because it wasn't specific
enough. The test now fails and has been ignored.
2019-05-09 13:57:57 +02:00
Pierre-Marie Padiou
50b4001853
Set tcp client timeout to 20s (#990)
So that it fails before the ask/api time out.
2019-05-09 11:41:05 +02:00
Fabrice Drouin
9f96b1b922 Set default chain to "mainnet" (#989)
Eclair is now configured to run on mainnet by default.
2019-05-09 11:30:26 +02:00
araspitzu
951bd0306b
Enrich test for internal eclair API implementation (fr.acinq.eclair.Eclair.scala) (#938)
* Add test to EclairImpl for `/send`, `/allupdates` and `/forceclose/`
2019-05-09 11:01:43 +02:00
Bastien Teinturier
03f4911e46 Wireshark dissector support (#981)
* Transport: add support for encryption key logging.
This is the format the wireshark lightning-dissector uses to be able to decrypt lightning messages.
2019-05-09 10:47:57 +02:00
Fabrice Drouin
ccdd42305e Replace UnknownPaymentHash and IncorrectPaymentAmount with IncorrectOrUnknownPaymentDetails (#984)
See https://github.com/lightningnetwork/lightning-rfc/pull/516 and https://github.com/lightningnetwork/lightning-rfc/pull/544
2019-05-09 10:03:04 +02:00
araspitzu
100cce1077 Update bash autocompletion for eclair-cli (#983)
* Update bash autocompletition file to suggest all the endpoints

* Update list of commands in eclair-cli help message
2019-05-09 09:45:47 +02:00
Pierre-Marie Padiou
7b874f23d2
Use proper closing type in ChannelClosed event (#977)
There was actually a change introduced by #944 where we used
`ClosingType.toString` instead of manually defining types, causing a
regression in the audit database.
2019-05-09 09:43:25 +02:00
Pierre-Marie Padiou
de5a7827bd
Fixed overflow issue with max duration (#975)
This is a regression caused by #971, because `Duration` has a max value of `Long.MaxValue` *nanoseconds*, not *seconds*.
2019-04-30 10:14:48 +02:00
Pierre-Marie Padiou
fb84dfb855
Smarter strategy for sending channel_updates (#950)
The goal is to prevent sending a lot of updates for flappy channels.

Instead of sending a disabled `channel_update` after each disconnection, 
we now wait for a payment to try to route through the channel and only 
then reply with a disabled `channel_update` and broadcast it on the
network.

The reason is that in case of a disconnection, if noone cares about that
channel then there is no reason to tell everyone about its current
(disconnected) state.

In addition to that, when switching from `SYNCING`->`NORMAL`, instead
of emitting a new `channel_update` with flag=enabled right away, we wait
a little bit and send it later. We also don't send a new `channel_update` if
it is identical to the previous one (except if the previous one is outdated).

This way, if a connection to a peer is unstable and we keep getting
disconnected/reconnected, we won't spam the network.

The extra delay allows us to remove the change made in #888, which was
a workaround in case we generated `channel_update` too quickly.

Also, increased refresh interval from 7 days to 10 days. There was no
need to be so conservative.

Note that on startup we still need to re-send `channel_update` for all 
channels in order to properly initialize the `Router` and the `Relayer`.
Otherwise they won't know about those channels, and e.g. the 
`Relayer` will return `UnknownNextPeer` errors.

But we don't need to create new `channel_update`s in most cases, so 
this should have little or no impact to gossip because our peers will
already know the updates and will filter them out.

On the other hand, if some global parameters (like relaying fees) are
changed, it will cause the creation a new `channel_update` for all
channels.
2019-04-29 16:39:59 +02:00
araspitzu
4ba4ce8096 API: Support query by channelId or shortChannelId everywhere (#969)
Add support for querying a channel information by its `shortChannelId`.
2019-04-26 17:45:59 +02:00
araspitzu
83dc8176a9 Various fix and improvements in time/timestamp handling (#971)
This PR standardizes the way we compute the current time as unix timestamp 

- Scala's Platform is used and the conversion is done via scala's concurrent.duration facilities
- Java's Instant has been replaced due to broken compatibility with android
- AuditDB events use milliseconds (fixes #970)
- PaymentDB events use milliseconds
- Query filters for AuditDB and PaymentDB use seconds
2019-04-26 17:10:30 +02:00
Pierre-Marie Padiou
65918ec0de
Rename eclair.bak to eclair.sqlite.bak (#968)
This removes any ambiguity about what the content of the file is about.
2019-04-25 16:34:05 +02:00
araspitzu
45a5b6f9a7
Fix flaky test in PaymentLifecycleSpec (#967)
* Use local random pamentHash for each test in paymentlifecyclespec, intercept the route request before the router.
2019-04-25 15:28:03 +02:00
Fabrice Drouin
650c5049d5
Better handling of closed channels (#944)
* Remove closed channels when application starts

If the app is stopped just after a channel has transition from CLOSING to CLOSED, when the  application starts again if will be restored as CLOSING. This commit checks channel data and remove closed channels instead of restoring them.

* Channels Database: tag closed channels but don't delete them

Instead we add a new `closed` column that we check when we restore channels.

* Document how we check and remove closed channels on startup
2019-04-24 17:32:53 +02:00
Fabrice Drouin
595c23c38c
Electrum: make debug logs shorter (#964) 2019-04-24 16:50:25 +02:00
Fabrice Drouin
a87a111c71
Channel: Log additional data (#943)
* Channel: Log additional data

Log local channel parameters, and our peer's open or accept message.
This should be enough to recompute keys needed to recover funds in case of unilateral close.
2019-04-24 16:47:34 +02:00
Dominique
159e6da21f
ElectrumWallet should not send ready if syncing (#963)
This commit is already embedded in version `0.2-android-beta22`.
2019-04-24 16:45:57 +02:00
araspitzu
aef27d8f9c
Print stack trace when crashing during boot sequence (#949)
* Print stack trace when crashing during boot sequence

* Use friendly message when db compatibility check fails
2019-04-24 10:23:57 +02:00
Fabrice Drouin
064ba7df91
Backup: explicitely specify move options (#960)
* Backup: explicitely specify move options

We now specify that we want to atomically overwrite the existing backup file with the new one (fixes
a potential issue on Windows).
We also publish a specific notification when the backup process has been completed.
2019-04-23 17:11:53 +02:00
Fabrice Drouin
bed47de5e3
Live channel database backup (#951)
* Backup running channel database when needed

Every time our channel database needs to be persisted, we create a backup which is always
safe to copy even when the system is busy.

* Upgrade sqlite-jdbc to 3.27.2.1

* BackupHandler: use a specific bounded mailbox

BackupHandler is now private, users have to call BackupHandler.props() which always
specifies our custom bounded maibox.

* BackupHandler: use a specific threadpool with a single thread

* Add backup notification script

Once a new backup has been created, call an optional user defined script.
2019-04-19 22:35:12 +02:00
Pierre-Marie Padiou
f563ca0897
Add channel errors in audit db (#955)
We now keep track of all local/remote channel errors in the audit db.
2019-04-19 16:28:02 +02:00
Pierre-Marie Padiou
44778a72c9
Set MAX_BUFFERED to 1,000,000 (#948)
Note that this doesn't mean that we will buffer 1M objects in memory:
those are just pointers to (mostly) network announcements that already
exist in our routing table.

Routing table has recently gone over 100K elements (nodes,
announcements, updates) and this causes the connection to be closed when
peer requests a full initial sync.
2019-04-19 14:18:57 +02:00
Pierre-Marie Padiou
84c0fab433
Added a timeout for channel open request (#928)
Until now, if the peer is unresponsive (typically doesn't respond to
`open_channel` or `funding_created`), we waited indefinitely, or until the
connection closed.

It translated to an API timeout for users, and uncertainty about the
state of the channel.

This PR:
- adds an optional `--openTimeoutSeconds` timeout to the `open` endpoint, that will
actively cancel the channel opening if it takes too long before reaching
state `WAIT_FOR_FUNDING_CONFIRMED`.
- makes the `ask` timeout configurable per request with a new `--timeoutSeconds`
- makes the akka http timeout slightly greater than the `ask` timeout

Ask timeout is set to 30s by default.
2019-04-18 18:30:51 +02:00
Fabrice Drouin
6afe28d147
Electrum: do not persist transaction locks (#953)
Locks held on utxos that are used in unpublished funding transactions should not be persisted.
If the app is stopped before the funding transaction has been published the channel is forgotten
and so should be locks on its funding tx utxos.
2019-04-17 19:10:14 +02:00
Pierre-Marie Padiou
9032da5326
Add a proper payments database (#885)
There is no unique identifier for payments in LN protocol. Critically,
we can't use `payment_hash` as a unique id because there is no way to
ensure unicity at the protocol level.

Also, the general case for a "payment" is to be associated to multiple
`update_add_htlc`s, because of automated retries. We also routinely
retry payments, which means that the same `payment_hash` will be
conceptually linked to a list of lists of `update_add_htlc`s.

In order to address this, we introduce a payment id, which uniquely
identifies a payment, as in a set of sequential `update_add_htlc`
managed by a single `PaymentLifecycle` that ends with a `PaymentSent` or
`PaymentFailed` outcome.

We can then query the api using either `payment_id` or `payment_hash`.
The former will return a single payment status, the latter will return a
set of payment statuses, each identified by their `payment_id`.

* Add a payment identifier

* Remove InvalidPaymentHash channel exception

* Remove unused 'close' from paymentsDb

* Introduce sent_payments in PaymentDB, bump db version

* Return the UUID of the ongoing payment in /send API

* Add api to query payments by ID

* Add 'fallbackAddress' in /receive API

* Expose /paymentinfo by paymentHash

* Add id column to audit.sent table, add test for db migration

* Add invoices to payment DB

* Add license header to ExtraDirective.scala

* Respond with HTTP 404 if the corresponding invoice/paymentHash was not found.

* Left-pad numeric bolt11 tagged fields to have a number of bits multiple of five (bech32 encoding).

* Add invoices API

* Remove CheckPayment message

* GUI: consume UUID reply from payment initiator

* API: reply with JSON encoded response if the queried element wasn't found

* Return a payment request object in /receive

* Remove limit of pending payment requests!

* Avoid printing "null" fields when serializing an invoice to json

* Add index on paymentDb.sent_payments.payment_hash

* Order results in descending order in listPaymentRequest
2019-04-16 17:03:21 +02:00
rorp
70d7db7f96 Set max payment attempts from configuration (#931)
With a default to `5`.
2019-04-15 15:19:39 +02:00
araspitzu
3eceb90fa0
Expose the websocket over HTTP GET to work properly with basic auth (#934)
* Expose the websocket over HTTP GET
* Add test for basic auth over websocket endpoint
2019-04-10 11:07:19 +02:00
Fabrice Drouin
a37fd38d65
API: fix fee rate conversion (#936)
Our `open` API calls expects an optional fee rate in satoshi/byte, which is the most widely
used unit, but failed to convert to satoshi/kiloweight which is the standard in LN.
We also check that the converted fee rate cannot go below 253 satoshi/kiloweight.
2019-04-09 11:32:12 +02:00
Pierre-Marie Padiou
fa5d0235cb
Make Electrum tests pass on windows (#932)
There was an obscure Docker error when trying to start an Electrum
server in tests. [1]

It appears that there is a conflict between Docker and Hyper-V on some
range of ports.

A workaround is to just change the port we were using.

[1] https://github.com/docker/for-win/issues/3171
2019-04-04 11:38:11 +02:00
araspitzu
2aa088e0e6 Fix eclair-cli to work with equal sign in arguments (#926)
* Fix eclair cli argument passing

* Modify eclair-cli to work with equals in arguments

* Eclair-cli: show usage when wrong params are received

* Remove deprecated call from eclair-cli help message [ci skip]
2019-04-03 19:19:56 +02:00
araspitzu
3a56ad9133
Send events when HTLCs settle on-chain (#884)
* send events when htlc settle on-chain

* send events when a payment is received/relayed/sent

* send events when a payment is failed

* Add test for websocket

* Use nicer custom type hints when serializing to json (websocket)

* Fix bech32 prefix for regtest

* Separate cases for bech32 testnet and regtest for BOLT11 fallback address
2019-04-03 11:41:08 +02:00
Fabrice Drouin
288aa75305
Electrum: update client name (#930)
* Electrum: use 3.3.4 as client name

* Electrum Pool: more specific message on disconnect

Specify wether we lost connection to our master server or to a backup server.
2019-04-03 10:58:26 +02:00
Andrea
e3f8d9b140
Formatting, remove unused param in comments 2019-04-03 10:29:41 +02:00
Fabrice Drouin
617ccf4d11
Minor API fixes (#929)
Move GetInfoResponse and AuditResponse out of the api package
Fix parameter handling for audit() and networkFees()
2019-04-02 16:23:49 +02:00
Pierre-Marie Padiou
933913de08
Add random delay to rebroadcast (#925)
* add random delay to rebroadcast

* ignore `BadMessage` while disconnected

* ignore `DelayedRebroadcast` while disconnected
2019-04-01 13:31:41 +02:00
Pierre-Marie Padiou
9c37448ebf
Rollback tx if disconnected in WAIT_FOR_FUNDING_SIGNED (#923) 2019-03-30 20:28:54 +01:00
Andrea
1b91609aec
Separate cases for bech32 testnet and regtest for BOLT11 fallback address 2019-03-29 19:02:43 +01:00
Andrea
77eb1de4f3
Merge remote-tracking branch 'origin/extended-api-pm' into extended-api-pm 2019-03-29 19:00:18 +01:00
Andrea
5e476fddc4
Fix bech32 prefix for regtest 2019-03-29 18:59:59 +01:00
Fabrice Drouin
c99026828c
Electrum: fixes and improvements (#924)
* Electrum: Update mainnet servers list

* Electrum: make pool address selection more readable

We connect to a random server we're not already connected to.

* Electrum Tests: increase "wait for ready" test timeout

If was a bit short and sometimes failed on travis.

* Electrum: better parsing of invalid responses

On testnet some Electrum servers are not compliant with the protocole version they advertise
and will return responses formatted with 1.0 rules.
2019-03-29 18:56:00 +01:00
pm47
dd52cdc61f
added tests 2019-03-29 18:16:44 +01:00
araspitzu
9624383f22
cleaner isSentByLocal
Co-Authored-By: pm47 <pm47@users.noreply.github.com>
2019-03-29 18:14:32 +01:00
Pierre-Marie Padiou
14da6d3c23
Update eclair-core/src/main/scala/fr/acinq/eclair/channel/Helpers.scala
Co-Authored-By: araspitzu <a.raspitzu@protonmail.com>
2019-03-29 17:26:11 +01:00
Andrea
008b2cad87
re-introduce dropTail strategy for websocket buffer, let the unhandled messages be handled by akka's default 2019-03-29 17:09:10 +01:00
Andrea
09618a8764
Log error with warn level in websocket actor, use backpressure strategy for websocket queue. 2019-03-29 16:57:59 +01:00
Andrea
52b17fe75f
Use nicer custom type hints when serializing to json (websocket) 2019-03-28 17:54:56 +01:00
Andrea
9a7e2158be
Add test for websocket 2019-03-28 16:07:51 +01:00
Andrea
c8d38871e9
remote println from test, 2019-03-28 15:21:04 +01:00
Andrea
5b423b1571
Remove 'fallbackAddress' from /receive API 2019-03-28 14:54:28 +01:00
Andrea
10bc4ab811
Reorganize specialized formats for the websocket 2019-03-28 14:51:21 +01:00
Andrea
cd703f2d4a
Merge branch 'master' into extended-api-pm 2019-03-28 11:24:12 +01:00
Andrea
c557f70af5
Finish merging master 2019-03-28 11:22:37 +01:00
Andrea
8ec8574d97
Merge branch 'master' into extended-api-pm
# Conflicts:
#	eclair-core/src/main/scala/fr/acinq/eclair/api/Service.scala
#	eclair-core/src/main/scala/fr/acinq/eclair/payment/PaymentEvents.scala
#	eclair-core/src/test/scala/fr/acinq/eclair/api/JsonSerializersSpec.scala
2019-03-28 10:44:24 +01:00
Fabrice Drouin
e2ff5c857b
Set version to 0.3-SNAPSHOT (#920)
We have enough major changes from the last release to justify switching to 0.3
2019-03-28 09:49:17 +01:00
araspitzu
2505e80283
Factor out nodeId API param, rename /channels API param to nodeId. (#919) 2019-03-27 15:49:22 +01:00
araspitzu
a4b94004e4 API: use form data instead of JSON-RPC (#894)
Port the existing API functionalities over a new structure of HTTP endpoints, with the biggest difference being the usage of **named parameters** for the requests (responses are unchanged). RPC methods have become endpoints and the parameters for each are now passed via form-params (clients must use the header "Content-Type" : "multipart/form-data"), this allows for a clearer interpretation of the parameters and results in more elegant parsing code on the server side. It is possible to still use the old API version via a configuration key.

Old API can be used by setting `eclair.api.use-old-api=true`.
2019-03-26 18:10:09 +01:00
araspitzu
89ddc52640 Deal with channels with fees=0 when computing a route (#905)
* Treat channels with fees=0 as if they had feeBase=1msat while we compute a route

* Add test to ensure we build the onion attaching no fees if they were not required by the channel_update
2019-03-26 14:00:15 +01:00
Pierre-Marie Padiou
57e43cc65d
Check WatchSpent in constant time (#916)
This is done with a secondary map that indexes watched utxos.
2019-03-25 18:09:30 +01:00
araspitzu
06b2337ed9
Rework database initialization (#911)
* Initialize the database outside the node param constructor
* Do not create folders during StartupSpec
* Simplify syntax for instantiating test Databases
* Rework parameter passing to database initialization
* Force UTF-8 file encoding on all platform.
2019-03-25 18:04:30 +01:00
Fabrice Drouin
3b7afd92d4
Use bitcoin-lib 0.11 which embeds libsecp256k1 (#907)
* Use bitcoin-lib 0.11, which embeds libsecp256k1

* Unit tests: generate dummy sig from 32 random bytes

We now use a version of bitcoin-lib which embeds JNI bindings for libsecp256k1,
and it will only sign data that is 32 bytes long (in Bitcoin and LN you always
sign data hashes, not the actual data).

* Use maven 3.6.0 and a different mirror

* RoutingSyncSpec: don't create databases at init time

We called nodeParams which created a new in-memory sqlite database everytime we created "fake" routing info
2019-03-25 14:42:06 +01:00
Pierre-Marie Padiou
8ea4bd0468
Don't send updates if no filter has been set (#912)
We mistakenly implemented `Peer.timestampInRange` as opt-out whereas it
should be opt-in.
2019-03-25 14:37:12 +01:00
araspitzu
3fda5ddede Remove unused param 'randomize' from SendPayment command (#910)
Cleanup of the SendPayment command in which there was a leftover field 'randomize' currently replaced by its analogous in the RouteParams.
2019-03-22 14:39:06 +01:00
Fabrice Drouin
32d8a08ad1 Improve unit tests (#908)
* Bitcoin tests: generate 150 blocks instead of 500

We don't need to generate 432 blocks to activate segwit but we still need to have
spendable coins and coinbase maturity is 100 blocks even on regtest.

* Electrum client: test against mainnet Electrum servers

Previous test against testnet servers was flaky because testnet Electrum
servers are unrelable. Here we test against our own server on mainnet (and
2 servers from our list for the pool test).
2019-03-22 13:26:55 +01:00
araspitzu
1c9ac1d62d Ensure the cost function in path-finding is monotonic (#904)
Make sure the cost function in path-finding is monotonic
2019-03-20 10:43:48 +01:00
Fabrice Drouin
4aa7a1ca9f Upgrade to bitcoin 0.17.1 (#826)
Bitcoin Core 0.18 is about to enter RC cycle and should be release soon (initial target was April). It is not compatible with 0.16 (some of the RPC calls that we use have been removed. They're still available in 0.17 but tagged as deprecated). 

With this PR, eclair will be compatible with 0.17 and the upcoming 0.18, but not with 0.16 any more so it will be a breaking change for some of our users. Supporting the last 2 versions is the right option and we should be ready before 0.18 is actually released (its initial target was April).
2019-03-19 14:57:03 +01:00
Pierre-Marie Padiou
cc3395a5bb
Better logic for sending channel_updates (#888)
* don't spam with channel_updates at startup

Previous logic was very simple but naive:
- every time a channel_update changed we would send it out
- we would always make a new channel_update with the disabled flag set
at startup.

In case our node was simply restarted, this resulted in us re-sending a
channel_update with the disabled flag set, then a second one with the
disabled flag unset a few seconds later, for each public channel.

On top of that, this opened way to a bug: if reconnection is very fast,
then the two successive channel_update will have the same timestamp,
causing the router to not send the second one, which means that the
channel would be considered disabled by the network, and excluded from
payments.

The new logic is as follows:
- when we do NORMAL->NORMAL or NORMAL->OFFLINE or OFFLINE->NORMAL, we
send out the new channel_update if it has changed
- in all other case (e.g. WAIT_FOR_INIT_INTERNAL->OFFLINE) we do nothing

As a side effect, if we were connected to a peer, then we shut down
eclair, then the peer goes down, then we restart eclair: we will make a
new channel_update with the disabled flag set but we won't broadcast it.
If someone tries to make a payment to that node, we will return the
new channel_update with disabled flag set (and maybe the payer will then
broadcast that channel_update). So even in that corner case we are good.

* quick reconnection: bump channel_update timestamp

In case of a disconnection-reconnection, we first generate a
channel_update with disabled bit set, then after we reconnect we
generate a second channel_update with disabled bit not set.

If this happens very quickly, then both channel_updates will have the
same timestamp, and the second one will get ignored by the network.

A simple fix is to bump the second timestamp in this case.

* set channel_update refresh timer at reconnection

We only care about this timer when connected anyway. We also cancel it
when disconnecting.

This has several advantages:
- having a static task resulted in unnecessary refresh if the channel
got disconnected/reconnected in between 2 weeks
- better repartition of the channel_update refresh over time because at
startup all channels were generated at the same time causing all refresh
tasks to be synchronized
- less overhead for the scheduler (because we cancel refresh task for
offline channels (minor, but still)
2019-03-18 14:39:29 +01:00
Fabrice Drouin
5519d0aa6a Minor fixes (dependencies, unit tests) (#901)
Use bitcoin-lib v0.10 which has finally been synced to maven central.
Fix transactions unit test (the check in the test was using the whole locktime and not
the last 24 bits).
2019-03-15 12:54:33 +01:00
Pierre-Marie Padiou
b681cfca47
Replace BinaryData by scodec.bits.ByteVector (#896)
See https://github.com/ACINQ/bitcoin-lib/pull/31.

We still have to use `Array[Byte]` for low-level cryptographic primitives, and `akka.util.ByteBuffer` for tcp connections. In order to reduce unnecessary copies, we used `ByteVector.view(...)` as much as possible.

Took the opportunity to do a project-wide optimize imports. We might as well do it now since pretty much all files have been touched already.

NB: temporarily use bitcoin-lib 0.10.1-SNAPSHOT because maven central is very slow and we can't access the recently release 0.10 for now.
2019-03-14 12:43:28 +01:00
araspitzu
889e86a908
Move the param 'randomize' from RouteRequest to RouteParams (#895)
* Move the param 'randomize' from RouteRequest to RouteParams
2019-03-13 18:58:55 +01:00
Pierre-Marie Padiou
2712e30504
Better error logs for socks5 proxy (#893) 2019-03-12 13:37:27 +01:00
Fabrice Drouin
abba9f77ec NetworkDb: remove stale channels in batch (#886)
Add methods to delete channels and tags channels as pruned in batch which is much
more efficient in sqlite.

* Network db: minor changes in unit tests

Test pruning a few 1000s channels at once.

* NetworkDb API: use Iterators and not Seq

It's more consistent with our code base.
2019-03-12 13:34:40 +01:00
pm47
85df143c64
send events when htlc settle on-chain
This is a simpler alternative to #867, with the following limitations:
- no `OnChainRefundsDb` and associated API calls
- `PaymentSettlingOnChain` event will be sent exactly once per payment
and have less information
- we don't touch `HtlcTimeoutTx`
- use json4s type hints instead of manual attributes to case classes
2019-03-07 11:07:43 +01:00
Pierre-Marie Padiou
70397962b6
Clean channels with unexisting funding tx (#714)
There are several separate but related changes in this PR:

(a) Fast close on scenarii where we have nothing at stake (instead of going to `CLOSING` state). The previous process was not only slower (we had to wait for confirmations), but it never resolved when the funding tx hadn't been confirmed. Note that there is still an edge case where the funding tx never gets confirmed, we are fundee and we have something at stake (`push_msat` > 0).

(b) When *fundee*: after a timeout (5 days), if the funding tx hasn't reached `min_depth`, we cancel the channel.

(c) When *funder*: there is no timeout on the funding tx: however on restart, if we detect that our funding tx was doublespent, then we cancel the channel. Just because there is a doublespend doesn't mean that something malicious is going on: e.g. fee was to low, the tx was eventually removed from mempools and we just spent the inputs on something else).

Commits:

* set proper channelid in logs on restore

* fast close if we have nothing at stake

* added fundingTx and timestamp to DATA_WAIT_FOR_FUNDING_CONFIRMED

Also added migration codecs and tests

* implemented funding timeout for fundee

After a given delay, fundee will consider that the funding tx will never
confirm and cancels the channel.

Note that this doesn't apply to the funder, because our implementation
guarantees that we have sent out a funding tx, and the only way to be
sure that it will never be confirmed is that we double spend it. We just
can't rely on a timeout if we want to be safe.

* Electrum: detect if a wallet transaction has been double-spent

If it's in the mempool, or if it's been confirmed, then it's not double spent.
If it's not confirmed and not the mempool, we check if we have a transaction in
our wallet that sspends one of the inputs of our tx. If we find one, then it's been
double spent.
This will work with our funding txs, but not with their funding txs.

* fix regression with dataloss protection

The fast close causes a regression with dataloss protection, because
if we have nothing at stake we won't publish anything in case of
error (even if our peer asks us to).

This fixes #854.
2019-03-06 11:10:40 +01:00
Pierre-Marie Padiou
f9ead30b5c
Set default to-remote-delay-blocks to 720 (#879)
It sounds like a more reasonable default value, 144 was too short.

Keep using 144 blocks for our integration tests.
2019-02-25 18:00:17 +01:00
araspitzu
fe31f2d9d2 Routing heuristics (#821)
* Add route-weight-ratios to SendPayment/RouteRequest

* Update test channel_update with real world fee values

* Add maxFeeBase and maxFeePct to SendCommand, use high fee tolerance in integration test

* Expose randomized route selection feature in SendPayment, used in integration test too

* Add maxCltv to SendPayment/RouteRequest

* Implement boundaries for graph searching with cost, cltv, and size

* Enable searching for routes with size/CLTV/fee limits

* Expose RouteParams in RouteRequest and SendPayment

* If we couldn't find a route on the first attempt, retry relaxing the restriction on the route size

* Avoid returning an empty path, collapse the route not found cases into one

* When retrying to search for a route, relax 'maxCltv'

* Group search params configurations into a block

* Use the returning edges in 'ignoredEdges' when looking for a spur path

* Log path-finding params when receiving a route request

* Enforce weight ratios to be between (0,1]

* Make path-finding heuristics optional
2019-02-22 18:43:20 +01:00
Dominique
32d0500ef9 Use ypub prefix for Electrum xpub (#875)
* Updated prefix of electrum xpub to upub/ypub
2019-02-21 16:25:54 +01:00
Pierre-Marie Padiou
3db7b176d3
Update jeromq dependency (#852)
* jeromq 0.4.0->0.5.0
2019-02-21 16:02:39 +01:00
Fabrice Drouin
34e51c19cc
Use OpenJDK 11 as default JDK (#846)
* Upgrade to JDK11

Eclair can be built and used on Oracle JDK 1.8 or OpenJDK 11.
JavaFX is now embedded in eclair-node-gui and does not need to be installed separately.

* Install: update java download links

OpenJDK 11 is now our recommendation. Tell users to download java from https://jdk.java.net/11

* README: Rewrite installation instructions
2019-02-21 15:11:44 +01:00
Fabrice Drouin
c0af665990 Electrum fixes and improvements (#873)
* Electrum: don't ask for merkle proofs for unconfirmed txs

* Electrum: clear status when we get disconnected and still have pending history transactions

When we get disconnected and have script hashes for which we still have pending connections,
clear the script hash status. When we reconnect we will ask for its history again, this way we
won't miss anything. Since we rotate keys it should not result in heavy traffic (script hashes have
few history items).

* Electrum: represent and persist block heights as 32 bits signed ints

Int.MaxValue is about 40,000 years of block which should be enough, and it will fix the encoding
problem users on testnet when there's a reorg and one of their txs has a height of -1.

Side-note: changing the wallet codec can be done easily: if we cannot read and decode persisted data
we just start with an empty wallet and retrieve all wallet data from electrum servers, and once it's
ready it will be encoded with the new codec and saved.

* Electrum persistence: include a version number

It provides a clean way, when upgrading the app, of choosing whether to keep the same version and start from the
last persisted wallet (if the persistence format has not been changed or is compatible with the old one), or to
change the version and force starting from an empty wallet and downloading all wallet items from Electrum servers.

* ElectrumClient: remove useless buffer
2019-02-19 11:06:36 +01:00
Pierre-Marie Padiou
aeb4360464
Fixed computation of available balance (#868)
There was a unit mismatch. Added a test.
2019-02-18 18:48:01 +01:00
Pierre-Marie Padiou
7ae083ae29
Faster gui startup (#863)
The GUI took a very long time to startup because nodes and channels were
stored in javafx `ObervableList` which doesn't allow random access. We
can't easily use `ObservableMap` because `TableView` do not support
them.

As a workaround, created an `IndexedObservableList` which keeps track of
indices in `ObservableList` so that we can still have fast random
access.

Also, now grouping announcements in `NetworkEvent` instead of sending
them one by one.
2019-02-14 15:01:19 +01:00
Pierre-Marie Padiou
57a9e6ed86
Support all-uppercase payment requests (#862) 2019-02-12 20:37:53 +01:00
araspitzu
efa42f5967 Make sure the path-finding algorithm can't use a channel twice in a route (#857)
* Make sure the path-finding algo can't go backward when searching
2019-02-12 15:49:19 +00:00
araspitzu
94c1c36252 Fix discrepancy between config keys (#860) 2019-02-12 10:39:23 +00:00
Pierre-Marie Padiou
884812ade0
Reimplemented BOLT 11 with scodec (#856) 2019-02-11 19:45:56 +01:00
araspitzu
4291bef88d Parametric route search (#844)
* Enable searching for routes with size/CLTV/fee limits

* Expose the RouteParams in RouteRequest

* Expose the RouteParams in SendPayment

* Rename DEFAULT_ROUTE_MAX_LENGTH

* If we couldn't find a route on the first attempt, retry relaxing the restriction on the route size

* Avoid returning an empty path, collapse the route not found cases into one

* When retrying to search for a route, relax 'maxCltv'

* Move the default params for route searching in the conf, refactor together router params into a config class

* Remove max-payment-fee in favor of router.search-max-fee-pct

* Group search params configurations into a block

* Rename config keys for router path-finding
2019-02-11 17:30:04 +00:00
Dominique
b29874ea70 Increased max-to-local-delay-blocks to 2016 (#853)
Max CLTV of 2016 agreed in 2018 Adelaide's meeting for 1.1 specs.
2019-02-08 15:11:21 +01:00
Pierre-Marie Padiou
fe48e5540c
Stop disconnected peer when it has no channels (#840)
When we are disconnected and we don't have any channel with that peer,
we can stop it and remove its address from db.

We also need to handle the case where we are in `DISCONNECTED` or
`INITIALIZING` state and we are notified that the last channel we had
with that peer just got closed.

Note that this opens a race condition in the switchboard, if we receive
an incoming connection from that same peer right after we stopped it,
and before the switchboard received the `Terminated` event. If that
happens, then `Peer.Connect` or `Peer.OpenChannel` will timeout. We
could also have the switchboard listens to deadletter events, but that
seems a bit over the top.

Also, removed switchboard's map of peers. Instead, we use the actor's 
children list, and access peers using the recommended `child()` method.
Now the 'peers request only returns an `Iterable` instead of a `Map`. This 
removes the need to watch child actors, and thus removes the race
condition when peers were stopped. As a trade-off, peer lookup is now
in O(log(N)) instead of O(1) (with N being the number of peers), but this
seems acceptable.
2019-02-08 13:29:46 +01:00
Fabrice Drouin
9f6d07ae44 Improve Electrum start-up time (#848)
Persist a partial view of our wallet to enable faster startup time.

Users will be able to create transactions as soon as we've checked that we're connected to a "good" electrum server. In the unlikely event where they were able to create and try to publish a transaction during the few seconds it took to finish syncing and their utxo was spent (because they were trying to use an unconfirmed utxo that got double spent, or if they spent their utxo themselves from another wallet initialized with the same seed  while his one was offline), publishing the tx will fail. 

* Electrum: persist wallet state

Wallet state is persisted and reloaded on startup.

* Electrum wallet: fix handling of headers chunk

When we receive a tx that is older than our checkpoints, we download and check
the header chunk that it's in, check it and connect it to our chain.

* Electrum: advertise wallet transactions

Send notifications for all wallet transactions once we're connected and synced

* Electrum: add timestamp to wallet events

Add an optional timestamp to TransactionReceived and TransactionConfidenceChanged, which is the timestamp of the block the tx was confirmed in (if any).

* Electrum client: use a Close message

This will fix concurrency issues where handlers are called when the actor
is already stopped.
2019-02-08 11:28:38 +01:00
Dominique
267037a937
Add balance and channel lifecycle events to the audit db (#827)
Note that balance events are logged at most once every 30s, and only when
the balance actually changes (e.g. won't log if a payment is failed).

Also, only send `AvailableBalanceChanged` when needed.

We were sending this event everytime we sent a `commit_sig`, which is
incorrect because our balance doesn't change if, say, we are signing an
incoming htlc.

Note that we only send this event in `NORMAL` state, not in `SHUTDOWN`
state, because the balance is not really _available_ in the latter.
2019-02-08 10:41:09 +01:00
rorp
808bf14d20 Support for Tor onion services (#736)
This includes support for hosting onion services, and connecting to them, which are two separate things:
- Opening an onion service implie interacting with the tor daemon controller, which requires authentication. We support both `SAFECOOKIE` and `HASHEDPASSWORD` authentication mechanisms, with a default to `SAFECOOKIE`. We support v2 and v3 services, with a default to v3 as recommended by the tor project.
- Connecting to onion services requires tunnelling through tor's local SOCKS5 proxy.

Incoming and outgoing tor connections are thus separate matters that needs to be configured independently. A specific documentation has been added to guide users through these steps.

Big thanks to @rorp for doing the heavy lifting on all this!
2019-02-08 10:18:59 +01:00
araspitzu
968961248e
Allow to disable route randomization via configuration (#851)
* Allow to enable/disable the route randomization feature from the configuration

* Move router related configuration keys in router block.
2019-02-05 17:35:47 +01:00
araspitzu
3165f5dc36
(Router) Always select direct channel if there is one to the target (#850)
During route searching, bypass the route randomization to always select the direct channel if there is one to the target.
2019-02-05 14:00:31 +01:00
Pierre-Marie Padiou
1da0017311
Proper handling of gossiped channels being spent (#838)
While it makes sense to exclude from the routing table channels for
which there is a spending tx in the mempool, we shouldn't blame our
peers for considering the channel still opened if the spending tx hasn't
yet been confirmed.

Also, reworked `ValidateResult` with better types. It appears that the
`NonexistingChannel` error wasn't really useful (a malicious peer would
probably point to an existing funding tx, so there is no real difference
between "txid not found" and "invalid script"), so it was replaced by
`InvalidAnnouncement` error which is a more serious offense (punished
by a disconnection, and probably a ban when we implement that sort of
things).
2019-01-31 16:03:01 +01:00
araspitzu
4db9c8daf7 Optional route randomization (#842)
* Make route randomization optional (enabled by default), option is exposed in SendPayment/RouteRequest

* Fix non deterministic behavior in IntegrationTest
2019-01-30 15:21:22 +01:00
Pierre-Marie Padiou
7ff694c711
Ignore reconnections requests to the same peer with the same address (#835)
* ignore reconnections requests to the same address

* don't wait 1s before connecting on restart
2019-01-29 18:38:41 +01:00
rorp
a91cd631e8 Replace initialization futures Future[Boolean] by Future[Done] (#836)
Fixes #772.
2019-01-29 10:25:11 +01:00
Pierre-Marie Padiou
3954e39bf8
Set timestamp filter lower bound to current time (#837)
We previously assumed that the `gossip_timestamp_filter` only applied to
future messages, so we set `first_timestamp` to 0 in order to have a
pass-all filter.

But BOLT 7 actually says that the remote peer:
> SHOULD send all gossip messages whose timestamp is greater or equal to
first_timestamp, and less than first_timestamp plus timestamp_range

Which means that the way our filter was set, the remote peer would dump
the entire routing table on us.

By setting `first_timestamp` to the current time, we achieve what we
want. The synchronization of older messages is done by sending a
`query_channel_range` and then one or several `query_short_channel_ids`.
2019-01-28 15:58:35 +01:00
pm47
a331a69406
Merge branch 'master' into audit-extended 2019-01-24 10:25:39 +01:00
Fabrice Drouin
629cb22192
ChannelSelectionSpec: use akka.event.NoLogging (#834)
akka.http.impl.util.DefaultNoLogging is not available in akka 2.3 which we use on Android.
2019-01-23 21:18:48 +01:00
araspitzu
3953bc400e Fix flaky test in IntegrationSpec (#832) 2019-01-22 17:49:11 +01:00
pm47
e7415a2a7c
Merge branch 'master' into audit-extended 2019-01-22 10:52:08 +01:00
Pierre-Marie Padiou
3aa5754490
Relay to channel with lowest possible balance (#784)
* relay to channel with lowest possible balance

Our current channel selection is very simplistic: we relay to the
channel with the largest balance. As time goes by, this leads to all
channels having the same balance.

A better strategy is to relay to the channel which has the smallest
balance but has enough to process the payment. This way we save larger
channels for larger payments, and also on average channels get depleted
one after the other.

* added tests...

...and found bugs!

Note that there is something fishy in BOLT 4, filed a PR:
https://github.com/lightningnetwork/lightning-rfc/pull/538

Also, first try of softwaremill's quicklens lib (in scope test for now)

* minor: fixed typo (h/t @btcontract)
2019-01-21 22:08:58 +01:00
pm47
3279226902
Merge branch 'relay-to-smaller-channel' into audit-extended 2019-01-21 19:14:47 +01:00
sstone
3f72b441d8
Set version to 0.2-SNAPSHOT 2019-01-18 20:15:04 +01:00
sstone
eb0e45a8b1
set version to 0.2-beta9 2019-01-18 19:01:59 +01:00
Pierre-Marie Padiou
a52acdcaf1
Auto probing of local channels (#762)
This is a simple mechanism to test our direct peers by sending fake
small payments to them. A probe is considered successful if the peer
replies with an `UnknownPaymentHash` error.

Probing is configurable and disabled by default.
2019-01-18 14:39:06 +01:00
araspitzu
d5fe47572c Compute k-shortest routes (#813)
We use Yen's algorithm to find the k-shortest (loopless) paths in a graph, and dijkstra as search algo.

We then pick a random route up among the cheapest ones.
2019-01-17 21:15:51 +01:00
Fabrice Drouin
74d454d904
Electrum: improve error handling (#825)
* Electrum: always stop clients when there's an error

We will automatically connect to another server, and it's much cleaner
that restarting right away

* Electrum: improve handling of server errors

* Electrum Pool: don't switch to our current master

In regtest (and probably testnet too) it may seem that our master server went
straight to current height + 2 when blocks are created very quickly, so
check first that the server is not already our master before we switch.
2019-01-17 20:59:06 +01:00
araspitzu
67dc37c525 Impose a max length of 32 bytes on UTF-8 encoded aliases (#828)
Co-Authored-By: araspitzu <a.raspitzu@protonmail.com>
2019-01-17 19:23:08 +01:00
n1bor
6438c65ad9 Add JSON serializer for PaymentRequest (#798)
Also, renamed `checkinvoice` to `parseinvoice`. `checkinvoice` still works but is deprecated.
2019-01-17 15:46:02 +01:00
araspitzu
6d638e9941
Fix findroute documentation (#817)
* Fix the README and help message about the 'findroute' API parameters
2019-01-16 18:35:53 +01:00
pm47
b7745470b5
fixup: we want to compare with the original balance before the first event 2019-01-15 19:44:31 +01:00
pm47
5b9858934f
throttle logging of balance update events 2019-01-15 19:30:53 +01:00
pm47
0c9cd1620b
minor: fixed typo (h/t @btcontract) 2019-01-14 19:14:06 +01:00
Dominique
e3b2992934
Readibility improvements in GUI (#801)
* Improved amounts readability (fixes #542) and added the Bits unit 
denomination in the documentation

* Improved channel panel UI layout

* Added a confirmation dialog when closing a channel, with a short summary 
of the channel to be closed

The balance bar has been shrunk in size so that it should not be mistaken as 
a separator element between channels. The channel's balance, capacity and 
peer node id are now more visible. Also added the short channel id to the 
channel's pane.

fixes #690

* Added node's aggregated balance in the status bar

fixes #775
2019-01-14 16:53:52 +01:00
pm47
1df3455a72
only send AvailableBalanceChanged when needed
We were sending this event everytime we sent a `commit_sig`, which is
incorrect because our balance doesn't change if, say, we are signing an
incoming htlc.

Note that we only send this event in `NORMAL` state, not in `SHUTDOWN`
state, because the balance is not really *available* in the latter.
2019-01-14 11:31:56 +01:00
pm47
4005720a78
Merge branch 'master' into audit-extended 2019-01-14 10:57:42 +01:00
Pierre-Marie Padiou
10ea7bdc23
Make ping less aggressive (#782)
* send pings only after there is less activity

* make disconnect-on-ping configurable
2019-01-11 19:16:37 +01:00
araspitzu
fd3ff9184e Compute routes backward (#797)
* Support searching backward from the target

* Use the amount+fees with testing for min/max htlc value of edges

* Build the adjacency list graph with incoming edges instead of outgoing

* Make sure we don't find routes longer than the max allowed by the spec

* Remove default amount msat, enhance 'findroute' API

* Optimize tests for ignored edges in Dijkstra

* Enhance test for max route length, fix the length to 20 channels

* Add test for routing to a target that is not in the graph (assisted routes)
2019-01-11 17:20:01 +01:00
araspitzu
83b464cfcc Fix short_channel_id parsing and add RPC API test (#815)
* Correctly parse short channel id

* Add test for RPC APIs

* Put akka.http.version in parent project pom

Co-Authored-By: araspitzu <a.raspitzu@protonmail.com>
2019-01-11 14:59:33 +01:00
Fabrice Drouin
e082d89a3b
Electrum: download, verify and store headers (#776)
* Implement "GetHeaders" RPC call

* Add checkpoints and pow verification

* Don't resolve server address too soon

* Add testnet checkpoints

* Store headers in a sqlite wallet db

* Use 1.4 protocol

Request protocol version 1.4 (this is the default setting in Electrum wallet).
Retrieve and store all headers as binary blobs in bitcoin format.

* Insert headers in batch

* Optimize headers sync and persistence

We assume that there won't be a reorg of more that 2016 blocks (which
could be handled by publishing a new checkpoint) and persist our headers
except for the last 2016 we have received: when we restart, we will ask
our server for at least 2016 headers.

* Persists transactions

Transactions are persisted only when they've been verified (i.e. we've receive
a valid Merkle proof)

* Disable difficulty check on testnet and regtest

On testnet there can be difficulty adjustements even within a re-targeting window.

* Update checkpoints

* Use proper Ping message

`version` can not longer be sent as a ping as we did before.

* Don't ask for Merkle proofs for unconfirmed transactions

* Improve startup time

We now store a new checkpoint and headers up to that checkpoint as soon as our
best chain is 2016 + 500 blocks long

* Properly detect connection loss

* Update electrum mainnet servers list

Using the list from Electrum 3.3.2

* Don't open multiple connection to the same Electrum servers

We want to keep connection to 3 different servers, but when we have less than 3 different
addresses it's pointless to attempt to keep maintain 3 connections.
2019-01-11 14:08:46 +01:00
pm47
3eaf5a628a
added handler for AvailableBalanceChanged 2019-01-10 20:21:43 +01:00
pm47
fc680690ef
make auditor ignore unrelevant ChannelStateChanged events 2019-01-10 19:59:43 +01:00
pm47
7af05b3eee
added balance_updated and channel_events tables 2019-01-10 19:42:52 +01:00
pm47
c75082954b
added tests...
...and found bugs!

Note that there is something fishy in BOLT 4, filed a PR:
https://github.com/lightningnetwork/lightning-rfc/pull/538

Also, first try of softwaremill's quicklens lib (in scope test for now)
2019-01-06 00:15:20 +01:00
pm47
405759f52c
relay to channel with lowest possible balance
Our current channel selection is very simplistic: we relay to the
channel with the largest balance. As time goes by, this leads to all
channels having the same balance.

A better strategy is to relay to the channel which has the smallest
balance but has enough to process the payment. This way we save larger
channels for larger payments, and also on average channels get depleted
one after the other.
2019-01-05 17:29:03 +01:00
Pierre-Marie Padiou
52f671384e
Detect broken HTLC links at startup (#757)
If we have stopped eclair while it was forwarding HTLCs, it is possible
that we are in a state were an incoming HTLC
was committed by both sides, but we didn't have time to send
and/or sign the corresponding HTLC to the downstream node.

In that case, if we do nothing, the incoming HTLC will
eventually expire and we won't lose money, but the channel
will get closed, which is a major inconvenience.

This check will detect this and will allow us
to fast-fail HTLCs and thus preserve channels.
2019-01-04 17:34:50 +01:00
araspitzu
75a18e2446 Set a max value of 2016 blocks for to_self_delay (#795)
This will be a hard requirement in BOLT 1.1
2019-01-03 16:59:26 +01:00
Pierre-Marie Padiou
ad31d89e54
Disable local channels below reserve (#763)
The goal is to reduce attempts from other nodes in the network to use
channels that are unbalanced and can't be used to relay payments.

This leaks information about the current balance and is a privacy
tradeoff, particularly in this simplistic implementation. A better way
would be to add some kind of hysteresis in order to prevent trivial
probing of channels.
2019-01-03 14:02:39 +01:00
Pierre-Marie Padiou
9da330478a
Add exclusive locks to channels sqlite db (#781)
* added an exclusive write lock to channels sqlite db

* added close() method in db traits
2019-01-03 11:01:53 +01:00
Pierre-Marie Padiou
8887ac2043
Relay fail message *after* it is cross-signed (#754)
We were previously handling `UpdateFailHtlc` and
`UpdateFailMalformedHtlc` similarly to `UpdateFulfillHtlc`, but that is
wrong:
- a fulfill needs to be propagated as soon as possible, because it
allows us to pull funds from upstream
- a fail needs to be cross-signed downstream (=irrevocably confirmed)
before forwarding it upstream, because it means that we won't
be able to pull funds anymore. In other words we need to be absolutely
sure that the htlc won't be fulfilled downstream if we fail it upstream,
otherwise we risk losing money.

Also added tests.
2019-01-02 16:44:05 +01:00
araspitzu
7f3b101426 Use BOLT specific formatting for short channel id (#793) 2019-01-02 14:40:24 +01:00
araspitzu
255c280bd0 Routing: use custom implementation for the shortest path algorithm and the graph (#779)
* Consider htlc_minimum/maximum_msat when computing a route

* Compare shortChannelIds first as it is less costly than comparing the pubkeys

* Remove export to dot functionality

* Remove dependency jgraph

* Add optimized constructor to build the graph faster

* Use fibonacci heaps from jheaps.org

* Use Set instead of Seq for extraEdges, remove redundant publishing of channel updates

* Use Set for ignored edges
2018-12-20 16:52:42 +01:00
Dominique
be5d188a38
Tweak parameters to optimize ElectrumClient connections (#788)
* use TCP_NODELAY and PooledByteBufAllocator

* added reference to interesting slidedeck
2018-12-20 14:14:23 +01:00
Dominique
a80ff1f6d9 Cleaned up mainnet electrum servers list (#786) 2018-12-18 17:21:28 +01:00