Commit Graph

5128 Commits

Author SHA1 Message Date
Ryan Staudt
0c3abbecbb rpcserver: Add handleGetCFilterV2 tests. 2020-07-31 17:48:21 -05:00
Ryan Staudt
72ebb6040e rpcserver: Add handleGetCFilterHeader tests. 2020-07-31 17:48:21 -05:00
Ryan Staudt
40e8a97216 rpcserver: Add handleGetCFilter tests. 2020-07-31 17:48:21 -05:00
Ryan Staudt
66625a413d rpcserver: Add Filterer and FiltererV2 interfaces.
This adds Filterer and FiltererV2 interfaces to rpcserver, which
decouples rpcserver from the implementation to retrieve committed
filters and allows mocked versions to be provided for testing.
2020-07-31 17:48:21 -05:00
Dave Collins
157995d932
connmgr: Improve dial timeout test synchronization .
This modifies the dial timeout test to make use of a channel for the
cancellation signal synchronization instead of a sleep to prevent false
positive test failures in slower environments such as CI servers.
2020-07-29 20:47:20 -05:00
Dave Collins
6c6da6df18
blockchain: Optimize chain tip tracking.
Part of adding nodes to the block index involves updating the current
chain tip in order to be able to efficiently keep track of all known
chain tips.

Since there can be multiple tips at a given height, the current code
uses a slice for each height to track them.  However, this is wasteful
since it is extremely rare for there to be more than one chain tip at
the same height.

It is particularly notable at startup since the block index has to load
up hundreds of thousands of nodes.

Given that, this modifies the chain tip tracking logic to avoid creating
a slice unless it is actually needed by making use of an entry with a
static field for the normal case and a dynamic slice for the case when
more than 1 tip for the height actually exists.

It also keeps a running sum of the total number of known chips for use
as a hint to further reduce allocations when serving up related data and
updates the chain tip tests to ensure 100% branch coverage is maintained.

The end result is that  this reduces the total number of allocs at
startup by around 1%, which results in faster startup, and also reduces
the overall memory usage by a few MB for nodes that have a long running
history of side chains.
2020-07-29 19:25:03 -05:00
Dave Collins
125fc41c76
peer: Optimize logging.
Similar to the recent changes made to txscript logging, this updates the
peer code to take advantage of the ability to determine the logging
level when constructing expensive log strings in order to avoid the
allocations caused by closures and removes the no longer necessary code
related to creating them.
2020-07-28 16:51:48 -05:00
Dave Collins
f2839da8d1
txscript: Optimize trace logging.
After recent optimizations, the current next biggest offender of more
allocations than would be expected revealed by profiling is due to the
trace logging closures for the scripting engine execution.

Once upon a time, there was no way to check the current logging level
with the logging infrastructure at the time and thus a logging closure
was used to defer the fairly expensive construction of the trace logging
information until it was actually invoked (meaning tracing is enabled).

However, those closures come at the cost of allocations, and since
script execution is something that happens non-stop during normal
operation, those allocations really add up, as the profiling shows.

As some point, the logging infrastructure was changed out, and it is now
possible to determine the logging level, so this updates the code to
take advantage of that and avoid the closures while still only
performing the fairly expensive construction of trace logging
information when tracing is enabled. In other words, with this change
there is zero cost (other than the conditional check, of course) when
tracing is not enabled.

Finally, this also removes the no longer necessary code related to
creating the logging closures.
2020-07-28 16:48:20 -05:00
Dave Collins
b570934648
txscript: Optimize alt stack drop.
Profiling shows that approximately 0.67% of the total allocations are
coming from DropN even though it should realistically be zero.

The cause is that DropN returns an error if it is called with 0 and the
code that drops the alternate data stack in between scripts calls it
with the depth of the alternate data stack, which is almost always zero.

The call has no observable effect on the result of the script execution
in that case since the error is intentionally ignored, there is nothing
to be dropped, and is therefore correct semantically, however, it is
wasteful to generate and throw away the error.

There really is no good reason for the internal stack implementation to
return an error from DropN when called with 0 since it equivalent to a
NOP.

I have done a careful analysis of all call sites to ensure the change
will break any observable semantics.

Thus, this modifies DropN to remove the unnecessary error condition and
updates the associated internal stack implementation test accordingly.
2020-07-28 16:44:34 -05:00
Dave Collins
726178a13d
blockchain: Remove internal dbnamespace package.
This removes the internal blockchain/dbnamespace package in favor of
defining the bucket names and keys locally in the file that works with
them in the blockchain package.

An internal package for this gives the false impression that there is
some expectation that other packages in the module would be digging
around in the internals of what is intended to be an implementation
detail of the package.

It would also significantly complicates the ability to change the
database implementation.
2020-07-28 15:46:06 -05:00
Josh Rickmar
488369379c rpcserver: Sort getpeerinfo results by ID
The IDs are assigned in increasing order, so this has the net effect
of peer results returned in the order they were connected.  It also
keeps the order the same across multiple calls.

This matches the behavior of the current pull request implementing
getpeerinfo in dcrwallet.
2020-07-28 15:45:18 -05:00
Dave Collins
7e099b7edc
multi: Resolve go1.15 vet complaints. 2020-07-28 11:15:03 -05:00
Dave Collins
acc0174b44
txscript: Check equality via secp256k1 methods.
This modifies the signature cache Exists method to make use of the new
efficient equality testing methods provided by the secp256k1 types
versus indirectly checking their equality via their serializations.

The following benchmark shows a before and after comparison of a typical
signature cache exists check:

name             old time/op     new time/op     delta
-------------------------------------------------------------------------
SigCacheExists   400ns ± 0%      62ns ± 0%       -84.54%  (p=0.000 n=5+5)

name             old alloc/op    new alloc/op    delta
--------------------------------------------------------------------------
SigCacheExists   256B ± 0%       0B              -100.00%  (p=0.008 n=5+5)

name             old allocs/op   new allocs/op   delta
--------------------------------------------------------------------------
SigCacheExists   4.00 ± 0%       0.00            -100.00%  (p=0.008 n=5+5)
2020-07-27 21:30:54 -05:00
Dave Collins
a4e2443a74
rpcserver: Use internal quit chan for ws sync.
This modifies the websocket notification manager to make use of the
newer pattern that synchronizes the Run context with an internal quit
channel to ensure that any exported functions that communicate with the
goroutines can't possibly hang during shutdown.

This was previously done by storing the context in the struct (which is
generally discouraged) and protecting it by a mutex which means every
single notification had to take the mutex.

As can be seen by the diff of this commit, this approach is not only
more performant, it's also less code.
2020-07-27 18:58:01 -05:00
Dave Collins
d7532bd518
blockchain: Optimize stake node pruning.
Profiling revealed that the number one cause of allocations in
blockchain is currently due to stake node pruning, which definitely
should not be the case.

Upon closer examination, the culprit is that every single time the stake
nodes are pruned the entire ancestry of nodes prior to the prune height,
including those that have already been pruned, are appended to the list
of nodes to prune.  In practice, this amounts to hundreds of thousands
of nodes and thus the slice is resized multiple times (likely around
18 times per run given the current runtime semantics).

This remedies that by ensuring that only the nodes that actually need to
be pruned are added to the relevant slice and also preallocating space
per a hint that will handle the number of nodes expected to be pruned
with a 99% confidence level to further reduce allocations when more than
a single node needs to be pruned.

It also renames the slice to reflect what it actually does.
2020-07-27 12:21:46 -05:00
Dave Collins
b0d9399c7d
blockchain: Set pruning interval to tgt block time.
This modifies the pruning logic to set the pruning interval to the
target block time for the given chain parameters instead of hard coding
a constant with the mainnet value.
2020-07-27 12:21:46 -05:00
Dave Collins
b1b24a0cfd
blockchain: Rename last prune time field.
This renames the field that tracks the last time the stake nodes were
pruned to lastPruneTime to more accurately reflect its purpose.
2020-07-27 12:21:45 -05:00
Donald Adu-Poku
f5085109e0 multi: add rpcCPUMiner adaptor.
This adds an rpc adaptor for cpu miner, handling cases
where the underlining miner being passed is nil.
2020-07-25 21:33:10 -05:00
Donald Adu-Poku
3ea786998e multi: add BlockTemplater interface.
This adds the BlockTemplater interface to the rpcserver
package. Its corresponding adapter has also been added.
2020-07-25 19:56:02 -05:00
Donald Adu-Poku
5a1c705165 rpcserver: add verifychain & getdifficulty tests.
This adds rpc tests for the verifychain and getdifficulty rpcs.
2020-07-25 19:43:16 -05:00
Donald Adu-Poku
ee0d826094 multi: add rpcserver.CPUMiner.
This adds the rpcserver.CPUMiner interface to
facilitate cpu miner related rpc tests.
2020-07-25 19:28:02 -05:00
Donald Adu-Poku
dcddbbabb4 multi: add SanityChecker interface.
This adds the sanity checker interface and update
the function signature of CheckBlockSanity.
Associated tests and mocks updated/added.
2020-07-25 19:17:27 -05:00
Dave Collins
f73d738c24
main: Simplify startup logic slightly.
This modifies the main startup logic to make use of the fact that the
server now has its lifecycle tied to the same context that is cancelled
when a shutdown signal is received.

The end result is that the separate goroutine it was running in before
and associated synchronization logic is no longer necessary.
2020-07-25 18:14:55 -05:00
Dave Collins
264df84ae6
docs: Update for removal of fees module. 2020-07-25 18:10:43 -05:00
Dave Collins
eb4a0a06ac
fees: Move to internal.
This makes the fees package an internal package such that it will no
longer be an exported module.  The only place its functionality is
really needed is for the internal implementation of dcrd itself and thus
having an unnecessary exported module significantly increases the
maintenance burden.

This is part of the overall effort to reduce the total number of modules
and eventually get to the point that it will be possible to follow
semver for the root module.

It is worth noting that there are a few constants, which will be listed
below, that were exported from this module that callers might have
previously relied upon.  However, due to previous discussions about the
goal of removing the module, a code search has revealed that there are
no known callers that rely on them.

The aforementioned constants are:

- DefaultMaxBucketFeeMultiplier
- DefaultMaxConfirmations
- DefaultFeeRateStep

Overview of the major changes:

- Move the following files from fees -> internal/fees:
  - README.md
  - cmd/dumpfeedb/dumpfeedb.go
  - doc.go
  - estimator.go
  - log.go
- Remove fees/go.mod and fees/go.sum
- Make the README.md and doc.go files match the new reality
- Update all import paths in the repository accordingly
- Remove the dependency from the root go.mod
- Run go mod tidy on all modules
2020-07-25 18:10:39 -05:00
Dave Collins
1f308b4cb2
rpcserver: Use func for semver string.
This constructs the RPC API semantic version string from the individual
major, minor, and path constants versus requiring updates in both
places.  This is something contributors have previously forgotten to
update, so it makes sense to avoid the need to do it altogether.
2020-07-25 17:54:10 -05:00
Ryan Staudt
61705d7501 rpcserver: Remove math/rand init and import.
This removes math/rand from rpcserver as it is being initialized but it
is not being used anywhere.
2020-07-24 23:29:59 -05:00
Ryan Staudt
09f74d9ea9 rpcserver: Rename RPCServer to Server.
This renames RPCServer to Server since this is now referenced as
rpcserver.Server from outside of the rpcserver package.
2020-07-24 23:29:59 -05:00
Ryan Staudt
ff79d17569 rpcserver: Rename NewRPCServer to New.
This renames NewRPCServer to New since this is now referenced as
rpcserver.New from outside of the rpcserver package.
2020-07-24 23:29:59 -05:00
Ryan Staudt
24daadf5b9 rpcserver: Rename RpcserverConfig to Config.
This renames RpcserverConfig to Config since this is now referenced as
rpcserver.Config from outside of the rpcserver package.
2020-07-24 23:29:59 -05:00
Ryan Staudt
c6881b3288 rpcserver: Move to internal.
This moves rpcserver from the main module to a standalone internal
package.

Overview of the major changes:

- Move the following files from the main module -> internal/rpcserver:
  - rpcserver.go
  - rpcserver_test.go
  - rpcserverhandlers_test.go
  - rpcserverhelp.go
  - rpcserverhelp_test.go
  - rpcwebsocket.go
- Update the README.md and doc.go files
- Update all import paths in the repository accordingly
- Update `testDataPath` in rpcserverhandlers_test.go
- Update all `rpcsLog` references to `log`
- Define the following convenience functions and vars in rpcserver.go:
    - `normalizeAddress`
    - `directionString`
    - `zeroHash`
2020-07-24 23:29:59 -05:00
Ryan Staudt
69227557e1 rpcserver: Move genCertPair to server.go.
This moves `genCertPair` from rpcserver.go to server.go.  `genCertPair`
is only used by server.go and is being moved there in anticipation of
rpcserver being moved to a separate internal package.

This is part of the effort to move rpcserver into a separate internal
package.
2020-07-24 23:29:59 -05:00
Ryan Staudt
c08d34ad2e rpcserver: Export rpcwebsocket Notify functions.
This adds the following exported RPCServer functions, which delegate to
the corresponding underlying rpcwebsocket notification functions:
  - NotifyWork
  - NotifyStakeDifficulty
  - NotifyNewTickets
  - NotifyBlockConnected
  - NotifySpentAndMissedTickets
  - NotifyBlockDisconnected
  - NotifyReorganization
  - NotifyWinningTickets

This is done so that blockmanager.go and server.go can still access
these functions after rpcserver is moved to a separate internal package.

This is part of the effort to move rpcserver into a separate internal
package.
2020-07-24 23:29:59 -05:00
Ryan Staudt
92ab26beb5 rpcserver: Export RPC server, config, and new.
This exports `rpcServer`, `rpcserverConfig`, and `newRPCServer` from
rpcserver.go and updates all references accordingly.

This is part of the effort to move rpcserver into a separate internal
package.
2020-07-24 23:29:59 -05:00
Ryan Staudt
e79cbef1e0 rpcserver: Remove PeerNotifier dependency.
This removes the dependency that rpcserver has on the PeerNotifier
interface defined in blockmanager.go.  It accomplishes this by removing
the call to AnnounceNewTransactions and instead calling the already
defined ConnMgr.RelayTransactions interface method, followed by directly
calling NotifyNewTransactions to notify websocket clients of the newly
accepted transactions.

This is part of the effort to move rpcserver into a separate internal
package.
2020-07-24 23:29:59 -05:00
Ryan Staudt
41e0d57315 rpcserver: Remove log config dependencies.
This removes the dependency that rpcserver has on the
supportedSubsystems and parseAndSetDebugLevels functions defined in
config.go.  It accomplishes this by creating and implementing a
rpcserver.LogManager interface.

This is part of the effort to move rpcserver into a separate internal
package.
2020-07-24 23:29:59 -05:00
Ryan Staudt
2afb586cc0 rpcserver: Remove server.go dependencies.
This removes the dependency that rpcserver has on the const
maxProtocolVersion and var userAgentVersion defined in server.go.  It
accomplishes this by passing these values through the rpcserverConfig
struct.

This is part of the effort to move rpcserver into a separate internal
package.
2020-07-24 23:29:59 -05:00
Ryan Staudt
254c66ca2c rpcserver: Remove global config dependency.
This removes the dependency that rpcserver has on the global config in
the main module.  It accomplishes this by passing the necessary config
information through the rpcserverConfig struct.  Additionally, this adds
a Lookup method to the rpcserver.ConnManager interface.

This is part of the effort to move rpcserver into a separate internal
package.
2020-07-24 23:29:59 -05:00
Dave Collins
a6feca22cd
cpuminer: Rework to use bg template generator.
This significantly reworks the CPU miner to make use of the background
block template generator that was introduced in the previous release as
well as to convert its lifecycle to use the expected pattern for running
subsystems based on contexts.

The switch to using the background block template generator provides all
the benefits it offers to miners such as:

- Intelligent vote propagation handling
- Improved handling of chain reorganizations necessary when the current
  tip is unable to obtain enough votes
- Current state synchronization
- Near elimination of stale templates when new blocks and votes are received

This is particularly important in testing scenarios, such as when using
the simulation network, since the difficulty is so low that the code
this is replacing is often able to mine blocks so fast it ends up mining
multiple alternative blocks while waiting for the votes to show up which
makes testing with it more difficult.

This is no longer an issue when requesting template updates since the
background block template generator has logic to provide the votes a
chance to arrive before building and notifying the template.

Another modification this makes is to also make use of template
notifications when mining blocks mined via the discrete mining mode and
to only count blocks if they successfully extend the main chain.  This
also significantly helps in testing scenarios since it means the testing
code and/or testers using the simulation test network can rely on the
requested number of blocks actually being added to the main chain even
if more need to be generated to make that happen.

Along similar lines, one area this change does not address, but does
pave the way to support and would be useful in the future for the
simulation test network is to add some additional logic to provide
ticket purchases a chance to arrive when prior to stake validation
height, since that is another area that often complicates testing.

In terms of the lifecycle changes, this replaces the Start, Stop, and
Wait methods with a single method named Run and arranges for it to block
until the provided context is cancelled.  This is more flexible for the
caller since it can easily turn blocking code into async code while the
reverse is not true.

The new Run method waits for all goroutines that it starts to shutdown
before returning to help ensure an orderly shutdown.

In addition, the semantics of Run are different from Start/Stop in that
the Start method launched the default number of mining worker goroutines
whereas Run only starts the CPU miner with the main infrastructure
goroutines and zero workers, meaning it will be idle until explicitly
requested to mine by the caller setting the number of workers to use.

Since there are exported methods that send messages to the CPU miner
infrastructure goroutines via a channel and those goroutines are stopped
during the shutdown process, all sends select across both the channel in
question as well as a quit channel which is closed when the context is
canceled.  This ensures callers can't end up blocking on send to a
goroutine that is no longer running without needing additional mutexes.

As part of the conversion, it improves a lot of the comments to better
describe the expected behavior and further integrates the context to
support cancellation where it previously was not supported.

Finally, all callers are updated to use the new API and the server is
modified to take advantage of the new lifecycle semantics by taking
ownership of the CPU miner code directly in its Run method where it more
logically makes sense and only creating the template generate and CPU
miner infrastructure if needed based on the configuration options.
2020-07-23 12:36:09 -05:00
Dave Collins
65da0cb422
cpuminer: Refactor code to its own package.
This does the minimum work necessary to refactor the CPU miner code into
its own internal package.  The idea is that separating this code into
its own package will improve its testability and ultimately be useful to
other parts of the codebase such as the various tests which currently
effectively have their own stripped-down versions of this code.

The API will certainly need some additional cleanup and changes to make
it more usable outside of the specific circumstances it was originally
designed to support (namely the generate RPC), however it is better to
do that in future commits in order to keep the changeset as small as
possible during this refactor.

Overview of the major changes:

- Create the new package
- Move internal/mining/cpuminer.go -> internal/mining/cpuminer/cpuminer.go
- Update mining logging to use the new cpuminer package logger
- Rename CPUMinerConfig to Config (so it's now cpuminer.Config)
- Rename NewCPUMiner to New (so it's now cpuminer.New)
- Add exported BestSnapshot method to mining.BlkTmplGenerator
- Add exported TxSource method to mining.BlkTmplGenerator
- Update all references to the cpuminer to use the package
- Add a skeleton README.md
- Add a skeleton doc.go
2020-07-22 20:06:28 -05:00
Dave Collins
407d6dcbb4
mining: Improve comment for UpdateBlockTime. 2020-07-22 20:06:24 -05:00
Donald Adu-Poku
935467a868 rpcserver: add missed and live tickets rpc tests.
This adds tests for the missed and live ticket rpcs
as well as the validate address rpc.
2020-07-22 18:36:16 -05:00
David Hill
66807231ce build: golangci-lint v1.28.3 2020-07-21 12:33:51 -05:00
David Hill
fff10b5e09 wire: return detectable err from makeEmptyMessage. 2020-07-21 12:33:51 -05:00
David Hill
a927f2ace2 wire: formatting fixes - no functional change 2020-07-21 12:33:51 -05:00
Dave Collins
3a88a1ba65
docs: Update for removal of mining module. 2020-07-21 12:27:46 -05:00
Dave Collins
6ea800f516
mining: Move to internal.
This makes the mining package an internal package such that it will no
longer be an exported module.  The only place its functionality is
really needed is for the internal implementation of dcrd itself and thus
having an unnecessary exported module significantly increases the
maintenance burden.

This is part of the overall effort to reduce the total number of modules
and eventually get to the point it will be possible to follow semver for
the root module.

It is worth noting that there are a few constants, which will be listed
below, that were exported from this module that callers might have
previously relied upon.  However, due to previous discussions about the
goal of removing the module, a code search has revealed that there are
no known callers that still rely on them.

The aforementioned constants are:

- UnminedHeight
- MinHighPriority

Overview of the major changes:

- Move the following files from mining -> internal/mining:
  - README.md
  - cpuminer.go
  - doc.go
  - miningerror.go -> error.go
  - log.go
  - mining.go
  - mining_test.go
  - policy.go
  - policy_test.go
- Remove mining/go.mod and mining/go.sum
- Make the README.md and doc.go files match the new reality
- Update all import paths in the repository accordingly
- Remove the dependency from the root go.mod
- Run go mod tidy on all modules
2020-07-20 18:07:59 -05:00
Dave Collins
a524703265
docs: Update for removal of mempool module. 2020-07-20 14:09:48 -05:00
Dave Collins
3547f21f89
mempool: Move to internal.
This makes the mempool an internal package such that it will no longer
be an exported module.  The only place its functionality is really
needed is for the internal implementation of dcrd itself and thus having
an unnecessary exported module significantly increases the maintenance
burden.

This is part of the overall effort to reduce the total number of modules
and eventually get to the point it will be possible to follow semver for
the root module.

It is worth noting that there are a few constants, which will be listed
below, that were exported from this module that callers might have
previously relied upon.  However, due to previous discussions about the
goal of removing the module, a code search has revealed that there are
no known callers that still rely on them.

The aforementioned constants are:

- MaxStandardTxSize
- DefaultMinRelayTxFee
- BaseStandardVerifyFlags

Overview of the major changes:

- Move the following files from mempool -> internal/mempool:
  - README.md
  - doc.go
  - error.go
  - log.go
  - mempool.go
  - mempool_test.go
  - policy.go
  - policy_test.go
- Remove mempool/go.mod and mempool/go.sum
- Make the README.md and doc.go files match the new reality
- Update all import paths in the repository accordingly
- Remove the dependency from the root go.mod
- Run go mod tidy on all modules
2020-07-20 05:02:35 -05:00
Dave Collins
69a417232c
mempool: Remove deprecated DisableLog func.
This removes the deprecated DisableLog function since the major version
has been bumped since the last release.
2020-07-19 17:46:39 -05:00