Commit Graph

4992 Commits

Author SHA1 Message Date
Donald Adu-Poku
2e07bdcff1 server: ban peers on wire protocol errors.
This bans peers, mostly non-whitelisted
peers, for not conforming to the wire protocol.
2020-05-07 19:57:12 -05:00
matthawkins90
c8e3fec516 docs: Update Code Contribution Guidelines
Added clarity about multi-line commit messages, as well as defined rules
regarding linking to other issues or PRs.
2020-05-06 15:51:09 -05:00
Wisdom Arerosuoghene
bb830e670f
multi: CancelPending error for no pending conns.
This modifies the CancelPending method of the connection manager to
return an error when there are no pending connections for the provided
address or the connection manager is already shutting down.

This change, in turn, ensures that attempts to remove a pending
connection that doesn't exist via RPC returns an error as expected.
2020-05-06 13:30:43 -05:00
Dave Collins
50fe5b7ad3
connmgr: Rework HTTPS seeding.
This reworks the HTTPS seeding in the connmgr package and server to make
it easier to use properly and to make use of a couple of the new
capabilities it offers.

The current implementation of the SeedAddrs function is basically a
carbon copy of the older DNS seeding function with some slight
modifications and, as a result, its API is geared more towards the older
functionality which is not the most ideal for the new functionality.

First, since the SeedAddrs function is synchronous, as opposed
to the older async DNS seeder version, an OnSeed callback adds
unnecessary complexity since the addresses can simply be returned from
the function instead.

Next, this existing function does not make it very clear how to use the
additional filtering parameters and it also always sets them in the
query string which unnecessarily imposes a specific implementation on
the seeding server.  More desirable behavior is to only set the specific
query parameters when the caller wishes to apply specific filters.

Finally, the code in the server that consumes the returned seeds
currently still contains a quirk that was required by the old DNS
seeding semantics which no longer apply where it attributes the source
of the addresses to the first returned address.

This addresses all of those issues and a couple of other minor nits.
The following is an overview of the changes:

- Modify the SeedAddrs func signature to return the addresses directly
  instead of using an OnSeed callback
- Introduce a new HttpsSeederFilters struct along with exported
  functions that allow the caller to selectively specify seed filters
- Modify the SeedAddrs func signature to accept a variable number of
  filters specified via the aforementioned functions
  - This allows only the specific filters the caller desires to be
    specified and is easily extensible to support more filters with no
    breaking API changes in the future should it be necessary
- Modify the implementation of the SeedAddrs func to only set the query
  params for the various filter options if the caller provided them
- Improve the logging to properly handle any invalid addresses returned
  from the seeder
- Improve the readability of the random time offset code in SeedAddrs
- Update the server code in accordance with the API changes
- Modify the server code that adds the discovered addresses to the
  address manager to look up the IP address of the HTTPS seeder and use
  that as the source address
  - Fall back to the first returned address in the extremely rare event
    the HTTPS seeder IP lookup fails
2020-05-04 22:10:59 -05:00
Dave Collins
ce2195fbc3
main: Update go.mod for recent rpcclient bumps.
This updates the main go.mod direct dependencies to account for the
recent rpcclient bumps as follows:

- github.com/decred/dcrd/dcrutil/v3@v3.0.0-20200503044000-76f6906e50e5
- github.com/decred/dcrd/rpc/jsonrpc/types/v2@v2.0.1-0.20200503044000-76f6906e50e5
- github.com/gorilla/websocket@v1.4.2
2020-05-04 21:47:38 -05:00
Dave Collins
984af9b0c9
connmgr: Rename TorLookupIPContext to TorLookupIP.
Now that the deprecated non-context version of TorLookupIP has been
removed, this renames the version that accepts a context to take its
place.
2020-05-04 21:34:01 -05:00
Dave Collins
7c02293c4f
connmgr: Add test for connect context cancel.
This adds a test to ensure the Connect method works as intended when
provided with a context that times out before a dial attempt succeeds.
2020-05-04 21:25:44 -05:00
Dave Collins
0f3ef4dec3
connmgr: Add test for dial timeout.
This adds a test to ensure the dial timeout configuration option works
as intended.
2020-05-04 21:25:41 -05:00
Dave Collins
26c088aee0
connmgr: Remove deprecated TorLookupIP func.
This removes the deprecated TorLookupIP function since the major version
has been bumped since the last release.
2020-05-04 14:50:14 -05:00
Dave Collins
fba0da4c1b
connmgr: Remove deprecated SeedFromDNS func.
This removes the deprecated SeedFromDNS function and associated
LookupFunc type since the major version has been bumped since the last
release.
2020-05-04 14:50:14 -05:00
Dave Collins
93060bdb8d
connmgr: Remove deprecated DisableLog func.
This removes the deprecated DisableLog function since the major version
has been bumped since the last release.
2020-05-04 14:50:10 -05:00
Dave Collins
9480b9aaf5
server: Remove unused interrupt chan param.
This removes the interrupt channel parameter from the newServer function
since it is no longer used due to the use of contexts instead.
2020-05-04 14:01:17 -05:00
Dave Collins
3fd3c74867
multi: Run go mod tidy on all modules. 2020-05-04 13:54:54 -05:00
David Hill
80e5fcdf23
rpcclient: bump to newer modules. 2020-05-04 13:51:41 -05:00
Dave Collins
76f6906e50
wire: Use new errors.Is capabilities in tests.
The error handling in the tests for the wire package were recently
updated to make use of the new errors.Is and errors.As standard library
functions introduced in Go 1.13, but they were only updated mechanically
as opposed to being updated to use the new capabilities of errors.Is
checking for a specific error code as opposed to just the error type.

Consequently, this updates the tests to remove all of the additional
type checking via errors.As and instead look for the exact expected
error code via errors.Is.  This change is desirable since only examining
the type of error doesn't prove that the test is actually hitting the
specific error it intends too, rather only that it is hitting an error
of the same type.

It also corrects a couple of tests and error returns that were
discovered to be incorrect in the process.
2020-05-02 23:40:00 -05:00
buck54321
4758df966f wire: Test no-relay case in TestVersionWire
The test structs were created for testing the DisableRelayTx = true
case, but they weren't being used. Linter didn't pick up on it
because both test structs were being modified.
2020-05-02 20:56:15 -05:00
David Hill
d86202be39 build: use newer github and linter versions 2020-05-01 22:56:35 -05:00
David Hill
7d7a66d8f3 wire: use std errors api 2020-05-01 22:56:35 -05:00
Josh Rickmar
899553bbde rpcclient: Remove dcrwallet methods
Methods calls on the Client type for performing dcrwallet JSON-RPCs
are being removed in order to simplify the dependency graphs between
projects. These calls are moving to a package in the dcrwallet module
that will abstract the actual connection and calling logic through an
interface (which rpcclient.Client will satisfy).

All callbacks for dcrwallet notifications have been removed.
Dcrwallet has not produced JSON-RPC notifications for several years at
this point.

The example program connecting to dcrwallet is also removed here. If
the dependencies of this program appeared in the go.mod (they don't
currently, as go-spew is missing), then rpcclient would be right back
to requiring the dcrwallet module. This example program may reappear
in the new wallet package. Any pointers towards the new wallet
package should be done in comments, not code that will update the
modules.
2020-05-01 20:28:57 -05:00
Dave Collins
c1722eb7b2
wire: Improve error handling.
This updates the error handling in the wire package to be more
consistent with the rest of the code base, tightens some of the error
handling in message parsing to harden the protocol, and adds tests to
ensure the error implementations works correctly with the standard
library errors.Is and errors.As functions introduced in Go 1.13.

In particular, the error code semantics have been changed so the first
error starts at 0 instead of treating 0 is a sentinel "no error" value
because it goes against Go best practices which designed the errors
specifically to avoid the use of sentinel values.  Further, the use of 0
as a sentinel means that there are two different ways to indicate "no
error", namely nil and an error code of zero.  Thus callers have to know
that they need to check for both conditions which is incredibly error
prone.  In fact, there is already at least one instance in the code base
that has that mistake in the tests.

Finally, while here, this hardens the protocol by enforcing more strict
parsing requirements lower in the software stack instead of relying on
upper layers to take care of it.
2020-05-01 20:24:15 -05:00
Dave Collins
502a6e3b81
docs: Remove stray trailing spaces in README.md. 2020-05-01 17:33:29 -05:00
Dave Collins
7278961335
docs: Remove outdated btcd refs from README.md.
This removes the references to btcd from README.md since the code base
has diverged so much that there isn't very much in common anymore and
thus the text no longer reflected reality.
2020-05-01 17:33:29 -05:00
Dave Collins
3c128b725c
peer: Set a default idle timeout if not specified.
This modifies the peer initialization code to set a default idle timeout
of two minutes if the caller did not specify one.

This is desirable since the default is the zero value otherwise, which
results in an immediate disconnection for callers that don't specify a
timeout.

It is worth noting that the server code already has a default setting
that can be configured via the command-line or a configuration file
which is used to configure the peers, so this has no effect on dcrd, but
it is a nice addition for other consumers of the peer package.
2020-05-01 17:29:28 -05:00
Josh Rickmar
787d3f5c0f multi: Split dcrctl to own repo and update docs
The dcrctl project is being split out of the dcrd repo and module into
its own repo (https://github.com/decred/dcrctl) and module
(decred.org/dcrctl).  This commit removes the dcrctl main package and
updates documentation throughout the repo mentioning dcrctl and its
build instructions.
2020-05-01 13:57:20 -05:00
Marco Peereboom
e12b0a7191
connmgr: Fix potential panic via RPC.
req or Addr are potentially not set when the state machine moves to
handleCancelPending. Ignore this case.
2020-04-30 18:46:07 -05:00
matthawkins90
b60c60ffe9 docs: Clarify README.md installation guides. 2020-04-21 16:38:27 -05:00
Dave Collins
307bf49936
schnorr: Correct a couple of typos in README.md. 2020-04-13 13:07:17 -05:00
Dave Collins
3487f099ee
docs: Update README.md to reflect reality.
This updates the primary `README.md` for the most recent versions of Go,
minimum recommended requirements, and to reflect reality for the latest
version of the test script.
2020-04-13 13:06:49 -05:00
Dave Collins
71f06fbd45
secp256k1: Optimize pubkey parse.
This modifies the ParsePubKey function to avoid an additional
unnecessary check if the point is on the curve in the compressed pubkey
case because it necessarily has already determined if that is true via
the point decompression from the x coordinate.

Also, prevent x from escaping to the heap for the error print in
compressed case while here.

benchmark                          old ns/op    new ns/op   delta
------------------------------------------------------------------
BenchmarkParsePubKeyCompressed     11901        11745       -1.31%
BenchmarkParsePubKeyUncompressed   335          325         -2.99%

benchmark                          old allocs   new allocs  delta
-------------------------------------------------------------------
BenchmarkParsePubKeyCompressed     2            1           -50.00%
BenchmarkParsePubKeyUncompressed   2            1           -50.00%

benchmark                          old bytes    new bytes   delta
-------------------------------------------------------------------
BenchmarkParsePubKeyCompressed     128          80          -37.50%
BenchmarkParsePubKeyUncompressed   128          80          -37.50%
2020-04-13 13:05:57 -05:00
Dave Collins
f5800f367f
secp256k1: Add uncompressed pubkey parse benchmark. 2020-04-13 13:05:57 -05:00
Dave Collins
2f1f4e4ba3
secp256k1: Add compressed pubkey parse benchmark. 2020-04-13 13:05:56 -05:00
Dave Collins
4fc3c02ca0
secp256k1: Update README.md and doc.go.
This updates the README.md and doc.go files to reflect reality after all
of the latest changes.
2020-04-13 13:01:06 -05:00
Dave Collins
6ab35c24d3
schnorr: Add verify signature example. 2020-04-08 14:08:16 -05:00
Dave Collins
008388b6fe
schnorr: Add sign message example. 2020-04-08 14:08:16 -05:00
Dave Collins
6da2ef8106
secp256k1: Use specialized types in public key.
This updates the internal implementation of the PublicKey type to use
the FieldVal type instead of big.Ints and modifies NewPublicKey to
accept the FieldVals and updates all callers and tests in the repository
accordingly.

Public key parsing and serialization are now completely free of
big.Ints.

Finally, it moves the X and Y methods to the ellipticadaptor.go file
since they are only intended to provide interop with the standard
library elliptic curve adaptor code.

This reduces the number of allocations needed in the ecdsa compact
signature recovery as well as schnorr signature verification and results
in a slight speedup for both.

The following benchmarks show the before and after comparison:

benchmark                 old ns/op    new ns/op    delta
----------------------------------------------------------
BenchmarkRecoverCompact   198012       196173       -0.93%
BenchmarkSigVerify        179170       177805       -0.76%

benchmark                 old allocs   new allocs   delta
-----------------------------------------------------------
BenchmarkRecoverCompact   36           32           -11.11%
BenchmarkSigVerify        19           15           -21.05%

benchmark                 old bytes    new bytes    delta
-----------------------------------------------------------
BenchmarkRecoverCompact   1745         1617         -7.34%
BenchmarkSigVerify        977          848          -13.20%
2020-04-08 14:07:18 -05:00
Dave Collins
2f7c043d84
secp256k1: Add IsOnCurve method to PublicKey.
This adds a new method to the PublicKey type which can be used to
determine if the public key is on the secp256k1 curve directly.

It also adds tests to ensure proper functionality.
2020-04-08 14:02:37 -05:00
Dave Collins
0486437cb2
hdkeychain: Use errors api and require go 1.13+. 2020-04-08 13:59:05 -05:00
Dave Collins
063bf26a1c
secp256k1: Explicit pubkey parsing errors in tests.
This updates the public key parsing tests to test for the explicit
reason for the failure to ensure that each test is actually testing the
intended condition.
2020-04-08 13:54:06 -05:00
Dave Collins
9060dc3afe
secp256k1: Add pubkey parsing error infrastructure.
This introduces a new error type and associated error code for public
key errors which can be used with the errors.Is and errors.As interfaces
to programmatically detect the failure reason and updates the public key
parsing code to return appropriate error codes.

It includes full tests for the new error infrastructure.
2020-04-08 13:54:06 -05:00
Dave Collins
86b11abbd6
secp256k1: Rework pubkey tests.
This reworks the tests related to public keys to them more consistent
with the rest of the package and improve their coverage to include all
of the possible error paths.

It also brings back the hybrid pubkey parsing to match what the function
comment claims and corrects the tests accordingly.  The restriction as
to which specific types of pubkeys are allowed must be (and is) enforced
by the opcode(s) that interpret them as opposed to the lib that
implements the more generic specification.

The following is an overview of the changes:

- Convert all tests to use hex strings
- Add and verify the expected x and y coordinates from a parsed pubkey
- Bring back support for parsing hybrid keys
- Correct remaining existing tests for hybrid pubkeys
- Add negative tests for all combinations of encoding what would
  otherwise be valid pubkey with the wrong format byte and y coord
  oddness
- Add negative tests for x and y coordinates == p for the uncompressed,
  compressed, and hybrid formats
- Add negative tests for all formats such that either the x or y
  coordinate are > p and are constructed in such a way that they would
  be valid if the coordinate is taken mod p to ensure implementations
  don't incorrectly ignore the range check
2020-04-08 13:54:02 -05:00
Dave Collins
10224b9c3c
ecdsa: Use PutBytesUnchecked for serialize.
This updates the code to use the recently-added PutBytesUnchecked of
the FieldVal and ModNScalar types to serialize directly into larger
buffers where possible to avoid the need for additional copies.
2020-04-08 13:27:01 -05:00
Dave Collins
e31486afc6
schnorr: Use PutBytesUnchecked for serialize.
This updates the code to use the recently-added PutBytesUnchecked of
the FieldVal and ModNScalar types to serialize directly into larger
buffers where possible to avoid the need for additional copies.

The following benchmarks show a before and after comparison of the
affected operations:

benchmark               old ns/op    new ns/op    delta
--------------------------------------------------------
BenchmarkSign           52682        52284        -0.76%
BenchmarkSigVerify      178050       176977       -0.60%
BenchmarkSigSerialize   87.5         73.3         -16.23%

benchmark               old allocs   new allocs   delta
--------------------------------------------------------
BenchmarkSign           16           16           +0.00%
BenchmarkSigVerify      21           21           +0.00%
BenchmarkSigSerialize   1            1            +0.00%

benchmark               old bytes    new bytes    delta
--------------------------------------------------------
BenchmarkSign           960          960          +0.00%
BenchmarkSigVerify      1105         1105         +0.00%
2020-04-08 13:23:13 -05:00
Dave Collins
72fee1dc72
schnorr: Add benchmark for Signature.Serialize. 2020-04-08 13:23:13 -05:00
Dave Collins
00629c7982
hdkeychain: Use specialized secp256k1 types.
This optimizes the various internal implementations that make use of
secp256k1 to use the specialized ModNScalar and JacobianPoint types
instead of big.Ints.

The following benchmarks show a before and after comparison:

benchmark                 old ns/op     new ns/op   delta
-----------------------------------------------------------
BenchmarkDeriveHardened   3702          3547        -4.19%
BenchmarkDeriveNormal     4053          3587        -11.50%
BenchmarkPrivToPub        94.7          94.0        -0.74%
BenchmarkDeserialize      5825          5797        -0.48%
BenchmarkSerialize        9509          9402        -1.13%

benchmark                 old allocs   new allocs   delta
-----------------------------------------------------------
BenchmarkDeriveHardened   14           12           -14.29%
BenchmarkDeriveNormal     15           12           -20.00%
BenchmarkPrivToPub        1            1            +0.00%
BenchmarkDeserialize      4            3            -25.00%
BenchmarkSerialize        3            3            +0.00%

benchmark                 old bytes    new bytes    delta
-----------------------------------------------------------
BenchmarkDeriveHardened   1360         1232         -9.41%
BenchmarkDeriveNormal     1409         1232         -12.56%
BenchmarkPrivToPub        112          112          +0.00%
BenchmarkDeserialize      320          256          -20.00%
BenchmarkSerialize        272          272          +0.00%
2020-04-08 13:18:13 -05:00
Dave Collins
984e09db00
ecdsa: Correct README.md documentation links. 2020-04-08 05:11:12 -05:00
Dave Collins
6f9bcbbebc
secp256k1: Add PutBytesUnchecked to ModNScalar.
This adds a new method to ModNScalar named PutBytesUnchecked which can
be used by callers to serialize the scalar directly into a target byte
slice with the caveat that the caller must ensure the target has at
least 32 bytes available or it will panic.

This is useful in cases where the caller wishes to write into a larger
buffer without having to first serialize into a 32-byte array and copy
it.

It also updates the tests to ensure it works as expected.
2020-04-08 04:22:45 -05:00
Dave Collins
be20f291c8
secp256k1: Add PutBytesUnchecked to FieldVal.
This adds a new method to FieldVal named PutBytesUnchecked which can be
used by callers to serialize the field value directly into a target byte
slice with the caveat that the caller must ensure the target has at
least 32 bytes available or it will panic.

This is useful in cases where the caller wishes to write into a larger
buffer without having to first serialize into a 32-byte array and copy
it.

It also updates the tests to ensure it works as expected.
2020-04-08 04:22:41 -05:00
Dave Collins
277e4e4574
schnorr: Rename error codes to better match reality.
This renames some error codes to more closely match the failure as
follows:

- ErrBadInputSize -> ErrInvalidHashLen
- ErrInputValue -> ErrPrivateKeyIsZero
- ErrPointNotOnCurve -> ErrPubKeyNotOnCurve
- ErrBadSigRYValue -> ErrSigRYIsOdd
- ErrBadSigRNotOnCurve -> ErrSigRNotOnCurve
2020-04-08 04:08:49 -05:00
Dave Collins
0cd04a0b0c
schnorr: Remove unused error codes.
This removes the following unused error codes:

- ErrRegenSig
- ErrRegenerateRPoint
- ErrBadNonce
- ErrNonmatchingR
2020-04-08 04:08:49 -05:00
Dave Collins
bba36ac56e
schnorr: Use specialized types in signature type.
This updates the internal implementation of the Signature type to use
the ModNScalar and FieldVal types instead of big.Ints and modifies
NewSignature to accept the types accordingly.

Signature parsing and signing operations are now completely free of
big.Ints.  Verification still relies on them due to their use in public
keys.

It also removes the now unused primitives.go file.
2020-04-08 04:03:34 -05:00