Commit graph

118 commits

Author SHA1 Message Date
Calin Culianu
1462717c85
Added max_buffer and max_history config variables, plus some DoS prot.
The DoS protection added is against unlikely and hard-to-pull-off memory
exhaustion attacks whereby clients fill our receive buffers with data
without sending newlines.  If we detect our receive buffer (default 4MB
buffer size) is starting to fill, we time clients out quickly if no
newline arrives within 5 seconds.
2020-01-06 12:32:16 +02:00
Calin Culianu
d579a41cb3
Nit: Made all of the shared_ptr<Options> -> shared_ptr<const Options>
Except for in App.cpp, which is the place where they are created and the
only place the options may be modified (at app startup as a result of
parsing config).

Otherwise the options are const, which ensures they are immutable on the
type level (and thus don't need to be guarded with locks and can be
accessed by multiple threads for reading).
2020-01-06 08:57:24 +02:00
Calin Culianu
647e2db4c8
Fee historgram testing 2020-01-05 02:49:42 +02:00
Calin Culianu
5b75bdc2e9
comment/typo nits in Storage.cpp 2020-01-01 23:38:38 +02:00
Calin Culianu
4ae2b684ca Linux compile fixups and Qt 5.11 fixups 2019-12-31 19:29:48 +02:00
Calin Culianu
3ba9a40b9d
Fixups to Mempool data structure (maintained by Controller.cpp)
Made the txos be a vector, rather than a slow-ish/bloated map. This
should be much faster.

Also various other small tune-ups.

Added "Broadcast tx.." message to log on broadcast.

More to come!
2019-12-31 16:22:52 +02:00
Calin Culianu
9b351f29ae
More efficiencies added to mempool synch, plus ENABLED NOTIFICATIONS
Cleaned up the code a bit.  Still a WIP and needs performance tuning.
2019-12-31 13:14:27 +02:00
Calin Culianu
a37d623ca2
Initial steps in making the mempool use less memory & CPU
- Made the hashXTxs mapped_type be a std::vector<TxRef> rather than a
std::set<TxRef>.  We manually maintain the uniqueness and sort invariant
as we process the mempool.  Vectors are much more compact memory-wise
and also have better insert characteristics, so we prefer them here.

Also, we never check for set membership, so the O log N lookup of the
set is not warranted.

We do however potentially build very large hashX -> TxRef arrays so a
std::vector is much better.

After this fix, we need to see what else in this data structure is
wasteful. I may get rid of the "txos" std::map<IONum, TXOInfo> in favor of a
simple and direct std::vector<TXOInfo>
2019-12-31 04:17:06 +02:00
Calin Culianu
62ce6344d6
Added MaxHistory; truncate the history in getHistory and listUnspent
This is to avoid super expensive lookups for pathologically huge
histories (such as the blitz address).

The limit is currently 250,000 items.  TODO: Make this configurable
and/or tune this value.
2019-12-30 11:31:34 +02:00
Calin Culianu
532f11e95d
Fixed crash bug in SubsMgr::removeZombies + redid ownership of SubsMgr
- The crash bug was due to misuse of
robin_hood::unordered_flat_map::erase (it's still kind of bizarre that
it would crash the way it did, but I suppose the next iterator was
invalidated somehow).  We instead grab the next returned by .erase() and
now it works ok.

- We redid the ownership model of SubsMgr.  Rather than having
Controller own it and pass it all the way down to the Server instances,
we instead have Storage create & own it.  This is needed because in the
future Storage may need to invalidate cached sub statuses as blocks are
undone in undoLatestBlock, and/or it may need to atomically add
notifications as blocks are added.  So it makes sense for Storage to own
the SubsMgr (as it also owns the Mempool, which has similar ephemeral
qualities).
2019-12-30 10:33:35 +02:00
Calin Culianu
c4641bd589
Got rid fo the inferior "LRU::Cache" in favor of CostCache
Replaced it everywhere it was used (2 places), and removed LRUCache.h
from the project.
2019-12-29 16:04:11 +02:00
Calin Culianu
a831cce8ac
Modified license preamble to say "Bitcoin Cash" instead of "Electron Cash"
The hope is maybe people will adopt this with other wallets...
2019-12-28 23:48:24 +02:00
Calin Culianu
405335c173
Added LICENSE preamble to all top-level source files
GPL v3. YAY!
2019-12-28 23:46:05 +02:00
Calin Culianu
20da1f0c8b
Misc odds and ends
Added license to project, and other goodies. Added -T option for poll
timer. Misc other things added.
2019-12-28 23:26:16 +02:00
Calin Culianu
1691bf0fe4
nit 2019-12-28 18:30:47 +02:00
Calin Culianu
2b804684fa
nit 2019-12-28 16:22:55 +02:00
Calin Culianu
8aa0d92bf8
Updated get_balance to include unconfirmed balance
This is from the mempool
2019-12-28 16:13:24 +02:00
Calin Culianu
75314137c4 wip 2019-12-28 15:10:07 +02:00
Calin Culianu
2dd133806b
Set undo depth to 100 blocks.
Because.. why not? :)
2019-12-24 21:15:48 +02:00
Calin Culianu
1c81ad8b98
Replaced LRU::Cache with a cost-based cache for Height2TxHashes
We wrapped the QCache class which is a very light weight, cost-based LRU
cache.  It wasn't thread safe so we wrapped all its methods in
thread-safe versions.

Now we have a very accurate bound on memory usage for caching.  We may
want to replace the other height2txhash cache with this new class as
well since this new cost cache seems to be much more efficient.
2019-12-24 13:18:44 +02:00
Calin Culianu
d73e96e1df
Added more stubs, tweaks to be able to test with EC
- Turns out EC doesn't send the "jsonrpc" key in its requests. Grr. So
we fudge it to support EC.
- Various other tweaks and protocol stub impl.'s added to get EC to
like us and talk to us.
- Increased the side of the lruHeight2Hashes cache for testing.  We need
to replace this cache altogether with a memcost-based cache though, so
we can set 100MB or something as the maximum cache size and just have at
it. Right now we cannot do that as the LRU::Cache is "size" based (and
each block may have 1 tx or 64000 tx's, both having 1 entry in the
cache!!).
2019-12-24 02:01:55 +02:00
Calin Culianu
3034cada1c
Added server.features
Also added a mechanism to query peer port, peer address, etc from
AbstractConnection instances.

Added genesisHash() public thread-safe function to Storage.
2019-12-23 11:12:29 +02:00
Calin Culianu
3321d9f440
Prevented a potential deadlock in Merkle::Cache
Use byzantine code, get byzantine behavior.

This should be the last of the fixes hopefully.

Also in this commit: Some simplification in Servers.cpp and some comment
fixups and exception message cleanup.
2019-12-23 09:12:17 +02:00
Calin Culianu
2a92368d92
Small nit: print "Loading database ..." earlier
rocksdb may introduce a huge delay on startup if user hit ctrl-c when
doing initial synch.

We were printing this message way too late.

Fixed.
2019-12-22 23:59:19 +02:00
Calin Culianu
146ce39514
Bugfix in Merkle cache
Oops. :) Truncate was buggy. Fixed.

Also made all the merkle functions throw rather than just print errors
to the log.  They do print errors to the log as well for critical
errors.
2019-12-22 23:51:53 +02:00
Calin Culianu
8b6181b579
Replaces headers db with headersFile. This should be much faster.
hopefully it's as durable in case of crashes.. we do fsync when updating
it so.. hopefully that's enough.
2019-12-22 11:40:25 +02:00
Calin Culianu
e275767a7b
DB changes, phase 1. Added 'dirty' flag, added utxoct to meta table, ...
We skip the utxo count check at startup unless the new -C (--checkdb)
flag is set, in which case we do a very thorough check of all utxos and
the corresponding scripthash_unspent entries.

We set a dirty flag during block addition / removal and if it's set, we
abort the app immediately and ask the user to do a full resynch.

Since adding/removing blocks isn't 100% ACID, this is needed until we
figure out how to redo the data to make everything ACID.

Also in this commit -- attempted to see if I could make the header
reading faster from the db by using MultiGet.  It turns out it doesn't
help much.
2019-12-22 10:55:24 +02:00
Calin Culianu
0596f59bcf
Nits to the new merkleCache
Put it in a private area and access it via public methods in Storage to
preserve proper encapsulation.
2019-12-22 02:11:16 +02:00
Calin Culianu
fe5d7ea5b9
Merkle yay!
Got the cp_height thing working for blockchain.block.header

Next up, the "headers" version. W00t! :D
2019-12-22 01:20:24 +02:00
Calin Culianu
22608edac6
Massive refactoring for Severs.cpp and fixup to RPC.cpp
Servers.cpp now is much simplified. Writing RPC methods should be far
easier and less boilerplatey now.  We throw RPCError internally now when
we want to indicate an error -- this reduces the boilerplace
significantly.

Also we wrote a generic bitcoind_async function for submitting requests
to bitcoind which takes care of some of the boilerplate involved and
handles errors, etc, automatically (can be overridden by caller).

Also a bugfix to the ThreadPool -- if it caught an exception
completion() would still be called (which is not what we want).

Now it calls failure() appropriately at the right time on exception, and
does not call completion().

Also RPC.cpp had the sendResult() take a "method" argument which was
ignored.  Deleted the argument to simplify usage.

So far the code is looking good.
2019-12-21 18:55:48 +02:00
Calin Culianu
98e14a9e9b
Nit to Storage stats pring: slightly more accurate size estimate
Take into account each hash's sizoef(QByteArray) which should be a few
words per hash.
2019-12-20 21:33:49 +02:00
Calin Culianu
ba0c7b913f
Added some stats port printing for Storage caches
This is so we can debug and tune them later -- see how much memory they
use in a typical usage pattern.
2019-12-20 21:28:42 +02:00
Calin Culianu
5e5d0b5375
Slight performance tweak/nit: Cache/return block hashes in bitcoind memorder
This is in Storage.cpp's txHashesForBlock, which has been renamed to
txHashesForBlockInBitcoindMemoryOrder since it is the only function that
returns results in this memory order. We do it this way so we may cache
the reversed results and return them quickly the next time, thereby
hopefully reducing the performance cost on cache hits.
2019-12-20 20:42:45 +02:00
Calin Culianu
2e9c2dab63
Using our new Merkle.cpp, fully implemented 2 of the transaction methods
blockchain.transaction.id_from_pos is now fully implemented, as is
blockchain.transaction.get_merkle.

They both use a cache to store the txHashes for a particular block, and
resort to reading from the disk file if not found in cache.

This cache stores the txHashes for 500 of the most recently queried
blocks.  In the worst pathological circumstances of 64k tx's per block,
the cache would use about 1.5GB of memory at worst.  In practice it will
use much less as blocks are empty.
2019-12-20 19:12:05 +02:00
Calin Culianu
4009b9a970
Initial implementation of the Merkle namespace
It seems to have all the utility functions we need to create those
merkle branch things for the protocol.
2019-12-20 14:30:39 +02:00
Calin Culianu
41f32c4ebf
Implemented more protocol methods partially
We can test stuff now. Yay.  It's fast.

Still missing:

- merkle related stuff
- scripthash subscription methanism (and notifications)
- mempool related stuff
- figure out how the scheme will work for submitting requests to
bitcoind (bitcoindmgr->submitRequest seems ready to go for this and can
be called from Client instances).
2019-12-19 14:29:48 +02:00
Calin Culianu
4e3c893714
Added a bunch of RPC methods, etc
Yay! Getting there! :D
2019-12-18 17:53:16 +02:00
Calin Culianu
fceab52f79
Nits and tweaks in Storage.cpp
- Added a general blocksLock for code like getHistory and listUnspent to
be sure chain isn't changing while it's returning results.

- Added various nits and tweaks and tuneups and comments
2019-12-17 11:27:46 +02:00
Calin Culianu
d8d7fea942
Refactored to use batching for utxo set updates (and shunspent)
This should be a little bit faster for initial synch especially.  Also
cleaned up some APIs a little bit.
2019-12-16 18:41:34 +02:00
Calin Culianu
5e064c5b44
Some slight refactorings to reduce boiletplate in Storage.cpp 2019-12-16 16:42:27 +02:00
Calin Culianu
9ebe563cb0
some nits and comments 2019-12-15 23:40:52 +02:00
Calin Culianu
bbd96ff50b
Got undo working! Yay! We are getting closer to being a full real
server!

It still needs some testing but it appears to work.  We roll back
blocks. It's fast and good. Yay!
2019-12-15 20:52:21 +02:00
Calin Culianu
09932eb62f
Brought back CompactTXOVec which we will need for undo 2019-12-15 18:25:09 +02:00
Calin Culianu
696bb6fa45
Partial impl. of undoLatesBlock().. more to come 2019-12-15 18:19:38 +02:00
Calin Culianu
9a6073917b
Added more hooks and structure in preparation for undo code
Yay.
2019-12-15 12:33:02 +02:00
Calin Culianu
6499c33209
Added guard to not issue useless delete-old-undo-info commands.
We only issue commands to the db to delete old undo info if we know for a fact it's
in the db. This should slightly improve performance on initial synch
where we download hundreds of thousands of blocks and have 0 undo in the
db (so the undo deletes were just wasting cycles and bloating rocksdb's
log files).
2019-12-15 09:43:32 +02:00
Calin Culianu
d7338d5b87
Moved a lock up to top in addBlock 2019-12-15 09:14:56 +02:00
Calin Culianu
bfbe86a795
Added saving of undo data to db
The save is done for the last 10 blocks before latest tip.

We also have to unconditionally issue Delete commands to the db for all
block heights >10 blocks ago.  So far the unconditional issuance of the
delete command does not seem to impact performance.

We could mitigate the Delete command by keeping track of the olders
header we know exists in the db -- but we don't bother for now.
2019-12-15 09:08:31 +02:00
Calin Culianu
627241cb08
Refactor: added the ShallowTmp and DeepCpy templates as QByteArray helpers
This will reduce typing and hopefully make it very explicit which type
of QByteArray is being created in the code as a defensive progarmming
measure to eliminate errors... because the QByteArray::FromRawData
static function doesn't capture its very unsafe nature in its name (and
the fact that it is a "contagious" property it brings to the
QByteArray).  The "ShallowTmp" naming captures this.
2019-12-14 12:22:40 +02:00
Calin Culianu
1d7163f099
Added UndoInfo Serialize/Deserialize and test 2019-12-14 11:42:26 +02:00